Short version · Lessons 1-3

Recap of lessons 1-3

Key topics of the three lessons in concentrated form. Then - practice.

What we'll refresh

Six topics · one page

types · if/elif
for · while · range
list · slices
tuple · set · dict
def · *args · **kwargs
lambda · sorted

No new topics - just what you already know, all on one screen. After that - 25 tasks in Jupyter.

Lesson 1

Variables, types, input, conditions

# types are inferred automatically name = "Anna" # str age = 30 # int price = 19.99 # float ok = True # bool # input and conversion n = int(input("Number: ")) # input is always str! # conditions if n > 0: print("plus") elif n < 0: print("minus") else: print("zero")
Key points
  • type(x) - check the type of a variable
  • int(), float(), str() - type conversion
  • colon and indent are required
  • comparisons: == != < > <= >=
  • logical: and / or / not

Lesson 2

Loops and lists

# for + range for i in range(1, 6): # 1..5 (no 6) print(i) # while + break/continue n = 0 while n < 10: n += 1 if n % 2: continue # skip odd if n > 6: break # lists and slices a = [10, 20, 30, 40, 50] a[0]; a[-1] # 10, 50 a[1:4]; a[::-1] # slice, reverse a.append(60); a.pop(); a.sort()
Remember
  • range(start, stop, step) - upper bound is excluded
  • break - exit the loop, continue - skip iteration
  • indices from zero, -1 - last element
  • [start:stop:step] - list slice
  • list is mutable, tuple is not

Lesson 3 · part 1

Collections - cheat sheet

Type Literal Mutable When to use
list[1, 2, 3]yesgrowing sequence
tuple(1, 2, 3)nofixed bundle (x, y)
set{1, 2, 3}yesunique · fast in O(1)
dict{"k": 1}yeslookup by name O(1)
u = {"name": "Anna", "age": 30} u["name"]; u.get("city", "-") # access + default for k, v in u.items(): print(k, v) a, b = {1,2,3}, {2,3,4} a | b; a & b; a - b # union, intersect., diff.
Picking a collection
  • «lookup by name» - dict
  • «unique values» - set
  • «fixed set» - tuple
  • «mutable, ordered» - list

Lesson 3 · part 2

Functions: def, *args, lambda

# regular function with default def price_tax(p, tax=0.12): return p * (1 + tax) # *args - tuple, **kwargs - dict def log(level, *parts, **meta): print(level, parts, meta) log("INFO", "user", "in", id=42) # lambda + sorted people = [("Anna", 30), ("Boris", 25)] sorted(people, key=lambda p: p[1])
Remember
  • return exits the function
  • defaults only for immutable types (NOT [], NOT {})
  • *args - tuple of positional, **kwargs - dict of keyword
  • lambda x: expr - one line, one expression
  • scope - LEGB rule

All together

Final example

One function, all three lessons: types, loop, dict, list, *args, default, lambda.

def analyze(*scores, passing=60): passed = [s for s in scores if s >= passing] return { "total": len(scores), "passed": len(passed), "avg": sum(passed) / len(passed) if passed else 0, "top": max(scores, default=0), } print(analyze(85, 42, 73, 91, 55))
{'total': 5, 'passed': 3, 'avg': 83.0, 'top': 91}

If this code makes sense - you're good. If not - on to practice.

Practice

15 tasks in Jupyter

.ipynb
lecture04_tasks.ipynb
5 sections · 15 tasks
Скачать RU Yuklab olish UZ Download EN

After practice

You will be able to

  • Confidently work with types
  • Write if/elif/else without a cheat sheet
  • Understand slices and range
  • Choose between list/tuple/set/dict
  • Write functions with *args/**kwargs
  • Apply lambda and sorted/filter/map
Next step

Lesson 4 - OOP: classes, methods, inheritance. Combining data and behavior.

Questions?

Telegram: @gokalqurt

Python · Recap · short
RU UZ EN
1 / 10