Week 1: Introduction#
Exercise 1.1: Before we get started#
Consider each of the following pieces of code. Without running the code, answer the questions:
Which variables exist,
What are the values and the types of the variables?
Write down your answers on paper or however you prefer.
- Exercise 1.1a
b = 10 b = b - 5 b = 2 * (b - 5)
- Exercise 1.1b
q = 14 q = q - 4 q = 0.5 * (q + 2)
- Exercise 1.1c
k = 20 k = k/2 k = 3 * (k -5)
- Exercise 1.1d
a = 15.5 b = a a = 7
- Exercise 1.1e
a = 5 b = 3 a = b b = a
- Exercise 1.1f
this = 15 this = 'that'
- Exercise 1.1g
i = 15 i = 1 + 1 i = i + 1
- Exercise 1.1h
letters = 'abc' letters = letters + 'x' letters = letters + 'y'
- Exercise 1.1i
test = 'test' test = 2 * test + 'no'
- Exercise 1.1j
day = 15 valid = day>0 and day<32
- Exercise 1.1k
name = 'Filip' age = 12 c = name * age
Exercise 1.2: Installation#
Open the installation instructions and follow all of the steps.
Exercise 1.3: Using Python in the terminal#
Using the terminal you opened during step 5 of the installation instructions, do the following exercises:
- Exercise 1.3a:
Type
python
followed byenter
in the terminal (trypython3
if that doesn’t work). When you have done that you can interactively enter Python commands in the terminal, and we now call it a Python interpreter.In the Python interpreter:
Enter
a = 3
.Use the
print
statement to check that the value ofa
is indeed 3.Enter
a + 2
.Check the value of
a
again.Enter
a = a + 2
.Check the value of
a
one more time.
- Exercise 1.3b:
Perform the following operations:
Create a new variable
x
with the value 4.Create a new variable
y
with the value 7.Create a new variable
z
and set its value to the sum ofx
andy
.Print the variable
z
to ensure that its value is in fact 11.
- Exercise 1.3c:
Declare a variable
name
and assign your own name as a string to it. Then, assign the value 25 as an integer to the same variable. Explain what happens and why.- Exercise 1.3d:
Assign the value 8 to a variable
num
. Add 4 tonum
and store the result back innum
. Write down the steps to achieve this.
Exercise 1.4: Creating a Python script#
Create and run a simple Python script from the terminal:
In VSCode, create an empty text file.
Within the file, write a line of Python code that prints
I run from a file
.Save the file as
my_file.py
in the folder02002students/cp/ex01
.In the terminal, navigate to the where you saved the script using the
cd
command.Execute the script by running the command
python my_file.py
in the terminal.Modify your script to print something else.
Save it again, and execute the script to verify that it has changed.
Exercise 1.5: Revisiting Exercise 1.1#
Use Python to check that you have correctly determined the values of the variables in Exercise 1.1.
Use print
statements to check the values of the variables. You can write Python scripts
to be executed to keep your solution for later, or check each of the exercises in the Python interpreter.
Do you get the same answers as in Exercise 1.1? If not, redo Exercise 1.1.
Exercise 1.6: Built-in functions and operators#
Python comes with many useful functions and operators pre-defined. In this exercise we will explore some of them.
import math
imports the module math
which contains commonly used mathematical functions.
Exercise 1.6a
Define a variable k=16.2
. Then, use the functions int
, round
, math.floor
and math.ceil
to convert k
to an integer, round it to the nearest integer, round it down to the nearest integer and round it up to the nearest integer, respectively.
Exercise 1.6b
Repeat Exercise 1.6a for the value k = 16.8
.
Exercise 1.6c
Repeat Exercise 1.6a for the value k = -16.2
.
Exercise 1.6d
Repeat Exercise 1.6a for the value k = -16.8
.
Exercise 1.6e
Define a variable x
and assign it a value of 2. Then, use the functions abs
, math.sqrt
and math.pow
to compute the absolute value of x
, the square root of x
and x
squared, respectively.
Exercise 1.6f
Try the following code in the Python interpreter, and reason about what each line does.
a = 12
b = 7
print(a + b)
print(a % b) # remainder of a/b (modulus)
print(a / b)
print(a // b) # what is the difference between / and //?
print(max(a, b))
print(a < b)
print (a==b)
Exercise 1.6g
Try the following expressions in the Python interpreter, and figure out what they do.
a = True
b = False
print(a and b)
print(a or b)
Exercise 1.6h
Try the following expressions in the Python interpreter, and figure out what they do.
a = 4
b = 8
c = 72
print((a < c) and (b < c))
print((a < c) or (a == b))
Exercise 1.6i
Try the following expressions in the Python interpreter, and figure out what they do.
a = 8
b = 12
x = '6'
y = ' years'
print(a + b)
print(a + y) # why does this give an error?
print(x + y)
print(str(a) + y)
Exercise 1.7: Working with strings#
Create two string variables first_name
and last_name
with your first and last names. Concatenate three strings:
first_name
, a string containing one space character, and last_name
. Store the result in a variable called full_name
.
Exercise 1.8: Swapping value of variables#
Given two variables a
and b
with values 3 and 7 respectively, write down the steps you would take to swap their values. Verify your solution with Python.
Tip
Use a third variable temp
.