Exercises

1. Combining strings using concatenation

Create two string variables, 'greeting' and 'name', and concatenate them to print a personalized greeting.

Code
greeting = "Hello, "
name = "world!"
print(greeting + name)

Declare two more string variables, 'first_name' and 'last_name', and concatenate them to print the full name.

Code
first_name = "John"
last_name = "Doe"
print("Full Name: " + first_name + " " + last_name)

2. Arithmetic expressions in Python

Calculate and print the result of the expression: x = 10 + 5 * 2

Code
x = 10 + 5 * 2
print("Result of x:", x)

Create a new variable 'y' and assign the result of the expression: y = 3 ** 2

Code
y = 3 ** 2
print("Result of y:", y)

3. Exponentiation in Python

Calculate and print the result of 2 raised to the power of 4 using the exponentiation operator.

Code
result = 2 ** 4
print("2 to the power of 4:", result)

4. Modulo operator in Python

Calculate and print the remainder when 15 is divided by 4 using the modulo operator.

Code
x = 15 % 4
print("Remainder:", x)

5. Augmented assignment in Python

Initialize variables x and y with some values.

Code
x = 5
y = 3

Use augmented assignment to add y to x and print the result.

Code
x += y
print("Result of x += y:", x)

6. Comments in Python

Add comments to the following code explaining the purpose of each line.

Code
# Calculate the sum of two numbers
num1 = 10
num2 = 20
sum_result = num1 + num2

Print the result

Code
print("Sum:", sum_result)

7. Understanding data types - int vs float

Declare an integer variable 'integer_var' and a float variable 'float_var'.

Code
integer_var = 5
float_var = 3.14

Print the data type of each variable.

Code
print("Type of integer_var:", type(integer_var))
print("Type of float_var:", type(float_var))

8. Multi-line string in Python

Create a multi-line string containing your address.

Code
address = """123 Main Street
Cityville, USA
Zip: 12345"""

Print the multi-line string.

Code
print(address)

9. Booleans in Python

Declare boolean variables indicating weather conditions.

Code
is_sunny = True
is_raining = False

Print the values of the boolean variables.

Code
print("Is it sunny?", is_sunny)
print("Is it raining?", is_raining)

10. Type error in Python

Fix the type error in the following statement and print the corrected string.

f1 = 0.25
f2 = 40.0
p = f1 * f2
bs = "The price is " + p
print(bs)
Code
f1 = 0.25
f2 = 40.0
p = f1 * f2
bs = "The price is " + str(p)
print(bs)

11. For loop with countries

Create a list of countries: Spain, France and Germany.

Code
countries = ["Spain", "France", "Germany"]

Use a for loop to print a message for each country.

Code
for country in countries:
    print("The country is " + country)

12. For loop with numbers

Create a list of numbers: [1, 2, 3].

Code
numbers = [1, 2, 3]

Use a for loop to calculate the product of all numbers and print the result.

Code
total = 1
for num in numbers:
    total *= num

print("Product of numbers:", total)

13. Return statements in Python

Define a function 'calculate_sum' that takes two parameters and returns their sum.

Code
def calculate_sum(a, b):
    return a + b

Use the function to calculate and print the sum of 7 and 3.

Code
result = calculate_sum(7, 3)
print("The sum is:", result)

Create a function 'is_positive' that takes a number as a parameter and returns True if it's positive, False otherwise.

Code
def is_positive(number):
    return number > 0

Test the 'is_positive' function with both positive and negative numbers and print the results.

Code
print(is_positive(5))  # Should print True
print(is_positive(-2))  # Should print False

14. Defining a square function and calling it

Define a function 'square' that takes a number as a parameter and returns its square.

Code
def square(num):
    return num * num

Use the 'square' function to calculate and print the square of 8.

Code
result = square(8)
print("The square is:", result)

Create a function 'calculate_area' that calculates the area of a square given its side length. Use the 'square' function to find the area of a square with side length 5.

Code
def calculate_area(side_length):
    return square(side_length)

area = calculate_area(5)
print("The area of the square is:", area)

15. Using a for loop

Write a program that uses a for loop to print the numbers from 1 to 5.

Code
for i in range(1, 6):
    print(i)

16. Comparison operators in Python

Create a function 'compare_numbers' that takes two numbers as parameters and prints a message indicating which number is greater or if they are equal.

Code
def compare_numbers(x, y):
    if x > y:
        print(f"{x} is greater than {y}.")
    elif x < y:
        print(f"{x} is less than {y}.")
    else:
        print(f"{x} is equal to {y}.")

Test the 'compare_numbers' function with different pairs of numbers.

Code
compare_numbers(7, 3)
compare_numbers(5, 8)
compare_numbers(4, 4)