Programming basics#
Programming is a way of telling the computer what to do. Computer programs are a kind of recipe, like cooking or knitting recipes. However, cooking recipes can sometimes be vague or imprecise. For example, an instruction could be “add 4-5 eggs” or “bake for 25-30 minutes till done”. Because humans have discretion, we can make sense of such vague instructions. Unfortunately, computers don’t have discretion. Therefore, computer programs must be both precise and correct.
Using Python as a Calculator#
One simple use of Python is as a calculator. We can use the four arithmetic operations: + - * /
. Multiplication is done with the asterisk *
, and division with the slash /
.
2 + 3
1 - 2
3 * 4
5 / 2
2.5
For more complicated expressions, we can use parenthesizes.
3 * (2 + 1)
9
Printing to the Screen (Terminal)#
Jupyter Notebook only displays the value of the final statement in a cell. If you want to see the value of other statements, you need to print them to the screen.
In Python, you can use the function print() to display things on the screen. Put the item(s) to be printed inside the parentheses.
print(3 * 2)
print(0, 1, 2)
6
0 1 2
We can also print text:
print('See you in court')
See you in court
Text strings can be enclosed in single or double quotes, but you must use the same type of quote before and after the string:
print("See you in court")
See you in court
Variables#
In the examples above, the computer “forgets” the information as soon as it has been printed. We need some way to store information in our program. This is what variables are for. Variables are like handles we use to retrieve information.
In Python, variables can be defined or assigned a value as shown in the code below.
count = 0
count = 20
print(count)
20
The =
in Python does not mean equality as in mathematics, but is an assignment where the value or expression on the right hand side is stored in the variable on the left side.
We can see this when we increase the count. No variable can be equal to itself plus something:
count = count + 1
Variable Names#
It’s important to choose good variable names. The names are meant to be read and understood by people, whether it’s a colleague or yourself in a couple of months. Make descriptive, meaningful variable names, that are neither too short or nor too long.
Variable names can contain:
all letters in the English alphabet.
underscore: _
numbers, but a variable name cannot start with a number
This is a valid, but perhaps too long variable name:
name_of_defendant_01 = 'John Smith'
Variables are Independent#
Python variables are independent of each other. Let’s define two variables. Alice is 20 years old, and her classmate Bob is the same age:
alice_age = 20
bob_age = alice_age
Then it’s Alice’s birthday. Now, what’s the value of bob_age?
alice_age = 21
print(bob_age)
20
The result is 20, bob_age
hasn’t changed even though alice_age
did.
The variable bob_age
is independent from alice_age
, even though it was initially assigned the value from alice_age
.
This is just the way variables are defined in Python. It would be perfectly fine to have another kind of variable where the two variables are connected, but this is not how standard Python variables work.
Statements and Expressions#
A statement tells Python to do something, while an expression has a value, for example a number. A statement is often a line of code, but long statements can span multiple lines. This line is a single statement that contains two expressions:
print(bob_age, 4 * 3)
20 12
Data Types#
Python has three basic data types which are sometimes called variable types:
str
, short for string, is for textint
, short for integer, are whole numbersfloat
is for decimal numbers, which are also known as floating-point numbers
We can use the function type()
to get the type of an item:
type('hi')
str
type(2)
int
type(2.0)
float
Note
A number in quotes is interpreted as a string, and can’t be used in arithmetic.
type('2')
str
Commenting Code#
Comments can make your code easier to understand.
You can use a #
character to start a single line comment.
To write a comment consisting of multiple lines, you use three
quotes '''
before and after the comment.
# Comments can clarify code
defendant_name = 'John Doe' # This is an end of line comment
'''
You can also write a comment spanning multiple lines.
This is useful for documentation.
These comments are enclosed in three single quotes.
'''
Calling Functions and Methods#
There are two types of functions in Python: regular functions and object methods that are tied to a data object.
Calling Regular Functions#
Python has a number of built-in functions that we can use. When we use a function, we say that we are calling it.
For example, we can get the length of a string with the function len()
:
defendant = 'John Doe'
length = len(defendant)
print(length)
8
The items within the parenthesis are called the arguments of the function.
Functions can have zero or more arguments. We have used the print()
function with multiple arguments:
print('Length of the string:', length)
Length of the string: 8
Nested Function Calls#
Above, we called the two functions separately, on different lines. We can also nest the function calls on a single line. Then, the result of the innermost call is used as the argument to the outer call.
print(len(defendant))
Calling Methods#
Strings are a kind of objects in Python. Objects are a collection of data and functions that are grouped together. Functions that belong to an object are called methods. We can use methods belonging to string objects.
<string>.upper()
returns the string in uppercase. <string>
is a placeholder for the name of a string variable.
print(defendant.upper())
JOHN DOE
<string>.lower()
returns the string in lowercase.
<string>.replace(old, new)
replaces all occurrences of the substring old
with the string new
:
print(defendant.replace('Doe', 'Smith'))
John Smith
Getting input from the user#
Python has the built-in function input(prompt)
to ask the user for input.
The argument prompt
is a string that will be displayed to the user.
client_name = input("Client name: ")
print('New client:', client_name)
Client name: Scar
New client: Scar
Warning
Python does not stop you from using the name of a built-in function as a variable name. But if you do that, the function will no longer work, because the name now refers to something else. Below, we try to assign a value to the variable input
. This breaks the function input()
.
Show code cell content
original_input = input # save built-in before we overwrite it
input = input("Client name: ") # DON'T do this, breaks the function input()
print('New client:', input)
input2 = input("Client address: ")
Client name: Scar
New client: Scar
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [22], in <cell line: 3>()
1 input = input("Client name: ") # DON'T do this, breaks the function input()
2 print('New client:', input)
----> 3 input2 = input("Client address: ")
TypeError: 'str' object is not callable
Show code cell content
input = original_input # restore built-in input() function
Warning
When we execute a cell, Jupyter Notebook stores the variable it defines in the execution kernel.
This means that even if we fix the code by changing the variable name from input
to something else, our definition of input still exists. To erase the variable we must restart the kernel by clicking “Kernel” and “Restart” in the Jupyter Notebook menu.
Converting between strings and numbers#
Sometimes we need to convert a string to a number (or vice versa).
We can do this with the functions int()
, float()
, and str()
.
For example, the function input()
always returns a text string:
age = input('How old are you? ')
double_age = age * 2
print('In', age, 'years, you will be', double_age)
How old are you? 21
In 21 years, you will be 2121
Note
Notice that because age
is a string, it gets repeated not multiplied.
We need to convert it, and we can do so by chaining int()
and input()
on the same line.
By converting the value immediately, there’s no risk of using it before it’s been converted.
age = int(input('How old are you? '))
double_age = age * 2
print('In', age, 'years, you will be', double_age)
How old are you? 21
In 21 years, you will be 42
We can also convert between integers and floating-point numbers:
age = 3.8
print(int(age))
3
Rounding Errors
As we can see above, when converting from float
to int
the decimals are always rounded down, making 3.8 into 3.
This will lead to rounding errors if numbers are converted incautiously.
By normal math rules, 3.8 should be rounded up to 4.
If you need correct rounding, you should use the round()
function.
Importing libraries and modules#
We have seen that Python has several built-in functions, for example print()
and input()
.
Python also has a standard library that we can use. A library is a collection of code that we can use as building blocks in our programs. We can often find and download libraries written by other people to use in our programs. The Python standard library comes with Python, so we don’t need to install it.
The Python library is divided into modules. A module groups together related functions.
We can use the datetime
module to get the current date:
import datetime
writ_of_summons_date = datetime.date.today()
print('''In accordance with the Dispute Act Section 9-4(2)h), unless special
circumstances necessitate it, the main hearing has to be set for a date
less than six months after the date of submission of the writ of summons,
which was submitted on''', writ_of_summons_date)
In accordance with the Dispute Act Section 9-4(2)h), unless special
circumstances necessitate it, the main hearing has to be set for a date
less than six months after the date of submission of the writ of summons,
which was submitted on 2022-08-09
We might use the imported function repeatedly.
To save typing, we can import date
directly.
We can also import multiple things:
from datetime import date, datetime
print(date.today())
2022-08-09
Parsing dates#
We can use the function datetime.strptime(time, time_format)
to convert (parse) dates in string format into machine-readable format.
First, we must import the datetime library:
from datetime import datetime
Dates can be written using different separators between the parts. The format below is used in ECHR-OD, which we will sometimes use as example data in this course. We will discuss ECHR-OD further in Appendix: JSON and Web APIs.
time = '30/11/2016 16:45:00'
Next, we must specify the time format to use for parsing. This must match the format of the time in the string, which means that both the separators and the order of the fields must be the same.
The time format is a bit cryptic, but it is documented in the Python documentation.
time_format = '%d/%m/%Y %H:%M:%S'
Now, we do the actual conversion/parsing:
time = datetime.strptime(time, time_format)
The variable time now contains the time in machine-readable format. We can use the separate components, such as the year:
time.year
2016
Or the day:
time.day
30
String concatenation#
We saw above that we use the +
operator to add numbers. We can also use +
with strings to glue them together. This is called string concatenation.
first_name = 'John'
last_name = 'Doe'
full_name = first_name + last_name
print(full_name)
JohnDoe
Note
Notice that the two strings are concatenated together without any space in-between. If we want a space, we must insert it ourselves:
full_name = first_name + ' ' + last_name
print(full_name)
John Doe
Extra: String multiplication
We can also use multiplication with strings. This will repeat the string. This is kind of a curiosity, that you won’t often need in practice:
print(‘See you in court, ‘ * 3) print(‘-’ * 60)
See you in court, See you in court, See you in court,
F-strings#
Concatenating just a couple of strings works well. But if we want to include multiple variables in a string, we will need many quotes and plus-signs (or commas), which can be easily misplaced. F-strings (formatted strings) are a simple way to include variables in a string:
print(f'''Client name: {full_name}
first name: {first_name}, last name: {last_name}''')
Client name: John Doe
first name: John, last name: Doe
For debugging, it can be useful to print the value of a variable along with its name.
F-strings are a shortcut for this, notice the =
at the end:
print(f'{full_name=}')
full_name='John Doe'