less = []
greater = []
for x in [1, 3, 4, 2, 8, 6]:
if x<3:
less.append(x)
else: # conditional statement
greater.append(x)
print("less=", less, "greater=", greater)
a=[1,2,3,]
a
b=a
b
a.append(4)
b
x = -5
if x < 0:
print("It's negative")
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")
x = 0
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")
x
y
print(x)
print(y)
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")
sequence = [1, 2, 0, 3, 6, 9, 2, 1]
total = 0
for value in sequence:
total += value # total = total + value
print("total =", total)
sequence = [1, 2, 0, 3, 6, 9, 2, 1]
total = 0
for value in sequence:
total += value # total = total + value
print("total =", total)
sequence = [1, 4, None, 4, None, 5]
total = 0
for value in sequence:
if value is None:
continue
total += value
print(total) # Continue adding values while ignoring specified input "None" in "continue"
sequence = [1, 6, 0, 3, 5, 2, 1]
total_until_5 = 0
for value in sequence:
if value == 5:
break
total_until_5 += value
print(total_until_5) # Stop adding values once specified input "5" encountered in "break"
for i in range(4): # indicates numerical values 0-4
for j in range(4):
if j > i:
break # if the y coord. is greater than x, break
print ((i, j)) # coordinate values identified here
x = 256 # a variable x = 256
total = 0
while x > 0: #when x is greater than 0, print x
print(x)
if total > 500:
break # when x is greater than 500, break
total += x
print(total)
x//= 2
x = 0
if x < 0:
print("negative!")
elif x == 0:
print("Zero")
pass
else:
print("positive")
x = 5
if x < 0:
print("negative!")
elif x == 0:
print("Zero")
pass
else:
print("positive")
seq = [10, 20, 30, 40] # entered a sequence of four numerical values
for i in range(len(seq)):
print("element %d: %d" %(i, seq[i])) # print integers (%d) with element name (staring at 0)
# enumerate function
l1 = ["eat", "sleep", "repeart"]
# creating enumerating objects
obj1 = enumerate(l1)
print("Return type:", type(obj1))
print(list(enumerate(l1)))
a = [8, 1, 2, 4, 0, 6, 2]
sorted(a)
a
print(sorted("COP2000 Python Programming")) # takes each individual character of the string and sorts it via the ASCII map
seq1 = ["Uno", "Dos", "Tres"] # ex: translation
seq2 = ["one", "two", "three"]
zipped = zip(seq1, seq2)
list(zipped)
seq3 = [False, True] # zip three sequences; zips first two elements regardless of length
list(zip(seq1, seq2, seq3)) # can zip any number of sequences
for index, (a, b) in enumerate(zip(seq1, seq2)): # takes the pair of a, b and correlates to the two sequences to join them together
print (f"{index}: {a}, {b}",)
list(reversed(range(10))) # remember that the list is from 0-9, not 0-10
def apply_to_list(some_list, f):
return [f(x) for x in some_list] # do not require this part; complex and stores information
ints = [2, 4, 6, 7, 3, 1, 9]
apply_to_list(ints, lambda x: x ** 2) # squaring is not a karat
ints = [2, 4, 6, 7, 3, 1, 9]
apply_to_list(ints, lambda x: x ** 2) #lambda makes transferring information easier
! wget "https://www.gutenberg.org/files/76/76-0.txt" -O adventures.txt
! ( head -10 adventures.txt) > adventures10.txt # large file; just taking first ten lines
f = open("adventures10.txt", encoding="utf-8") # f = hook of website
for line in f:
print(line)