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
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
<class 'str'> <class 'int'> <class 'float'> <class 'bool'>
Text ke andar variables daalne ke liye f-string sabse aasaan tareeka hai:
Example
print(f"{name} ki age {age} saal hai aur height {height} feet.")
Output
Riya ki age 24 saal hai aur height 5.4 feet.
Numbers ke saath math
Example
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
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
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
[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
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
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
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
28.61
['Delhi', 'Mumbai', 'Pune']
Unique cities: 3
if / elif / else — decisions
Example
marks = 67
if marks >= 80:
grade = "A"
elif marks >= 60:
grade = "B"
else:
grade = "C"
print("Grade:", grade)
Output
Grade: B
Python data types ek nazar me
Type | Example | Kab use karein |
|---|---|---|
|
| Counts, IDs, poore numbers |
|
| Price, marks, measurements |
|
| Naam, city, category |
|
| Yes/No flags |
|
| Ordered values, badal sakte hain |
|
| Naam wali values, ek record |
|
| Fixed values |
|
| Unique values |
Practice
- Apne 5 dosto ki ages ki list banaiye aur average age nikaaliye.
- Ek dictionary banaiye jisme aapki favourite movie ka naam, saal aur rating ho. Rating update kariye.
- List
[3, 7, 3, 9, 7, 1]me kitni unique values hain?