← Back to Home

Python Variables

By Instructor

A variable is a name used to store data in memory. Variable ek container ya name hota hai jo values ko store karta hai.

This comprehensive guide covers Python variable rules, data types (string, int, float, bool), user inputs, string formatting with f-strings, variable swapping, and practice questions with solutions.

By Instructor7 chaptersPython 3.xBeginner to Advanced
variables.py
name = "Gagan"
age = 18

print(f"Mera naam {name} hai aur meri age {age} hai")
python variables.py → Mera naam Gagan hai aur meri age 18 hai
Chapter 1

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:

Syntax
variable_name = value

Basic Examples

examples.py
full_name = "gagan"
print(full_name)

laptop = "asus"
print(laptop)

gagan = "teacher"
print(gagan)
Key Takeaway

Variables serve as pointers to data stored in computer memory. Whenever you call a variable name, Python retrieves its assigned value.

Chapter 2

Naming Rules & Conditions

Python variables standard syntax rules follow karte hain. In rules ko disregard karne par syntax errors arise hote hain:

  1. Cannot start with a number: Variable names number se start nahi ho sakte (e.g. 1x invalid syntax par x1 valid hai).
  2. No special characters except underscore (_): Variable names me special characters (like !, @, $, -, or spaces) allowed nahi hote. Faqat underscore (_) allowed hai (e.g. first_name valid, last name invalid).
  3. Case-Sensitive: Python case-sensitive language hai. name aur Name do completely different variables treat hote hain.
Variable NameStatusReason / Description
first_nameValidUses underscore correctly
1xInvalidStarts with a digit
last nameInvalidContains space
gender_maleValidStandard snake_case format
Name vs nameValidCase-sensitive: both exist separately
Chapter 3

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: True or False.
Variable NameValueTypeDescription
meri_city"Chandigarh"stringEnclosed in quotes
gagan_ki_age18integerWhole number
mere_marks98.9floatDecimal point value
gender_maleTruebooleanHolds True or False
salary"2220"stringNumbers in quotes become strings
datatypes.py
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}")
Chapter 4

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.

user_input.py
Name = input("apna naam dalo = ")
age = int(input("apni age dalo = "))
print(f"Mera naam {Name} hai aur meri age {age} hai")
addition_input.py
a = int(input("enter the first number = "))
b = int(input("enter the second number = "))
print(f"the output of a and b = {a + b}")
Chapter 5

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}"

fstrings.py
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}")
Expression evaluation inside f-strings

F-strings accept valid Python math operations inside curly braces directly: f"Cost = {price * quantity}".

Chapter 6

Variable Swapping

Python single line tuple unpacking mechanism se without temporary variable value swap support karta hai.

swapping.py
a = 10
b = 20

a, b = b, a

print("a =", a)
print("b =", b)
Chapter 7

Practice Questions & Solutions

Comprehensive set of practice problems with solutions:

1. Print Name using F-String

sol_01.py
name = "Ali"
print(f"My name is {name}")

2. Name and City

sol_02.py
name = "Ali"
city = "Delhi"
print(f"My name is {name} and I live in {city}")

3. Sum / Subtraction of Two Numbers

sol_03.py
a = 10
b = 5
print(f"inka minu = {a - b}")

4. Area of Rectangle

sol_04.py
length = 10
width = 5
print(f"Area of rectangle is {length * width}")

5. Total Cost Calculation

sol_05.py
price = 50
quantity = 3
print(f"Total cost = {price * quantity}")

6. Age Example

sol_06.py
age = 21
print(f"I am {age} years old")

7. Mathematical Operations

sol_07.py
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

sol_08.py
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

sol_09.py
base = 10
height = 5
print(f"Area of triangle = {base * height}")

10. Average of Two Numbers

sol_10.py
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

sol_11.py
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

sol_12.py
length = int(input("Enter the length "))
breadth = int(input("Enter the breadth "))
print(f"area of recatngle is {length * breadth}")