Python basics

Printing

Exercise 1
Print the text “It works” (or any other short string).

Exercise 2
Write a programme which produces the following output:

This is the first line.

 This is the second line.

 This line contains a        tab.


Making Calculations

Exercise 3
Create two variables, and assign a numerical value to both of them. Next, print their sum and their product.

Exercise 4
Write a program which can calculate the number of seconds in seven weeks. How many hours are there in seven weeks?

Exercise 5
Given an exchange rate of 1 euro to 1,2409 dollars, how many American dollars can you buy for 150 euro? And how many euros can you buy for 150 dollars? N.B. multiply the original amount by the exchange rate for the first question, and divide the original amount by the exchange rate for the second question.

Selection

Exercise 6
First, create a variable named ‘year’, and assign it an integer in between 1500 and 2000. Next, add some code which can produce a textual description of the century this year is in. For example, if ‘year’ has value 1575, the program should print the text ‘sixteenth century’.

Exercise 7
For a given university course, 25% of the final mark is determined by the mid-term assignment, and the remaining 75% is determined by the final exam. In your program, create two variables which can capture the grades earned for these two assignments. Next, calculate the final grade for the course and try to determine whether or not this is a pass mark. A pass mark is 5.5 or higher.

 

Working with strings

Exercise 8
Create the following variables:

placeName = ‘Leiden’
province = ‘South Holland’
country = ‘The Netherlands’
 

Concatenate these strings into a longer string which looks as follows: ‘Leiden, South Holland (The Netherlands)’

 

Exercise 9
Create a string variable with the following value:

text = “Monty Python’s Flying Circus“

Extract the word ‘Python’ using a slice which makes use of character indices.

 

Exercise 10
Copy the code below:

Word1 = unique
word2 = biodiversity

Use these two variables to print the word ‘university’ – so without typing in this word yourself.

 

Exercise 11
Create a variable with the value ‘vladimir’ and a variable with the variable ‘nabokov’. Use these two variables to create a third variable with the value ‘Nabokov, V.’. Note that the first character of the lastname and the initial of the author are in upper case.

 

Exercise 12
Copy the code below.

quote1 = ‘The truth is rarely pure and never simple’
quote2 = ‘Experience is the name we give to our mistakes’

Print the quote which has has the highest number of characters.

 

The solutions to all of these exercises can be found here