LINAS Python Training Course¶

Term 1, Week 1: Inputs, Outputs & Arithmetic¶

Printing¶

In Python, outputs are displayed in the console using the print command.

In [ ]:
print("Hello, world!")

Python processes code line by line. To execute two commands, type one after the other.

In [ ]:
print("Python executes the first line...")
print("...and then the second.")

Exercise One¶

Fix the following code snippet.

In [ ]:
print("Fix me!")print("Thank you!")

Write a piece of code in the below box that prints the phrase "Hello World!".

In [ ]:
 

Syntax Errors¶

Syntax errors refer to an error in the way your Python code has been written - it can't understand how to process the code.

Syntax errors are the most common type of errors a new coder will come across. If you get them, go back to previous examples and try to spot differences between them and your code.

Exercise Two¶

Fix the following syntax errors.

In [ ]:
print(hello world)
In [ ]:
print hello world

Arithmetic¶

We can also print the value of certain arithmetic operations.

In the below, the computer adds 2 to 1, and prints the result.

In [ ]:
print(2 + 1)
In [ ]:
print(5 - 3)
In [ ]:
print(4 * 10)
In [ ]:
print(25 / 5)
In [ ]:
print(5**2)

You can control the order of operations in longer calculations with parentheses.

In [ ]:
print((1 + 3) * (9 - 2))

Exercise Three¶

Write code to print the following:

a) 74 + 26

b) 53 - 12

c) 10 x 10

d) 2^3

e) (5^2) + (5 - 2)

In [ ]:
 

Comments¶

We use comments to annotate what code is doing. They help other people - and you - to understand your code.

Lines are turned into comments by putting a # at the start. When Python reads this, it won't run.

In [ ]:
#This line is a comment

print("This line is not a comment")

Exercise Four¶

Fix the below syntax errors.

In [ ]:
this line is a comment

print(hello world)

Variables¶

We can save down outputs to use later on in the code. For this, we need to use variables.

In [ ]:
variable = 2+2

print(variable)

We can name variables almost anything we like. They can't have spaces, and can only include letters, numbers, and underscores. They also can't start with a number.

To assign a variable, we need to use the = sign.

Exercise Five¶

Create a variable that stores the value of 3 x 3, and print it.

In [ ]:
 

Manipulating Variables¶

We can change the value assigned to a variable by overwriting it.

In [ ]:
#set variable value to 5
test_variable = 5

#print the variable value
print(test_variable)

#overwrite the variable value
test_variable = 10

#print the new variable value
print(test_variable)

In notebooks, when a variable is defined, all the subsequent code cells have access to the variable:

In [ ]:
print(test_variable)

We can also perform operations on variables to change its value without outright overwriting it.

In [ ]:
test_variable = test_variable + 3

print(test_variable)

Multiple Variables¶

We often use multiple variables in coding when doing long calculations that require many inputs.

In [ ]:
#calculate the number of seconds in a year

# Create variables
num_years = 1
days_per_year = 365 
hours_per_day = 24
mins_per_hour = 60
secs_per_min = 60

# Calculate number of seconds in one year
total_secs = secs_per_min * mins_per_hour * hours_per_day * days_per_year * num_years
print(total_secs)

We could write this explicitly as 1365246060, but if we want to change one of the variables (e.g. the number of years) it's easier to track with variables.

In [ ]:
num_years = 2

#calculate number of seconds in 2 years
total_secs = secs_per_min * mins_per_hour * hours_per_day * days_per_year * num_years

print(total_secs)

Exercise Six¶

Create two variables that store 2 different numbers, and print the sum of them.

In [ ]:
 

Extra Credit: Inputs¶

If we don't want to set a variable value ahead of time, we can use the input function to get it from the user.

In [ ]:
variable_1 = input("Type 'Hello World' here:")

print("")
print(variable_1)

We can use multiple inputs to store multiple variables at once.

In [ ]:
variable_1 = input("First word here:")

variable_2 = input("Second word here:")

print("The sum of word 1 and word 2 is:")

print(variable_1 + " " + variable_2)

This isn't right! Why doesn't our sum work correctly here?

Exercise Seven¶

Create a variable that stores a user input, and print it.

In [ ]:
 

Can we use inputs to do arithmetic?

In [ ]:
variable_1 = input("First number here:")

variable_2 = input("Second number here:")

print("The sum of number 1 and number 2 is:")

print(variable_1 + variable_2)

This isn't right! Why doesn't our sum work?

In [ ]: