Lesson 2 of 5

Variables and Data Types

Storing Information in Variables

A variable is a labelled box that holds a value. You create one by choosing a name, writing an equals sign, and giving it a value. The equals sign is the assignment operator.

name = "Ayesha"
age = 19
height = 1.63
is_student = True

Common Data Types

  • str — text, written inside quotes, e.g. "Dhaka"
  • int — whole numbers, e.g. 42
  • float — numbers with a decimal point, e.g. 3.14
  • bool — a truth value, either True or False

You can check the type of any value with the built-in type() function.

print(type(age))     # <class 'int'>
print(type(name))    # <class 'str'>

Naming Rules

Variable names may contain letters, digits, and underscores, but they cannot start with a digit and cannot be a Python keyword. Choose descriptive names: total_marks is far clearer than x.

Working with Numbers

Python handles arithmetic with familiar operators.

a = 10
b = 3
print(a + b)   # 13  addition
print(a - b)   # 7   subtraction
print(a * b)   # 30  multiplication
print(a / b)   # 3.333... division (always a float)
print(a // b)  # 3   floor division
print(a % b)   # 1   remainder (modulo)
print(a ** b)  # 1000 exponent

Joining Strings

An f-string lets you place variables directly inside text. Put an f before the opening quote and wrap variables in curly braces.

name = "Ayesha"
age = 19
print(f"{name} is {age} years old.")
Remember: a variable holds whatever was last assigned to it. Assigning a new value replaces the old one.
Next
How did you find this lesson?

Discussion

1
To join the discussion, comment and react, log in or create an account.
Jit
Jit 3 months ago
Ei জিনিষ ta bujtechina ,
Keu আরেকটু details vabe bujiye dile valo হতো ...