naming as well for clean code bad and good example.
Type Conversion and Casting Input Output and User Interaction Operator Precedence and Expressions
Variables Data Types and Operators¶
Variables¶
Python uses variables to store information that can be used later in a program. Think of a variable as a name tag attached to an object in memory — not the box itself, but the label stuck onto it.
🧱 Creating Variables¶
A variable is created when you assign a value to a name using the = sign.
name = "Chandravesh"
age = 31
revenue = 45000.75
is_active = TrueYou can later use these variables anywhere in your program:
print("Name:", name)
print("Age:", age)
print("Revenue:", revenue)
print("Active:", is_active)🎯 What Really Happens in Memory¶
When you write:
x = 10Python creates an object (10) in memory, and x is just a reference (a label) that points to that object.
If you do this:
y = xNow both x and y point to the same object in memory.
x = 10
y = x
print(id(x))
print(id(y))✅ The id() function shows that both have the same ID, meaning they point to the same memory location.
| Variable | Points To | Object Value |
|---|---|---|
x | 👉 | 10 |
y | 👉 | 10 (same object as x) |
Now, if you change the object:
x = 20
print(y)❗ y still points to the old object (10), because you changed what x points to, not the object itself.
💡 Analogy¶
Think of x and y as name tags stuck to a coffee mug.
Both can point to the same mug ☕, but if x changes its tag to a new mug, y still remains attached to the old one.
Variables¶
A variable is created when you assign a value to a name using the = sign.
name = "Chandravesh"
age = 30
revenue = 45000.75
is_active = TrueYou can later use these variables anywhere in your program.
print("Name:", name)
print("Age:", age)
print("Revenue:", revenue)✅ Naming Rules
Must start with a letter or underscore (
_)Can contain letters, numbers, and underscores
Case-sensitive (
name≠Name)Avoid using keywords like
for,if,class, etc.
🧮 Data Types¶
Every value in Python has a type.
| Data Type | Example | Description |
|---|---|---|
int | 10 | Whole numbers |
float | 3.14 | Decimal numbers |
str | "Python" | Text or characters |
bool | True, False | Logical values |
list | [1, 2, 3] | Ordered, changeable collection |
tuple | (1, 2, 3) | Ordered, unchangeable collection |
dict | {"name": "Alex", "sales": 200} | Key-value pairs |
You can check a variable’s type with the built-in type() function.
x = 42
y = "Analytics"
print(type(x)) # <class 'int'>
print(type(y)) # <class 'str'>⚙️ Operators in Python¶
Operators perform operations on variables and values.
Arithmetic Operators¶
| Operator | Description | Example |
|---|---|---|
+ | Addition | 10 + 5 → 15 |
- | Subtraction | 10 - 3 → 7 |
* | Multiplication | 4 * 2 → 8 |
/ | Division | 8 / 2 → 4.0 |
% | Modulus (Remainder) | 9 % 2 → 1 |
** | Exponent | 2 ** 3 → 8 |
Comparison Operators¶
| Operator | Meaning | Example |
|---|---|---|
== | Equal to | 5 == 5 → True |
!= | Not equal to | 5 != 3 → True |
> | Greater than | 10 > 5 → True |
< | Less than | 2 < 8 → True |
Logical Operators¶
| Operator | Description | Example |
|---|---|---|
and | True if both are True | (5 > 2 and 4 < 10) → True |
or | True if one is True | (5 > 2 or 4 > 10) → True |
not | Reverses result | not (5 > 2) → False |
🔁 Type Conversion¶
You can change one data type to another using:
x = "100"
y = int(x) # converts string to integer
z = float(x) # converts string to float
print(type(y), type(z))Why Naming Conventions Exist¶
Good naming makes code easier to read, debug, and share. Python follows certain rules and best practices:
Variable names cannot be Python keywords (like
class,if,for).They must start with a letter or underscore (
_), not a number.They are case-sensitive (
Name≠name).
These conventions help both humans and computers understand the code clearly!
Why You Can't Use Words Like |
🧠 Quick Practice Questions¶
What is the difference between a variable and an object in Python?
What will this code print and why?
a = [1, 2, 3] b = a b.append(4) print(a)Predict the output:
x = 5 y = x x = 7 print(y)Create variables to store:
Your name, age, monthly income, and whether you are a student. Then print them in one line using:
print(name, age, income, is_student)Write a Python program that:
Takes two numbers
aandb.Prints their sum, difference, and product.
Example:
a = 10
b = 3
## your code herePredict the output without running the code:
x = 5
y = 10
print(x > 2 and y < 5)
print(x == 5 or y == 5)
print(not (x == y))Which of the following are valid variable names? a)
2priceb)_pricec)classd)price2Predict the output:
value = 5 Value = 10 print(value + Value)Identify the data types:
a = 10 b = "Python" c = 3.14 d = TrueFind a reserved keyword using
keyword.kwlistand try using it as a variable. What error do you get? Replace it with a meaningful name.Write a short Python snippet that calculates profit margin using variables with clear business-style names (
cost_price,selling_price,profit_margin).
# Your code hereExercises¶
Exercise 1¶
Write filter_and_square(nums) that returns squares of even integers from nums (ignore non-integers).
Exercise 2¶
Implement safe_divide_list(nums, denom) that divides each numeric n in nums by denom, skipping non-numeric entries and avoiding ZeroDivisionError.
Exercise 3¶
Create flatten_unique(list_of_lists) that flattens a nested list and returns unique values in sorted order.