TGViewer
پایتون | Data Science | Machine Learning پایتون | Data Science | Machine Learning @python4all_pro · 24.3K subscribers
Post #1931 2.51K
image_2025-08-17_09-32-25.png983.2 KB
👈چت شیت برای مصاحبه پایتون

1. Swap variables without a temporary one

a, b = 5, 10
a, b = b, a


2. One-line if-else (ternary)

result = "Even" if x % 2 == 0 else "Odd"


3. List Comprehension

squares = [x**2 for x in range(10)]
evens = [x for x in range(10) if x % 2 == 0]


4. Set and Dict Comprehension

unique = {x for x in [1,2,2,3]}        # remove duplicates
squares = {x: x**2 for x in range(5)}  # dict comprehension


5. Most common element in a list

from collections import Counter
most_common = Counter(['a','b','a','c']).most_common(1)[0][0]


6. Merging dictionaries (Python 3.9+)

a = {'x': 1}
b = {'y': 2}
merged = a | b


7. Returning multiple values

def stats(x):
    return max(x), min(x), sum(x)

high, low, total = stats([1, 2, 3])


8. Using zip to iterate over two lists

names = ['a', 'b']
scores = [90, 85]

for n, s in zip(names, scores):
    print(f"{n}: {s}")


9. Flattening nested lists

nested = [[1,2], [3,4]]
flat = [item for sublist in nested for item in sublist]


10. Default values in a dictionary

from collections import defaultdict
d = defaultdict(int)
d['apple'] += 1   # no KeyError


11. Lambda in one line

square = lambda x: x**2
print(square(4))


12. enumerate with index

for i, v in enumerate(['a', 'b', 'c']):
    print(i, v)


13. Sorting by key or value

d = {'a': 3, 'b': 1, 'c': 2}
sorted_by_val = sorted(d.items(), key=lambda x: x[1])


14. Reading file lines into a list

with open('file.txt') as f:
    lines = f.read().splitlines()


15. Type Hints

def add(x: int, y: int) -> int:
    return x + y


🔍برای دسترسی به چیت شیت‌های دیگه ی کانال با #cheat_sheet جستجو کنید

#پایتون #Python

📱 @Python4all_pro
  • ❤ 2
More from @python4all_pro
  1. Sep 29, 2026What better way to understand a powerful tool like Claude Code than to build your own vers…
  2. Sep 28, 2026🤖 نسل جدید فیلترشکن‌های EASY2RAY با استفاده از هوش مصنوعی! 📱 اتصال بدون دردسر و بدون نیا…
  3. Sep 28, 2026🧨 ۲۰۰ آموزش جدید دیگر، جایگزین آموزش‌های قبلی شد... 💯 ۴۰۰+۲۰۰ آموزش در فرادرس، هر آموزش…
  4. Sep 28, 202625 GitHub Repositories Every Python Developer Should Know! Want to improve your Python ski…
  5. Sep 27, 202640+ FREE Python Books from Amazon! Want to improve your Python skills without spending a f…
  6. Sep 26, 2026📖 Turn a PDF into an Animated Flipbook with Python! Want to make your PDFs more interacti…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →