Exercises

Make sure you have read section Using python online before you start with the exercises.

Using python as calculator

Exercise 1-1: Simple Arithmetic

Try out the following operations in the Python interpreter:

- Add 15 and 32.
- Subtract 7 from 50.
- Multiply 8 by 12.
- Divide 144 by 12.
- Find the remainder when 27 is divided by 4.
Exercise 1-2: Using Exponents
- Calculate 5 squared.
- Calculate 2 cubed (i.e., 2 to the power of 3).
Exercise 1-3: Order of Operations

Evaluate the following expressions. Can you anticipate the results?

- 3+5×23+5×2
- (3+5)×2(3+5)×2
- 10−4/210−4/2
- (10−4)/2(10−4)/2
- 2×(3+4)÷22×(3+4)÷2
- 10**(4/2)/3
- 10**4/2/3
- 100−45×2+10
- 25÷5+3225÷5+32
- (8×4+2)÷2×(8×4+2)÷2
Exercise 1-4: Working with Floats and Integers
- Divide 7 by 2. What is the result?
- Now use integer division to divide 7 by 2.
- Calculate 15.5×215.5×2.
- Subtract 3.2 from 10.0.
Exercise 1-5: Using Variables in Calculations
- Assign the value 10 to a variable called x.
- Assign the value 20 to a variable called y.
- Calculate the sum of x and y.
- Assign the value of the expression (x+y)×2(x+y)×2 to a variable called z.
- Print the value of z.

Writing your own simple programs

Exercise 2-1: Basic Output
  1. Write a python program that prints a short introduction about yourself, including your name, age, and favorite hobby. For example,
Hello! My name is Alex.
I am 25 years old.
My favorite hobby is painting.
  1. Slightly rewrite the program by defining three variables (name, age, hobby) that will be used in the print statement of your introduction.
Exercise 2-2: Calculator Program

Create a python program that asks the user to input two numbers. Then, perform and display the result of basic arithmetic operations (addition, subtraction, multiplication, and division) on these numbers. Note, that you will have to convert the data types of the input numbers from string to integer (or float) before you can perform the calculations. Upon running the program, the output could look like this:

Enter the first number: 8
Enter the second number: 3
The sum is: 11
The difference is: 5
The product is: 24
The quotient is: 2.6666666666666665
Exercise 2-3: Temperature Conversion

Write a Python program that asks the user to input a temperature in Celsius and then converts the temperature to Fahrenheit. The formula is:

Fahrenheit = (Celsius×9/5) + 32

The output could look like this:

Enter the temperature in Celsius: 25
The temperature in Fahrenheit is: 77.0
Exercise 2-4: Variable Swapping

Create a program that defines two variables, x and y, prints their values, and then swaps their values without using a third variable.

The output could look like this:

x is 5 and y is 10.
Hold on a second, the values might just have been swapped:
Now, x is 10 and y is 5.
Exercise 2-5: Name Length

Ask the user to enter their first and last name. Then, display the total number of characters (including spaces) in their full name.

The output could look like this:

Enter your first name: John
Enter your last name: Doe
Your full name has 8 characters.

Stepping into more complex ground

Exercise 3-1: Simple If Statements
  1. Write a Python expression directly in the interpreter to check if a variable x is greater than 10. Try it with different values.
  2. Assign the number 10 to the variable threshold. Is x greater than threshold?
Exercise 3-2: If-Else Statements
  1. Write a Python expression directly into the interpreter that checks whether a number is even or odd. (You can use the modulus operator % to check for even numbers). If the number is even print out The number is even, otherwise print The number is odd.
  2. Instead of writing the expression into the interpreter, put it into a program that asks the user to input a number. The program should then print whether the number is even or odd.
Exercise 3-3: Comparing Two Numbers: If-Elif-Else Statements

Compare two numbers and print which one is larger, or if they are equal. Like in the exercise before, try doing this directly in the interpreter first, and then in a short program.

Exercise 3-4: Nested If Statements

Write a program that checks if a number is positive, negative, or zero, and also whether the number is even or odd.

Solutions

Here you will find the solutions to the above exercises. Note that you can copy and paste simple expressions and statements into the interpreter here, or more complex programs into the editor in the top panel here (then click Run).

# Solution 1: Direct output
print("Hello! My name is Alex.")
print("I am 25 years old.")
print("My favorite hobby is painting.")

# Solution 2: Using variables
name = "Alex"
age = 25
hobby = "painting"
print("Hello! My name is " + name + ".")
print("I am " + str(age) + " years old.")
print("My favorite hobby is " + hobby + ".")
# Solution
# Ask for user input
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))

# Perform calculations
sum_result = num1 + num2
difference = num1 - num2
product = num1 * num2
quotient = num1 / num2

# Display results
print("The sum is: " + str(sum_result))
print("The difference is: " + str(difference))
print("The product is: " + str(product))
print("The quotient is: " + str(quotient))
# Solution
# Ask for temperature input in Celsius
celsius = float(input("Enter the temperature in Celsius: "))

# Convert to Fahrenheit
fahrenheit = (celsius * 9/5) + 32

# Display the result
print("The temperature in Fahrenheit is: " + str(fahrenheit))
# Solution
# Define two variables
x = 5
y = 10

# Display initial values
print("x is " + str(x) + " and y is " + str(y) + ".")

# Swap the values without using a third variable
x = x*y
y = x/y
x = x/y

# Display the swapped values
print("Hold on a second, the values might just have been swapped:")
print("Now, x is " + str(x) + " and y is " + str(y) + ".")
# Solution
# Ask for the user's first and last name
first_name = input("Enter your first name: ")
last_name = input("Enter your last name: ")

# Calculate the length including spaces
full_name = first_name + " " + last_name
name_length = len(full_name)

# Display the result
print("Your full name has " + str(name_length) + " characters.")
# Solution 1:
x = 15
if x > 10:
    print("x is greater than 10")

# Solution 2: Using a threshold variable
threshold = 10
if x > threshold:
    print("x is greater than " + str(threshold))
# Solution 1: Direct interpreter use
number = 7
if number % 2 == 0:
    print("The number is even")
else:
    print("The number is odd")

# Solution 2: Using a program that asks for user input
number = int(input("Enter a number: "))
if number % 2 == 0:
    print("The number is even")
else:
    print("The number is odd")
# Solution
a = 5
b = 10
if a > b:
    print("a is greater than b")
elif a < b:
    print("b is greater than a")
else:
    print("a and b are equal")
# Solution
num = -4
if num > 0:
    if num % 2 == 0:
        print("The number is positive and even")
    else:
        print("The number is positive and odd")
elif num < 0:
    if num % 2 == 0:
        print("The number is negative and even")
    else:
        print("The number is negative and odd")
else:
    print("The number is zero")