Goal of Week 1

This week, we will:

  • Spend a little bit of time getting farmiliarized with the awesomeness of Jupyter Notebook (and also learn a few oddities that comes with it)
  • Try a few simple programs.
  • Learn the difference between Print() and Return.
  • And if we have time, we can load data from an excel/csv file.

Note

  • Remember to run each invidual cell you can hit Ctrl+Enter
  • If you want to add a cell above or below, you can hit Esc + A or Esc + B. Or you can go to the toolbar at the top -> Insert -> Insert Cell Above/Insert Cell Below
  • If you want to run all the cells altogether, go to the toolbar -> Cell -> Run All.
  • In the case something catastrophic happens and everything explodes, keep calmn, go to the the toolbar -> Kernal -> Restart

Let's get started with our first program!


In [1]:
print("Hello World")


Hello World

Awesome. Our "program" prints out exactly what we want to see. Now let's try to do some simple arithmetic.


In [1]:
6 + 6


Out[1]:
12

As you can see, the beauty of using a notebook is that it's very interactive. Unlike when you use IDLE or an IDE like Pycharm, each of the cell in this notebook can be indepently ran on its own OR be used all together. It also shows you the result right away.

Let's try some more simple operations.


In [6]:
6 - 4


Out[6]:
2

In [2]:
6*6


Out[2]:
36

In [3]:
6**6


Out[3]:
46656

In [4]:
6%4


Out[4]:
2

In [5]:
6//4


Out[5]:
1

So that's all pretty neat. Let's see now what will happen if we want to define some variables.


In [13]:
a = 6
b = 10
my_sum = a+b

If you just run the cell just like that, nothing will show up. This is because all you did was telling these variables to be something. You haven't called them out yet. Let's do that then.


In [14]:
a


Out[14]:
6

In [15]:
b


Out[15]:
10

In [16]:
my_sum


Out[16]:
16

That's neat. But let's say I want to print my result out a little bit more explicitly with some annotation of what the program is showing.


In [10]:
print("My result is" + my_sum)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-c6036218d305> in <module>()
----> 1 print("My result is" + my_sum)

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

That's weird. It didn't work. Let's google this [TypeError: Can't convert 'int' object to str implicitly ] error to see what it means. I found this: http://stackoverflow.com/questions/13654168/typeerror-cant-convert-int-object-to-str-implicitly

According to the top answer, "You cannot concatenate a string with an int. You would need to convert your int to string using str function, or use formatting to format your output."

So let's try the top answerer's solution now. We will put the str() surround my_sum to see if it works.


In [12]:
print("My result is " + str(my_sum))


My result is 16

Awesome. So now we know my_sum was apparently recognized and interpreted as interger in Python so when we try to add it to a word, Python couldn't figure out what we were trying to do. Now it can understand that yes, temporarily, we want to treat my_sum as a word so we can print it out.

Since I want to remember why I put my_sum in the str() thingy. I'm going to comment about it so I can remember later. Comments in Python are preceded with #. Everytime Python sees this #, it's going to ignore the rest of the line


In [17]:
print("My commented result is " + str(my_sum)) # cast my_sum as a string so I can print the whole statement


My commented result is 16

Great! Now let's go back to when we did our first program, "Hello World". What we just did was printing the words "Hello World". Let's see what happens when we try to do return instead of print.


In [3]:
return ('Hello World')


  File "<ipython-input-3-6f80631caf3c>", line 1
    return ('Hello World')
                          ^
SyntaxError: 'return' outside function

You can see that if you just return ("Hello World") instead of print, Python will give you a [SyntaxError: 'return' outside function error]. Let's search what the error is.

Not sure what everything means, but this might look like a problem: http://stackoverflow.com/questions/26824659/python-return-outside-function-syntax-error

The return command is part of the function definition, so must be indented under the function introduction (which is def donuts()). If it weren't, there would be no way for the interpreter to know that the return statement wasn't just a part of your broader code.

So it seems like return only works if it's called within a thing called "function". Let's try to do that.


In [6]:
def returnHelloWorld():
    return ("Hello World")

In [2]:
returnHelloWorld()
# if you just run this cell by itself NameError: name 'printHelloWorld' is not defined


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-a310237a119a> in <module>()
----> 1 printHelloWorld

NameError: name 'printHelloWorld' is not defined

Note that if you haven't ran the last cell, Python is going to be very confused and give a

NameError: name 'printHelloWorld' is not defined error

This is a very odd quirk of using Jupyter Notebook. The reason is each of these cells are acting indepently from each other. So if you just wrote the cell and don't run it (either using Ctrl+Enter or by navigating to Cell -> Run cells), the program will never be recognized.

So let's run the def printHelloWorld cell and see what happens when we call it again.


In [7]:
returnHelloWorld()


Out[7]:
'Hello World'

Awesome! Now it works!

Now before we go into something a little bit more "advanced", this will be a really good opportunity for me to show some difference between print and return. Hopefully you will never have to run the agony of figuring out what went wrong with your functions.

I'm going to write a new function that will print, not return, Hello World. In Python, a function starts with def. Not sure why they did it that way, but we are going to go with it.

def nameOfFunction(stuffYouWantToPassInIfThereIsAny):
    stuff you want to do # note how everything inside is indented

In [8]:
def printHelloWorld():
    print('Hello World')

Let's call printHelloWorld now


In [10]:
printHelloWorld()


Hello World

OK Quynh, this seems exactly the same as what returnHelloWorld did. What's the difference?

There's a difference! I will show you. Let's say now I want to have a new variable named "my_result", and I want to assign whatever result I get from the HelloWorld function to the variable "my_result". Then, I want whatever contained in my_result and add ", I'm here" to the end of it. Let's see how it works out.


In [15]:
my_result1 = returnHelloWorld()

In [18]:
my_result1 + ", I'm here"


Out[18]:
"Hello World, I'm here"

In [13]:
my_result2 = printHelloWorld()


Hello World

In [19]:
my_result2 + ", I'm here"


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-2bb499c0f66e> in <module>()
----> 1 my_result2 + ", I'm here"

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

The printHelloWorld function apparently didn't work like we wanted. Let's search up this error and see what comes up.

http://stackoverflow.com/questions/23372824/typeerror-unsupported-operand-types-for-nonetype-and-str

And the reason for this is because when we use the print statement, it only prints out for us too see. But it never returns anything so that the world outside of our printHelloWorld function can use. So as a matter of fact, my_result2 is actually just "None", or nothing.

Alright, that's all we need to mind about print and return. If you ever run into a problem where you don't know why your data is not showing up the way you want it too. This could be one of the reasons why.