Week 2: Boolean and Logical Operators Flashcards
What are comparison operators?
What are some common comparison operators?
Comparison operators are used to compare 2 values and return a Boolean value. (Recall that Boolean values are either true or false)
What are logical operators? What are they used for?
What are some common logical operators?
Logical operators are used on Boolean statements (either True or False)
used to combine boolean statements, allowing you to evaluate multiple conditions.
What is the output for the comparison operators Greater than (>) and Less than (<) used on 2 equal numbers
it would return the value False - does not include values that are equal
To incude values that are equal , use <= and >=
What happens if you don’t give any string to the input function?
You CAN give the input function no prompt - it simply reads in a value without outputting anything to the console
What is the Order of Operations/ Rules of Precedence for Operators?
- parentheses
- arithmetic operators (using their precedence rules)
- relational, (in)equality and membership operators
- not (logical operator)
- and (logical operator)
- or (logical operator)
What are chain comparisons
python offers a way to short form common logical expressions such as a<b and b<c
we can do the same thing with chain comparisons:
a<b<c
What are the indentation rules for if, else, elif
the code contained in the statement must be indented with 4 spaces or 1 TAB. PEP 8 recommends using the 4 spaces.
You have to stay consistent as python does not allow mixing of tabs and spaces.
Pycharm does this for us by converting tab to 4 spaces automatically, but be careful on other programs
Explain the if statement (What is it, what does it allow you to do, how does the code for it run)
the if statement takes a Boolean expression and only executes the code contained in the statement is the expression evaluates to True
- if the condition of the if statement is not True, then the code inside the statement does not run and we jump to next line of code after the statement
- allows us to create a branch in our code
Explain the if…else statement
the else statement can be added to an if statement to run a block of code if the Boolean expression evaluates to False
- if the condition of the if statement is True then it will run the code for that statement
- if the condition of the if statement is False then it will run the code for the else statement
Then after running either the if or else statement , it will jump to next line of code after the statement
Explain the if…elif…else statement
(What does elif stand for, what it is used for, how does the code run)
by adding elif statements, we can have as many branches as we like
- elif stands for “else if”
- allows us to run a block of code if the statements before were False and its Boolean statement evaluates to True
- it checks through all the statements and then runs the code of the first condition that it finds True, then jumps to next block underneath
- if none of the conditions are true, it will run the code in the else statement
- if there is no else statement and none of them are true it will jump to the next line of code underneath the statement without running any of the code in the statement
What is the syntax of if..elif..else statements
if statement:
- type if, then type the condition then type “:”
- 4 spaces or 1 tab, then type the code inside the statement
elif statement:
- type elif then condition then “:”
- 4 spaces or 1 tab then type the code inside the statement
else statement:
- type else then “:”
- 4 spaces or 1 tab then type the code inside the else statement
What is the syntax for nested if statements?
What are String comparison operators?
String comparison operators are like regular comparison operators, but they have a few quirks when they are used on strings
Later = Greater in the alphabet, so a string is greater it comes later in the alphabet when sorted alphabetically
Capitalization issue:
- all capital letters are said to come before
What are membership operators? How do they work when using them on strings?
Membership operators allow us to check if an element is in or not in a collection
For strings, we can use them to check if a substring is in or not in a larger string
- for strings capitalization matters, so “Hello” is in “Hello World” but “hello” is not in “Hello World”
What are string methods for comparisons? Name and explain them
there are a number of built in string methods that would be helpful when comparing or processing strings