28
loading...
This website collects cookies to deliver better user experience
squares = []
for x in range(10):
squares.append(x**2)
result
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
squares = [x**2 for x in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
first = ['a1', 'a2']; second = ['b1', 'b2']
result = []
for i in first:
for j in second :
pair = (i, j)
result.append(pair)
result = [(i,j) for i in first for j in second]
result = {}
for x in range(10):
result[x] = x**2
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
result = {x: x**2 for x in range(10)}
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
fruits = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
new = []
for fruit in fruits:
if fruit not in new:
new.append(fruit)
['banana', 'apple', 'pear', 'orange']
# List instead of set
fruits = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
# Actually make `new` a list
new = list(set(fruits))
['banana', 'apple', 'pear', 'orange']
x, y = (15, 5)
x
15
y
5
fruit, num, date = ['apple', 5,(2021, 11, 7)]
date
(2021, 11, 7)
x = [1, 2, 3, 4, 5]
result = x[:3]
result
[1, 2, 3]
sequence[start:stop:step]
x = [10, 5, 13, 4, 12, 43, 7, 8]
result = x[1:6:2]
result
[5, 4, 43]