Lecture 2 - Loops Flashcards

1
Q

A way to do something over and over again.

A

Loops

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

a type loop will repeat a block of code over and over again

A

While

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

While Syntax

A

i = 3
while i != 0:
print(“meow”)

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

When you get stuck in a loop that executes forever, you can press ________________ on your keyboard to break out of the loop.

A

Control + C

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

One cycle through a loop

A

Iteration

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

This loop iterates through a list of items.

A

For

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

Data type used to store multiple items in a single variable.

A

List

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

For Syntax

A

for i in [0, 1, 2]:
print(“meow”)

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

This function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.

A

Range()

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

Range() Syntax

A

Range(start, stop, step)

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

This keyword explicitly tells Python to go to the next iteration of a loop.

A

Continue

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

This keyword tells Python to “break out” of a loop early before it has finished all of its iterations

A

Break

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

Continue & Break Syntax

A

while True:
n = int(input(“What’s n? “))
if n < 0:
continue
else:
break

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

This function returns the number of items in an object.

A

Len()

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

Len() Syntax

A

mylist = [“apple”, “banana”, “cherry”]
x = len(mylist)

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

A data structure that allows you to associate keys with values.

A

Dict

17
Q

These keys are used to create a dictionary

A

{}

18
Q

Dict {} Syntax

A

students = {
“Hermoine”: “Gryffindor”,
“Harry”: “Gryffindor”,
“Ron”: “Gryffindor”,
“Draco”: “Slytherin”,
}
print(students[“Hermoine”])
print(students[“Harry”])
print(students[“Ron”])
print(students[“Draco”])

19
Q

Parameter that enables results to seperate by a comma (or whatever other character/s are used)

A

sep=”, “

20
Q

Sep Syntax

A

for student in students:
print(student, students[student], sep=”, “)

21
Q

This keyword is used to define a null value, or no value at all.

A

None

22
Q

None Syntax

A

students = [
{“name”: “Hermoine”, “house”: “Gryffindor”, “patronus”: “Otter”},
{“name”: “Harry”, “house”: “Gryffindor”, “patronus”: “Stag”},
{“name”: “Ron”, “house”: “Gryffindor”, “patronus”: “Jack Russell terrier”},
{“name”: “Draco”, “house”: “Slytherin”, “patronus”: None},
]