Learn how Python stores data in memory using variables, discovers the five core data types, masters operators, type casting, and user input — the engine of every Python program.
In programming, data needs to be stored, processed, and reused multiple times. Instead of writing values repeatedly, we use variables to store information in memory.
A variable is a name given to a memory location that stores data. Think of it as a labelled container holding a value you can use or change later.
Data types define what kind of value a variable holds. Python automatically detects the type based on the assigned value — no need to declare types manually.
int age = 25;, Python simply needs age = 25. Python figures out the type automatically. This makes Python fast to write and easy to read.Stores any whole number — positive, negative, or zero. No decimal point.
Stores numbers with a decimal point. Used for measurements, prices, and calculations.
Stores text enclosed in single or double quotes. Letters, words, sentences — any text.
Only two possible values. Used in conditions, decisions, and logic. Always capitalized.
Represents the intentional absence of any value. Like a placeholder or "empty" variable.
Operators are symbols used to perform operations on values and variables. Python has four main categories.
| Operator | Name | Example → Result |
|---|---|---|
| + | Addition | 10 + 5 → 15 |
| - | Subtraction | 10 - 3 → 7 |
| * | Multiplication | 4 * 5 → 20 |
| / | Division | 10 / 2 → 5.0 |
| % | Modulus (remainder) | 10 % 3 → 1 |
| ** | Exponent (power) | 2 ** 3 → 8 |
| // | Floor Division | 10 // 3 → 3 |
| Operator | Equivalent To | Example |
|---|---|---|
| = | Assign | x = 10 |
| += | x = x + n | x += 5 → 15 |
| -= | x = x - n | x -= 3 → 7 |
| *= | x = x * n | x *= 2 → 20 |
| /= | x = x / n | x /= 2 → 5.0 |
| Op | Meaning | Example → Result |
|---|---|---|
| == | Equal to | 5 == 5 → True |
| != | Not equal | 5 != 3 → True |
| > | Greater than | 10 > 5 → True |
| < | Less than | 3 < 8 → True |
| >= | Greater or equal | 5 >= 5 → True |
| <= | Less or equal | 4 <= 6 → True |
Type Casting means converting one data type into another. Python provides built-in functions for this.
| Function | Converts To | Example | Result |
|---|---|---|---|
| int() | Integer | int("42"), int(3.9) | 42, 3 (truncates) |
| float() | Float | float(10), float("3.14") | 10.0, 3.14 |
| str() | String | str(100), str(True) | "100", "True" |
| bool() | Boolean | bool(0), bool(1) | False, True |
= operator. Python is dynamically typed, so you don't need to declare the type — it's inferred automatically. Example: x = 10 creates an integer variable.int (whole numbers like 25), float (decimal numbers like 3.14), str (text like "Hello"), bool (True or False), and NoneType (represents no value). Python detects types automatically based on the assigned value.int() converts to integer, float() to float, str() to string, and bool() to boolean. It's especially useful when working with user input, since input() always returns a string.input() function always returns a string, regardless of what the user types. If the user enters the number 25, Python stores it as the string "25", not the integer 25. To use it as a number, you must convert it: age = int(input("Enter age: ")).= is the assignment operator — it stores a value in a variable: x = 5. == is the comparison operator — it checks if two values are equal and returns True or False: x == 5 → True. Confusing them is one of the most common Python mistakes.name and Name are different variables. Reserved keywords like if, for, while cannot be used as variable names.Write every line yourself — the best way to learn is by doing, not just reading.
Write a Python program to add two numbers and print the result.
Write a program to find the remainder when one number is divided by another.
Take input from the user and check the data type of the entered value using the type() function.
Given a = 34 and b = 80, use a comparison operator to determine if a is greater than b. Print the result.
Write a Python program to calculate the average of two numbers entered by the user.
Write a Python program to calculate the square of a number entered by the user.
You have successfully completed Chapter 2 — Variables & Data Types. You can now build programs that store, process, and interact with data!
Two chapters down, ten to go. You're building serious Python foundations — keep the momentum!