🏠 Home 💡 Python Tips 💼 Career 💰 Paise Kamao ⭐ Reviews 🐍 Free Course
HomePython Tips › 10 Python Tips jo Har Beginner ko Pata H...

💡 10 Python Tips jo Har Beginner ko Pata Honi Chahiye

Advertisement

Ye Tips Kyon Zaroori Hain?

Python seekhna aasaan hai, lekin efficiently Python likhna seekhna alag baat hai. Bahut saare beginners chhoti chhoti cheezein miss kar dete hain jo unki coding 10x fast kar sakti thi. Yeh 10 tips aapko ek better Python programmer banayengi.

💡 Pro Tip: Yeh tips sikhne ke baad apne purane code par try karo — aap khud fark dekh payenge!

1. List Comprehension — Loop ko 1 Line mein Likho

Sabse bada time-saver hai list comprehension. Ek loop jo 5 lines mein likhte the, ab 1 line mein!

python
# Old way — 4 lines
squares = []
for i in range(10):
    squares.append(i**2)

# New way — 1 line!
squares = [i**2 for i in range(10)]

# With condition
evens = [x for x in range(20) if x % 2 == 0]

2. f-Strings — Variables Ko String Mein Daalo

Python 3.6+ mein f-string sabse clean aur fast tarika hai string formatting ka. format() ko bhool jao!

python
name = "Rahul"
age = 20
salary = 50000

# Clean f-string
print(f"Naam: {name}, Umar: {age}")
print(f"Salary: Rs{salary:,}")        # Rs50,000
print(f"Pi = {3.14159:.2f}")          # Pi = 3.14
print(f"Upper: {name.upper()}")       # RAHUL

3. Enumerate — Index aur Value Dono Ek Saath

Jab bhi loop mein index bhi chahiye, enumerate() use karo. range(len(list)) likhna band karo!

python
fruits = ["Apple", "Banana", "Mango"]

# Old way — complicated
for i in range(len(fruits)):
    print(i, fruits[i])

# New way — clean!
for i, fruit in enumerate(fruits, 1):
    print(f"{i}. {fruit}")

4. Zip — Do Lists Ek Saath Loop Karo

python
names  = ["Rahul", "Priya", "Amit"]
scores = [85, 92, 78]

for name, score in zip(names, scores):
    print(f"{name}: {score}")

5. Dictionary get() — KeyError Se Bachao

Direct dict[key] se KeyError aa sakta hai. get() safe hai!

python
user = {"name": "Rahul", "age": 20}

# Risky — KeyError agar key nahi mili
# print(user["email"])

# Safe — default value milega
email = user.get("email", "N/A")
print(email)  # N/A

6. Walrus Operator := (Python 3.8+)

python
# Assign aur use ek hi step mein
while chunk := file.read(8192):
    process(chunk)

# List mein bhi
results = [y for x in data if (y := process(x)) is not None]

7. *args aur **kwargs — Flexible Functions

python
def greet(*names):
    for name in names:
        print(f"Hello, {name}!")

greet("Rahul", "Priya", "Amit")

def show_info(**details):
    for key, val in details.items():
        print(f"{key}: {val}")

show_info(name="Rahul", age=20, city="Delhi")

8. with Statement — Files Auto Close

python
# with se auto close — koi leak nahi
with open("file.txt", "r") as f:
    data = f.read()
# File yahan automatic close ho gayi!

9. sorted() + key= — Custom Sort

python
students = [("Rahul", 85), ("Priya", 92), ("Amit", 78)]

# Marks se sort — descending
by_marks = sorted(students, key=lambda x: x[1], reverse=True)
print(by_marks)  # Priya first!

10. Collections Module — Power Tools

python
from collections import Counter, defaultdict

# Word frequency — bahut aasaan!
words = "python python java python java c".split()
freq = Counter(words)
print(freq.most_common(2))  # Top 2 words

# defaultdict — KeyError nahi aayega
dd = defaultdict(int)
dd["count"] += 1  # No error!
🎯 Summary: Ye 10 tips regularly use karo — ek ek karke practice karo. Pehle week mein f-string aur list comprehension master karo, phir baaki!
Advertisement