Learning to program
Contents
Learning to program#
There are two primary challenges for people to learn to code:
Thinking like a computer (we call this the algorithms)
Understanding the words and symbols to use to tell the computer how to run the steps of the algorithm (syntax)
Algorithms#
Algorithms are simply a set of steps required to complete a task. We use algorithms all the time - for instance, the steps used to make a peanut butter and jelly sandwich or something very complex such as framing a house. Algorithms have a few different kinds of steps - for instance, we have steps for PREPARING, TASK EXECUTION, DECISION MAKING and REPEATING instructions. Each of the steps can be general steps - like grabbing peanut butter from the cabinet or taking an action on a particular item such as OPEN the peanut butter jar. Learning to think in this way is one of the first challenges for people learning to code.
Syntax#
In order to make a computer complete the tasks that we desire, we need to talk in a language that can be understood by the computer. This is called language syntax. There are many different languages/syntaxes that humans can use to tell the computer what to do. Python is the language we’ll use because it reads alot like english and can become more intuitive with a few less symbols than other languages.
Often times, new learners, once they have successfully identified the algorithm that completes the task at hand, converting this algorithm to words, phrases and symbols that the computer understands is the second challenge. These notebooks are focused on Python syntax, but students should constantly be on the lookout for the algorithms as well. The algorithms used here are small/simple tasks - but they are often building blocks for more complex algorithms.
Let’s talk Python#
Okay, there are many, many valuable resources to learn the ins and outs of Jupyter notebooks, so I’ll leave you to explore the many ways they can be used. If you are already a Python whiz try some of the Coding Challenges or explore Jupyter Notebook Tips,Tricks.
Now, one more thing before we get too far. You will see in many places, where there is non-computer code that gives other humans an idea of what is going on in the code. We call these pieces of text comments. Comments in Python start with a #
symbol and tell the computer that everything following this symbol on the current line is to be ignored. Comments are a valuable part of coding as they can help you to capture what you are trying to do (algorithm) before writing any code (syntax) and can be used once the code is in place to remember or clarify what exactly a bit of code is supposed to do. Here are a few examples of comments.
# this is the first comment
spam = 1 # and this is the second comment
# ... and now a third!
text = "# This is not a comment because it's inside quotes."
You will see comments sprinkled in among the examples, these don’t need to be typed to make your code work, but it’s a good idea to get in the habit early on so that you have good practice
Tip
You can always open these interactive notebooks in JupyterHub to run them, make changes and generally experiment. For help with this see the Welcome To Jupyter page
Strings, numbers, and lists#
All programming languages can deal with many different kinds of datatypes. The most common ones are boolean (True
or False
), numbers, strings, and collections of items. Numbers are represented just by typing them out with no special characters. Strings are characters that are surrounded by either '
or "
as we will see a bit later. Boolean values can only either be True
or False
and are incredibly valuable as we’ll see in a bit.
# Output a number
5
# Output a string
"This is a string"
# Output a boolean
True
# This is a list
[5, "Some String", True]
Output#
Typically in a Python program if we want to show the user some response we need to use a print function to see the result. Here’s a few examples:
print("This is a string")
print("This is a number: ", 5)
print("This is a boolean:", True)
One of the reasons we use Jupyter notebooks is so that we can see the result immediately without having to run the entire code base everytime, we can just run one piece of code at a time. Also, Jupyter doesn’t require us to print
our output (it’s just a handy way to try something without worrying about extra syntax).
# We can add numbers
2+2
Working with numbers#
Computers do math really well. Working with numbers is one of the easiest ways to bridge the gap between algorithms and syntax. If I were to ask you to count up the number of apples and oranges in a grocery basket, you know that you can simply count the apples and add to that number the number of oranges. This translates really well to Python, adding two numbers is done with a +
sign. The next section covers all the different operations we can perform on numbers.
Operators#
Operators are the constructs that execute on operands. For instance, in the equation 4+5,the operator is ‘+’ and the operands are 4 and 5. For more details on all the Python Operators check the on-line docs.
A few of the most valuable ones to us are ‘+’, ‘-’, ‘*’ (multiplication), ‘/’ (division), ‘>’, ‘<’, ‘=’, and ‘==’.
53*285
With Python, math works exactly as you expect. Equations are evaluated left to right, and the order of precidence can be changed using parentheses.
5+5*5 # This equals 30 because 5*5 happens first (order of precidence)
(5+5)*5 # This evaluates to 50 because we have told Python to add first due to parenthesis
5*(5+5) # Same thing, this will be 50 because of parens
Feel free to go to the previous cell and try a few more calculations. Or if you want to keep the original, just ‘insert’ a cell above this one and try a few others.
Caution
Python uses a single =
sign to specify assignment, whereas a double =
like ==
is a check for equality. So,
a = 5 # Set the value of a variable named 'a' to be the value 5
a == 5 # Returns True if the value of the variable a is 5 or False otherwise
Therefore in the example below, after assigning x to the value 10, the second statement tells us if the value of x is indeed 10
x = 10 # Doesn't display anything, we are just assigning the value 10 to the variable x
print(f'The value of x is {x}')
x == 10 # Displays True
Booleans#
Boolean dataypes play an extremly important role in programming. In fact, booleans are the only way in which decisions can be made in our code. (In the next section we’ll see how these decisions are made).
As a reminder, of the rules of boolean math
AND |
True |
False |
---|---|---|
True |
True |
False |
False |
False |
False |
OR |
True |
False |
---|---|---|
True |
True |
True |
False |
True |
False |
So therefore the operators >
greater than, <
less than and ==
equal to (and their counterparts >=
, <=
, and ~ (not)) are extremely important. Run the next cell for examples
print('Is 10 equal to 10? ')
print(10 == 10) # This will return True because 10 is equal to 10
print()
print ('Is 10 less than 5?',10 < 5) # This will of course be false
This one is a little trickier, before you run the cell what do you think the result will be?
(15 < 5) == False
Did this come out the way you thought? Let’s break it down a bit…
We start by evaluating the left side, (15 < 5). Of course we know that this is False so we can replace the left side with False
False == False
Now it’s easy to see, that False does indeed equal False, therefore the result is True!
Variables#
As you are no doubt already aware, variables are text used to store values in our programs. The computer doesn’t care what you call your variables, but other humans do, so name your variables something useful. For instance, take a look at this code, can you tell what the purpose of the code is?
# Clearly the result is to print the value of 5*12, but why?
x1 = 5
x2 = 12
x3 = x1*x2
print(x3)
A better option might be:
# In this example, it is very clear that we are calculating area of a rectangle
# The width of the rectangle is 5, the height of the rectangle is 12
# and the area of the rectangle is the product of the width and the height
width = 5
height = 12
area = width*height
print(area)
There are just a few rules for naming variables in Python
Variable names can only contain alpha-numeric characters and underscores
That is A-Z, a-z, 0-9 and
_
The variable name must start with a letter or the underscore
_
characterThe variable cannot start with a number
By convention, variable names are local case and if the name is two or more words, the words are separated by an _
. You will see variables that start with the _
character, but this is a special convention, so it’s best to avoid.
# Legal variable names
myvar = 'Joe'
my_var = 'Joe'
myVar = 'Joe'
MY_VAR = 'JOE'
my_var2 = 'Joe2'
# Illegal variable names
2my_var = 'Joe'
my-var = 'Joe'
my&var = 'Joe'
Strings#
Besides numbers, Python can also manipulate strings. Strings are one of the most basic concepts in many languages and have tremendous value, as we most often use text to communicate with users. Strings are used to provide real time feedback (think text boxes and labels, usernames and passwords), error messages, status logs, and communication with other programs. Getting good with manipulating strings is extremely important to be successful. Read about all the things you can do with strings in the Python tutorial.
Strings in Python can be expressed in several ways. They can be enclosed in single quotes(‘…’) or double quotes(“…”), both are equally acceptable.
print('spam eggs') # single quotes
print("Yes, I said that") # double quotes
If you need to use one or the other in your string definition, you can use the ‘’ as an escape character or just define the string using the opposite type of quote (that is if you need a single quote then wrap your string in double quotes). Run the next cell to see the results (note that the output for the interactive cell will always quote strings in single quotes.
'doesn\'t' # use \' to escape the single quote...
"doesn't" # ...or use double quotes instead
'"Yes," they said.'
"\"Yes,\" they said."
'"Isn\'t," they said.'
If you want a cleaner version of the output try using print()
instead
print('spam eggs')
print('"Yes," they said.')
Joining strings together#
Strings have some other handy features as well. Say we want to put two strings together, that’s easy just use the ‘+’ operator.
print('Hello '+'World!') # Two strings concatenated together
What’s more, all Python objects can be represented as strings (some more cleanly than others) so we can use this handy trick to show the result of a function for instance, we just have to tell Python to convert the thing to a string.
print('4 + 3 = ' + str(7)) # We need to tell Python this is supposed to be a string
Of course this also works with variables so long as they are strings as well
seven = '7' # Assigning the value of 7 to the variable named seven
print('4 + 3 = ', seven) # Since 'seven' is already a string (see the quotes above) no need to tell Python to convert.
String formatting#
This can be a little clunky when dealing with a string that has some variable in the middle of it like: “Hi, my name is Michael. What’s your name?”
In order to change the name Michael
to a variable the result would be something like
my_name = 'Michael'
'Hi, my name is '+my_name+'. What\'s your name?'
That gets ugly real quick. Fortunately, Python has helped us out by giving us ‘f-strings’ (f stands for format). So what we can do instead is to put an expression in the middle of the string and have it figured out when we need the string. So the last example looks like:
my_name = 'Michael'
f'Hi, my name is {my_name}. What\'s your name?'
In this way, everything in the {}
are figured out at the last possible minute. Here’s a few more examples
#Try it!
my_name = 'Joe'
print(f'Hi, my name is {my_name}. What\'s your name?')
And since everything inside the {}
is just seen as Python code we can ask Python to do some work and put the result into the string right away rather than putting in an intermediate variable
# We can use the result of a statement in a string
x = 4+8
print(f'Calculating stuff is easy too, see 4+8 = {x}')
# Or we can let Python calculate it straight away and skip defining the variable
print(f'Calculating stuff is easy too, see 4+8 = {4+8}')
Input#
Sending the results of calculations to the screen, to a file or across the world is super helpful and one of the main activities we’ll automate with Python. Also soliciting input from outside the program is equally useful. We’ll look at many different ways of getting data later on, but for now, the simplest way to do this is to use the input
function. input
will prompt the user to type some text in response to an optional prompt. Try the next cell and see what happens. (You may notice that the cell continues to run until you have answered the prompt and hit the enter key.)
input('Hello, what is your name?')
Just like other actions in Jupyter, the result of the operation is immediately shown in the cell’s result. (You should see the response you gave in the result cell.) Usually we want to keep track of what was given to us so that we can use it later on. In this case, we can use a variable to capture the result of the input
function and then print it. See the next example.
# Ask the user for their name, store the result in the variable user_name
user_name = input("Hello, what is your name?")
print("Your name is: " + user_name)
One thing that is important to note is that the result of the input()
function is always a string, even if the user types in numbers. This means if we want to assume that the result is a number that we will have to convert it from a string to a number. Try the next example:
Note
If you don’t enter a number as input, you will get an error message. We’ll talk about how to deal with this a bit more gracefully later, but for now it would be best to just input numbers at the prompt.)
# Ask the user for their age, store the result in user_age
user_age = input(f"Hello, {user_name}. What is your age?")
print('You said your age is: ', int(user_age))
Exercise 1
Can you now combine what you’ve learned so far and ask the user for their name and then ask the user for their age
You should then respond by greeting the user with the name and age.
Replace the ...
in the following code block
# Prompt the user for their name
u_name = ...
# Prompt the user for their current age
u_age = ...
# Output a greeting
print(...)
Lists#
Python knows about a number of different compound data types, (i.e. ways to store many values together). Two of the most common are list and dictionary. We’ll start first with a list and then give you a chance to work with a list in a simple challenge.
Lists are written as a set of values between square brackets that are separated by commas. While lists can have different types of ‘elements’, most often they are the same type. (e.g. list of strings, list of numbers, list of booleans etc.)
squares = [1,4,9,16,25,36] # This is a list of squares
people = ['Alice', 'Bob', 'Charlie'] # this is a list of people's names
print("Squares: ", squares)
print('People:', people)
What is super handy about lists is that they can be indexed, that is we can ask for a particular item in the list. The first item in the list is index 0 the last item in the list is the length of the list-1
. So a list with 5 elements can be indexed on any value between 0 and 4.
print('The item in index 0 of the list (the first item) is: ',squares[0])
print('The item in index 4 of the list (the fifth item) is: ',squares[4])
# What happens if you try to get the item at index 6?
# Do you expect to get the 6th item in the list? (36) or something else?
Slicing#
Sometimes we want more than one item in the list, in that case we can ask Python to get sequential items by telling it the first item we want and the last item. (It can be a little confusing, but Python doesn’t include the right side of the slice, so that a slice of 0:2 only includes items 0 and 1).
people[0:2] # Give me the items in slot 0 and slot 1
squares[1:4] # Return the items from slot 1 to slot 4 (non-right inclusive)
The full syntax of the slice
subscripting method is [start:stop:step] meaning if we want to get every other item then we just define the step
to be 2, if we want every 3rd item, then the step is 3 and so on.
squares = [1,4,9,16,25,36]
# This notation says, starting at index 1, move 2 spots and give me the next number until I reach index 6
squares[1:6:2]
And if we leave a part out, Python guesses what we mean. For instance if we leave out the starting point, Python assumes 0, if we leave out the end point, Python assumes the end, and if we leave out the step Python assumes 1.
print(f'Leaving out the starting point')
squares[:4:2] # Give me the items starting at index 0 to index 4 by 2's
print(f'Leaving out the end point')
squares[2::3] # Give me the items starting at index 2 (9) to the end, by 3's
Finally, Python also knows what to do with negative numbers. We can use negative numbers to index starting at the right or as our step.
squares[-1] # Give me the first value starting on the right of the list
squares[5::-1] # Starting at index 5, give me all the values in reverse order
For more see sequence types (including strings) and slicing.
Lists support concatenation just like strings
squares + [49,64,81]
Lists are changable too, so we can do something like
f'Names before the change: {people}'
people[1] = 'Bonita'
f'Names after the change: {people}'
Another handy feature of sequence types is that they support several built-in functions. Probably one of the most useful is the len()
function, which returns an integer that shows the length of the sequence.
len(people)
len(squares)
Finally, lists can contain other lists. In the example below, I’ve created a list of numbers n
and a list of letters l
. Then I created a list that has two items in it, the list n
and the list l
.
n = [1,2,3] # Define a list of integers and assign it to a variable named 'n'
l = ['a','b','c'] # This is a list of characters in a list called 'a'
x = [n,l] # Here we are creating a list of lists!
print(x)
The first item in the list x
is actually the list n
, and the second item in the list is the list of letters l
print('The item at index 0 is a list of numbers : ', x[0])
print(f'The item at index 1 is a list of letters: ', x[1])
So if we want to get at the items in list n
we can unpack the list into another variable or use a shortcut.
# We can assign the list to a new variable first and then index it
y = x[0] # Assign y to the list of numbers
print(f'The value in the index 1 of the sublist is {y[1]}\n')
# Or we can just do a double subscript
print(f'Same value using the double subscript approach:')
print(x[0][1])
Example: Hello Worl?#
This next bit of code is kind of messed up. Use what you have learned about strings, integers and indexing to fix the message stored in message
. It seems that when I typed up this example I left out a letter and also used a question mark instead of a !
. Can you fix this by using indexing and replacement and then print the parts into a single line? You should be able to
print("Before the fix.")
message = ['Hello', 'Worl','?']
print(f'{message[0]} {message[1]}{message[2]}')
# Replace the `...` in these next two lines with the proper words
message[1] = ...
message[2] = ...
print("After the fix.")
print(f'{message[0]} {message[1]}{message[2]}')
Example: Indexing days of the month# One more challenge.#
The list here is the number of days in the months of the year. Complete the code below so that it tells the days of the month requested
# A list with the days of the month
days_in_month = [31,28,31,30,31,30,31,31,30,31,30,31]
# Since June is the 6th month of the year, we want the 6th item in the list
print('The number of days in June is: ', days_in_month[5])
print('The number of days in December is: ', ...)
# Get the number of days in Jan
# Get the number of days in Feb
# Add these two previous values together and use this to print the answer in the string below
print('The number of days in Jan and Feburary added together is:',...)
Dictionaries#
Lists are great when we are okay with accessing the items in order or if we know exactly where the items in the list are that we want, but sometimes it is handy to access items in a collection using a more descriptive name. One approach is to use a data structure called a dictionary. Dictionaries are similar to lists in that they store a collection of items, but instead of using numbers to get to the content we can use strings.
Name |
Number |
---|---|
Alice |
555-479-2222 |
Bob |
555-555-1234 |
Charles |
888-555-9889 |
Say we want to store a list of people’s phone numbers. It would be really inconvenient to put there name and phone number in a list and then search everyone for who we are looking for.
phone_numbers = ['Alice 555-479-2222',
'Bob 555-555-1234',
'Charles 888-555-9889']
Instead we can store the phone numbers in a list and use the names as the ‘keys’ to lookup the phone numbers.
In order to define a dictionary we have to provide both the ‘key’ (the index) and the ‘value’ (the thing we are storing). These are separated by a ‘:’ and we separate items within the dictionary with ‘,’ just like in a list.
# This says, create a dictionary where Alice, Bob and Charlie are the keys/index
# and their numbers are the values
# Notice, the items are on separate lines for readability
phone_numbers = {'Alice':'555-479-2222',
'Bob':'555-555-1234',
'Charles':'888-555-9889'}
# Then we can grab just Alice's phone number
print("Alice's phone number is " + phone_numbers['Alice'])
# And also we can get Bob's
print(f"Bob's phone number is: {phone_numbers['Bob']}")
Example Dictionaries#
In the next code block, again we’ll see the days of the month, but instead of knowing which month it is in the year, we’re going to store the dates using the month’s name.
days_in_month_dict = {'Jan':31,'Feb':28,'Mar':31, 'Apr':30, 'May':31, 'Jun':30, 'Jul':31, 'Aug':31, 'Sep':30, 'Nov':30, 'Oct':31, 'Dec':31}
print('The number of days in June is: ', days_in_month_dict['Jun'])
print('The number of days in December is: ', ...)
# Get the number of days in Jan
# Get the number of days in Feb
# Add these two previous values together and use this to print the answer in the string below
print('The number of days in Jan and Feburary together is:',...)
Conclusion#
This is a good start and a very basic introduction to types in Python, there are many, many more things that can be done with built-in functions to manipulate strings, numbers, dates and lists. In order to do anything interesting (more complex algorithms) our code needs to be able to make decisions and execute different actions based on conditions - often many times over. In the next notebook we’ll look at how to get Python to make decisions and act on the results.
Feel free to play around here by adding cells and experimenting with different language elements. When you are ready to move on, head over to the next section.