Code Snippets

# swap (two variables)
x = 3
y = 5

temp = x
x = y
y = temp

print(x, y)
# get one from a list
x = [5, 3, 33, 567, 2]
print(x[1])
# min -- ex: min([3, 8, 2])
x = [3, 8, 2]
m = x[0]
idx = 0
for j in range(len(x)):
    if x[j] < m:
        m = x[j]
        idx = j
print(idx, m)
# reverse -- ex: x.reverse()
x = [4, 2, 98, 3]
y = []
while len(x) > 0:
    y.append(x.pop())
print(y)
# sort a list -- ex: x.sort()
x = [4, 2, 98, 3]
y = []
while len(x) > 0:
    m = min(x)
    y.append(m)
    x.pop(x.index(m))
print(y)
# sum
x = [4, 2, 98, 3]
sum = 0 
for t in x:
    sum = sum + t
print(sum)
# average
x = [4, 2, 98, 3]
sum = 0 
for t in x:
    sum = sum + t
print(sum / len(x))