In programming, a loop is a programming structure that repeats a set of instructions until a specified condition is met. Loops are commonly used in programming because, compared to repeated lines of code, they save time, reduce error, and are easy to read.
A loop to play a sound may look like this:
UNTIL 4 sounds have been played: Play a sound
Conditional statements or conditional control structures allow a program to have different behaviors depending on certain conditions being met.
Intuitively, this mimics the way humans make simple decisions and act upon them. For example, reasoning about whether to go outside might look like:
We could keep adding clauses to make our reasoning more sophisticated, such as “If it is sunny, then wear sunscreen”.
In programming, control flow is the order in which statements and instructions are executed. Programmers are able to change a program’s control flow using control structures such as conditionals.
Being able to alter a program’s control flow is powerful, as it lets us adapt a running program’s behavior depending on the state of the program. For example, suppose a user is using a banking application and wants to withdraw $500. We certainly want the application to behave differently depending on whether the user has $20 or $1000 in their bank account!
elif statement allows for continued checks to be performed after an initial
if statement. An
elif statement differs from the
else statement because another expression is provided to be checked, just as with the initial
If the expression is
True, the indented code following the
elif is executed. If the expression evaluates to
False, the code can continue to an optional
else statement. Multiple
elif statements can be used following an initial
if to perform a series of checks. Once an
elif expression evaluates to
True, no further
elif statements are executed.
# elif Statementpet_type = "fish"if pet_type == "dog":print("You have a dog.")elif pet_type == "cat":print("You have a cat.")elif pet_type == "fish":# this is performedprint("You have a fish")else:print("Not sure!")
or operator combines two Boolean expressions and evaluates to
True if at least one of the expressions returns
True. Otherwise, if both expressions are
False, then the entire expression evaluates to
True or True # Evaluates to TrueTrue or False # Evaluates to TrueFalse or False # Evaluates to False1 < 2 or 3 < 1 # Evaluates to True3 < 1 or 1 > 6 # Evaluates to False1 == 1 or 1 < 2 # Evaluates to True
The equal operator,
==, is used to compare two values, variables or expressions to determine if they are the same.
If the values being compared are the same, the operator returns
True, otherwise it returns
The operator takes the data type into account when making the comparison, so a string value of
"2" is not considered the same as a numeric value of
# Equal operatorif 'Yes' == 'Yes':# evaluates to Trueprint('They are equal')if (2 > 1) == (5 < 10):# evaluates to Trueprint('Both expressions give the same result')c = '2'd = 2if c == d:print('They are equal')else:print('They are not equal')
The Python not equals operator,
!=, is used to compare two values, variables or expressions to determine if they are NOT the same. If they are NOT the same, the operator returns
True. If they are the same, then it returns
The operator takes the data type into account when making the comparison so a value of
10 would NOT be equal to the string value
"10" and the operator would return
True. If expressions are used, then they are evaluated to a value of
False before the comparison is made by the operator.
# Not Equals Operatorif "Yes" != "No":# evaluates to Trueprint("They are NOT equal")val1 = 10val2 = 20if val1 != val2:print("They are NOT equal")if (10 > 1) != (10 > 1000):# True != Falseprint("They are NOT equal")
In Python, relational operators compare two values or expressions. The most common ones are:
<=less than or equal to
>=greater than or equal too
If the relation is sound, then the entire expression will evaluate to
True. If not, the expression evaluates to
a = 2b = 3a < b # evaluates to Truea > b # evaluates to Falsea >= b # evaluates to Falsea <= b # evaluates to Truea <= a # evaluates to True
if statement is used to determine the execution of code based on the evaluation of a Boolean expression.
ifstatement expression evaluates to
True, then the indented code following the statement is executed.
Falsethen the indented code following the
ifstatement is skipped and the program executes the next line of code which is indented at the same level as the
# if Statementtest_value = 100if test_value > 1:# Expression evaluates to Trueprint("This code is executed!")if test_value > 1000:# Expression evaluates to Falseprint("This code is NOT executed!")print("Program continues at this point.")
else statement provides alternate code to execute if the expression in an
if statement evaluates to
The indented code for the
if statement is executed if the expression evaluates to
True. The indented code immediately following the
else is executed only if the expression evaluates to
False. To mark the end of the
else block, the code must be unindented to the same level as the starting
# else Statementtest_value = 50if test_value < 1:print("Value is < 1")else:print("Value is >= 1")test_string = "VALID"if test_string == "NOT_VALID":print("String equals NOT_VALID")else:print("String equals something else!")
and operator performs a Boolean comparison between two Boolean values, variables, or expressions. If both sides of the operator evaluate to
True then the
and operator returns
True. If either side (or both sides) evaluates to
False, then the
and operator returns
False. A non-Boolean value (or variable that stores a value) will always evaluate to
True when used with the
True and True # Evaluates to TrueTrue and False # Evaluates to FalseFalse and False # Evaluates to False1 == 1 and 1 < 2 # Evaluates to True1 < 2 and 3 < 1 # Evaluates to False"Yes" and 100 # Evaluates to True
Booleans are a data type in Python, much like integers, floats, and strings. However, booleans only have two values:
Specifically, these two values are of the
bool type. Since booleans are a data type, creating a variable that holds a boolean value is the same as with other data types.
is_true = Trueis_false = Falseprint(type(is_true))# will output: <class 'bool'>
The Python Boolean
not operator is used in a Boolean expression in order to evaluate the expression to its inverse value. If the original expression was
True, including the
not operator would make the expression
False, and vice versa.
not True # Evaluates to Falsenot False # Evaluates to True1 > 2 # Evaluates to Falsenot 1 > 2 # Evaluates to True1 == 1 # Evaluates to Truenot 1 == 1 # Evaluates to False