koheikawata's Blog
Wednesday, March 4, 2020
Python 101
List comprehensions in for loop
Example 1
``` list = [1, 2, 3, 4, 5, 6, 7, 8] [print(i) for i in list] ```
output
``` 1 2 3 4 5 6 7 8 ```
Example 2
``` list = [1, 4, 9, 16, 25, 36, 49] for i in range(len(list) - 1): dif = [list[i] - list[i+1] for i in range(len(list) - 1)] print(dif) ```
output
``` [-3, -5, -7, -9, -11, -13] ```
Conditional statement without if
Example 1
``` a = 5 c = 0 < a and a < 10 print(c) ```
output
``` True ```
Example 2
``` a = 11 c = 0 < a and a < 10 print(c) ```
output
``` False ```
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment