Computer science Conditionals
4.1 Boolean
What is a Boolean?
Answer: A value which is true or false; it is a variable type.What does
type(True)return?
Answer:bool(indicating Boolean type).What is
str(True)used for?
Answer: To convert a Boolean to a true value type (string).What is an
ifstatement?
Answer: Runs a program on specific conditions.What is the syntax of an
ifstatement?
Answer:pythonCopy code
if (condition): # code block else: # code blockWhat should I know about the
ifbranch?
Answer:Prints something if the condition is met.
Must evaluate to a Boolean (True or False).
What should I know about the
elifbranch?
Answer:It has a condition.
Only runs if the first
ifcondition is not met.
What should I know about the
elsebranch?
Answer:Executes the program if the condition in
ifandelifis not met.No specific condition is required.
4.2 if Statements & 4.3 Comparison Operators
What are the comparison operators in Python?
Answer:==: equal to!=: not equal to<: less than>: greater than<=: less than or equal to>=: greater than or equal to
How do you check if more than one condition is true?
Answer: Useandoror.What is the order of the alphabet when comparing strings?
Answer:Symbols/numbers
Uppercase letters
Lowercase letters
How do you compare strings in Python?
Answer:==: equal to!=: not equal to<: earlier in dictionary order>: later in dictionary order
4.4 Logical Operators
Given
x = 5, what is the result ofx < 10?
Answer:TrueWhat are the logical operators in Python (according to priority, with description)?
Answer:not: Changes a true value to false (negates the value).and: True if both conditions are true.or: True if at least one condition is true.
What is a truth table?
Answer:Shows values of Boolean expressions given
xandy.Used to analyze logic in expressions.
What is a Venn Diagram used for in logical operators?
Answer:Visually shows logical operators.
Refer to additional resources for more information on:
Order of operators
De Morgan's Law
Short circuits
4.5 Floating Point Numbers and Rounding
Why do we use the
roundcommand?
Answer: Because Python sometimes rounds very weirdly; it’s used to round a number to a specific decimal value.What does
round(value, decimals)do?
Answer: Rounds a number to a given number of decimal places.What is
floatin Python?
Answer: Changes an element to a float type (decimal).How do you generate a random number?
Answer:Use
randintinside of a random function.Imports the
randomlibrary (provides random options).
What is a package in Python?
Answer: Organizes related modules into a hierarchical structure.