Chapter4 Flashcards

1
Q

example of using loop

A

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.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

give example of meaningful name when using loops?

A

for cat in cats , for item in list_of_items:, for dog in dogs… etc singular and plural

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What usually comes after for loop?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what hapen if u forget to indent line inside for?

A

Forgetting to indent additional line , python will run perfectly but you will get wrong result. So, have all the code indent inside the for

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what happen when u indent unecessary in python?

A

Indenting unnecessary : When u unintentionally indent , python will throw an error to you.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What about colon: after for in python?

A

Colon: is necessry , if you forget it , python will through error message

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what does range function do?

A

Range Function : allow u to create range of numbers

		for value in range(1,5):…
		    print(value)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is ist Comprehensions?

A

[w for w in x if condition]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Slicing a List , withoud starting index what does that means?

A

Without a starting index, Python starts at the beginning of the list:print(players[0:3])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

1) players[-3:]……..?

A

negative indicate last 3 position and empty indicate till end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

how to Looping Through a Slice?

A

You can loop through slice player in players[:3]: print(player.title())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

how to Copy a List?

A

a. To copy a list , asiigment of a list to new list don’t work
b. To copy a list us slice [:] ,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what is tuples ?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to style your Code ?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what is the importance of Blank lines in code?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly