# 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
= "Alex"
name = 25
age = "painting"
hobby print("Hello! My name is " + name + ".")
print("I am " + str(age) + " years old.")
print("My favorite hobby is " + hobby + ".")
Exercises
Make sure you have read section Using python online before you start with the exercises.
Using python as calculator
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.
- Calculate 5 squared.
- Calculate 2 cubed (i.e., 2 to the power of 3).
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
- 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.
- 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
- 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.
- Slightly rewrite the program by defining three variables (
name
,age
,hobby
) that will be used in the print statement of your introduction.
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
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
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.
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
- Write a Python expression directly in the interpreter to check if a variable x is greater than 10. Try it with different values.
- Assign the number 10 to the variable
threshold
. Is x greater than threshold?
- 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 outThe number is even
, otherwise printThe number is odd
. - 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.
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.
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
# 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
# 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: 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")