This week, we will:
To run Python, search your program for IDLE. Open that program.
Now something like this will popup:
That's a Python shell. You can play around with the console like assigning variables, adding numbers, using it as a calculator and it's pretty nice.
However, typically we want to write something nicer so for our purposes, we will write Python scripts or programs. Let's do that.
Choose File -> New File. Now another blank screen will pop up. Let's write our first simple program. Type . . .
print("Hello World")
Now go back to the navigation bar and click Run. It will ask if you're OK and ask you to save the file. Name the file whatever you want and let's save.
Now let's start with some basic math operations Python has.
In [1]:
3 + 5 # addition
Out[1]:
In [2]:
3 - 5 # subtraction
Out[2]:
In [5]:
3 * 5 # multiplication
Out[5]:
In [7]:
3/5 # division
Out[7]:
In [8]:
3 ** 5 # power
Out[8]:
In [9]:
4 // 3 # taking the interger
Out[9]:
In [13]:
5 % 2 # this is a modular operator which takes the remainder. In this case, 5 divided 2 results in 2 and 1 as the remander.
Out[13]:
In [16]:
2%4
Out[16]:
Alright so that was fun, let's say now I want to calculate y = m*x + b for multiple m, x, and b values. I want to assign numbers to things so you can use and re-use them over again. Turns out it's pretty easy with Python.
In [1]:
x = 5
m = 3
b = 10
y = m*x + b
print(y)
Awesome! That's what I wanted. Now let's say I want to be a little bit more explicit with how I print my y. I want to have it explicitly say "y = 25". Let's try to change up how I print that.
In [2]:
print("y =" + y)
Well that's not good. Python is giving me a TypeError: Can't convert 'int' object to str implicitly
. What does that mean? Let's try to google that.
The first result I came accross was this: http://stackoverflow.com/questions/13654168/typeerror-cant-convert-int-object-to-str-implicitly
So it seems like there was a problem with my "y = " being a string and my y being NOT a string. Let's look at the fix the person suggests and see if it works.
In [3]:
print("y =" + str(y))
Awesome that works. So it seems the reason was because we tried to add a bunch of words together with a number, which is illegal. Python doesn't quite understand what we wanted to do with it. It's like adding a table and an orange together. It doesn't make sense.
However, notice the statement that was printed out. It wasn't pretty. The number is not spaced properly. So let's go back and add a space between = and the number to prettify that.
In [4]:
print("y = " + str(y))
Anyway that problem introduces us to types! Now there a few a types of things in Python.
In [23]:
type("random things I'm saying")
Out[23]:
Python thinks "random things I'm saying" is a string! So anything in quotation means Python will interpret it as a string of characters. It's not a number, it's not a list, it's a series of words.
In [25]:
type(5)
Out[25]:
Again, Python 5 is just an integer. Makes sense. Let's see what it thinks of decimal number.
In [28]:
type(5342.3243)
Out[28]:
Wow. It's a float. Why would Python think that? Apparently because there's a lot of more fancy stuff going on under the hood to carry decimals. What would happen if I put 5 in quotation marks. Will Python think it's a string?
In [27]:
type("5")
Out[27]:
Apparently so! Remember everything in quotation marks is considered a string of characters.
So to recap: we have now learned about string, int and float in Python. There's a bunch more data types we will cover in the future but for now let's remember that.
A lot of time we want to be interactive and actually get numbers or data from the users using our program. Python has a really neat function called input that will allow us to do that. Let's try it out.
In [ ]:
user_input = input() # type things in the box below
In [6]:
print("My user_input is " + user_input)
So I don't know what Python did but it takes what I wrote in and printed it out nicely. That's neat. Now let's try to do some calculation with the number I entered.
In [ ]:
user_input + 432
Again, the TypeError pops up. And again, it's because the types of the things we are adding aren't matching up. Let's do some investigation.
In [ ]:
type(user_input)