- Notifications
You must be signed in to change notification settings - Fork 0
Chapter 4
Jarlin Almanzar edited this page Oct 26, 2019
·
3 revisions
magicians= ['alice', 'david', 'Carolina']
formagicianinmagicians:
print(magician)forvalueinrange(1, 5):
# inclusive will not include the last value in a rangeprint(value)#Starts at 2 and increments by 2 until it reaches or passes the final numbers that being 11forvalueinrange(2, 11, 2):
print(value)squares= []
forvalueinrange(1, 11):
square=value**2squares.append(square)
print(squares)digits= [1,2,3,4,5,6,7,8,9,10]
print(min(digits))
print(max(digits))
print(sum(digits))List comprehension combines the for loop and the creation of new elements into one line, and automatically appends each new element.
squares= [value**2forvalueinrange(1, 12)]
print(squares)# [0:3] would return the elements from the indices 0 to 2# [:3] Can also be omitted# [0:] Given start value to endplayers= ['adam', 'atom', 'atem', 'ahtam']
print(players[0:3])players= ['adam', 'atom', 'atem', 'ahtam']
forplayerinplayers[:-1]:
print(player)
print("")
forplayerinplayers[:3]:
print(player)
print("")
forplayerinplayers[::-1]:
print(player)my_foods= ['Pizza', 'falafel', 'carrot cake']
friends_food=my_foods[:] print(my_foods)
print(friends_food)A list of items that cannot be changed, as they are immutable, and an immutable list is called a tuple Defining a Tuple: It looks like a list instead we use parenthesis
dimension= (200,300)
print(dimension[0])
print(dimension[1])dimensions= (200,300)
fordimensionindimensions:
print(dimension)dimensions= (300,400)
fordimensionindimensions:
print(dimension)It's recommend to have four spaces or a tab per indentation level DO NOT MIX SPACES AND TABS
Each line should be less than 80 characters, some prefer 99 characters Comments should at most 72 character per line
To group parts of your program visually, use blank lines