← Computer Programming I

Quick reference

Values and variables

name = "Ali"          # str
age = 18              # int
height = 1.75         # float
is_student = True     # bool
type(value)

Input and type conversion

text = input("Enter text: ")
whole_number = int(input("Enter a whole number: "))
decimal_number = float(input("Enter a number: "))
text_value = str(value)

input() always returns a string. Convert the result when the program needs a number.

Arithmetic operators

Operator Meaning
+ addition
- subtraction
* multiplication
/ division
// floor division
% remainder
** exponentiation

Use parentheses to control the order of operations:

result = (a + b) * c

Comparison operators

Operator Meaning
== equal to
!= not equal to
< less than
<= less than or equal to
> greater than
>= greater than or equal to

A comparison produces either True or False.

Boolean operators

condition_1 and condition_2
condition_1 or condition_2
not condition
  • and is True when both conditions are True.
  • or is True when at least one condition is True.
  • not reverses a Boolean value.

Conditional statements

if condition:
    statement
if condition:
    statement
else:
    statement
if condition_1:
    statement
elif condition_2:
    statement
else:
    statement
  • Put a colon after every condition and after else.
  • Indent the statements that belong to each branch.
  • Conditions are checked from top to bottom.
  • Only the first matching branch in an if–elif–else statement runs.

Problem 1: Pass or fail

Write a program that reads a student’s exam score.

  • If the score is 50 or higher, print Pass.
  • Otherwise, print Fail.

Example 1

Enter the score: 73
Pass

Example 2

Enter the score: 42
Fail

Problem 2: Compare two numbers

Write a program that reads two integers and compares them.

  • If the first number is greater, print The first number is greater.
  • If the second number is greater, print The second number is greater.
  • If the numbers are equal, print The numbers are equal.

Example 1

Enter the first number: 12
Enter the second number: 7
The first number is greater

Example 2

Enter the first number: 5
Enter the second number: 5
The numbers are equal

Problem 3: Even or odd

Write a program that reads an integer.

  • If the number is even, print Even.
  • Otherwise, print Odd.

Use the remainder operator % to make the decision.

Example 1

Enter an integer: 18
Even

Example 2

Enter an integer: 7
Odd

Problem 4: Divisible by 3 and 5

Write a program that reads an integer and prints one of these messages:

  • Divisible by both if the number is divisible by both 3 and 5;
  • Divisible by 3 if it is divisible only by 3;
  • Divisible by 5 if it is divisible only by 5;
  • Not divisible by 3 or 5 otherwise.

Example 1

Enter an integer: 30
Divisible by both

Example 2

Enter an integer: 9
Divisible by 3

Example 3

Enter an integer: 7
Not divisible by 3 or 5

Problem 5: Leap year

Write a program that reads a year and determines whether it is a leap year.

A year is a leap year if:

  • it is divisible by 400; or
  • it is divisible by 4 but not divisible by 100.

Print Leap year or Not a leap year.

Example 1

Enter a year: 2024
Leap year

Example 2

Enter a year: 1900
Not a leap year

Example 3

Enter a year: 2000
Leap year

Problem 6: Number of real roots

A quadratic equation has the form:

ax² + bx + c = 0

Its discriminant is calculated as:

D = b² − 4ac

Write a program that reads a, b, and c, then calculates the discriminant.

  • If D > 0, print Two real roots.
  • If D == 0, print One real root.
  • If D < 0, print No real roots.

You may assume that a is not zero.

Example 1

Enter a: 1
Enter b: -5
Enter c: 6
Two real roots

Example 2

Enter a: 1
Enter b: 2
Enter c: 1
One real root

Example 3

Enter a: 1
Enter b: 0
Enter c: 1
No real roots

Problem 7: Classify a triangle

Write a program that reads three side lengths.

First, determine whether the lengths can form a triangle. They form a triangle only if:

  • each pair of sides has a sum greater than the remaining side.

If the lengths do not form a triangle, print Not a triangle.

Otherwise, print:

  • Equilateral if all three sides are equal;
  • Isosceles if exactly two sides are equal;
  • Scalene if all three sides are different.

Example 1

