less = []
greater = []
for x in [1,2,3,4,8,6]:
if x < 3:
less.append(x)
else:
greater.append(x)
print("less = ", less, "greater = ", greater)
a=[1,2,3]
a
b=a
b
a.append(4)
# .append adds to a set
b
x=-5
if x<0:
print("It's negative")
x
0
print(x)
if int(x)<0:
print("It's negative")
elif int(x)==0:
print("Equal to zero")
elif 0 < int(x) < 5:
print("Positive but smaller than 5")
else:
print("positive and larger or equal to 5")
sequence = [1, 2, 0, 4, 6, 5, 2, 1]
for value in sequence:
total += value #total = total+value
print( total)
sequence = [1,2, None, 4, None, 5]
total = 0
for value in sequence:
if value is None:
continue
total += value
print(total)
sequence = [1,2,0,4,6,5,2,1]
total_until_5 = 0
for value in sequence:
if value == 5:
break
total_until_5 +=value
print(total_until_5)
for i in range(4):
for j in range(4):
if j > i:
break
print((i,j))
x = 256
total=0
while x>0:
print(x)
if total>500:
break
total +=x
print(total)
x //=2 # // integer division ignore fraction, / will pop out a float value
x = 0
if x < 0:
print("negative!")
elif x == 0:
print("Zero")
pass
else:
print("positive!")
seq = [10, 20, 30, 40]
for i in range(len(seq)):
print("element %d: %d" %(i, seq[i]))
# enumerate function
l1 = ["eat", "sleep", "repeat"]
# creating enumerate objects
obj1 = enumerate(l1)
print ("Return type:", type(obj1))
print (list(enumerate(l1)))
# enumerate function
l1 = ["eat", "sleep", "repeat"]
# creating enumerate objects
obj1 = enumerate(l1)
print ("Return type:", type(obj1))
print (list(enumerate(l1)))
a= [7,1,2,6,0,3,2]
sorted(a)
a
print(sorted("COP2000 Python Programming"))
seq1 = ["Un", "Dos", "Tres"]
seq2 = ["one", "two", "three"]
zipped = zip(seq1, seq2)
list(zipped)
seq3 = [False, True]
list(zip(seq1, seq2, seq3))
for index, (a, b) in enumerate(zip(seq1, seq2)):
## once enumeratedtogether (added together) print
## in this order of a,b
print(f"{index}: {a}, {b}") # https://realpython.com/python-f-strings/
list(reversed(range(10)))
def apply_to_list(some_list, f):
return [f(x) for x in some_list]
ints = [2,4,5,8,1,3,5]
apply_to_list(ints, lambda x: x**2)
## ** is squaring
! wget "https://www.gutenberg.org/files/76/76-0.txt" -O adventures.txt
! ( head -10 adventures.txt) > adventures10.txt
f= open("adventures10.txt", encoding="utf-8")
for line in f:
print(line)