Lecture 2: Loops Flashcards

1
Q

What is the while loop and when do we use it?

A

While loop can run a set of statements as long as a condition is true
We generally use this loop when we don’t know the number of times to repeat it

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

How to exit a loop that executes forever?

A

press control-c to break out of the loop

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

How to write i = i + 1 in another way?

A

i += 1

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

What is the for loop?

A

a for loop iterates (تتكرر) through a list of items

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

What is the list and how do we write it in python?

A

A list is a type of data that stores multiple items in a single variable.
we use square brackets [ ] to write it in python

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

What is the range() function? what is its common use?

A

it returns a sequence of numbers between the given range and it usually starts counting from 0

it s commonly used in a for loop to iterate the loop a certain number of times

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

_ means?

A

a variable name that we never touch it or need it

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

Explaine this piece of code:
print ( “meow \n “ * 3, end=”” )

A

This code will meow 3 times and by adding end=”” and the \n we tell the compiler to add a line break at the end of each meow

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

One of the most common ways to use a while loop is?

A

Validate (التحقق من صحة) the input of the user

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

What are the main statements in terms of loops in python? (with ex)

A
  • break statement stops the loop
    -continue statement skips a single iteration in a loop

if n < 0:
continue
else:
break

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

What is the len() function?

A

The len() function returns the number of items (length) in an object.
ex: the number of items in a list

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

What is a dictionary?

A

dicts or dictionaries is a data structure that allows you to associate keys with values

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

What is the difference between a list and a dictionary?

A

the list is a list of multiple values, a dict associates a key with a value.

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

What does sep=”, “ creat for us?

A

creates a clean separation of a , between each item printed.

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

Show me the best way to print something at certain times

A

print ( “#” * 3)

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