Enter the first side: 5
Enter the second side: 5
Enter the third side: 5
Equilateral

Example 2

Enter the first side: 5
Enter the second side: 5
Enter the third side: 8
Isosceles

Example 3

Enter the first side: 3
Enter the second side: 4
Enter the third side: 5
Scalene

Example 4

Enter the first side: 1
Enter the second side: 2
Enter the third side: 3
Not a triangle

Problem 8: Rock, paper, scissors

Write a program for two players.

Each player enters one of these choices:

  • rock
  • paper
  • scissors

The rules are:

  • rock beats scissors;
  • scissors beats paper;
  • paper beats rock.

Print Draw, Player 1 wins, or Player 2 wins.

You may assume that both players enter a valid choice.

Example 1

Player 1: rock
Player 2: scissors
Player 1 wins

Example 2

Player 1: paper
Player 2: scissors
Player 2 wins

Example 3

Player 1: rock
Player 2: rock
Draw

Problem 9: Middle of three numbers

Write a program that reads three integers and prints the middle value when the numbers are arranged from smallest to largest.

The numbers may be equal.

Example 1

Enter the first number: 8
Enter the second number: 3
Enter the third number: 5
Middle value: 5

Example 2

Enter the first number: 4
Enter the second number: 9
Enter the third number: 4
Middle value: 4

Example 3

Enter the first number: -2
Enter the second number: -7
Enter the third number: -4
Middle value: -4

Problem 10: Progressive electricity bill

Write a program that reads the number of electricity units used and calculates the total bill in so‘m.

Use these fictional rates:

  • The first 100 units cost 500 so‘m per unit.
  • The next 100 units cost 700 so‘m per unit.
  • Any units above 200 cost 1,000 so‘m per unit.

Each rate applies only to the units in that range. For example, for 120 units, the first 100 cost 500 so‘m each, and the remaining 20 cost 700 so‘m each.

You may assume that the input is a non-negative integer.

Example 1

Enter units used: 80
Total bill: 40000 so‘m

Example 2

Enter units used: 120
Total bill: 64000 so‘m

Example 3

Enter units used: 250
Total bill: 170000 so‘m

Problem 11: Next date

Write a program that reads a day, month, and year, then prints the next date.

You may assume that the input is a valid date and the year is a positive integer.

The number of days in each month is:

  • 30 days: April (4), June (6), September (9), November (11).
  • 31 days: January (1), March (3), May (5), July (7), August (8), October (10), December (12).
  • February (2): 29 days in a leap year and 28 days otherwise.

A year is a leap year if:

  • it is divisible by 400; or
  • it is divisible by 4 but not divisible by 100.

Print the next date in day/month/year format. Leading zeros are not required.

Example 1

Enter the day: 15
Enter the month: 9
Enter the year: 2026
Next date: 16/9/2026

Example 2

Enter the day: 30
Enter the month: 4
Enter the year: 2026
Next date: 1/5/2026

Example 3

Enter the day: 28
Enter the month: 2
Enter the year: 2024
Next date: 29/2/2024

Example 4

Enter the day: 31
Enter the month: 12
Enter the year: 2026
Next date: 1/1/2027

Problem 12: ATM withdrawal

An ATM contains only 50,000-so‘m and 20,000-so‘m notes. The number of notes of each type is limited.

Write a program that reads:

  • the number of available 50,000-so‘m notes;
  • the number of available 20,000-so‘m notes;
  • the amount a customer wants to withdraw.

Print Withdrawal possible if the ATM can pay the exact amount using the available notes. Otherwise, print Withdrawal impossible.

You may assume that the note counts are non-negative integers and the requested amount is a positive integer.

Example 1

Enter the number of 50000-so‘m notes: 1
Enter the number of 20000-so‘m notes: 3
Enter the withdrawal amount: 60000
Withdrawal possible

Example 2

Enter the number of 50000-so‘m notes: 1
Enter the number of 20000-so‘m notes: 2
Enter the withdrawal amount: 60000
Withdrawal impossible

Example 3

Enter the number of 50000-so‘m notes: 1
Enter the number of 20000-so‘m notes: 2
Enter the withdrawal amount: 90000
Withdrawal possible