Wednesday, August 27th, 2025¶
Variables¶
Last time, we had just started discussing defining variables in Python.
a = 1
a
1
Note: Jupyter will display the output of the last line of any cell. We can view the contents of a defined variable by ending a cell with the variable name.
a = 1
b = 3
c = 7
a
b
c
7
Variable names must:
- Start with a letter or underscore (
_
) - Contain only letters, numbers, or underscores.
this_is_a_variable = 7
this_is_a_variable
7
_variable_starting_with_underscore = 5
_variable_starting_with_underscore
5
5variable_starting_with_number = 10
Cell In[6], line 1 5variable_starting_with_number = 10 ^ SyntaxError: invalid decimal literal
num1 = 1
num5 = 5
num5
5
Very often, we'll use underscores as spaces to separate words in our variable names. Another style (known as camelcase) uses capital letters to denote the start of a new word.
thisIsACamelCaseVariable = -5
thisIsACamelCaseVariable
-5
In general, we want to choose concise variable names that helps the reader understand what they represent.
For example, recall the exercise from last class where we performed long-division:
a = 102
b = 42
c = a // b
d = a % b
c
2
d
18
c*b + d
102
In the above, the variable names do not help to illustrate what each variable represents. Let's try to improve on this:
numerator = 102
denominator = 42
quotient = numerator // denominator
remainder = numerator % denominator
quotient
2
remainder
18
quotient * denominator + remainder
102
We can define several variables simultaneously by separating the variables and their respective defintitions by commas:
a, b, c = 1, 2, 3
a
1
b
2
c
3
This can be very useful if we ever want to swap the meaning of two variables. As an example, suppose we define a = 1
and b = 2
, but then want to swap their values.
a = 1
b = 2
old_a = a
a = b
b = old_a
a
2
b
1
Alternatively, we can use simultaneous assignment to swap them without an intermediate variable:
a = 1
b = 2
a,b = b,a
a
2
b
1
Working with strings¶
In Python, strings are used to hold text data. We can define strings by surrounding some text by double quotes "
or single quotes '
:
this_is_a_string = 'Hello, welcome to MTH 337.'
this_is_a_string
'Hello, welcome to MTH 337.'
There are many operations that can be performed on strings. For example:
Addition of strings:
this_is_a_second_string = "I'm having fun!"
this_is_a_string + ' ' + this_is_a_second_string
"Hello, welcome to MTH 337. I'm having fun!"
Adding strings together concatenates them.
Multiplying a string with an integer:
this_is_a_second_string * 5
"I'm having fun!I'm having fun!I'm having fun!I'm having fun!I'm having fun!"
Multiplying a string by an integer contatenates that number of copies together.
Multiplying a string with a float?
this_is_a_string * 2.0
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[31], line 1 ----> 1 this_is_a_string * 2.0 TypeError: can't multiply sequence by non-int of type 'float'
Multiplying two strings?
'123' * '456'
Multi-line strings can be used using triple-single-quotes '''
to surround the string:
multiline_string = '''This is a multi-line string.
Here is line 2.
My name is line 3!'''
multiline_string
Notice that our multi-line string does not display the way we might hope. If a want to correctly render a multi-line string, we can use the print
function.
print(multiline_string)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[32], line 1 ----> 1 print(multiline_string) NameError: name 'multiline_string' is not defined
The print
function can be used anytime we want to display some information.
a = 1
b = 2
c = 3
print(a)
print(b)
print(c)
a
1 2 3
1
Types¶
So far, we've talked about integers, floats, and strings. There are often times where we might want to convert between these datatypes.
- The
int
function will try to convert an input to an integer type - The
float
function will try to convert an input to a float type - The
str
function will try to convert an input to a string type
int('123') * 2
246
str(123) * 2
'123123'
float(3)
3.0
float('123')
123.0
The int
function will truncate a float and drop any decimal part.
int(4.5677)
4
int(-6.8933)
-6
Sometimes we might want to round to the nearest integer.
round(4.5677)
5
round(-6.8933)
-7
Note: Python comes with several built-in functions, like the round
function. We can see this when Jupyter changes the name round
to green-text. Advice: Try to avoid using these built-in names with your own variables.
print('Hello')
Hello
String formatting¶
Very often, we have some string template that we want to fill in with calculated data. For example, suppose we want to display a product of two numbers and the result.
a = 52
b = 772
my_str = 'The product of ' + str(a) + ' and ' + str(b) + ' is ' + str(a*b) + '.'
print(my_str)
The product of 52 and 772 is 40144.
The print
function can take in several inputs, and will display each after the other separted by a space. We can print all of this using a single print statement by separating our inputs by commas:
print('The product of',a,'and',b,'is',a*b,'.')
The product of 52 and 772 is 40144 .
There are several ways that we can accomplish this in a sleeker fashion. One way is by using the .format
method. Methods like the .format
method are functions that are attached to objects, in this case to a string. This method can be called on some string in following way:
<some string>.format(<format options>)
.
In the simplest case, we can create a string that contains placeholders denoted by curly braces {}
, then supply values to the .format
method that will sequentially fill in these placeholders.
my_str = 'The product of {} and {} is {}.'
print(my_str.format(a,b,a*b))
a,b = 7,4
print(my_str.format(a,b,a*b))
The product of 52 and 772 is 40144. The product of 7 and 4 is 28.
print('The prime factorization of {} is {}.'.format(12, '2*2*3'))
The prime factorization of 12 is 2*2*3.
Working with lists in Python¶
Another datatype in Python are list
s which contain ordered collections of objects. To define a list, we surround a comma-separated collection with square brackets.
my_list = [1,2,3,4,5,6]
my_list
[1, 2, 3, 4, 5, 6]
print(my_list)
[1, 2, 3, 4, 5, 6]
To access elements of a list, we use square brackets again along with an index. Python is a 0-based indexing language, which means the index of each list starts at 0
. That is, 0
indicates the first item in the list.
my_list[0]
1
my_list[3]
4
We can also access elements of a list by counting backward from the end using negative indices.
- The
-1
st index gives the last element. - The
-2
nd index gives the second to last element.
print(my_list)
print(my_list[-1])
[1, 2, 3, 4, 5, 6] 6
print(my_list)
print(my_list[-3])
[1, 2, 3, 4, 5, 6] 4
Note: We will get an error if we access indices beyond the length of the list:
my_list[6]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) Cell In[66], line 1 ----> 1 my_list[6] IndexError: list index out of range
my_list[-10]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) Cell In[67], line 1 ----> 1 my_list[-10] IndexError: list index out of range
What sorts of operations can we perform on lists? For arithmetic operations, lists work very similarly to strings.
Addition of lists:
[1,2,3] + [4,5,6,7,8]
[1, 2, 3, 4, 5, 6, 7, 8]
Multiplying a list and an integer:
[1,2,3] * 6
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
Lists and strings share many properties. We can convert a string to a list using the list
function:
list('1234567')
['1', '2', '3', '4', '5', '6', '7']
We can find the length of a list (or string) using the len
function.
len('1234567')
7
len([1,2,3,5,6])
5
What if we want to convert a list of string characters to a string? The str
function can be used to convert objects to strings.
my_list = [1,2,3,4,5]
str(my_list)
'[1, 2, 3, 4, 5]'
Can we freely convert the string '12345'
to a list and then back to a string?
my_str = '12345'
my_list = list(my_str)
str(my_list)
"['1', '2', '3', '4', '5']"
It does not look like this is doing quite what we want, since the resulting includes the list delimitors and element separators.
We can fix this by using the .join
method on a string. The .join
method takes in a list of strings and concetanates them. However, it uses the object from which it is called to separate each concatenation. If we call the .join
method from an empty string ''
, we will get simple concatenation.
''.join(my_list)
'12345'
Let's use a separator -
between the concatenated elements:
'-'.join(my_list)
'1-2-3-4-5'
n = 12
fact = ['2','2','3']
print('The prime factorization of {} is {}.'.format(n, '*'.join(fact)))
The prime factorization of 12 is 2*2*3.
print('A',' is a '.join(['gig','gig','gig']))
A gig is a gig is a gig
Working with loops in Python¶
We can perform iterative operations using a for
loop. For example, we can iterate through the items in a list and perform some desired operations.
The syntax for writing a for
loop is:
for (some variable name) in (some iterable object):
(do something)
The variable (some variable name)
will sequentially take on each of the values stored in (some iterable object)
, and for each value will (do something)
.
my_list = [1,2,3,4]
for n in my_list:
print(n**2)
1 4 9 16
Key info: The spacing in Python is critical!!!. In particular, the spacing decides what operations are part of a for
loop and what operations are not.
my_list = [1,2,3,4]
for n in my_list:
print(n**2)
print('Hello')
1 4 9 16 Hello
my_list = [1,2,3,4]
for n in my_list:
print(n**2)
print('Hello')
print('Done')
print('Hello again?')
1 Hello 4 Hello 9 Hello 16 Hello Done Hello again?
We can also use for
loops inside other for
loops. We call these "nested loops".
For example, let's write nested loops that iterate through all combinations of integers from the two lists [1,2,3]
and [4,5,6]
and prints out the sum for each combination.
for n in [1,2,3]:
for m in [4,5,6]:
print('{} + {} = {}'.format(n,m,n+m))
print('Done with n={}'.format(n))
print('Done')
1 + 4 = 5 1 + 5 = 6 1 + 6 = 7 Done with n=1 2 + 4 = 6 2 + 5 = 7 2 + 6 = 8 Done with n=2 3 + 4 = 7 3 + 5 = 8 3 + 6 = 9 Done with n=3 Done
What if we wanted to iterate through pairs from each list? That is, suppose we want to consider the lists in parallel and iterate through the three pairs (1,4)
, (2,5)
, and (3,6)
.