Wednesday, January 28th, 2026¶
Last week, we familiarized ourselves with Jupyter notebook and running Python code. We saw how to perform various arithmetic operations and compared the integer and float datatypes. We also begin discussing Markdown cells.
More with Markdown¶
We can generate bulleted (un-numbered) using a space and hyphen:
- This is item 1.
- Here's item 2
- Item 3
We can also nest lists using additional spaces:
- This is item 1
- This is a sub-item of item 1
- Sub-item 1b
- Sub-item 1c
- This is item 2
- This is item 3
- Sub-item 3a
- Sub-item 3b
We can also create numbered lists using 1., 2., etc. Note: A numbered list will always be enumerated 1, 2, 3... regardless of how you've actually numbered the items.
- Step 1
- Step 2
- Step 3
- Final step
Back to integers vs. floats¶
The normal division symbol / will always return a floating point number. That is, a/b will return the closest floating point number to $\frac{a}{b}$.
10 / 2
5.0
Sometimes, we would prefer to return an integer number. In those cases, we can this using the // operation. The integer division // will always return the quotient from performing long division. That is, a//b will return the largest integer less than or equal to $\frac{a}{b}$ (e.g. by dropping the decimal portion).
10 // 2
5
100 // 7
14
14*7
98
Another way to think of integer division is by relating it to long division. Recall that For any integers $a$ and $b\not=0$, there are unique integers $q$ (the quotient) and $r$ (the remainder) such that $$a = b\cdot q + r \qquad \text{with} \qquad 0 \leq r < b.$$ For example, by performing long division to divide $100$ by $7$, we find that the quotient is $14$ and the remainder is $2$. That is, $100 = 7\cdot 14 + 2$.
In Python, the quotient $q$ can be found by performing integer division //. Similarly, can get the remainder $r$ using the modulus operator, %. This is called modular division.
100 % 7
2
Let's check that Python has correctly found the quotient and remainder.
14*7 + 2
100
Variable assignment in Python¶
Very often, we want to perform operations on some input that might change. We can define variables using the = symbol:
a = 100
b = 7
c = a // b
d = a % b
After defining a variable, we can refer back to its value by typing in the name.
Note: Jupyter will display the output of the last line of any cell. For example, we can display the value of a variable by "executing" the variable name as the last operation in a code cell.
c
14
d
2
b*c + d
100
Variable names¶
Variable names must:
- Start with a letter or underscore (
_) - Contain only letters, numbers, or underscores.
thisisavariable = 17
thisisanothervariable = 22
thisisavariable + thisisanothervariable
39
_THIS_variable_has_NUMBERS_1234 = 123456
_THIS_variable_has_NUMBERS_1234
123456
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.
this_is_a_variable = 17
this_is_another_variable = 22
thisIsACamelcaseVariable = -5
In general, we want to choose concise variable names that helps the reader understand what they represent.
For example, recall the exercise above where we performed long-division:
In the above, the variable names do not help to illustrate what each variable represents. Can we come up with names that might work better?
numerator = 100
denominator = 7
quotient = numerator // denominator
remainder = numerator % denominator
denominator * quotient + remainder
100
a = 100
b = 7
q = a // b
r = a % b
Simultaneous variable assignment¶
We can define several variables simultaneously by separating the variables to be assigned with commas followed by an equal sign = and their respective comma-separated values.
a, b = 100, 7
q, r = a // b, a % b
q,r
(14, 2)
Note: the divmod function can be used to simultaneously calculated the integer quotient and the remainder.
q, r = divmod(a,b)
q, r
(14, 2)
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,b
(2, 1)
Alternatively, we can use simultaneous assignment to swap them without introducing an intermediate variable.
a = 1
b = 2
a,b = b,a
a,b
(2, 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 = 'This is a string. It contains text data.'
this_is_a_string
'This is a string. It contains text data.'
this_string_uses_double_quotes = "This is another string with text data."
this_string_uses_double_quotes
'This is another string with text data.'
"I can't believe it!"
"I can't believe it!"
'This string contains a quotation. "Eureka!!"'
'This string contains a quotation. "Eureka!!"'
There are many operations that can be performed on strings. For example:
Addition of strings:
'String 1' + 'String 2' + 'Third string'
'String 1String 2Third string'
Adding strings together concatenates them.
Multiplying a string with an integer:
'Test string' * 5
'Test stringTest stringTest stringTest stringTest string'
Multiplying a string by an integer contatenates that number of copies together.
Can we multiply a string with a float?
'Test string' * 2.5
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[38], line 1 ----> 1 'Test string' * 2.5 TypeError: can't multiply sequence by non-int of type 'float'
Can we multiply two strings?
'string 1' * 'string 2'
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[39], line 1 ----> 1 'string 1' * 'string 2' TypeError: can't multiply sequence by non-int of type 'str'
Multi-line strings can be used using triple-single-quotes ''' to surround the string:
"this is line 1 of a string
Does this work as line 2?
Doesn't look too promising"
Cell In[40], line 1 "this is line 1 of a string ^ SyntaxError: unterminated string literal (detected at line 1)
my_string = '''this is line 1 of a string
Does this work as line 2?
Looks too promising'''
my_string
'this is line 1 of a string\nDoes this work as line 2?\nLooks too promising'
Notice that our multi-line string does not display the way we might hope. If we want to correctly render a multi-line string, we can use the print function. More broadly, executing a string as the last line of a cell will display the raw string, while the print function will display the rendered string.
print(my_string)
this is line 1 of a string Does this work as line 2? Looks too promising
print('This is a multi-line string.\nThis is line 2.')
This is a multi-line string. This is line 2.
The print function can be used anytime we want to display some information.
a = 100
b = 7
q = a // b
r = a % b
print('The quotient is')
print(q)
print('The remainder is')
print(r)
print('q*b + r = ')
print(q*b+r)
The quotient is 14 The remainder is 2 q*b + r = 100
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
intfunction will try to convert an input to an integer type. - The
floatfunction will try to convert an input to a float type. - The
strfunction will try to convert an input to a string type.
int(5.0)
5
We can use the type function to check an object's type.
type(5.0)
float
type(5)
int
type(int(5.0))
int
int(3.9)
3
int(-2.6)
-2
The int function will truncate a float and drop any decimal part.
int('12346')
12346
int('three')
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[61], line 1 ----> 1 int('three') ValueError: invalid literal for int() with base 10: 'three'
Sometimes we might want to round to the nearest integer.
round(3.9)
4
round(-2.6)
-3
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.
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 = 100
b = 7
q = a // b
r = a % b
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 quotient is', q, '.')
print('The remainder is', r, '.')
print('q*b + r =', q*b + r)
The quotient is 14 . The remainder is 2 . q*b + r = 100
print('The quotient is ' + str(q) + '.')
print('The remainder is ' + str(r) + '.')
print('q*b + r = ' + str(q*b+r))
The quotient is 14. The remainder is 2. q*b + r = 100
There are several ways that we can accomplish this in a more slick way. 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.
q_string = 'The quotient is {}.'
r_string = 'The remainder is {}.'
print(q_string.format(q))
print(r_string.format(r))
The quotient is 14. The remainder is 2.
combined_string = '''The quotient is {}.
The remainder is {}.'''
print(combined_string.format(q, r))
The quotient is 14. The remainder is 2.