Python Basics: Variables, Data Types, List aur Dictionary

Lesson 4 of 26

Data Science me Python sabse zyada use hoti hai. Achhi baat ye hai ki hume poori Python nahi, uska ek chhota hissa hi chahiye. Is lesson me wo basics hain jo aapko har din kaam aayenge.

Variables aur data types

Variable ek naam hai jisme hum value store karte hain. Python khud samajh leti hai ki value kis type ki hai:

Example

Python
name = "Riya"          # str  - text
age = 24               # int  - poora number
height = 5.4           # float - decimal number
is_student = True      # bool - True / False

print(type(name), type(age), type(height), type(is_student))

Output

Plain Text
<class 'str'> <class 'int'> <class 'float'> <class 'bool'>

Text ke andar variables daalne ke liye f-string sabse aasaan tareeka hai:

Example

Python
print(f"{name} ki age {age} saal hai aur height {height} feet.")

Output

Plain Text
Riya ki age 24 saal hai aur height 5.4 feet.

Numbers ke saath math

Example

Python
print(17 / 5)    # normal division
print(17 // 5)   # floor division - sirf poora hissa
print(17 % 5)    # remainder (modulus)
print(2 ** 10)   # power
print(round(3.14159, 2))

Output

Plain Text
3.4
3
2
1024
3.14

List — values ki ordered collection

List Data Science me bahut use hoti hai. Isme kai values ek saath rakhte hain, aur index 0 se shuru hota hai:

Example

Python
sales = [1200, 850, 430, 2100, 990]
sales.append(1500)          # end me nayi value

print(sales)
print("Pehla:", sales[0], "| Aakhri:", sales[-1])
print("Pehle 3:", sales[:3])
print("Total:", sum(sales), "| Count:", len(sales))
print("Sorted:", sorted(sales))

Output

Plain Text
[1200, 850, 430, 2100, 990, 1500]
Pehla: 1200 | Aakhri: 1500
Pehle 3: [1200, 850, 430]
Total: 7070 | Count: 6
Sorted: [430, 850, 990, 1200, 1500, 2100]

sales[:3] ko slicing kehte hain — index 0 se 3 tak (3 shamil nahi). Ye concept aage NumPy aur Pandas me bhi bilkul aise hi chalega.

Dictionary — key : value pairs

Dictionary me har value ka ek naam (key) hota hai. Ek record ya row ko represent karne ke liye ye perfect hai:

Example

Python
student = {"name": "Aman", "city": "Delhi", "marks": 88}

print(student["name"])
student["marks"] = 91              # value update
student["course"] = "Data Science" # nayi key
print(student)
print(student.get("phone", "Not available"))  # key na ho to default
print(list(student.keys()))

Output

Plain Text
Aman
{'name': 'Aman', 'city': 'Delhi', 'marks': 91, 'course': 'Data Science'}
Not available
['name', 'city', 'marks', 'course']

Aage chal ke dekhenge ki Pandas ka DataFrame banane ka sabse common tareeka dictionary hi hai — har key ek column banti hai.

Tuple aur Set

Tuple list jaisa hai par badla nahi ja sakta. Set me sirf unique values rehti hain — duplicates hataane ke liye useful:

Example

Python
location = (28.61, 77.20)   # tuple: latitude, longitude
print(location[0])

cities = ["Delhi", "Mumbai", "Delhi", "Pune", "Mumbai"]
unique_cities = set(cities)
print(sorted(unique_cities))
print("Unique cities:", len(unique_cities))

Output

Plain Text
28.61
['Delhi', 'Mumbai', 'Pune']
Unique cities: 3

if / elif / else — decisions

Example

Python
marks = 67

if marks >= 80:
    grade = "A"
elif marks >= 60:
    grade = "B"
else:
    grade = "C"

print("Grade:", grade)

Output

Plain Text
Grade: B

Python data types ek nazar me

Type

Example

Kab use karein

int

42

Counts, IDs, poore numbers

float

3.14

Price, marks, measurements

str

"Delhi"

Naam, city, category

bool

True

Yes/No flags

list

[1, 2, 3]

Ordered values, badal sakte hain

dict

{"a": 1}

Naam wali values, ek record

tuple

(1, 2)

Fixed values

set

{1, 2}

Unique values

Practice

  1. Apne 5 dosto ki ages ki list banaiye aur average age nikaaliye.
  2. Ek dictionary banaiye jisme aapki favourite movie ka naam, saal aur rating ho. Rating update kariye.
  3. List [3, 7, 3, 9, 7, 1] me kitni unique values hain?