Save
PL101FINALS
Save
Share
Learn
Content
Leaderboard
Learn
Created by
Definitelynotjace
Visit profile
Cards (29)
In computer programming,
loops
are used to repeat a block of code.
There are 2 types of loops in Python:
for loop, while loop
In Python, the
for loop
is used to run a block of code for a certain number of times. It is
used to iterate over any sequences such as list, tuple, string, etc.
The syntax of the for loop is:
for val in sequence
:
#
statement(s)
What's the output?
languages = ['Swift,'Python','Go','JavaScript']
for language in languages:
print(language)
Swift
Python
Go
JavaScript
A
range
is a series of values between two numeric intervals.
We use Python's built-in function
range()
to define a range of values.
values = range(4)
for i in values:
print(i)
OUTPUT:
0
1
2
3
digits = [0, 1, 5]
for i in digits:
print(i)
else:
print("No items left.")
OUTPUT:
0
1
5
No items left.
When we have a for loop inside another for loop, it’s called a
nested
for
loop.
The
break
statement
is used to exit the for loop prematurely. It’s used to break the for loop when a specific condition is met.
We can use
continue
statements
inside a for loop to skip the execution of the for loop body for a specific condition.
A
while
loop
implements the repeated execution of code based on a given Boolean condition.
The code that is in a while block will execute as long as the while statement evaluates to True.
You can think of the
while
loop
as a repeating conditional statement.
As opposed to for loops that execute a certain number of times,
while
loops
are conditionally
based, so you don’t need to know how many times to repeat the code going
Python
while
loop
is used to run a specific code until a certain condition is met.
The
for
loop
is usually used when the number of iterations is known.
while
loop
is usually used when the number of iterations is unknown.
An
infinite loop
occurs when a program keeps executing within one loop, never leaving it.
and
and
or
are logical operators used to combine multiple conditions in Boolean
expressions.
The
and operator
returns True if both of the
conditions
it
connects
are
True
; otherwise, it
returns False.
The
or operator
returns True if at least one of the conditions it connects is True; it returns False
only if both conditions are False.
The
break statement
is used to terminate the loop immediately when it is encountered.
We can use the
break
statement
with for loop to terminate the loop when a certain condition is
met.
The
continue
statement
is used to skip the current iteration of the loop and the control flow of
the program goes to the next iteration.
In Python programming, the
pass
statement
is a null statement which can be used as a
placeholder for future code.
A
function
is a block of code that performs a specific task when the function is
called (invoked).
A
parameter
is actually a variable, but there are two important factors that make
parameters
different
and
special
step
parameter
It tells the range function how many numbers to skip between each count.