Goal of Week 1

This week, we will:

  • Spend a little bit of time getting farmiliarized with Python the language. What we think are the pros and cons with the language.
  • Install Python.
  • Learn some basic operators in Python.
  • Learn about variables and the different types of data in Python (int, float, string, etc.).
  • Learn about getting user's input using the input() function.
  • Write our first if, else if, and else statements.

Note

  • In this notebook, I will run the code cell-by-cell to show you the error and the result that Python gives out every time we run a snippet of code. However, you will most likely use a IDE (a.k.a Integrated Development Environment) like the default Python IDE, PyCharm, or something else, which is absolutely fine.

Let's get started!

1. Install Python

  • If you haven't had Python installed, head to this link and click ‘Download Python 3.5.2’
  • Install :)

2. Open Python & Create Our Program

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.

Basic Math Operators in Python

Now let's start with some basic math operations Python has.


In [1]:
3 + 5 # addition


Out[1]:
8

In [2]:
3 - 5 # subtraction


Out[2]:
-2

In [5]:
3 * 5 # multiplication


Out[5]:
15

In [7]:
3/5 # division


Out[7]:
0.6

In [8]:
3 ** 5 # power


Out[8]:
243

In [9]:
4 // 3 # taking the interger


Out[9]:
1

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]:
1

In [16]:
2%4


Out[16]:
2

Variables

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)


25

Printing

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)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-4bedd5c4c32c> in <module>()
----> 1 print("y =" + y)

TypeError: Can't convert 'int' object to str implicitly

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))


y =25

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))


y = 25

Anyway that problem introduces us to types! Now there a few a types of things in Python.

Data Types


In [23]:
type("random things I'm saying")


Out[23]:
str

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]:
int

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]:
float

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]:
str

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.

Getting User's Input

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 [3]:
user_input = input() # type things in the box below


4324

In [2]:
print("My user_input is " + user_input)


My user_input is 4324

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 [4]:
user_input + 432


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-e0d28031836d> in <module>()
----> 1 user_input + 432

TypeError: Can't convert 'int' object to str implicitly

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 [6]:
type(user_input)


Out[6]:
str

Wow. So even though we input a number, Python thinks that's a string! That's outrageous. So that means if I want to do math operations with that number, I have to convert it to an int or a float so Python know.


In [7]:
user_input = int(user_input)

In [8]:
type(user_input)


Out[8]:
int

In [9]:
user_input + 432


Out[9]:
4756

Now it's legal! Let's now move on to make if statements.

If, Else If, and Else

Almost in any programming language, you can write a boolean statement to see if things are true or false.


In [12]:
my_number = 5

In [15]:
if my_number > 3:
    print("my_number is > 3")
if my_number <= 10:
    print("my_number is <= 10")
if my_number < 0:
    print("my_number is < 0")


my_number is > 3
my_number is <= 10

So that works as we expected. Python runs through the 3 conditions, check if my_number satisty each one, and does the work. Now let's see what happens when we use an if, elif, and else instead.


In [14]:
if my_number > 3:
    print("my_number is > 3")
elif my_number <= 10:
    print("my_number is <= 10")
elif my_number < 0:
    print("my_number is < 0")
else:
    print("I don't know who I am!")


my_number is > 3

Even though my_number satisfy the second condition (my_number <= 10), the second print statement didn't happen! The reason for this because once Python run through the first condition, meets the requirement (my_number > 3), it does the job, and is done! There's no more work for Python to do!

So the if, elif, else case would be extremely useful if you are running through a set of criteria and needs to satisfy only 1 requirement.