Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit93a00c2

Browse files
varian97Akuli
authored andcommitted
Add example with vs without list comprehension
1 parent52073d8 commit93a00c2

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

‎basics/lists-and-tuples.md‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ We'll talk more about loops [in the next chapter](loops.md).
142142
```
143143

144144
Another useful things about list is**comprehension**.
145-
**Comprehension** is a way toloop the list in single line. It makes our code morepythonic.
145+
**Comprehension** is a way toconstruct a list in single line. It makes our code moreclean, shorter and easier to read.
146146

147147
```python
148148
>>> numbers= [1,2,3,4,5]
@@ -152,6 +152,18 @@ Another useful things about list is **comprehension**.
152152
>>>
153153
```
154154

155+
without comprehension:
156+
157+
```python
158+
>>> numbers= [1,2,3,4,5]
159+
>>> numbers_squared= []
160+
>>>for numberin numbers:
161+
... numbers_squared.append(number**2)
162+
>>> numbers_squared
163+
[1,4,9,16,25]
164+
>>>
165+
```
166+
155167
We can also use slicing and indexing to change the content:
156168

157169
```python

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp