Chapter4 Flashcards
example of using loop
magicians = [‘alice’, ‘david’, ‘carolina’]
for magician in magicians:
print(magician)
b. The second line tells Python to pull a name from the list magicians, and store it in the variable magician
“For every magician in the list of magicians, print the magician’s name.”
give example of meaningful name when using loops?
for cat in cats , for item in list_of_items:, for dog in dogs… etc singular and plural
What usually comes after for loop?
is a summary of what had happened inside the for loop.
When you’re processing data using a for loop, you’ll nd that this is a good way to summarize an operation that was performed on an entire data set. For example, you might use a for loop to initialize a game by running through a list of characters and displaying each character on the screen. You might then write an unindented block after this loop that displays a Play Now button after all the characters have been drawn to the screen.
what hapen if u forget to indent line inside for?
Forgetting to indent additional line , python will run perfectly but you will get wrong result. So, have all the code indent inside the for
what happen when u indent unecessary in python?
Indenting unnecessary : When u unintentionally indent , python will throw an error to you.
What about colon: after for in python?
Colon: is necessry , if you forget it , python will through error message
what does range function do?
Range Function : allow u to create range of numbers
for value in range(1,5):… print(value)
What is ist Comprehensions?
[w for w in x if condition]
Slicing a List , withoud starting index what does that means?
Without a starting index, Python starts at the beginning of the list:print(players[0:3])
1) players[-3:]……..?
negative indicate last 3 position and empty indicate till end
how to Looping Through a Slice?
You can loop through slice player in players[:3]: print(player.title())
how to Copy a List?
a. To copy a list , asiigment of a list to new list don’t work
b. To copy a list us slice [:] ,
what is tuples ?
a. List contains items that change during execution or at anytime
b. Tuple does not chage
c. Python refers to values that cannot change as immutable, and an immutable list is called a tuple.
d. Tuple looks the same with list , except you are usin parenthesis rather than using square brackets
e. You can loop over all the values in a tuple using a for loop, just as you did with a list:
f. Although you can’t modify a tuple, you can assign a new value to a variable that holds a tuple. So if we wanted to change our dimensions, we could rede ne the entire tuple
g.
How to style your Code ?
a. Indentation : PEP 8 recommends that you use four spaces per indentation level
b. Line Length : 80 characters is recommended for python lines of codes
Comments are limited also to 72 characters per line
what is the importance of Blank lines in code?
i. To group parts of your program visually, use blank lines.
ii. For example, if you have ve lines of code that build a list, and then another three lines that do something with that list, it’s appropriate to place a blank line between the two sections. However, you should not place three or four blank lines between the two sections.
iii. Blank lines won’t affect how your code runs, but they will affect the readability of your code. The Python interpreter uses horizontal inden- tation to interpret the meaning of your code, but it disregards vertical spacing.