x = 10
x
y= 20
y
x
for i in range(10):
print(i)
x = 100
print(x)
x = [1,2,3]
x.append(4)
x = x* 2
print(x)
prices = {
'pizza' : 45,
'hamburger' : 59,
'salad' : 33,
'dessert' : 25,
'soup' : 30,
'pie' : 32
}
order = ['pizza', 'pizza', 'salad', 'dessert']
sum = 0
for item in order:
sum+=prices[item]
print(sum)
sum( [prices[item] for item in order] )
i=0
while i<8:
print(2**i)
i+=1
for i in range(8):
print(2**i)
newlist = []
for i in range(8):
newitem = 2**i
newlist.append(newitem)
newprices = { item : prices[item] * 2 for item in prices}
print(newprices)
prices.items()
item = ('pizza', 45)
name = item[0]
price = item[1]
name, price = ('pizza', 45)
print(name, price)
name2, price2 = item
print(name2, price2)
for name, price in prices.items():
print(name, '-->>', price)
newprices = { name : price * 2 for name, price in prices.items()}
print(newprices)
order
order[2:]
order[:3]
order[::-1]
x = 10
y = x
x += 1
print(y)
x = [1,2,3]
y = x
x += [4]
print(y)
{
1 : []
}
{
[] : 1
}
2/3
2//3
float(2)
list(range(10))
int("42")
str(42)
[]
[]
()
(2)
(1,2,3)
(1,2)
(1,)
1,
x = 10
y = 'blah'
z = x
x = y
y = x
x, y = y, x
print(x,y)
order
order[0]
order[1]
order[-1]
order[-2]
order[-2 + len(order)]
len(order)
order[-10]
order[-3:]
order
'hello ' * 3
[1] * 3
m = [ [ 0, 0, 0 ] ]* 3
m
m[0][0] = 99
print(m)
x = [1,2,3, 2, 1]
sorted(x)
x
x.sort()
print(x)
fruits = ['watermelon', 'apple', 'grapes', 'melon', 'peach', 'citrus']
sorted(fruits)
len
sorted(fruits, key=len)
prices = {
'apple' : 20,
'melon' : 7,
'peach' : 12,
'grapes' : 25,
'watermelon' : 5,
'citrus' : 5
}
def getprice(fruit):
return prices[fruit]
sorted(fruits, key=getprice)
[1] < [2, 2]
[2] < [1, 1]
[2] < [10,1]
[10, 2, 1000] < [10,100, 0]
int
x = int
int = float
int(10)
type(10)
int = type(10)
a = len
a
len = 10
len("hello")
len = print
len("hello")
import math
math
x = math
x
del x
len = a
len('hello')
a = {"hello"}
a
a += {'hello', 'world' }
a = set()
x = """hello"""
a.update({x})
print(a)
a
x = 10
x < 20
x == [1,2,3]
bool(10)
bool(0)
bool([0])
bool([])
result = []
if result :
print("got results")
else:
print('failed | empty result | eof | eos')
bool(result)
score = 87
if score > 0 and score < 100:
print('yes')
if 0<score<100<score**2< 10000:
print('yes')
x = "hello"
y = "HELLO".lower()
z = "hel" + "lo"
x == y == z == 'moshe'
2 in [1,2,3]
0 in [1,2,3]
fruits
"melon" in fruits
"car" in fruits
"car" not in fruits
max
min
sum
all
any
all([1, [2], "3"])
all([1, [2], "3", 0])
all([1, [2], "3", ""])
fruits
prices
[ {
'name' : fruit,
'price' : prices[fruit],
'+vat' : prices[fruit]*1.17
}
for fruit in fruits]
[ [fruit, prices[fruit]] for fruit in fruits if prices[fruit] <= 10]
def make_price_pair(fruit):
return [fruit, prices[fruit]]
make_price_pair('melon')
list(map(make_price_pair, fruits))
[ [fruit, prices[fruit]] for fruit in fruits ]
def keep_cheap_fruits(fruit):
return prices[fruit] <= 10
list(filter(keep_cheap_fruits, fruits))
[ [fruit, prices[fruit]] for fruit in fruits if prices[fruit] <= 10 and len(fruit)<= 5]
matrix = []
for i in range(1, 6):
row = []
matrix.append(row)
for j in range(1, 11):
row.append(i*j)
from pprint import pprint
pprint(matrix)
[ [i*j for i in range(1, 6)] for j in range(1, 11) ]
matrix
[ matrix[i][j] for i in range(5) for j in range(10)]
list(map(len, fruits))
list(reversed(fruits))
enumerate(fruits)
range(10**12)
fruit_iterator = reversed(fruits)
next(fruit_iterator)
fruits
for fruit in reversed(fruits):
print(fruit)
dir(fruit_iterator)
a = range(10)
b = a.__iter__()
next(b)
list(reversed(fruits))
song = """\
Lovely Spam! Wonderful Spam!
Lovely Spam! Wonderful Spam
Spa-a-a-a-a-a-a-am
Spa-a-a-a-a-a-a-am
Spa-a-a-a-a-a-a-am
Spa-a-a-a-a-a-a-am
Lovely Spam! (Lovely Spam!)
Lovely Spam! (Lovely Spam!)
Lovely Spam!
Spam, Spam, Spam, Spam!
"""
with open('spam.txt', 'w') as f:
print(song, file=f)
import os
os.system('spam.txt')
fin = open('spam.txt')
next(fin)
next(fin)
for line in fin:
print(line)
reduce
for line in open('spam.txt'):
print(line)