Welcome back to our Python programming series! In Part 1, we installed Python, explored IDLE, and wrote our first program using print(). Now it is time to go deeper. In this tutorial, you will learn how Python stores information using variables, the different types of data Python understands, and how to perform calculations and comparisons using operators.
This is Part 2 of our Python for ICSE and ISC Students series. If you have not read Part 1 yet, we recommend starting there first.
What Are Variables in Python?
A variable is like a labelled container that holds a value in your program. Think of it as a box with a name tag. You put something inside the box, and whenever you need that value, you use the name tag to find it.
In Python, you create a variable simply by assigning a value to a name using the equals sign (=).
Rules for Naming Variables
Python has some rules you must follow when choosing variable names:
- A variable name must start with a letter (a–z, A–Z) or an underscore (_).
- It can contain letters, digits (0–9), and underscores, but no spaces or special characters.
- Variable names are case-sensitive. So Age, age, and AGE are three different variables.
- You cannot use Python keywords like if, else, for, while, True, False, None as variable names.
Data Types in Python
Every value in Python has a type. The type tells Python what kind of data it is dealing with and what operations can be performed on it. Python automatically detects the type when you assign a value — this is called dynamic typing.
1. Integer (int)
Whole numbers without any decimal point. They can be positive, negative, or zero.
2. Float (float)
Numbers that have a decimal point. Use these when you need precision, such as for measurements, percentages, or prices.
3. String (str)
A sequence of characters enclosed in single quotes or double quotes. Strings are used for text — names, messages, addresses, and more.
4. Boolean (bool)
A Boolean holds only two possible values: True or False. Booleans are essential for decision-making in programs. Notice that True and False start with capital letters in Python.
5. List
A list is an ordered collection of items enclosed in square brackets. Lists can hold items of different types and can be changed after creation (they are mutable).
6. Tuple
A tuple is similar to a list, but it is enclosed in parentheses and cannot be changed after creation (it is immutable). Use tuples when you want to store data that should not be modified.
7. Dictionary (dict)
A dictionary stores data as key-value pairs inside curly braces. Each key is unique, and you use the key to retrieve its corresponding value. Dictionaries are very useful for storing structured information.
Type Conversion (Typecasting)
Sometimes you need to convert one data type into another. Python provides built-in functions for this purpose.
Important: You cannot convert a non-numeric string to an integer. For example, int("hello") will cause a ValueError.
The input() Function
The input() function allows your program to receive data from the user while the program is running. It always returns a string, so you need to convert it if you want a number.
A common beginner mistake is forgetting to convert the input. If you write age = input("Enter age: ") and then try age + 1, Python will give a TypeError because it tries to add a number to a string.
Operators in Python
Operators are special symbols that perform operations on values. Let us explore the main categories.
Arithmetic Operators
Exam tip: The difference between / and // is a very common exam question. The / operator always gives a float result, while // gives an integer result by removing the decimal portion (floor division).
Comparison (Relational) Operators
Logical Operators
Logical operators combine multiple conditions. Python uses the English words and, or, and not instead of symbols.
Assignment Operators
Putting It All Together: Student Report Card
Let us write a complete program that uses variables, data types, input(), and operators together.
Quick Quiz: Test Your Understanding
Q1. What is the output of print(type(3.0))?
Answer: <class 'float'> — The decimal point makes it a float even though 3.0 looks like a whole number.
Q2. What is the difference between 17 / 5 and 17 // 5?
Answer: 17 / 5 gives 3.4 (regular division, always float). 17 // 5 gives 3 (floor division, removes the decimal).
Q3. What will print("5" + "3") output?
Answer: 53 — Both are strings, so Python concatenates them instead of adding.
Q4. Can you change an item in a tuple?
Answer: No. Tuples are immutable — their items cannot be changed after creation.
Q5. What does not (5 > 3 and 2 < 1) evaluate to?
Answer: True — 5 > 3 is True, 2 < 1 is False. True and False = False. not False = True.
Summary
- Variables store values and have naming rules
- Data types include int, float, str, bool, list, tuple, and dict
- Type conversion uses int(), float(), str() to change types
- input() reads user input as a string
- Operators perform arithmetic, comparison, logical, and assignment operations
What is Next?
In Part 3 of this series, we will put your knowledge to the test with practice questions and solved programs — output prediction, error finding, and complete programs to write on your own. Make sure you are comfortable with Parts 1 and 2 before moving on.
Happy coding!