less = []
greater = []
for x in [1,3,4,2,8,6]:
if x<3:
less.append(x)
else:
greater.append(x)
print("less =", less, "greater =", greater)
less = [1, 2] greater = [3, 4, 8, 6]
a = [1,2,3]
a
b = a
b
a.append(4)
b
x = -5
if x < 0:
print("It's negative")
It's negative
x
y
print(y)
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 than or equal to 5")
5
0
Equal to zero
sequence = [1,2,0,4,6,5,2,1]
total = 0
for value in sequence:
total += value # total = total + value
print("total =", total)
total = 1
total = 3
total = 3
total = 7
total = 13
total = 18
total = 20
total = 21
sequence = [1,2,None,4,None,5]
total = 0
for value in sequence:
if value is None:
continue
total += value
print(total)
12
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)
13
for i in range(4):
for j in range(4):
if j > i:
break
print ((i,j))
(0, 0)
(1, 0)
(1, 1)
(2, 0)
(2, 1)
(2, 2)
(3, 0)
(3, 1)
(3, 2)
(3, 3)
x = 256
total = 0
while x > 0:
print(x)
if total > 500:
break
total += x
print(total)
x //=2
256
256
128
384
64
448
32
480
16
496
8
504
4
x = 0
if x < 0:
print("negative!")
elif x == 0:
print("Zero")
pass
else:
print("positive!")
Zero
seq = [10,20,30,40]
for i in range(len(seq)):
print("element %d: %d" %(i, seq[i]))
element 0: 10
element 1: 20
element 2: 30
element 3: 40
# enumerate function
l1 = ["eat","sleep","repeat"]
# creating enumerate objects
obj1 = enumerate(l1)
print("Return type:", type(obj1))
print(list(enumerate(l1)))
Return type: <class 'enumerate'>
[(0, 'eat'), (1, 'sleep'), (2, 'repeat')]
a = [7,1,2,6,0,3,2]
sorted(a)
a
print(sorted("COP2000 Python Programming"))
[' ', ' ', '0', '0', '0', '2', 'C', 'O', 'P', 'P', 'P', 'a', 'g', 'g', 'h', 'i', 'm', 'm', 'n', 'n', 'o', 'o', 'r', 'r', 't', 'y']
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)):
print(f"{index}: {a}, {b}")
0: Un, one
1: Dos, two
2: Tres, three
list(reversed(range(10)))
def apply_to_list(some_list, f):
return [f(x) for x in some_list]
ints = [2,4,6,8,1,3,5]
apply_to_list(ints, lambda x: x**2)
! wget "https://www.gutenberg.org/files/76/76-0.txt" -O adventures.txt
! ( head -10 adventures.txt) > adventures10.txt
--2022-08-31 14:30:33-- https://www.gutenberg.org/files/76/76-0.txt
Resolving www.gutenberg.org (www.gutenberg.org)... 152.19.134.47, 2610:28:3090:3000:0:bad:cafe:47
Connecting to www.gutenberg.org (www.gutenberg.org)|152.19.134.47|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 610370 (596K) [text/plain]
Saving to: ‘adventures.txt’
adventures.txt 100%[===================>] 596.06K --.-KB/s in 0.07s
2022-08-31 14:30:33 (8.19 MB/s) - ‘adventures.txt’ saved [610370/610370]
f = open("adventures10.txt", encoding="utf-8")
for line in f:
print(line)
The Project Gutenberg eBook of Adventures of Huckleberry Finn, by Mark Twain (Samuel Clemens)
This eBook is for the use of anyone anywhere in the United States and
most other parts of the world at no cost and with almost no restrictions
whatsoever. You may copy it, give it away or re-use it under the terms
of the Project Gutenberg License included with this eBook or online at
www.gutenberg.org. If you are not located in the United States, you
will have to check the laws of the country where you are located before
using this eBook.