TOPIC 2: Variables, Data Types & Operators

Site: Learning Management System
Course: Edo College ICT Club Programme
Book: TOPIC 2: Variables, Data Types & Operators
Printed by: Guest user
Date: Saturday, 3 January 2026, 7:46 AM

1. What is a Variable?

A variable is a name that holds a reference to a data object. Python allows you to create variables and assign values without needing to declare their types explicitly (Python uses dynamic typing).

Syntax for Declaring Variables:

variable_name = value

Example 1:

age = 30  # Integer
name = "Alice"  # String

Here:

  • age holds an integer value of 30.
  • name holds a string value "Alice".

Example 2:

is_active = True  # Boolean
height = 5.9  # Float

Here:

  • is_active is a boolean variable set to True.
  • height is a float variable set to 5.9.

2. Creating Variables

Python has no command for declaring a variable.

A variable is created the moment you first assign a value to it.

Example


x = 5
v = "John"
print(x)
print(v)
    

Variables do not need to be declared with any particular type, and can even change type after they have been set.

Example


x = 4      # x is of type int
x = "Sally"  # x is now of type str
print(x)
    

3. Data Types

Python has several built-in data types. Here are the most commonly used ones:

  • int: Whole numbers (e.g., 10, -5).
  • float: Numbers with a decimal point (e.g., 3.14, -5.99).
  • str: Text (strings of characters) (e.g., "Alice", "Hello").
  • bool: Boolean values True or False.

Example:


age = 25      # int
height = 5.9  # float
name = "Alice"  # str
is_active = True  # bool

4. Strings and Formatting

What are Strings?

Strings are sequences of characters enclosed in quotes. They are used to represent text in Python. Strings can be enclosed in either single quotes (') or double quotes (").

  • Creating Strings:

name = 'Alice'
greeting = "Hello, " + name  # Concatenation
print(greeting)  # Output: Hello, Alice

String Formatting: Python provides several ways to format strings. You can use f-strings (available in Python 3.6+) or the .format() method.

Example 1: Using f-strings


name = "Alice"
age = 30
print(f"Hello, {name}! You are {age} years old.")  # Output: Hello, Alice! You are 30 years old.

Example 2: Using .format()


name = "Alice"
age = 30
print("Hello, {}! You are {} years old.".format(name, age))  # Output: Hello, Alice! You are 30 years old.

5. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, and division.

Operators:

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • %: Modulo (remainder)
  • **: Exponentiation

Example 1:


x = 10
y = 3

print(x + y)  # 13
print(x - y)  # 7
print(x * y)  # 30
print(x / y)  # 3.3333

Example 2:


z = 5
w = 2
print(z ** w)  # Exponentiation: 5^2 = 25
print(z % w)   # Modulo: remainder when 5 is divided by 2, Output: 1

6. Exercise

  1. Write a program that accepts the user's name and age, and formats it into a sentence "Hello, [name]! You are [age] years old."
  2. Write a program that accepts two numbers and formats them as "The sum of [num1] and [num2] is [sum]."
  3. Use f-strings to format a string containing your favorite color and food.
  4. Use .format() to format a string showing the difference between two numbers.
  5. Write a program to format the current year and birth year into a sentence showing the age.


7. Comparison Operators

Comparison operators are used to compare two values and return a boolean value (True or False).

  • ==: Equal to
  • !=: Not equal to
  • >: Greater than
  • <: Less than
  • >=: Greater than or equal to
  • <=: Less than or equal to

Example 1:


x = 5
y = 10

print(x == y)  # False
print(x != y)  # True
print(x > y)   # False

Example 2:


a = 20
b = 15

print(a >= b)  # True
print(a <= b)  # False

8. Logical Operators

Logical operators are used to combine conditional expressions.

  • and: Returns True if both conditions are True.
  • or: Returns True if at least one condition is True.
  • not: Reverses the result (True becomes False, and vice versa).

Example 1:


x = 5
y = 10

print(x > 3 and y < 15)   # True
print(x == 5 or y == 5)   # True
print(not(x > 5))         # True

Example 2:


a = 5
b = 20

print(a > 10 or b > 10)   # True
print(not(a > 10))        # True

9. Exercise

  1. Create a program that accepts two numbers and performs addition, subtraction, multiplication, and d
  2. Write a program that checks if a number is positive and even using logical operators.
  3. Write a program that calculates the remainder when dividing two numbers
  4. Write a program that accepts two numbers and checks if the first is greater than the second
  5. Create a program that checks if a number is greater than 10 and even.