Basics (Operations and Variables)

Welcome to this practical guide for extreme beginners in Pythonemoji, focusing on operations and variables. The exercises here aim to provide a strong foundation for newcomers to programming. Please feel free to reach out if you find any errors, typos, or have any questions—your feedback is invaluable! Don’t hesitate to email me with any inquiries, and enjoy your learning journey! 🚀📚

Nasser-eddine Monir (CC BY-NC-SA) ― 2024

First steps


Exercise 1

Write a program that prints the sentence 'Hello, World!'.

Solution
 print("Hello, World!")

Exercise 2

Write a program that prints the number 42.

Solution
 print(42)

Exercise 3

Write a program that prints the string '42'.

Solution
 print("42")
❗You notice that the integer ```42``` is different than the string ```"42"```.

Basic Operations


Exercise 4

Write a Python program that adds two numbers.

Solution
 5 + 3

Exercise 5

Write a Python program that subtracts one number from another.

Solution
 10 - 2

Exercise 6

Write a Python program that multiplies two numbers.

Solution
 5 * 7

Exercise 7

Write a Python program that divides one number by another (floating-point division).

Solution
 8/4

Exercise 8

Write a program that calculates the cube of the number 7.

Solution
 7**3

Exercise 9

You are planting a garden, and each plant needs 3 feet of space in every direction. If you have a square garden with sides of length 8 feet, how many square feet of space do you have for planting? Write a program to calculate the area of the square garden using an exponent.

💡 Hint: \(Area = side\;length^2\)

Solution
 8**2

Exercise 10

Write a program that performs floor division between 15 and 4.

💡 Hint: The floor division is the integer part of the quotient. For instance, let 7/2 = 3.5. The quotient is 3.5, and the integer part of the quotient is 3.

Solution
 15//4

Exercise 11

Write a program that computes the remainder of the division between 15 and 4.

💡 Hint: The remainder is the part left over after dividing one number by another when the division does not result in an integer.

Solution
 15%4

Exercise 12 ★★

You have 98 eggs. Write a program that calculates how many dozens (12 eggs) you have and how many eggs remain. Print each result of each operation on a separate line.

Solution
 print(98//12)
 print(98%12)

Exercise 13 ★★

You have 145 candies to distribute equally among 8 children. Write a program that calculates and prints how many candies each child gets and how many are left over. Print the result of each operation on a separate line.

Solution
 print(145//8)
 print(145%8)

Exercise 14 ★★

Write a program that computes and prints the tens place and ones place of the number 57 using // and %.

💡 Hint: The tens place of a number represents how many full sets of ten are in the number (e.g., Tens place of 63 is 6). The ones place represents the leftover units after accounting for the tens (e.g., Ones place of 63 is 3).

Solution
 print(57//10)
 print(57%10)

Exercise 15 ★★★

Write a program that computes the tens place of the number 657 using // and %.

💡 Hint: The tens place of 657 is 5.

Solution
 (657%100)//10

Exercise 16 ★★★

Write a program that calculates the sum of the digits in the number 357 using // and %.

💡 Hint: 3+5+7 = 15

Solution
 (357//100) + ((357%100)//10) + (357%10)

Variables and Types (Int, Float, String, Bool)


Exercise 1

Write a program to declare the following variables:

  • age with the value 25
  • price with the value 19.99
  • name with the value Alice
  • is_raining with the value False Each variable on a separate line.
Solution
 age = 25
 price = 19.99
 name = "Alice"  # or 'Alice'
 is_raining = False

Exercise 2

Declare two integers a equal to 10 and b equal to 20. Then, print their sum and their product. Each operation on a separate line.

Solution
 a = 10
 b = 20
 print(a + b)
 print(a * b)

Exercise 3

Copy the following code and write a program that prints the type of each variable.

💡 Hint: Use the type() function!

 age = 25
 price = 19.99
 name = "Alice"  # or 'Alice'
 is_raining = False
Solution
 print(type(age)) # Output: <class 'int'> 
 print(type(price)) # Output: <class 'float'> 
 print(type(name)) # Output: <class 'str'> 
 print(type(is_raining)) # Output: <class 'bool'> 

Exercise 4

Write a program that checks if 7 is greater than 5, assigns the result to a variable named result, and prints the result as a boolean. What is the result?

💡 Hint: you don’t need to use conditions (if/elif/else)!

Solution
 result = 7 > 5 
 print(result) 

Exercise 5

Write a program that checks if 11 is lower than 7, assigns the result to a variable named result, and prints the result as a boolean. What is the result?

💡 Hint: you don’t need to use conditions (if/elif/else)!

Solution
 result = 11 < 7 
 print(result)

Exercise 6

Declare a variable num equal to 10, convert it to a float (assign it to a new variable), and then print the result.

💡 Hint: Read about “casting” (or “conversion”).

Solution
 num = 10 
 num_float = float(num) 
 print(num_float)

Exercise 7

Write a program that:

  • Declares three variables x, y, and z equal respectively to 10, 11, and 12.
  • Computes the average and assigns the result to a variable named average.
  • Prints the average of the three variables.
Solution
 x = 10 y = 11 z = 12 
 average = (x + y + z) / 3 
 print(average) 

Exercise 8

Write a program that computes the simple interest as follows:

  • Declare three variables principal, rate, and time equal respectively to 1000, 5.0, and 3.
  • Compute the simple interest and assign the result to a variable named interest.
  • Print the simple interest.

💡 Use this formula: \(Interest = \frac{P \times R \times T}{100}\)

Solution
 principal = 1000 
 rate = 5.0 
 time = 3 
 interest = (principal * rate * time) / 100 
 print(interest) 

Exercise 9 ★★

Declare a float variable radius equal to 5.0 and a float variable pi equal to 3.14. Then, compute the area of a circle using the formula \(\pi r^2\). Finally, print the area.

Solution
 radius = 5.0 
 pi = 3.14 
 area = pi * (radius ** 2) 
 print(area) 

Exercise 10 ★★

Write a program to calculate the compound interest. Declare variables: principal is equal to 1500 rate is equal to 4.3 time is equal to 6 n is equal to 4 (compounds per year)

💡 Hint: Use the formula \(A = P \left( 1 + \frac{R}{100n} \right)^{nt}\)

Solution
 principal = 1500 
 rate = 4.3 
 time = 6 
 n = 4 
 amount = principal * (1 + (rate / (100 * n))) ** (n * time) 
 print(amount) 

Exercise 11 ★★

Write a program that declares an integer x equal to 10 and prints True if x is equal to 5 * 2, False if it’s not.

💡 Hint: You don’t need conditions (if/elif/else).

Solution
 x = 10 
 print(x == 5 * 2) 

Exercise 12 ★★★

Write a program that declares an integer n equal to 15 and prints True if it’s even, False if it’s odd.

💡 Hint: You don’t need conditions (if/elif/else).

Solution
 n = 15 
 print(n % 2 == 0)