From b7bab5e9fec8cff6ae8323481e68830766e61c84 Mon Sep 17 00:00:00 2001 From: "Dawid J. Kubis" Date: Wed, 27 Nov 2019 17:22:19 +0100 Subject: [PATCH] comprehensions --- 8/list_comprehensions.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/8/list_comprehensions.md b/8/list_comprehensions.md index e69de29..50ca98d 100644 --- a/8/list_comprehensions.md +++ b/8/list_comprehensions.md @@ -0,0 +1,21 @@ +# List Comprehensions +list comprehensions jsou super. +Dovolujou zkratit kod uplne strasne moc. + +Je to prakticky `for` kterej dovoluje filtrovat. + +Tohle: +```python +# chceme sebrat vsechna sudo cisla do 1000 +n = [] +for i in range(1000): + if i % 2 == 0: + n.append(i) +print(n) +``` +Je ekvivalence: +```python +n = [i for i in range(1000) if i % 2 == 0] +print(n) +``` +coz muzeme cist jako: "n je seznam vsech cisel do 1000 kde zbytek z deleni dvemi je nula, tl;dr vsech sudych cisel"