This notebook uses code snippets and explanations from this course.
Welcome to the course! In this course we will learn how to load, process and save data using a versatile programming language: Python. We are going to practise Python using Notebooks. These Notebooks contain instructions and so called 'code blocks'. The instructions are paragraphs of text that explain the concepts we are going to use. The 'code blocks' contain Python code.
Notebooks are pretty straightforward. Some tips:
Hint: when you're writing Python code, press Tab to auto-complete your variable names!
print()
If you have questions about this chapter, please refer to the forum on Canvas.
Now let's get started!
In [ ]:
# this will print some text
print("Hello, world!")
What happened here? Well, Python has a large set of built-in functions, and print()
is one of them. When you use this function, print()
outputs its argument to the screen. 'Argument' is a fancy word for "object you put in a function". In this case, the argument is the string "Hello, world!". And 'string' just means "a sequence of characters".
Did you also notice the first line starting with a hash (#) character? This is called a comment. We use comments to document our code and explain what's happening. These lines are not executed in Python. We will use them a lot in this course to make our code easy to understand!
Can you edit the block below in such a way that it will print out your own name?
In [ ]:
print("Hello, world!")
In [ ]:
# summing
print(3+2)
# subtracting
print(7-1)
In [ ]:
# multiplication
print(3*3)
# division
print(10/3)
In [ ]:
# power
print(5**2)
# combining stuff
print(5*2-3+4/2)
Instead of providing the string directly as an argument to the print
function, we can also create a variable that refers to the string value "Hello, world!". When you pass this variable to the print()
function, you get the same result as before:
In [ ]:
text = "Hello, world!"
print(text)
Such a piece of text ("Hello, world!") is called a string in Python (cf. a string of characters). Strings in Python must always be enclosed with 'quotes' (e.g. single or double quotes). Without those quotes, Python will think it's dealing with the name of some variable that has been defined earlier, because variable names never take quotes. The following distinction is confusing, but extremely important: variable names (without quotes) and string values (with quotes) look similar, but they serve a completely different purpose. Compare:
In [ ]:
name = "Patrick Bateman"
print("name") # this is a string value
print(name) # this is a variable name containing a string value
We can also assign numerical values to variables:
In [ ]:
x = 22
print(x)
If you vaguely remember your math classes in school, this should look familiar. It is basically the same notation with the name of the variable on the left, the value on the right, and the '=' sign in the middle. This is what is called assignment. We stored a value and named it using the '=' symbol, so that we can easily use it later on.
We can use the box metaphor to further explain this concept. The variable x
above behaves pretty much like a box on which we write an x with a thick, black marker to find it back later. In this box we can put whatever we want, such as a piece of text or a numerical value. In Python, the term variable refers to such a box, whereas the term value refers to what is inside this box.
Note that we can re-use variable names for other values, but that any assignment will overwrite the original value! In other words: when you re-asign a variable, you remove the content of the box and put something new in it. Each variable will always contain the value that you last assigned to it.
In [ ]:
text = "I like apples"
print(text)
text = "I like oranges"
print(text)
When we have stored values inside variables, we can do interesting things with these variables. Run the following code block to see what happens.
In [ ]:
x = 3
print(x)
print(x * x)
print(x + x)
print(x - 6)
Note that the variable names text
and x
used above are not part of Python. In principle, you could use any name you like. Even if you change the variable text
to something silly like pikachu
or sniffles
, the example would still work:
In [ ]:
sniffles = "Hello, world!"
print(sniffles)
However, variable names are only valid if they:
Even though you could use any variable name as long as they are valid, there are some naming conventions that are explained in the PEP8 Style Guide for Python Code. For now, it's enough to remember the following for naming your variables:
lowercase_with_underscores
style, with lowercase characters and underscores for separating words print
or sum
(these will turn green in Jupyter Notebooks)For example, the following variable name is valid, much more descriptive than x
would be and follows the naming conventions:
In [ ]:
seconds_in_seven_years = 220752000
print(seconds_in_seven_years)
We can also 'copy' the contents of a variable into another variable, which is what happens in the code below. In fact, what is happening is that the variable second_number
now refers to the same data object as first_number
. You should of course watch out in such cases: make sure that you keep track of the value of each individual variable in your code (later in the course, we will see that this is especially tricky with data types that are mutable, such as lists).
In [ ]:
first_number = 5
print(first_number)
second_number = first_number
first_number = 3
print(first_number)
print(second_number)
Have a look at this code at Python Tutor to see what's happening!
Up until now we have defined the values stored in the variables ourselves. However, we can also ask for input from a user. We'll make use of another built-in function: input(). This takes user input and returns it as a string. Try it below:
In [ ]:
text = input("Please enter some text: ")
print(text)
In [ ]:
# your code here
In [ ]:
days_in_year = 365
# assign each of the values to meaningful variable names
seconds_in_seven_years = days_in_year * # finish this line
print(seconds_in_seven_years)
In [ ]:
eggs = 3
_eggs = 6
5eggs = 5
eggs$ = 1
eggs123 = 9
ten_eggs = 10
TwelveEggs = 8
twelve.eggs = 12
In [ ]:
first_number = 3
second_number = 5
In [ ]:
# adapt the code
name1 = "Paul"
print("Hello,", name1)