A variable is simply a named container used to store data in memory so that it can be used and modified later in the program. Unlike some languages, Python does not require you to declare the type of a variable — it figures that out automatically based on the value you assign.
name = "Iqubal"
age = 28
course_fee = 15000.50
is_enrolled = True
print(name, age, course_fee, is_enrolled)# Assign multiple variables in one line
a, b, c = 10, 20, 30
print(a, b, c)
# Assign the same value to multiple variables
x = y = z = 100
print(x, y, z)Every value in Python has a data type that determines what kind of operations can be performed on it. Python automatically detects the data type — this is called dynamic typing.
| Data Type | Example | Description |
|---|---|---|
| int | age = 28 | Whole numbers, positive or negative |
| float | price = 499.99 | Decimal / floating-point numbers |
| str | name = "Samantus" | Text data, enclosed in quotes |
| bool | is_active = True | Only two values: True or False |
| list | items = [1,2,3] | Ordered, changeable collection |
| tuple | point = (10,20) | Ordered, unchangeable collection |
| dict | user = {"id":1} | Key-value pair collection |
| set | tags = {1,2,3} | Unordered collection of unique items |
| NoneType | result = None | Represents "no value" |
x = 28
y = 28.5
z = "Samantus"
print(type(x))
print(type(y))
print(type(z))Type casting means converting a value from one data type to another — for example, converting user input (which is always text/string) into a number so you can perform calculations on it.
a = "25" # string
b = int(a) # convert to integer
print(b + 5) # now math works
c = 9.8
d = int(c) # float to int (drops decimal)
print(d)
e = 100
f = str(e) # int to string
print("Fee: " + f)The input() function lets your program take data from the user, and print() displays output back to the screen.
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Hello", name, "- you are", age, "years old")name = "Priya"
fee = 15000
print(f"Hi {name}, your course fee is Rs.{fee}")Comments are notes in your code that Python ignores when running the program. They help you and others understand what the code does.
# This is a single-line comment
print("Hello") # this comment is at the end of a line
"""
This is a multi-line comment / docstring
often used to explain what a function does
"""Keywords are reserved words that have special meaning in Python and cannot be used as variable names.
| Category | Keywords |
|---|---|
| Value keywords | True, False, None |
| Logical operators | and, or, not |
| Conditionals | if, elif, else |
| Loops | for, while, break, continue, pass |
| Functions/Classes | def, return, class, lambda |
| Exception handling | try, except, finally, raise |
| Import | import, from, as |
Operators are special symbols used to perform operations on variables and values.
| Type | Operators | Example |
|---|---|---|
| Arithmetic | + - * / // % ** | 10 % 3 = 1 |
| Comparison | == != > < >= <= | 10 > 5 = True |
| Logical | and or not | True and False = False |
| Assignment | = += -= *= /= | x += 5 |
| Membership | in not in | "a" in "cat" = True |
| Identity | is is not | x is None |
a = 10
b = 3
print(a + b) # 13
print(a % b) # 1 (remainder)
print(a ** b) # 1000 (power)
print(a // b) # 3 (floor division)An expression is any combination of values, variables, and operators that Python can evaluate to produce a result.
price = 500
quantity = 3
discount = 0.10
total = price * quantity * (1 - discount)
print(total)Basic (5 Questions)
1. Create variables for your name, age, and city, and print them together.
2. Store two numbers in variables and print their sum, difference, and product.
3. Take a number as input from the user and print its data type.
4. Write a program using an f-string to print 'My name is ___ and I am ___ years old'.
5. Add comments explaining each line of one of your earlier programs.
Intermediate (5 Questions)
1. Take two numbers as input (as strings) and correctly convert and add them.
2. Write a program to swap the values of two variables without using a third variable.
3. Create a simple bill calculator: price, quantity, and print the total using an expression.
4. Demonstrate the difference between int(9.9) and round(9.9) with printed output.
5. List 5 Python keywords and explain what each one is used for.
Advanced (5 Questions)
1. Write a program that calculates GST (18%) on a product price and prints the final amount.
2. Explain, with an example, why input() always needs type casting for numeric operations.
3. Create a program using multiple assignment to initialize 4 variables in a single line.
4. Research the difference between is and == in Python and demonstrate with a code example.
5. Build a simple 'Student Fee Calculator' that takes fee, discount %, and prints the final payable fee.
Q1. Which of these is a valid variable name?
Q2. What is the data type of True in Python?
Q3. What does int("25") return?
Q4. Which operator gives the remainder of a division?
Q5. What does input() always return by default?
Assignment: Personal Expense Calculator
Expected Output: A working script that takes 3 inputs, correctly type-casts them, computes the total using an expression, and prints a nicely formatted summary line.
Use this space to write down key points, doubts, and your own examples from today's session.