Help and Documentation
Overview
Teaching: 5 min
Exercises: 0 minQuestions
Where can you find more help and documentation?
Objectives
Know where to get further help.
Help and documentation
Official Python Documentation
- Python’s official website is the primary resource for all things related to the Python programming language.
- https://www.python.org/
W3Schools
- W3Schools is a popular online platform for learning web development technologies.
- https://www.w3schools.com/python/
GeeksforGeeks
- GeeksforGeeks is a comprehensive platform for computer science and programming resources.
- https://www.geeksforgeeks.org/python-programming-language/
Key Points
Data Types
Overview
Teaching: 20 min
Exercises: 0 minQuestions
What kind of data types exist in Python?
What are the key differences between the data types?
Objectives
Understand the essential data types in Python
Explain what a string is, and what you can do with it
Explain the differences between integers and floats
Explain what a boolean is, and how to use comparisons
Data Types
There are four essential kinds of Python data with different powers and capabilities:
- Strings (Text)
- Integers (Whole Numbers)
- Floats (Decimal Numbers)
- Booleans (True/False)
Take a look at the two examples below.
What differences do you notice?
'Here is a some text'
42
You might be wondering…
Why is ‘Here is some text’ surrounded by quotation marks while 42 is not?
Because these are two different “types” of Python data. We will look closer at the different types below.
Note, that many Python editors and environments (such as Juptyter Lab etc.) will colour the output based on data type. E.g. in Jupyter Lab strings will be in red and integers in green.
Data Type |
Explanation |
Example |
---|---|---|
String |
Text |
'Anything goes 4 strings!' |
Integer |
Whole Numbers |
42 |
Float |
Decimal Numbers |
3.1415926 |
Boolean |
True/False |
False |
Check Data Types
You can check the data type of any value by using the function type()
.
type('Here is some text')
str
The output str
is short for string.
type(42)
int
The output int
is short for integer.
Strings
A string is a Python data type that is treated like text, even if it contains a number. Strings are always enclosed by either single quotation marks 'this is a string'
or double quotation marks "this is a string"
.
'this is a string'
"this is also a string, even though it contains a number like 42"
this is not a string
It doesn’t matter whether you use single or double quotation marks with strings, as long as you use the same kind on either side of the string.
Bonus
How can you have quotation marks inside a string?
Escape characters
Escape characters and how to tell Python to igonre special meanings. This can be handy if you need to make quotation marks inside a string. This can be done in two ways.
Use the opposite kind of quotation mark inside the string:
"She exclaimed, 'This is a quotation inside a string!'"
Or “escape” the quotation mark by using a backslash
\
before it:"She exclaimed, \"This is also a quotation inside a string!\""
String Methods
Each data type has different properties and capabilities. So there are special things that only strings can do, and there are special ways of interacting with strings.
For example, you can index and slice strings, you can add strings together.
Here are a few examples:
Index
Often in programming languages, individual items in an ordered set of data, can be accessed directly using a numeric index or key value. This process is referred to as indexing.
In Python, strings are ordered sequences of character data, and thus can be indexed in this way. Individual characters in a string can be accessed by specifying the string name followed by a number in square brackets []
.
String indexing in Python is zero-based: the first character in the string has index 0, the next has index 1, and so on. The index of the last character will be the length of the string minus one. It can be illustrated like this:
The individual characters can be accessed by index:
'I am a string'[0]
'I'
'I am a string'[7]
's'
Slice
Python allows a form of indexing syntax that extracts substrings from a string, known as string slicing.
If s
is a string, an expression of the form s[start:stop]
returns the portion of s
starting with position start
, and up to but not including position stop
:
'I am a string'[0:8]
'I am a s'
Concatenation
The +
operator concatenates strings. It returns a string consisting of the operands joined together, as shown here:
'I am a string' + ' and so am I'
'I am a string and so am I'
Notice that we have added a space in the beginning of the second string, otherwise there would be no space between ‘string’ and ‘and’. You can also add a space between two strings like this:
'I am a string' + ' ' + 'and so am I'
'I am a string and so am I'
Integers & Floats
An integer and a float are two Python data types for representing numbers.
- Integers represent whole numbers.
- Floats represent numbers with decimal points.
Integers and floats do not need to be placed in quotation marks.
type(42)
int
type(3.1415926)
float
Mathematical operations
You can do a large range of mathematical calculations and operations with integers and floats. Here are a few examples, for an extended overview you can fold out a table at the end of this section.
Multiplication
You can multiply in Python using the *
operator
4 * 2
8
Exponents
You can use **
as the exponent operator. An exponent is an expression of the number of times a number is muliplied by itself.
4 ** 2
16
Modulus
The modulus or remainder operator looks like this: a % b
.
However, it does not work as a percentage although it might look like one. Instead it divides a with b and the remainder is returned.
72 % 10
2
More mathemathical operators
These are just a few of the mathmathical operations in Python - see the table below from Python’s documentation about Built-in Types.
Click here to see more mathmathical operators
Operation |
Explanation |
---|---|
|
sum of |
|
difference of |
|
product of |
|
quotient of |
|
floored quotient of |
|
remainder of |
|
|
|
|
|
absolute value or magnitude of |
|
|
|
|
|
|
|
|
Booleans
Booleans are “truth” values. They report on whether things in your Python universe are True
or False
. There are the only two options for a boolean: True
or False
. The boolean operators are or, and, and not. They are used to check if certain conditions are met before the program continues running. This is extremely helpful and even if it seems a bit confusing right now, it will make sense later. Here are a few examples of using boolean logic.
13 < 17
True
In this example above we state that 13 is less than 17 - which returns True
because 13 is less than 17.
"hello" == "hi"
False
In the example above we state that ‘hello’ is equal to ‘hi’, which in the computers understanding it is not. Therefore, we get False
as the output.
666 == 777
False
In this example above we state that 666 is equal to 777, which it is not. Therefore, we get False
as the output.
Table of Boolean operations:
Operation |
Result |
---|---|
|
if either x or y is true, then |
|
if both x and y is true, then |
|
if x is false, then |
Comparisons
You can compare values in Python using different comparison operations. Comparisons are used to compare different values and objects in Python, which we will learn much more about later. For now take a look at the comparisions and their meaning in the table underneath.
This table summarizes the comparison operations:
Comparison operation |
Explanation |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Notice the difference between a single equals sign
=
and a double equals sign==
- A double equals sign
==
is used as the equals operator- A single equals sign
=
is used for variable assignment (We will learn more about this in the lesson about variables)
Key Points
There are 4 data types in Python: Integers, floats, strings, and booleans
You can use the built-in function
type()
to find the type of a valueStrings are text, they can be added to one another, you can slice them to get a substring, and use the index to acess the individual character
Integers are whole numbers. You can use mathemethical operations on them
Floats are decimal numbers. You can use mathemathical opreations on them
Booleans are either True or False
A double equals sign == is used as the equals operator, a single equals sign = is used for variable assignment
Variables and Assignment
Overview
Teaching: 15 min
Exercises: 15 minQuestions
How can I store data in programs?
Objectives
Write scripts that assign values to variables and perform calculations with those values.
Correctly trace value changes in scripts that use assignment.
Use variables to store values
Variables are one of the fundamental building blocks of Python. A variable is like a tiny container where you store values and data, such as filenames, words, numbers, collections of words and numbers, and more.
The variable name will point to a value that you “assign” it. You might think about variable assignment like putting a value “into” the variable, as if the variable is a little box 🎁
(In fact, a variable is not a container as such but more like an adress label that points to a container with a given value. This difference will become relevant once we start talking about lists and mutable data types.)
You assign variables with an equals sign (=
). In Python, a single equals sign = is the “assignment operator.” (A double equals sign ==
is the “real” equals sign.)
- Variables are names for values.
- In Python the
=
symbol assigns the value on the right to the name on the left. - The variable is created when a value is assigned to it.
- Here, Python assigns an age to a variable
age
and a name in quotation marks to a variablefirst_name
:
age = 42
first_name = 'Ahmed'
Variable names
Variable names can be as long or as short as you want, but there are certain rules you must follow.
Variables:
- Cannot start with a digit.
- Cannot contain spaces, quotation marks, or other punctuation.
- May contain an underscore (typically used to separate words in long variable names).
- Having an underscore at the beginning of a variable name like
_alistairs_real_age
has a special meaning. So we won’t do that until we understand the convention. - The standard naming convention for variable names in Python is the so-called “snake case”,
where each word is separated by an underscore.
For examplemy_first_variable
.
You can read more about naming conventions in Python here.
Use meaningful variable names
Python doesn’t care what you call variables as long as they obey the rules (alphanumeric characters and the underscore).
As you start to code, you will almost certainly be tempted to use extremely short variables names like f
. Your fingers will get tired. Your coffee will wear off. You will see other people using variables like f
. You’ll promise yourself that you’ll definitely remember what f
means. But you probably won’t.
So, resist the temptation of bad variable names! Clear and precisely-named variables will:
- Make your code more readable (both to yourself and others).
- Reinforce your understanding of Python and what’s happening in the code.
- Clarify and strengthen your thinking.
flabadab = 42
ewr_422_yY = 'Ahmed'
print(ewr_422_yY, 'is', flabadab, 'years old')
Use meaningful variable names to help other people understand what the program does.
The most important “other person” is your future self!
Python is case-sensitive
Python thinks that upper- and lower-case letters are different, so Name
and name
are different variables.
There are conventions for using upper-case letters at the start of variable names so we will use lower-case letters for now.
Off-Limits Names
The only variable names that are off-limits are names that are reserved by, or built into, the Python programming language itself — such as print
, True
, and list
. Some of these you can overwrite into variable names (not ideal!), but Jupyter Lab (and many other environments and editors) will catch this by colour coding your variable. If your would-be variable is colour-coded green, rethink your name choice. This is not something to worry too much about. You can get the object back by resetting your kernel.
Use print()
to display values
We can check to see what’s “inside” variables by running a cell with the variable’s name. This is one of the handiest features of a Jupyter notebook. Outside the Jupyter environment, you would need to use the print()
function to display the variable.
first_name
Ahmed
You can run the print()
function inside the Jupyter environment, too. This is sometimes useful because Jupyter will only display the last variable in a cell, while print()
can display multiple variables. Additionally, Jupyter will display text with \n
characters (which means “new line”), while print()
will display the text appropriately formatted with new lines.
- Python has a built-in function called
print()
that prints things as text. - Provide values to the function (i.e., the things to print) in parentheses.
- To add a string to the printout, wrap the string in single or double quotations.
- The values passed to the function are called ‘arguments’ and are separated by commas.
- When using the
print()
function, we can also separate with a ‘+’ sign. However, when using ‘+’ we have to add spaces in between manually.
print(first_name, 'is', age, 'years old')
Ahmed is 42 years old
print()
automatically puts a single space between items to separate them.- And wraps around to a new line at the end.
Variables must be created before they are used
If a variable doesn’t exist yet, or if the name has been misspelled, Python reports an error (unlike some languages, which “guess” a default value).
print(eye_color)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[1], line 1
----> 1 print(eye_color)
NameError: name 'eye_color' is not defined
The last line of an error message is usually the most informative. This message lets us know that there is no variable called eye_color in the script.
Variables Persist Between Cells
Variables defined in one cell exist in all other cells once executed, so the relative location of cells in the notebook do not matter (i.e., cells lower down can still affect those above).
- Notice the number in the square brackets
[ ]
to the left of the cell.- These numbers indicate the order, in which the cells have been executed.
- Cells with lower numbers will affect cells with higher numbers as Python runs the cells chronologically.
- As a best practice, we recommend you keep your notebook in chronological order so that it is easier for the human eye to read and make sense of, as well as to avoid any errors if you close and reopen your project, and then rerun what you have done.
Remember: Notebook cells are just a way to organize a program!
As far as Python is concerned, all of the source code is one long set of instructions.
Variables can be used in calculations
- We can use variables in calculations just as if they were values.
Remember, we assigned 42 toage
a few lines ago.
age = age + 3
print('Age in three years:', age)
Age in three years: 45
This code works in the following way. We are reassigning the value of the variable age
by taking its previous value (42) and adding 3, thus getting our new value of 45.
Use an index to get a single character from a string
- The characters (individual letters, numbers, and so on) in a string are ordered. For example, the string ‘AB’ is not the same as ‘BA’. Because of this ordering, we can treat the string as a list of characters.
- Each position in the string (first, second, etc.) is given a number. This number is called an index or sometimes a subscript.
- Indices are numbered from 0 rather than 1.
- Use the position’s index in square brackets to get the character at that position.
element = 'helium'
print(element[0])
h
Use a slice to get a substring
A part of a string is called a substring. A substring can be as short as a single character. A slice is a part of a string (or, more generally, any list-like thing). We take a slice by using [start:stop]
, where start
is replaced with the index of the first element we want and stop
is replaced with the index of the element just after the last element we want. Mathematically, you might say that a slice selects [start:stop]
. The difference between stop and start is the slice’s length. Taking a slice does not change the contents of the original string. Instead, the slice is a copy of part of the original string.
element = 'sodium'
print(element[0:3])
sod
Use the built-in function len()
to find the length of a string
The built-in function len()
is used to find the length of a string (and later, of other data types, too).
element = 'helium'
print(len(element))
6
Note that the result is 6 and not 7. This is because it is the length of the value of the variable (i.e. 'helium'
) that is being counted and not the name of the variable (i.e. element
)
Also note that nested functions are evaluated from the inside out, just like in mathematics. Thus, Python first reads the len()
function, then the print()
function.
Exercises
Choosing a Name
Which is a better variable name,
m
,min
, orminutes
? Why? Hint: think about which code you would rather inherit from someone who is leaving the library:
ts = m * 60 + s
tot_sec = min * 60 + sec
total_seconds = minutes * 60 + seconds
Solution
minutes
is better becausemin
might mean something like “minimum” (and actually does in Python, but we haven’t seen that yet).
Swapping Values
Draw a table showing the values of the variables in this program after each statement is executed. In simple terms, what do the last three lines of this program do?
x = 1.0 y = 3.0 swap = x x = y y = swap
Solution
swap = x # x->1.0 y->3.0 swap->1.0 x = y # x->3.0 y->3.0 swap->1.0 y = swap # x->3.0 y->1.0 swap->1.0
These three lines exchange the values in
x
andy
using theswap
variable for temporary storage. This is a fairly common programming idiom.
Predicting Values
What is the final value of
position
in the program below? (Try to predict the value without running the program, then check your prediction.)initial = "left" position = initial initial = "right"
Solution
initial = "left" # Initial is assigned the string "left" position = initial # Position is assigned the variable initial, currently "left" initial = "right" # Initial is assigned the string "right" print(position)
left
The last assignment to position was “left”
Can you slice integers?
If you assign
a = 123
, what happens if you try to get the second digit ofa
?Solution
Numbers are not stored in the written representation, so they can’t be treated like strings.
a = 123 print(a[1])
TypeError: 'int' object is not subscriptable
Slicing
What does the following program print?
library_name = 'social sciences' print('library_name[1:3] is:', library_name[1:3])
If
thing
is a variable name,low
is a low number, andhigh
is a high number:
- What does
thing[low:high]
do?- What does
thing[low:]
(without a value after the colon) do?- What does
thing[:high]
(without a value before the colon) do?- What does
thing[:]
(just a colon) do?- What does
thing[number:negative-number]
do?Solution
library_name[1:3] is: oc
- It will slice the string, starting at the
low
index and ending an element before thehigh
index- It will slice the string, starting at the
low
index and stopping at the end of the string- It will slice the string, starting at the beginning on the string, and ending an element before the
high
index- It will print the entire string
- It will slice the string, starting the
number
index, and ending a distance of the absolute value ofnegative-number
elements from the end of the string
Key Points
Use variables to store values.
Use meaningful variable names.
Python is case-sensitive.
Use
print()
to display values.Variables must be created before they are used.
Variables persist between cells.
Variables can be used in calculations.
Use an index to get a single character from a string.
Use a slice to get a substring.
Use the built-in function
len
to find the length of a string.
Data Types and Type Conversion
Overview
Teaching: 15 min
Exercises: 20 minQuestions
What kinds of data do variables store?
How can I convert one type to another?
Objectives
Explain key differences between integers and floating point numbers.
Explain key differences between numbers and character strings.
Use built-in functions to convert between integers, floating point numbers, and strings.
Recap from lesson about Data Types
Every value has a type
- Every value in a program has a specific type.
- Integer (
int
): whole numbers like 3 or -512. - Floating point number (
float
): fractional numbers like 3.14159 or -2.5. - Whole numbers may also be stored as floats, e.g.
1.0
, but1.0
would still be stored as afloat
. - Character string (usually called “string”,
str
): text (but not necessarily only letters).- Written in either single quotes or double quotes (as long as they match).
- The quotation marks aren’t printed using
print()
, but may appear when viewing a value in the Jupyter Notebook or other Python interpreter.
Use the built-in function type
to find the type of a value
We can use the built-in function type
to find out what type a value has.
- This works on variables as well.
- But remember: the value has the type — the variable is just a label.
- When you change the value of a variable to a new data type, the results of
print(type(your_variable))
will change accordingly.
print(type(52))
<class 'int'>
title = 'Biochemistry'
print(type(title))
<class 'str'>
Types control what operations (or methods) can be performed on a given value
- A value’s type determines what the program can do to it.
print(5 - 3)
2
print('hello' - 'h')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-67f5626a1e07> in <module>()
----> 1 print('hello' - 'h')
TypeError: unsupported operand type(s) for -: 'str' and 'str'
You can use the +
and *
operators on strings
- “Adding” character strings concatenates them.
full_name = 'Ahmed' + ' ' + 'Walsh'
print(full_name)
Ahmed Walsh
- Multiplying a character string by an integer N creates a new string that consists of that character string repeated N times (since multiplication is repeated addition).
- There are more ways that traditional math operators will work on other data types. There isn’t a perfect formula for figuring out what they do, so experimentation is valuable.
separator = '=' * 10
print(separator)
==========
Here, the variable “separator” is set to the value “=” (equals sign) ten times in a row.
Strings have a length (but numbers don’t)
- The built-in function
len
counts the number of characters in a string.
print(len(full_name))
11
- But numbers don’t have a length (not even zero).
print(len(52))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-f769e8e8097d> in <module>()
----> 1 print(len(52))
TypeError: object of type 'int' has no len()
Preventing Errors: Handling numbers and strings in Python Operations
- You cannot add numbers and strings.
print(1 + 'A')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-fe4f54a023c6> in <module>()
----> 1 print(1 + '2')
TypeError: unsupported operand type(s) for +: 'int' and 'str'
- This is not allowed because it’s ambiguous: should
1 + '2'
be3
or'12'
? - Some types can be converted to other types by using the type name as a function.
print(1 + int('2'))
print(str(1) + '2')
3
12
Integers and floats can be mixed freely in operations
- Integers and floating-point numbers can be mixed in arithmetic.
- Python automatically converts integers to floats as needed.
print('half is', 1 / 2.0)
print('three squared is', 3.0 ** 2)
half is 0.5
three squared is 9.0
Variables only change value when something is assigned to them
- If we make one cell in a spreadsheet depend on another, and update the latter, the former updates automatically.
- This does not happen in programming languages.
first = 1
second = 5 * first
first = 2
print('first is', first, 'and second is', second)
first is 2 and second is 5
- The computer reads the value of
first
when doing the multiplication, creates a new value, and assigns it tosecond
. - After that,
second
does not remember where it came from.
Exercises
Fractions
What type of value is 3.4? How can you find out?
Solution
It is a floating-point number (often abbreviated “float”).
print(type(3.4))
<class 'float'>
Automatic Type Conversion
What type of value is 3.25 + 4?
Solution
It is a float: integers are automatically converted to floats as necessary.
result = 3.25 + 4 print(result, 'is', type(result))
7.25 is <class 'float'>
Choose a Type
What type of value (integer, floating point number, or character string) would you use to represent each of the following? Try to come up with more than one good answer for each problem. For example, in # 1, when would counting days with a floating point variable make more sense than using an integer?
- Number of days since the start of the year.
- Time elapsed since the start of the year.
- Standard book loan period.
- Number of reference queries in a year.
- Average library classes taught per semester.
Solution
- Integer
- Float
- Integer
- Integer
- Float
Strings to Numbers
Where reasonable,
float()
will convert at string or an integer to a floating point number, andint()
wil convert a string or a floating point number to an integer.print("string to float:", float("3.4")) print("float to int:", int(3.4))
string to float: 3.4 float to int: 3
Note: conversion is some times also called typecast.
If the conversion doesn’t make sense, however, an error message will occur
print("string to float:", float("Hello world!"))
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-5-df3b790bf0a2> in <module>() ----> 1 print("string to float:", float("Hello world!")) ValueError: could not convert string to float: 'Hello world!'
Given this information, what do you expect the following program to do?
What does it actually do?
Why do you think it does that?
print("fractional string to int:", int("3.4"))
Solution
What do you expect this program to do? It would not be so unreasonable to expect the Python
int
command to convert the string “3.4” to 3.4 and an additional type conversion to 3. After all, Python performs a lot of other magic - isn’t that part of its charm?However, Python throws an error. Why? To be consistent, possibly.
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[1], line 1 ----> 1 print("fractional string to int:", int("3.4")) ValueError: invalid literal for int() with base 10: '3.4'
If you ask Python to perform two consecutive typecasts, you must convert it explicitly in code.
num_as_string = "3.4" num_as_float = float(num_as_string) num_as_int = int(num_as_float) print(num_as_int)
3
We could also write it in a single line like this:
int(float("3.4"))
Arithmetic with Different Types
Which of the following will print 2.0? Note: there may be more than one right answer.
first = 1.0 second = "1" third = "1.1"
first + float(second)
float(second) + float(third)
first + int(third)
first + int(float(third))
int(first) + int(float(third))
2.0 * second
Solution
Answer: 1 and 4.
- is correct
- gives 2.1
- gives an error because we cannot convert text to int directly
- is correct
- gives 2 (as an integer not as a float)
- gives an error because
second
is a string.
Division Types
Recap:
There are three different types of division:
- ‘Normal’ division (aka floating-point division) is what most people may be familiar with: 5 / 2 = 2.5
- Floor division, which cuts out the part after the period: 5 // 2 = 2
- Modulo division, which only keeps the remained after division: 5 % 2 = 1
In Python 3, the
/
operator performs floating-point division, the//
operator performs floor division, and the ‘%’ (or modulo) operator calculates the modulo division:print('5 / 3:', 5/3) print('5 // 3:', 5//3) print('5 % 3:', 5%3)
5 / 3: 1.6666666666666667 5 // 3: 1 5 % 3: 2
Exercise:
If
num_students
is the number of students enrolled in a course (let say 600), andnum_per_class
is the number that can attend a single class (let say 42), write an expression that calculates the number of classes needed to teach everyone.The output should look like this:
600 students, 42 per class 14 full classes, plus an extra class with only 12 students
Solution
Depending on requirements it might be important to detect when the number of students per class doesn’t divide the number of students evenly. Detect it with the
%
operator and test if the remainder that it returns is greater than 0.num_students = 600 num_per_class = 42 num_classes = num_students // num_per_class remainder = num_students % num_per_class print(num_students, 'students,', num_per_class, 'per class') print(num_classes, 'full classes, plus an extra class with only', remainder, 'students')
600 students, 42 per class 14 full classes, plus an extra class with only 12 students
Key Points
Every value has a type.
Use the built-in function
type
to find the type of a value.Types control what operations can be done on values.
Strings can be added and multiplied.
Strings have a length (but numbers don’t).
Preventing Errors: Handling numbers and strings in Python operations.
Integers and floats can be mixed freely in operations.
Variables only change value when something is assigned to them.
Built-in Functions and Help
Overview
Teaching: 25 min
Exercises: 15 minQuestions
How can I use built-in functions?
How can I find out what they do?
What kind of errors can occur in programs?
Objectives
Explain the purpose of functions.
Correctly call built-in Python functions.
Use help to display documentation for built-in functions.
Correctly describe situations in which SyntaxError and NameError occur.
Use comments to add documentation to programs
When coding, it is always a good idea to write comments explaining what the code does - and why.
Comments will help other people understand what the program does.
Just like we spoke about in regards to meaningful variable names, the most important “other person” is your future self!
# This sentence isn't executed by Python.
name = 'Python for Absolute Beginners' # Neither is this comment - anything after '#' is ignored.
A function may take zero or more arguments
We have seen some functions already (like print()
and type()
) — now let’s take a closer look.
- An argument is a value passed into a function.
- Any arguments you want to pass into a function must go into the
()
print("I am an argument and must go here.")
- You must always use parentheses, because this is how Python knows you are calling a function.
- You leave them empty if you don’t want or need to pass any arguments in.
len
takes exactly one argument.int
,str
, andfloat
create a new value from an existing one.print
takes zero or more arguments.print()
prints a blank line.
print('before')
print()
print('after')
before
after
Commonly-used built-in functions include max
, min
, and round
- Use
max
to find the largest value of one or more values. - Use
min
to find the smallest value of one or more values. - Both work on character strings as well as numbers.
- “Larger” and “smaller” use (0-9, A-Z, a-z) to compare letters.
- This means that:
'a'
is smaller than'b'
'A'
is smaller than'a'
'0'
is smaller than'A'
- This is useful for ordering alphabetically.
print(max(1, 2, 3))
print(min('a', 'b', 'c'))
print(min('a', 'A'))
3
a
A
To see more built-in functions, visit the Python Documentation
Functions may only work for certain (combinations of) arguments
max
andmin
must be given at least one argument.- And they must be given things that can meaningfully be compared.
print(max(1, 'a'))
TypeError: '>' not supported between instances of 'str' and 'int'
The error message tells us, that we cannot compare strings and integers.
Functions may have default values for some arguments
round
will round off a floating-point number.- By default, rounds to zero decimal places.
round(3.712)
4
- We can specify the number of decimal places we want.
round(3.712, 1)
3.7
Every function returns something
- Every function call produces some result.
- If the function doesn’t have a useful result to return,
it usually returns the special value
None
.
result = print('example')
print('result of print is', result)
example
result of print is None
Contrary to what we might expect, print
does not return any value as such. I carries out the execution (i.e., it prints),
but after that nothing is returned. Thus, it has a return value of None
.
Use the built-in function help
to get help for a function
- Every built-in function has online documentation.
help(round)
Help on built-in function round in module builtins:
round(...)
round(number[, ndigits]) -> number
Round a number to a given precision in decimal digits (default 0 digits).
This returns an int when called with one argument, otherwise the
same type as the number. ndigits may be negative.
Python reports a syntax error when grammar rules (that’s Python grammar, not English grammar) have been violated
You’ve seen errors when you try to use a function incorrectly, but you can also have errors when you use punctuation incorrectly.
- Python will run the program up until that point, but if the grammar of that line of code has produced an error, then the program will shut down with an error.
- Error messages will try and guide you to the point, where the error happened.
- Python error messages may vary slightly depending on your Python environment.
# Forgot to close the quotation marks around the string.
name = 'Feng
name = 'Feng
^
SyntaxError: unterminated string literal
# An extra '=' in the assignment.
age = = 52
age = = 52
^
SyntaxError: invalid syntax
- Look more closely at the error message:
print("hello world"
Cell In[1], line 1
print("hello world"
^
SyntaxError: incomplete input
- The message indicates a problem in cell number 1 on first line of the input (“line 1”).
- Next is the problematic line of code,
indicating the problem with a
^
pointer.
Python reports a runtime error when something goes wrong while a program is executing
In Python, a runtime error is an error that occurs while a Python program is running. These errors are also known as exceptions.
Runtime errors in Python are typically caused by unexpected conditions or events that disrupt the normal flow of the program.
They can happen for various reasons, for example:
age = 53
remaining = 100 - aege # mis-spelled 'age'
NameError: name 'aege' is not defined
- Fix syntax errors by reading the source.
- Fix runtime errors by tracing execution.
Exercises
Spot the Difference
- Predict what each of the
- Does
max(len(rich), poor)
run or produce an error message? If it runs, does its result make sense to you? (Remember that we are comparing the value of the variable and not the name of the variable.)rich = "gold" poor = "tin" print(max(rich, poor)) print(max(len(rich), len(poor)))
Solution
tin 4
What Happens When
- Explain in simple terms the order of operations in the following program: when does the addition happen, when does the subtraction happen, when is each function called, etc.
- What is the final value of
word
?word = 'blah ' word = max(min(word * 2 + 'blur ', 'aaah '), 'Ping') print(word)
Solution
aaah
- Initialization:
word
is assigned the initial value ‘blah ‘.- Inside
max()
function:
word * 2
results in ‘blah blah ‘. (Repeats the string ‘blah ‘ twice using*
.)- ‘blah blah ‘ + ‘blur ‘ results in ‘blah blah blur ‘. (Concatenates the two strings using
+
.)- ‘aaah ‘ is compared with ‘blah blah blur ‘. The
min()
function returns the smaller of the two strings, which is ‘aaah ‘.- Outer
max()
function:
- The result of the
min()
function (‘aaah ‘) is passed as an argument.- The
max()
function then compares ‘aaah ‘ with ‘Ping’ and returns the larger of the two strings, which is ‘aaah’, since lower case letters are indexed higher than upper case letters in Python.- Assignment: Finally, the value ‘aaah’ is assigned to the variable
word
.- Output: The program prints the value of
word
, which is ‘aaah’.
Why Not?
Why don’t
max
andmin
returnNone
when they are given no arguments?Solution
Both functions require at least one argument to execute.
print(max())
TypeError: max expected at least 1 argument, got 0
Last Character of a String
If Python starts counting from zero, and
len
returns the number of characters in a string, what index expression will get the last character in the stringname
? (Note: we will see a simpler way to do this in a later episode.)Solution
name[len(name) - 1]
Key Points
Use comments to add documentation to programs.
A function may take zero or more arguments.
Commonly-used built-in functions include
min
,max
, andround
.Functions may only work for certain (combinations of) arguments.
Functions may have default values for some arguments.
Every function returns something.
Use the built-in function
help
to get help for a function.Python reports a syntax error when it can’t understand the source of a program.
Python reports a runtime error when something goes wrong while a program is executing.
Lists
Overview
Teaching: 20 min
Exercises: 20 minQuestions
How can I store multiple items?
Objectives
Explain why programs need collections of items.
Write programs that create lists, index them, slice them, and modify them through assignment and method calls.
A list stores many items in a single structure
Scenario: You have set up an thermometer to do temperature measurements in a storage room for rare books.
- Doing calculations with a hundred variables called
temperature_001
,temperature_002
, etc., would be at least as slow as doing them by hand. - Use a list to store many items together.
- List items are contained within square brackets
[...]
. - List items are separated by commas
,
. - List items are ordered by their index number.
- List items are contained within square brackets
- Use
len
to find out how many items are in a list.
temperatures = [17.3, 17.5, 17.7, 17.5, 17.6]
print('temperatures:', temperatures)
print('length:', len(temperatures))
temperatures: [17.3, 17.5, 17.7, 17.5, 17.6]
length: 5
Use an item’s index to fetch it from a list
- Just like with strings, we can use index to find a given item in a list.
- And just like with strings, the elements in a list are 0-indexed. (See lesson about Data Types.)
print('zeroth item of temperatures:', temperatures[0])
print('fourth item of temperatures:', temperatures[4])
zeroth item of temperatures: 17.3
fourth item of temperatures: 17.6
Slice
- Just like with strings, we can use indexing syntax to slice lists.
- If
l
is a list, an expression of the forml[start:stop]
returns the portion ofl
starting with positionstart
, and up to but not including positionstop
. - Take a look at the example with the list of temperatures:
print(temperatures)
print(temperatures[1:4])
[17.3, 17.5, 17.7, 17.5, 17.6]
[17.5, 17.7, 17.5]
Lists’ items can be replaced by assigning to them
Use an index expression on the left of the assignment operator (=
) to replace a value:
temperatures[0] = 16.5
print('temperatures is now:', temperatures)
temperatures is now: [16.5, 17.5, 17.7, 17.5, 17.6]
Appending items to a list lengthens it
Use list_name.append
to add items to the end of a list:
print('temperatures is initially:', temperatures)
temperatures.append(17.9)
temperatures.append(18.2)
print('temperatures has become:', temperatures)
temperatures is initially: [16.5, 17.5, 17.7, 17.5, 17.6]
temperatures has become: [16.5, 17.5, 17.7, 17.5, 17.6, 17.9, 18.2]
append
is a method of lists.- Like a function, but tied to a particular object.
- Use
object_name.method_name
to call methods. - Use
help(list)
for a preview.
Table of list methods
For more in-depth information, see the Python Documentation.
Method | Description |
---|---|
append() |
Adds an element at the end of the list |
clear() |
Removes all the elements from the list |
copy() |
Returns a copy of the list |
count() |
Returns the number of elements with the specified value |
extend() |
Add the elements of a list (or any iterable), to the end of the current list |
index() |
Returns the index of the first element with the specified value |
insert() |
Adds an element at the specified position |
max() |
Calculates the maximum of all the elements of the list |
min() |
Calculates the minimum of all the elements of the list |
pop() |
Removes the element at the specified position |
remove() |
Removes the first item with the specified value |
reverse() |
Reverses the order of the list |
sort() |
Sorts the list |
Use del
to remove items from a list entirely
del list_name[index]
removes an item from a list and shortens the list.- Not a function or a method, but a statement in the language.
numbers = [2, 3, 5, 7, 11]
print('numbers before removing last item:', numbers)
del numbers[4]
print('numbers after removing last item:', numbers)
numbers before removing last item: [2, 3, 5, 7, 11]
numbers after removing last item: [2, 3, 5, 7]
The empty list contains no items
- Use
[]
on its own to represent a list that doesn’t contain any items.- “The zero of lists.”
- Helpful as a starting point for collecting values (which we will see in the next episode about For Loops).
Lists may contain items of different types
A single list may contain numbers, strings, and anything else.
goals = [1, 'Create lists.', 2, 'Extract items from lists.', 3, 'Modify lists.']
print(goals)
[1, 'Create lists.', 2, 'Extract items from lists.', 3, 'Modify lists.']
Lists are mutable
In Python, mutable data types, such as lists, are data structures that can be modified or changed after they are created. This means you can add, remove, or modify elements within a list without creating a new list.
For example, you can append new items, insert items at specific positions, remove items, and change the values of existing items in a list. This behavior contrasts with immutable data types, like integers, floats, and strings, where once created, their contents cannot be altered without creating a new object. (This was briefly mentioned in the lesson about Variables and Assignment.)
Character strings are immutable
Remember that you can get single characters from a character string using indexes in square brackets:
element = 'carbon'
print('zeroth character:', element[0])
print('third character:', element[3])
zeroth character: c
third character: b
But!
- You cannot alter the characters in a string after it has been created.
- Immutable: cannot be changed after creation. E.g., strings.
- In contrast, lists are mutable: they can be modified in place.
- Python considers the string to be a single value with parts, not a collection of values.
element[0] = 'C'
TypeError: 'str' object does not support item assignment
- Lists and character strings are both collections.
Notice the difference between overwriting and changing values
In Python, when you use the assignment operator (
=
) with a variable (and not a variable index!), it doesn’t change the original value of the variable. Instead, it replaces the current value of the variable with the new value on the right-hand side of the assignment.element = 'carbon' print(element) element = 'helium' print(element)
carbon helium
In the code above, the variable
element
initially holds the value ‘carbon’, but when the second line is executed, it is overwritten with the new value ‘helium’. The old value ‘carbon’ is effectively discarded, andelement
now contains ‘helium’.In contrast, when assigning to a list index:
elements = ['carbon', 'helium'] print(elements) elements[0] = 'hydrogen' print(elements)
['carbon', 'helium'] ['hydrogen', 'helium']
We don’t overwrite the variable
elements
but rather we change the content of the zeroth index.
Thus, the difference between immutable and mutable data types.
Indexing beyond the end of the collection is an error
- Python reports an
IndexError
if we attempt to access a value that doesn’t exist.- This is a kind of runtime error.
print('99th element of element is:', element[99])
IndexError: string index out of range
Exercises
Fill in the Blanks
Fill in the blanks so that the program below produces the output shown.
values = ____ values.____(1) values.____(3) values.____(5) print('first time:', values) values = values[____] print('second time:', values)
first time: [1, 3, 5] second time: [3, 5]
Solution
values = [] values.append(1) values.append(3) values.append(5) print('first time:', values) values = values[1:3] print('second time:', values)
first time: [1, 3, 5] second time: [3, 5]
How Large is a Slice?
If ‘low’ and ‘high’ are both non-negative integers, how long is the list
values[low:high]
?Solution
The list’s length would be equal to
high - low
.
Working With the End
What does the following program print?
element = 'helium' print(element[-1])
- How does Python interpret a negative index?
- If a list or string has N elements, what is the most negative index that can safely be used with it, and what location does that index represent?
- If
values
is a list, what doesdel values[-1]
do?- How can you display all elements but the last one without changing
values
? (Hint: you will need to combine slicing and negative indexing.)Solution
m
- A negative index begins at the final element.
-(N)
corresponds to the first index, which is the [0] index.- It removes the final element of the list.
- You could do the following:
print(values[0:-1])
Stepping Through a List
What does the following program print?
element = 'fluorine' print(element[::2]) print(element[::-1])
- If we write a slice as
low:high:stride
, what doesstride
do?- What expression would select all of the even-numbered items from a collection?
Solution
furn eniroulf
stride
indicates both the number of steps, and from which end: positive starts from first element, negative from the last element.element[1::2]
Slice Bounds
What does the following program print?
element = 'lithium' print(element[0:20]) print(element[-1:3])
Solution
lithium ''
There is no 20th index, so the entire string is captured.
There is no element after the -1 index.
Sort and Sorted
What do these two programs print? In simple terms, explain the difference between
sorted(letters)
andletters.sort()
.# Program A letters = list('gold') result = sorted(letters) print('letters is', letters, 'and result is', result)
# Program B letters = list('gold') result = letters.sort() print('letters is', letters, 'and result is', result)
Solution
Program A:
letters is ['g', 'o', 'l', 'd'] and result is ['d', 'g', 'l', 'o']
Program B:
letters is ['d', 'g', 'l', 'o'] and result is None
sorted(letters)
returns a sorted copy of the list without changing the original list, whileletters.sort()
sorts the original list but does not return anything, i.e. returnsNone
.
Copying (or Not)
What do these two programs print? In simple terms, explain the difference between
new = old
andnew = old[:]
.# Program A old = list('gold') new = old # simple assignment new[0] = 'D' print('new is', new, 'and old is', old)
# Program B old = list('gold') new = old[:] # assigning a slice new[0] = 'D' print('new is', new, 'and old is', old)
Solution
Program A:
new is ['D', 'o', 'l', 'd'] and old is ['D', 'o', 'l', 'd']
Program B:
new is ['D', 'o', 'l', 'd'] and old is ['g', 'o', 'l', 'd']
new = old
is assigningold
tonew
. This means that the two variables both point to the same value. Thus, changing the contents of either variable will affect the other. In contrast,new = old[:]
is a slice assignment, which will only return a copy ofold
.
From Strings to Lists and Back
Given this:
print('string to list:', list('tin')) print('list to string:', ''.join(['g', 'o', 'l', 'd'])) print('list to string:', '-'.join(['g', 'o', 'l', 'd']))
string to list: ['t', 'i', 'n'] list to string: gold list to string: g-o-l-d
- Explain in simple terms what
list('some string')
does.- What does
'-'.join(['x', 'y'])
generate?Solution
- It creates a list of the
some string
s characters as elements.- It creates a string composed of
x
andy
, separated by a hyphen character(-
).
Key Points
A list stores many items in a single structure.
Use an item’s index to fetch it from a list.
Use slicing to extract part of a list.
Lists’ items can be replaced by assigning to them.
Appending items to a list lengthens it.
Use
del
to remove items from a list entirely.The empty list contains no items.
Lists may contain items of different types.
Lists are mutable.
Indexing beyond the end of the collection is an error.
For Loops
Overview
Teaching: 20 min
Exercises: 20 minQuestions
How can I make a program repeat a task?
Objectives
Explain what for loops are normally used for.
Trace the execution of a simple (unnested) loop and correctly state the values of variables in each iteration.
Write for loops that use accumulator values.
A for loop executes commands once for each value in a collection
- Doing calculations on the values in a list one by one
is as painful as working with
temperature_001
,temperature_002
, etc. - A for loop tells Python to execute some statements once for each value in a list, a character string, or some other collection.
- “for each thing in this group, do these operations”
for number in [2, 3, 5]:
print(number)
- This
for
loop is equivalent to:
print(2)
print(3)
print(5)
- And the
for
loop’s output is:
2
3
5
- The variable name ‘number’ is a name that you choose, when you create the for loop.
- You can choose any variable name you want.
- But don’t use an already existing variable name, as this will then be overwritten.
- The variable persists after the loop is done and will contain the last used value.
- In the case above, ‘number’ will contain the integer 5 after the for loop is finished.
The first line of the for
loop must end with a colon, and the body must be indented
- The colon at the end of the first line signals the start of a block of statements.
- Python uses indentation to show nesting (rather than
{}
orbegin
/end
, which are used in some other programming languages).- Any consistent indentation is legal, but almost everyone uses four spaces or tab.
- IDEs like Jupyter Lab will automatically create an indentation after the colon.
for number in [2, 3, 5]:
print(number)
print(number)
^
IndentationError: expected an indented block after 'for' statement on line 1
- Indentation is always meaningful in Python.
firstName = "Jon"
lastName = "Smith"
lastName = "Smith"
^
IndentationError: unexpected indent
- This error can be fixed by removing the indentation at the beginning of the second line.
A for
loop is made up of a collection, a loop variable, and a body
for number in [2, 3, 5]:
print(number)
- The collection,
[2, 3, 5]
, is what the loop is being run on. - The body,
print(number)
, specifies what to do for each value in the collection. - The loop variable,
number
, is what changes for each iteration of the loop.
Loop variable names follow the normal variable name conventions
- Loop variables will:
- Be created on demand during the course of each loop.
- Persist after the loop finishes.
- Often be used in the course of the loop.
- So give them a meaningful name you’ll understand as the body code in your loop grows.
- Example:
for single_letter in ['A', 'B', 'C', 'D']:
instead offor asdf in ['A', 'B', 'C', 'D']:
.
- Use a new variable name to avoid overwriting a data collection you need to keep for later
The body of a loop can contain many statements
- You can have as much code as you like inside of a loop.
- But keep in mind, the more code you have, the more difficult it can be to keep track of.
numbers = [2, 3, 5]
for n in numbers:
squared = n ** 2
cubed = n ** 3
print("The number", n, "squared is:", squared, ", and the number", n, "cubed is:", cubed)
The number 2 squared is: 4 , and the number 2 cubed is: 8
The number 3 squared is: 9 , and the number 3 cubed is: 27
The number 5 squared is: 25 , and the number 5 cubed is: 125
Use range
to go through a sequence of numbers
- The built-in function
range
produces a sequence of numbers.- Not a list: the numbers are produced on demand to make looping over large ranges more efficient.
range(N)
is the numbers 0, 1, 2, … N-1
print('A range is not a list: range(0, 3)')
for number in range(0, 3):
print(number)
A range is not a list: range(0, 3)
0
1
2
Or use range
to repeat an action a set number of times
- You don’t actually have to use the iterable variable’s value.
- Use this structure to simply repeat an action some number of times.
- That number of times goes into the
range
function.
- That number of times goes into the
for number in range(5):
print("Again!")
Again!
Again!
Again!
Again!
Again!
Using accumulator variables
- A common pattern in programs is to:
- Initialize an accumulator variable to zero, the empty string, or the empty list.
- Update the variable with values from a collection.
# Sum all of the integers from 1 to 10.
total = 0
for number in range(10):
total = total + (number + 1)
print(total)
55
- Read
total = total + (number + 1)
as:- Add 1 to the current value of the loop variable
number
.- We have to add
number + 1
becauserange
produces the numbers 0…9, not 1…10.
- We have to add
- Add that to the current value of the accumulator variable
total
. - Assign that to
total
, replacing the current value.
- Add 1 to the current value of the loop variable
Exercises
Classifying Errors
Is an indentation error a syntax error or a runtime error?
Solution
It is a syntax error. The problem has to do with the placement of the code, not its logic.
Tracing Execution
Create a table showing the numbers of the lines that are executed when this program runs, and the values of the variables after each line is executed.
total = 0 for animal in ['cat', 'dog', 'elephant', 'fish']: total = total + 1
Solution
Code line # total
animal
1 0 N/A 2 0 'cat' 3 1 'cat' 2 1 'dog' 3 2 'dog' 2 2 'elephant' 3 3 'elephant' 2 3 'fish' 3 4 'fish' Explanation:
- Line 1 initializes the variable
total
with a value of 0.- Line 2 begins a for loop that iterates over the list
['cat', 'dog', 'elephant', 'fish']
.
- Line 2 is the first iteration, where
animal
takes the value ‘cat’, andtotal
retains 0.- Line 3 increments
total
by 1, making it 1, andanimal
retains the value ‘cat’.- Line 2 is the second iteration, where
animal
takes the value ‘dog’, andtotal
retains 1.- Line 3 increments
total
by 1, making it 2, andanimal
retains the value ‘dog’.- Line 2 is the third iteration, where
animal
takes the value ‘elephant’, andtotal
retains 2.- Line 3 increments
total
by 1, making it 3, andanimal
retains the value ‘elephant’.- Line 2 is the fourth iteration, where
animal
takes the value ‘fish’, andtotal
retains 3.- Line 3 increments
total
by 1, making it 4, andanimal
retains the value ‘fish’.After the program finishes,
total
is 4, andanimal
retains its value from the last iteration, which is ‘fish’.
Reversing a String
Fill in the blanks in the program below so that it prints “nit” (the reverse of the original character string “tin”).
original = "tin" result = ____ for char in original: result = ____ print(result)
Solution
result
is an empty string because we use it to build or accumulate on our reverse string.char
is the loop variable fororiginal
.- For each iteration of the loop,
char
takes on one value (character) fromoriginal
.- Add
char
to the beginning ofresult
to change the order of the string.- Our loop code should look like this:
original = "tin" result = "" for char in original: result = char + result print(result)
If you were to explain the loop step by step, the iterations would look something like this:
#First loop char = "t" result = "" char + result = "t" #Second loop char = "i" result = "t" char + result = "it" #Third loop char = "n" result = "it" char + result = "nit"
Practice Accumulating
Fill in the blanks in each of the programs below to produce the indicated result.
# Total length of the strings in the list: ["red", "green", "blue"] => 12 total = 0 for word in ["red", "green", "blue"]: ____ = ____ + len(word) print(total)
Solution
# Total length of the strings in the list: ["red", "green", "blue"] => 12 total = 0 for word in ["red", "green", "blue"]: total = total + len(word) print(total)
# List of word lengths: ["red", "green", "blue"] => [3, 5, 4] lengths = ____ for word in ["red", "green", "blue"]: lengths.____(____) print(lengths)
Solution
# List of word lengths: ["red", "green", "blue"] => [3, 5, 4] lengths = [] for word in ["red", "green", "blue"]: lengths.append(len(word)) print(lengths)
# Concatenate all words: ["red", "green", "blue"] => "redgreenblue" words = ["red", "green", "blue"] result = ____ for ____ in ____: ____ print(result)
Solution
# Concatenate all words: ["red", "green", "blue"] => "redgreenblue" words = ["red", "green", "blue"] result = "" for word in words: result = result + word print(result)
# Create acronym: ["red", "green", "blue"] => "rgb" # write the whole thing
Solution
acronym = '' colours = ["red", "green", "blue"] for colour in colours: acronym = acronym + colour[0] print(acronym)
Cumulative Sum
Reorder and properly indent the lines of code below so that they print an array with the cumulative sum of data. The result should be
[1, 3, 5, 10]
.cumulative += [sum] for number in data: cumulative = [] sum += number print(cumulative) sum = 0 data = [1,2,2,5]
Solution
data = [1,2,2,5] cumulative = [] sum = 0 for number in data: sum += number cumulative += [sum] print(cumulative)
[1, 3, 5, 10]
Identifying Variable Name Errors
- Read the code below and try to identify what the errors are without running it.
- Run the code and read the error message. What type of
NameError
do you think this is? Is it a string with no quotes, a misspelled variable, or a variable that should have been defined but was not?- Fix the error.
- Repeat steps 2 and 3, until you have fixed all the errors.
for number in range(10): # use a if the number is a multiple of 3, otherwise use b if (Number % 3) == 0: message = message + a else: message = message + "b" print(message)
Identifying Item Errors
- Read the code below and try to identify what the errors are without running it.
- Run the code, and read the error message. What type of error is it?
- Fix the error.
seasons = ['Spring', 'Summer', 'Fall', 'Winter'] print('My favorite season is ', seasons[4])
Solution
It is an index error:
IndexError: list index out of range
The problem is that
4
points to an item that doesn’t exist in the list. Remember the first item of a list in Python is0
.
Replaceseasons[4]
withseasons[0]
,seasons[1]
,seasons[2]
orseasons[3]
to have the different items of the list printed.
Key Points
A for loop executes commands once for each value in a collection.
The first line of the
for
loop must end with a colon, and the body must be indented.Indentation is always meaningful in Python.
A
for
loop is made up of a collection, a loop variable, and a body.Loop variables can be called anything (but it is strongly advised to have a meaningful name to the looping variable).
The body of a loop can contain many statements.
Use
range
to iterate over a sequence of numbers.
Conditionals
Overview
Teaching: 25 min
Exercises: 20 minQuestions
How can you use conditional statements to control program flow?
Objectives
Explain the use of if and else statements and simple Boolean expressions.
Explain the use of while statements.
Trace the execution of unnested conditionals and conditionals inside loops.
Use if
statements to control whether or not a block of code is executed
- An
if
statement (more properly called a conditional statement) controls whether some block of code is executed or not. - The structure is similar to a
for
statement:- First line opens with
if
and ends with a colon. - Body containing one or more statements is indented (usually by 4 spaces or tab).
- First line opens with
mass = 3.54
if mass > 3.0:
print(mass, 'is larger than 3.0')
mass = 2.07
if mass > 3.0:
print (mass, 'is larger than 3.0')
3.54 is larger than 3.0
Conditionals are often used inside loops
- There is not much point in using a conditional when we know the value (as above).
- But it is useful when we have a collection to process.
masses = [3.54, 2.07, 9.22, 1.86, 1.71]
for m in masses:
if m > 3.0:
print(m, 'is larger than 3.0')
3.54 is larger than 3.0
9.22 is larger than 3.0
Use else
to execute a block of code when an if
condition is not true
else
can be used following anif
.- It allows us to specify an alternative to execute when the
if
branch isn’t taken.
masses = [3.54, 2.07, 9.22, 1.86, 1.71]
for mass in masses:
if mass > 3.0:
print(mass, 'is larger than 3.0')
else:
print(mass, 'is smaller than 3.0')
3.54 is larger than 3.0
2.07 is smaller than 3.0
9.22 is larger than 3.0
1.86 is smaller than 3.0
1.71 is smaller than 3.0
Use elif
to specify additional tests
- You may want to provide several alternative choices, each with its own test.
- Use
elif
(short for “else if”) and a condition to specify these. - Must always be associated with an
if
. - Must come before the
else
(which is the “catch all”). elif
- likeelse
- is only executed if theif
condition is not.
masses = [3.54, 2.07, 9.22, 1.86, 1.71]
for mass in masses:
if mass > 9.0:
print(mass, 'is HUGE')
elif mass > 3.0:
print(mass, 'is larger')
else:
print(mass, 'is smaller')
3.54 is larger
2.07 is smaller
9.22 is HUGE
1.86 is smaller
1.71 is smaller
Conditions are tested once, in order
- Python steps through the branches of the conditional in order, testing each in turn.
- But if one conditional is fulfilled, Python will “step out of” the statement and not test the following conditionals.
- So ordering matters!
- Take a look at this script:
points = 85
if points >= 70:
print('grade is C')
elif points >= 80:
print('grade is B')
elif points >= 90:
print('grade is A')
grade is C
- What is the problem with the script above?
- The problem is that a grade can never be higher than ‘C’, since any
points
above 70 will be “caught” by theif
condition. Thus, theelif
conditions will never be tested. - A better way to write the above script, would be:
points = 85
if points >= 90:
print('grade is A')
elif points >= 80:
print('grade is B')
elif points >= 70:
print('grade is C')
grade is B
- Remember that Python scripts are run “chronologically”, i.e., from the top down.
- This means, that a script will not automatically go back and re-evaluate if values change.
speed = 10.0
if speed > 20.0:
print('moving too fast')
else:
print('adjusting speed')
speed = 50.0
adjusting speed
- Often conditionals are used in a loop to “evolve” the values of variables.
speed = 10.0
for i in range(5): # execute the loop 5 times
print(i, ':', speed)
if speed > 20.0:
print('moving too fast')
speed = speed - 5.0
else:
print('moving too slow')
speed = speed + 10.0
print('final speed:', speed)
0 : 10.0
moving too slow
1 : 20.0
moving too slow
2 : 30.0
moving too fast
3 : 25.0
moving too fast
4 : 20.0
moving too slow
final speed: 30.0
Compound statements; using
and
,or
, and parentheses
- Often, you want some combination of things to be true.
- You can combine relations within a conditional using
and
and/oror
.- This is called compound statements.
- Continuing the example above, suppose you have:
mass = [ 3.54, 2.07, 9.22, 1.86, 1.71] speed = [10.00, 20.00, 30.00, 25.00, 20.00] for i in range(5): if mass[i] > 5 and speed[i] > 20: print("Fast heavy object. Duck!") elif mass[i] > 2 and mass[i] <= 5 and speed[i] <= 20: print("Normal traffic") elif mass[i] <= 2 and speed[i] <= 20: print("Slow light object. Ignore it.") else: print("Whoa! Something is up with the data. Check it.")
- Just like with arithmetic, you can and should use parentheses whenever there is possible ambiguity.
- If no parentheses are used, Python will read code based on operator precedence.
- However, a good general rule is to always use parentheses when mixing
and
andor
in the same condition.- That is, instead of:
if mass[i] <= 2 or mass[i] >= 5 and speed[i] > 20:
You should write it like one of these two so it is perfectly clear to a reader (and to Python) what you really mean:
if (mass[i] < 2 or mass[i] > 5) and speed[i] > 20: if mass[i] < 2 or (mass[i] > 5 and speed[i] > 20):
- Notice that the two statements above test for something different!
- The first statement requires
mass
to be outside the range of 2 to 5 and thespeed
to be greater than 20.- The second statement requires either of these two to be true:
mass
must be smaller than 2, ormass
must be greater than 5 andspeed
must be greater than 20.
While loops
In Python, a while loop allows you to repeatedly execute a block of code as long as a certain condition is true. This can be useful when you want to perform an action multiple times until a specific condition is met.
The while
syntax
The basic structure of a while loop in Python is as follows:
while condition:
# Code to be executed as long as the condition is true
- The loop will continue to execute as long as the condition remains
True
. - Once the condition becomes
False
, the loop will exit.
count = 1
while count <= 5:
print(count)
count += 1
1
2
3
4
5
- In this example, the loop prints numbers from 1 to 5.
- The
count
variable is incremented in each iteration until it becomes greater than 5, at which point the condition becomesFalse
, and the loop stops.
Infinite loops
- Remember always to set a counter or similiar to make sure the
while
loop stops at some point. Otherwise, you create an infinite loop!- If you accidentally create an infinite loop, you can stop it by pressing the “stop” button (“Interrupt the kernel”) at the top of Jupyter Lab (and similar).
- If you don’t have a stop button, use CTRL-C to stop the script.
- Take a look at the code below. What is wrong with it?
i = 0
while True:
print(i)
i += 1
- The problem is that the
while
condition never turnsFalse
. Thus, the loop will carry on infinitely. - A way to stop the loop could be:
i = 0
while i <= 100:
print(i)
i += 1
Exercises:
Tracing Execution
What does this program print?
pressure = 71.9 if pressure > 50.0: pressure = 25.0 elif pressure <= 50.0: pressure = 0.0 print(pressure)
Solution
25.0
Trimming Values
Fill in the blanks so that this program creates a new list containing zeroes (0) where the original list’s values were negative and ones (1) where the original list’s values were zero or positive.
original = [-1.5, 0.2, 0.4, 0.0, -1.3, 0.4] result = ____ for value in original: if ____: result.append(0) else: ____ print(result)
[0, 1, 1, 1, 0, 1]
Solution
original = [-1.5, 0.2, 0.4, 0.0, -1.3, 0.4] result = [] for value in original: if value < 0: result.append(0) else: result.append(1) print(result)
Using the
input()
function
- Try out the code below.
- What does the script do?
- What does the function
input()
do?user_input = "" while user_input != "quit": user_input = input("Enter 'quit' to exit: ")
Solution
- This loop continuously asks the user for input until they type “quit.”
- It checks the value of user_input in each iteration.
A note on the
input()
function
- Note, the return value of the
input()
function is always a string, even if the input value looks like another data type.- I.e., if the user inputs
3
, the output will be the string'3'
.- To convert the return value to an integer, you can use the
int()
function.
The Number Guessing Game with hints
In this final exercise, we will write a Python program for a number guessing game!
This exercise builds on the knowledge you have gathered in this course up till now.
Try to solve it without looking at the solution.
Take it easy, don’t sweat it, and have fun writing your first real Python script!
The program should do the following:
- Generate a random secret number between 1 and 20.
- Ask the user to guess the secret number.
- Provide hints to the user based on their guess:
- If the guess is correct, print “Congratulations! You guessed the correct number.”
- If the guess is too low, print “Too low. Try again.”
- If the guess is too high, print “Too high. Try again.”
- Allow the user to keep guessing until they correctly guess the number.
- Count and display the number of attempts it took for the user to guess correctly.
Requirements:
- Use a
while
loop to repeatedly ask the user for their guess until they guess correctly.- Use
if
,elif
, andelse
statements to provide appropriate hints based on the user’s guess.Hints:
- You will need to use the
input()
function introduced in the exercise above.- We expect the users to only type in integers.
- You can use the
random
module to generate the secret number. Here’s an example of how to import and use it:import random # Generate a random secret number between 1 and 20 secret_number = random.randint(1, 20)
Sample Output (simplified):
You will need your output to look something like this:
Welcome to the Number Guessing Game! Guess the secret number (1-20): 10 Too low. Try again. Guess the secret number (1-20): 15 Too high. Try again. Guess the secret number (1-20): 12 Congratulations! You guessed the correct number in 3 attempts.
Solution
Are you really sure, you want to see the solution yet?
Only look if you have solved it yourself.
Remember:
Python pain is temporary, pride is forever!
“Yes, I am weak and I want to see the solution!”
import random # Generate a random secret number between 1 and 20 secret_number = random.randint(1, 20) # Initialize variables attempts = 0 guessed_correctly = False print("Welcome to the Number Guessing Game!") while not guessed_correctly: # Ask the user to guess the secret number user_guess = int(input("Guess the secret number (1-20): ")) # Increment the attempts counter attempts = attempts + 1 # Check if the guess is correct, too low, or too high if user_guess == secret_number: guessed_correctly = True print("Congratulations! You guessed the correct number in", attempts, "attempts.") elif user_guess < secret_number: print("Too low. Try again.") else: print("Too high. Try again.")
Key Points
Use
if
statements to control whether or not a block of code is executed.Conditionals are often used inside loops.
Use
else
to execute a block of code when anif
condition is not true.Use
elif
to specify additional tests.Conditions are tested once, in order.
A
while
loop allows you to repeatedly execute a block of code as long as a certain condition is true.
Evaluation
Overview
Teaching: 0 min
Exercises: 5 minQuestions
Please complete the evaluation!
Objectives
Help us make the course better.
Please complete the evaluation
We would greatly appreciate if you, after the course, would kindly spend 2-5 minutes filling out this questionnaire:
http://bit.ly/KUB2024
We appreciate your honest opinions!
Thank you!
Key Points