Lecture 2: Loops Flashcards
What is the while loop and when do we use it?
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 to exit a loop that executes forever?
press control-c to break out of the loop
How to write i = i + 1 in another way?
i += 1
What is the for loop?
a for loop iterates (تتكرر) through a list of items
What is the list and how do we write it in python?
A list is a type of data that stores multiple items in a single variable.
we use square brackets [ ] to write it in python
What is the range() function? what is its common use?
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
_ means?
a variable name that we never touch it or need it
Explaine this piece of code:
print ( “meow \n “ * 3, end=”” )
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
One of the most common ways to use a while loop is?
Validate (التحقق من صحة) the input of the user
What are the main statements in terms of loops in python? (with ex)
- break statement stops the loop
-continue statement skips a single iteration in a loop
if n < 0:
continue
else:
break
What is the len() function?
The len() function returns the number of items (length) in an object.
ex: the number of items in a list
What is a dictionary?
dicts or dictionaries is a data structure that allows you to associate keys with values
What is the difference between a list and a dictionary?
the list is a list of multiple values, a dict associates a key with a value.
What does sep=”, “ creat for us?
creates a clean separation of a , between each item printed.
Show me the best way to print something at certain times
print ( “#” * 3)