Lessons Basic Commands
Lessons Basic Commands
ascii() Returns a readable version of an object. Replaces none-ascii characters with escape
character
map() Returns the specified iterator with the specified function applied to each item
range() Returns a sequence of numbers, starting from 0 and increments by 1 (by default)
statements
def cubeVolume(sideLength):
return 0
return sideLength**3
Event-Controlled Loops
Collection-Controlled Loops (while / for)
Collections can be a string – a set of characters. for-loops for strings will loop through each character
in the string
Break
i = 1
while i <= 4:
print(i)
if i == 3:
break
i += 1
print("outside")
print(i)
Continue
list1 = [12, 15, 32, 42, 55, 75, 122, 150, 180, 87, 102, 100]
for item in list1:
if item > 120:
continue
elif item%3 == 0:
print(item)
without continue
list1 = [12, 15, 32, 42, 55, 75, 122, 150, 180, 67, 102, 100]
for item in list1:
if item <= 120 and item%3 == 0:
print(item)
While True or Not False condition loop exit
while True:
num = int(input("Please enter a number: "))
if num%2==0 and 21<num<29:
break
print("The number is", num)
success = False
while not success:
num = int(input("Please enter a number: "))
if num%2==0 and 21<num<29:
success = True
print("The number is", num)
Nested loops
V height = 4
width = 7
for i in range(height): #how many rows
for j in range(1, width+1): #the numbers in each row
print(j, end=" ")
print()
#Alternative code
height = 4
width = 7
for i in range(height):
for j in range(1, width+1):
print(j, end=" ")
if j == width:
print()
F Strings
num = 1
height = 68
width = 15
field = len(str(height*width))
for i in range(height):
for j in range(width):
print(f'{num:0{field}}', end=" ")
# at least 2 width, align to right by default
num += 1
print()
Length of a List
print(i, x)
n = friends.index("Emily")
longest = []
if len(word) == maxLen:
longest.append(word)
print(longest)
digitCount = 0
digit = 1
for d in str(num):
if d == str(digit):
digitCount += 1
print (digitCount)
wordCount = 0
word = "Ares"
wordCount += 1
print (wordCount)
guests.append('Charlie')
guests.append(friends)
• A better way of concatenating two lists together is to use the extend() method.
guests.extend(friends)
guests + friends
guests.insert(1,'Charlie')
# This will return a list with the new value Charlie added in position 1.
guests.insert(i,'Zoe')
guests.remove('Tom')
guests.pop()
The pop(i) method will remove the value at Position i in the list.
guests.pop(1)
The sort() method will sort a list. By default the sort will the ascending.
guests.sort()
Will return the list in the order A-Z: ['Austin', 'Emily', 'Harry', 'Ollie']
guests.sort(reverse=True)
Will return the list in the order Z-A: ['Ollie', 'Harry', 'Emily', 'Austin']
for i in range(len(stars)):
print(stars[i])
or
print(element)
print("Hello,", guest+"!")
Escape Characters – The Magical Backslash \ is used to insert characters that are illegal in a string
Escape Characters – \' and \" are used to insert illegal quotes in strings.
A string method called join() can be used to turn the list into a string and joins the values together
with whatever you want.
The string method split() can be used to split a string at the separator and stores each value in a
string.
split() and join() functions are like inverse functions, converting between strings and lists.
split(“, “) Function – Any separators can be added between (“ “), otherwise default is space
split("#", 2) – Number of Splits. The second parameter determines how many splits to do.
"" < " " < "0" < "1" < "9" < "A" < "B" < "Z" < "a" < "b" < "z"
and operator yields True if both Boolean values entered are True. ¼
not changes the value of a Boolean operator from True to False and False to True
any(list) returns:
all(list) returns