Closure#
Making sure you’ve read the reading material for Week 1, which you find under Week 1 and then Preparation.
In a closure session of every week, under Syllabus, we will list the programming concepts and the Python features which were covered in the week.
Syllabus Week 1: Introduction#
Basic syntax and terminology:
A program is a text with a sequence of commands executed line-by-line
Each time a program runs, it starts with a clean slate (no previous definitions)
Python programs are saved as text files with a
.py
extensionSyntax guidelines: comments, blank lines, indentation, and case sensitivity
Some ways to execute Python code:
Script mode in IDLE (practical knowledge: you should acquire this skill)
Jupyter notebooks and cell mode for some IDES (conceptual knowledge)
Running Python code directly in a browser
Key concepts introduced through examples, no formal definition yet:
Assignments
The
print()
functionString variables
Inputs to a function (later referred to as arguments)
Numeric variables for integers and floats
Common numeric operators:
+
,-
,*
,**
,/
Operator precedence and the use of parentheses to control precedence.
Simple functions:
abs()
,round()
Output of functions (later referred to as returned values)
Advanced#
Note about the material in Advanced section. The advanced material contains more some additional topics related to the weeks’s content. You are not required to read this material, and none of the exercises or exam questions will rely on this material.
The advanced material for week 1 covers the input()
function and the use of optional arguments for print()
.
We suggest you read:
W3schools: print() look at the parts about
sep
andend
.
Once you have read that, and experimented with it, feel free to do the following exercises.
Advanced 1.1: Weather Report with Python#
You’ll now use Python’s to make fun and personalized weather forecasts ☀️!
Advanced 1.1a: Use of the input
function.#
The function input
allows you to prompt the user for an input which can then be stored in a variable. Check the example below which
asks the user to enter their name, and prints a greeting. Run the code and try it out yourself! Note that print statement takes two things to be printed.
name = input("Enter your name:")
print("Hello", name)
Advanced 1.1b: Simple weather report#
Consider the simple weather report given below:
It's 25 degrees and sunny today!
Write a piece a python code where you:
Use
input
to ask for the temperature in celsius and store the result in a variable.Use
input
to ask for the weather condition (e.g., sunny, rainy, cloudy) and store the result in a variable.Print a weather report that matches the example above using the defined variables in a
print
statement.
Advanced 1.1c: Making a personalized report#
Now consider the weather report:
Weather Forecast: It's 21 degrees, partly cloudy, with winds at 10 km/h. Have a great day Peter!
Write a piece of python code where you:
Also ask for the wind speed and store the result in a variable.
Ask for a name (e.g. your name) and store the result in a variable.
Print a detailed weather forecast using a
print
statement to achieve the output as the example above using your own name.
Advanced 1.2: Beatboxing with Python#
In this exercise, you’re now going to learn Python how to beatbox! However, to keep things simple, we’ll start by considering only three sounds: “Boom”, “Tick” and “Ka”. These three sounds will be the basis of the rhythms you’ll work with.
A beatbox rhythm can be considered as a string containing a combination of sounds where each sound is separated by a dash (-
) character. For instance "Boom-Tick-Ka"
is a beatbox rhythm. Before following on with the first exercise, start by entering the sounds “Boom”, “Tick” and “Ka” using input() and save each string to a variable:
# Start by entering the sounds for your beat-box song using input()
sound1 = input("Enter the first sound: ")
sound2 = input("Enter the second sound: ")
sound3 = input("Enter the third sound: ")
Advanced 1.2a: Creating a rhythm#
To create the dashes in each rhythm, we’ll use the separation argument sep
in print
. Giving a string as the argument sep
will make Python print the separation string in between the other strings given to print
.
For instance: print("cats","dogs", sep=" and ")
will print "cats and dogs
.
In the same way, giving a dash character (-
) as the separation will result in a dash between each string.
#We just define the variables here, since input is not run for the solutions
sound1 = "Boom"
sound2 = "Tick"
sound3 = "Ka"
# Try printing the basic rhythm: "Boom-Ka"
print(sound1, sound3, sep='-')
Advanced 1.2b: Creating your own rhythm#
Print the beatbox rhythm "Boom-Tick-Ka-Tick"
using the sound variables and the sep
argument using print
:
Advanced 1.2c: Printing rhythms on the same line#
We now want to print a rhythm several times, but on the same line. To do this, we’ll use the argument end
of print
. Giving a string as the argument end
will make Python add this string add the end. By default, print
will add a new line at the end of every print statement. However, using the end
argument will override this.
Print the rhythm from the previous exercise three times on the same line using three print statements.
Hint: Use the end
argument of print
to keep the sounds on the same line. As an example, you can use the character "|"
Advanced 1.2d: Adding variations#
For dramatic effect, we´ll now add pauses as a separation between the other sounds in the rhythms.
Create a new string variable pause
and use it as a separation between other sounds.
Use this variable to create the following rhythm: "Ka-pause-Boom|Ka-pause-Boom|"
Advanced 1.2e: Using sounds as separations#
As separations, we would now like to use sounds.
Create a new variable sep
and set it to the character (-
).
Hint: sep = "-"
or sep = input("Enter \"-\": ")
Use Boom
as the separation sound to print the following rhythm: "Tick-Boom-Tick"
Advanced 1.2f: Replication of beats#
Use two print statements to print the following rhythm:
Boom-Tick-Boom|Boom-pause-Boom-pause-Tick-pause-Tick|
Use "Tick"
as the separation in the first print statement and use "pause"
as the separation in the second print statement.
Remember to use the end
argument to separate the rhythms.