What is a Variable
A variable is a named container used to store data in memory. Program execution dynamic values handling ke liye memory spaces refer karti hai jise variable kehte hain.
Basic syntax for declaring a variable is:
variable_name = value
Basic Examples
full_name = "gagan" print(full_name) laptop = "asus" print(laptop) gagan = "teacher" print(gagan)
Variables serve as pointers to data stored in computer memory. Whenever you call a variable name, Python retrieves its assigned value.
Naming Rules & Conditions
Python variables standard syntax rules follow karte hain. In rules ko disregard karne par syntax errors arise hote hain:
- Cannot start with a number: Variable names number se start nahi ho sakte (e.g.
1xinvalid syntax parx1valid hai). - No special characters except underscore (_): Variable names me special characters (like
!,@,$,-, or spaces) allowed nahi hote. Faqat underscore (_) allowed hai (e.g.first_namevalid,last nameinvalid). - Case-Sensitive: Python case-sensitive language hai.
nameaurNamedo completely different variables treat hote hain.
| Variable Name | Status | Reason / Description |
|---|---|---|
first_name | Valid | Uses underscore correctly |
1x | Invalid | Starts with a digit |
last name | Invalid | Contains space |
gender_male | Valid | Standard snake_case format |
Name vs name | Valid | Case-sensitive: both exist separately |
Data Types in Python
Variables multiple types of values store kar sakte hain. Python auto-detects variable data types based on the value assigned:
- String (
str): Sequence of characters enclosed in single quotes (' ') or double quotes (" "). - Integer (
int): Whole numbers without decimal points. - Float (
float): Decimal numbers. - Boolean (
bool): Stores only two truth values:TrueorFalse.
| Variable Name | Value | Type | Description |
|---|---|---|---|
meri_city | "Chandigarh" | string | Enclosed in quotes |
gagan_ki_age | 18 | integer | Whole number |
mere_marks | 98.9 | float | Decimal point value |
gender_male | True | boolean | Holds True or False |
salary | "2220" | string | Numbers in quotes become strings |
meri_city = "Chandigarh" mera_naam = "gagan" mere_marks = 98.9 gender_male = True salary = "2220" print(f"meri city ka naam {meri_city}, mera naam hai {mera_naam} aur mera marks {mere_marks} mera gender male hai = {gender_male} aur mera salary hai {salary}")
Taking User Input
Python me user se dynamic values fetch karne ke liye input() function use hota hai. Default behavior me input() hamesha string return karta hai. Numerical input ke liye type casting (int() ya float()) perform ki jaati hai.
Name = input("apna naam dalo = ") age = int(input("apni age dalo = ")) print(f"Mera naam {Name} hai aur meri age {age} hai")
a = int(input("enter the first number = ")) b = int(input("enter the second number = ")) print(f"the output of a and b = {a + b}")
F-Strings Formatting
An f-string (formatted string literal) is used to insert variables or expressions inside a string using curly braces {}.
Syntax: f"sentence {variable}"
name = "lokesh" gagan_ki_age = 18 height = 5.9 print(f"ludhiana wale ka naam {name} aur gagan ki age hai {gagan_ki_age} aur uski height hai {height}")
F-strings accept valid Python math operations inside curly braces directly: f"Cost = {price * quantity}".
Variable Swapping
Python single line tuple unpacking mechanism se without temporary variable value swap support karta hai.
a = 10 b = 20 a, b = b, a print("a =", a) print("b =", b)
Practice Questions & Solutions
Comprehensive set of practice problems with solutions:
1. Print Name using F-String
name = "Ali" print(f"My name is {name}")
2. Name and City
name = "Ali" city = "Delhi" print(f"My name is {name} and I live in {city}")
3. Sum / Subtraction of Two Numbers
a = 10 b = 5 print(f"inka minu = {a - b}")
4. Area of Rectangle
length = 10 width = 5 print(f"Area of rectangle is {length * width}")
5. Total Cost Calculation
price = 50 quantity = 3 print(f"Total cost = {price * quantity}")
6. Age Example
age = 21 print(f"I am {age} years old")
7. Mathematical Operations
a = 20 b = 4 print(f"Addition = {a + b}") print(f"Subtraction = {a - b}") print(f"Multiplication = {a * b}") print(f"Division = {a / b}")
8. Student Marks from User Input
name = input("enter your name = ") marks = int(input("enter your marks ")) print(f"Student ka naam {name} kya hai iska scored {marks} marks")
9. Area of Triangle
base = 10 height = 5 print(f"Area of triangle = {base * height}")
10. Average of Two Numbers
num1 = int(input("enter the first number = ")) num2 = int(input("enter the second number = ")) print(f"Average = {(num1 + num2) / 2}")
11. Sum of Two Inputs
num1 = int(input("enter the first number = ")) num2 = int(input("enter the second number = ")) print(f"sum of num and num2 = {num1 + num2}")
12. Rectangle Area from Input
length = int(input("Enter the length ")) breadth = int(input("Enter the breadth ")) print(f"area of recatngle is {length * breadth}")