Skip to main content

Lesson 2 of 5 15 min read

Variables, conditions and loops

Work with Python's basic types, make decisions with if, elif and else, and repeat work with for and while loops.

In this lesson you will

  • Use int, float, str and bool values and convert between them
  • Write conditions with comparison and logical operators
  • Use for loops with range and while loops

Variables and types

In Python you create a variable simply by assigning to it. The type comes from the value:

Python
age = 21              # int
height = 1.72         # float
name = "Meera"        # str
is_student = True     # bool (True or False, capitalised)

print(type(height))   # <class 'float'>
Python variables take their type from the value

age (int) holds 21; height (float) holds 1.72; name (str) holds "Meera"; is_student (bool) holds True

Python is dynamically typed: a variable can later hold a value of a different type, so choose clear names.

Convert between types when needed, especially for input:

Python
year_text = input("Year of joining: ")   # e.g. "2024"
year = int(year_text)
print(2026 - year, "years of study so far")

Arithmetic

Python
print(7 / 2)    # 3.5  true division always gives a float
print(7 // 2)   # 3    floor division
print(7 % 2)    # 1    remainder
print(2 ** 10)  # 1024 power

f-strings insert values into text: print(f"{name} is {age} years old") prints Meera is 21 years old.

Conditions

Python
marks = 82

if marks >= 90:
    grade = "A"
elif marks >= 75:
    grade = "B"
elif marks >= 60:
    grade = "C"
else:
    grade = "Needs improvement"

print(grade)   # B

Python's logical operators are words: and, or, not.

Python
age = 19
has_ticket = True
if age >= 18 and has_ticket:
    print("Welcome in")
if not has_ticket:
    print("Please buy a ticket")

for loops and range

A for loop goes through each item of a sequence:

Python
for i in range(1, 6):
    print(i, "squared is", i ** 2)

range(1, 6) produces 1, 2, 3, 4, 5. The end value is excluded. range(5) produces 0 to 4, and range(0, 10, 2) counts in steps of 2: 0, 2, 4, 6, 8.

while loops

A while loop repeats as long as its condition is true:

Python
savings = 0
week = 0
while savings < 1000:
    savings += 150
    week += 1
print(f"Reached ₹{savings} after {week} weeks")   # Reached ₹1050 after 7 weeks

break exits a loop early and continue skips to the next iteration, just like in Java.

Hands-on exercise

Write a number-guessing game. Store a secret number between 1 and 20 in a variable. Use a while loop to ask the user for guesses with input, convert each to int, and print "Too high", "Too low" or "Correct!" Stop the loop when the guess is right, then print how many attempts it took.

Knowledge check

3 questions • 70% to pass • Practice only, no certificate

  1. 1.What is printed?
    CODE
    total = 0
    for n in range(1, 4):
        total += n
    print(total)
  2. 2.What is the result of 9 // 2 in Python?
  3. 3.Which keyword starts an additional condition after if?
0 of 3 answered

Key concepts

  • Python infers types from values; convert text input with int() or float().
  • / always returns a float; // is floor division and % is the remainder.
  • if, elif and else make decisions; and, or and not combine conditions.
  • for loops iterate over sequences such as range; range excludes its end value.