Podcast
Questions and Answers
Which group does Python NOT divide operators into?
Which group does Python NOT divide operators into?
- Logical Operators
- Assignment Operators
- Arithmetic Operators
- Geometric Operators (correct)
What is the purpose of arithmetic operators in Python?
What is the purpose of arithmetic operators in Python?
- To determine the logic between variables
- To perform common mathematical operations (correct)
- To assign values to variables
- To compare variables
Which operator is used to find the division remainder in Python?
Which operator is used to find the division remainder in Python?
- **
- % (correct)
- /
- //
Assignment operators are used to perform mathematical calculations directly on values.
Assignment operators are used to perform mathematical calculations directly on values.
Which assignment operator is equivalent to 'x = x * y'?
Which assignment operator is equivalent to 'x = x * y'?
What is the primary use of comparison operators in Python?
What is the primary use of comparison operators in Python?
The operator !=
in Python means ______.
The operator !=
in Python means ______.
Logical operators in Python are used to perform calculations on numeric values.
Logical operators in Python are used to perform calculations on numeric values.
Which logical operator returns True
only if both statements are True
?
Which logical operator returns True
only if both statements are True
?
What is the main function of bitwise operators?
What is the main function of bitwise operators?
Before a bitwise operation is performed, what conversion does Python apply to numbers?
Before a bitwise operation is performed, what conversion does Python apply to numbers?
Match the bitwise operator with its description:
Match the bitwise operator with its description:
What do identity operators in Python compare?
What do identity operators in Python compare?
The identity operator is not
returns True
if both variables ______ the same object.
The identity operator is not
returns True
if both variables ______ the same object.
If two lists have the same elements in the same order, the 'is' operator will always return True when comparing them.
If two lists have the same elements in the same order, the 'is' operator will always return True when comparing them.
What is the purpose of membership operators in Python?
What is the purpose of membership operators in Python?
Which operator checks if a sequence is NOT present in an object?
Which operator checks if a sequence is NOT present in an object?
The 'in' operator returns True if an element with the specified value is present in the object being checked.
The 'in' operator returns True if an element with the specified value is present in the object being checked.
What does operator precedence describe?
What does operator precedence describe?
According to operator precedence, ______ and division have higher precedence than addition and subtraction.
According to operator precedence, ______ and division have higher precedence than addition and subtraction.
What happens when there are operations with the same precedence?
What happens when there are operations with the same precedence?
Which of the following operators has the highest precedence?
Which of the following operators has the highest precedence?
Booleans in Python represent one of three values: True, False, or Maybe.
Booleans in Python represent one of three values: True, False, or Maybe.
In Python, what is the result of comparing two values using comparison operators?
In Python, what is the result of comparing two values using comparison operators?
When a condition is run in an if
conditional statment, Python returns either ______ or False.
When a condition is run in an if
conditional statment, Python returns either ______ or False.
What is the purpose of the bool()
function in Python?
What is the purpose of the bool()
function in Python?
In Python, bool(0) will return True.
In Python, bool(0) will return True.
Which of the following values will return False
when passed to the bool()
function?
Which of the following values will return False
when passed to the bool()
function?
Floor division (//
) returns what part of the result?
Floor division (//
) returns what part of the result?
What does the expression x += 5
do?
What does the expression x += 5
do?
The following two python expressions will result in the same boolean True
or False
output. x > y is the same as y < x
The following two python expressions will result in the same boolean True
or False
output. x > y is the same as y < x
If x = 5, which of the following expressions will return False?
If x = 5, which of the following expressions will return False?
The <<
operator inserts the specified number of 0's from the ______ and let the same amount of leftmost bits fall off.
The <<
operator inserts the specified number of 0's from the ______ and let the same amount of leftmost bits fall off.
If operator precedence dictates that exponentiation is done before subtraction, what is the output of print(100 - 3 ** 3)
?
If operator precedence dictates that exponentiation is done before subtraction, what is the output of print(100 - 3 ** 3)
?
Match the type of expression to the operator used:
Match the type of expression to the operator used:
Given x = ["apple", "banana"]
, which expression evaluates to True
?
Given x = ["apple", "banana"]
, which expression evaluates to True
?
How are operations with the same precedence computed in an expression, such as 100/50*3
?
How are operations with the same precedence computed in an expression, such as 100/50*3
?
The bitwise operator ~
(NOT) inverts all the bits of a number.
The bitwise operator ~
(NOT) inverts all the bits of a number.
What is the value of bool("abc")
?
What is the value of bool("abc")
?
Assume these assignments are made: x = ["apple", "banana"]
, y = ["apple", "banana"]
. Then x == y
is ______ and x is y
is ______.
Assume these assignments are made: x = ["apple", "banana"]
, y = ["apple", "banana"]
. Then x == y
is ______ and x is y
is ______.
Flashcards
Arithmetic Operators
Arithmetic Operators
Operators used with numeric values for common math operations.
Assignment Operators
Assignment Operators
Assigns values to variables.
Comparison Operators
Comparison Operators
Determine equality or difference of variables/values.
Logical Operators
Logical Operators
Signup and view all the flashcards
Bitwise Operators
Bitwise Operators
Signup and view all the flashcards
Identity Operators
Identity Operators
Signup and view all the flashcards
Membership Operators
Membership Operators
Signup and view all the flashcards
Operator Precedence
Operator Precedence
Signup and view all the flashcards
Booleans
Booleans
Signup and view all the flashcards
Most Values are True
Most Values are True
Signup and view all the flashcards
Division Operator (/)
Division Operator (/)
Signup and view all the flashcards
Modulus Operator (%)
Modulus Operator (%)
Signup and view all the flashcards
Floor Division Operator (//)
Floor Division Operator (//)
Signup and view all the flashcards
Arithmetic Assignment Operators
Arithmetic Assignment Operators
Signup and view all the flashcards
Zero fill left shift
Zero fill left shift
Signup and view all the flashcards
Right shift
Right shift
Signup and view all the flashcards
Study Notes
Types of Python Operators
- Python divides operators into groups like arithmetic, assignment, comparison, logical, bitwise, identity, and membership operators.
Python Arithmetic Operators
- Used with numeric values for common mathematical operations.
- Addition (+) performs addition:
x + y
. - Subtraction (-) performs subtraction:
x - y
. - Multiplication (*) performs multiplication:
x * y
. - Division (/) performs division:
x / y
. - Modulus (%) returns the division remainder:
x % y
. - Floor division (//) returns the integer part of division:
x // y
. - Exponentiation (**) raises to a power:
x ** y
.
Python Assignment Operators
- Used to assign values to variables.
- Assignment (=) assigns a value:
x = y
. - Addition Assignment (+=) adds and assigns:
x += y
is the same asx = x + y
. - Subtraction Assignment (-=) subtracts and assigns:
x -= y
is the same asx = x - y
. - Multiplication Assignment (*=) multiplies and assigns:
x *= y
is the same asx = x * y
. - Division Assignment (/=) divides and assigns:
x /= y
is the same asx = x / y
. - Modulus Assignment (%=) gets modulus and assigns:
x %= y
is the same asx = x % y
. - Floor Division Assignment (//=) performs floor division and assigns:
x //= y
is the same asx = x // y
. - Exponentiation Assignment (**=) raises to a power and assigns:
x **= y
is the same asx = x ** y
.
Python Comparison Operators
- Used in logical statements to determine equality or difference.
- Comparison and logical operators return
True
orFalse
. - Equal (==) checks if values are equal:
x == y
. - Not equal (!=) checks if values are not equal:
x != y
. - Greater than (>) checks if one value is greater than the other:
x > y
. - Less than (<) checks if one value is less than the other:
x < y
. - Greater than or equal to (>=) checks if one value is greater than or equal to the other:
x >= y
. - Less than or equal to (<=) checks if one value is less than or equal to the other:
x <= y
.
Python Logical Operators
- Used to determine logic between variables or values.
and
returnsTrue
if both statements are true:x < 5 and x < 10
.or
returnsTrue
if one of the statements is true:x < 5 or x < 4
.not
reverses the result, returningFalse
if the result is true:not(x < 5 and x < 10)
.
Python Bitwise Operators
- Used to compare (binary) numbers; operations are performed on 32-bit binary numbers.
- Python converts numbers to 32-bit signed integers before a bitwise operation and converts the result back to a Python number.
- AND (&) sets each bit to 1 if both bits are 1, e.g.,
5 & 1
results in 1. - OR (|) sets each bit to 1 if one of two bits is 1, e.g.,
5 | 1
results in 5. - XOR (^) sets each bit to 1 if only one of two bits is 1, e.g.,
5 ^ 1
results in 4. - NOT (~) inverts all the bits, e.g.,
~5
results in -6. - Zero fill left shift (<<) shifts left by pushing zeros in from the right, letting leftmost bits fall off, e.g.,
5 << 1
results in 10. - Signed right shift (>>) shifts right by pushing copies of the leftmost bit in from the left, letting the rightmost bits fall off, e.g.,
5 >> 1
results in 2.
Python Identity Operators
- Used to compare the objects, not if they are equal, but rather if they are actually the same object with the same memory location.
is
returnsTrue
if both variables are the same object:x is y
.is not
returnsTrue
if both variables are not the same object:x is not y
.- Example:
x = ["apple", "banana"]
y = ["apple", "banana"]
z = x
print(x is z)
returnsTrue
becausez
is the same object asx
.print(x is y)
returnsFalse
becausex
is not the same object asy
, even if they have the same content.print(x == y)
returnsTrue
becausex
is equal toy
.print(x is not z)
returnsFalse
becausez
is the same object asx
.print(x is not y)
returnsTrue
becausex
is not the same object asy
.print(x != y)
returnsFalse
becausex
is equal toy
.
Python Membership Operators
- Used to test if a sequence is present in an object.
in
returnsTrue
if a sequence with the specified value is present in the object:x in y
.not in
returnsTrue
if a sequence with the specified value is not present in the object:x not in y
.- Example:
x = ["apple", "banana"]
print("banana" in x)
returnsTrue
because a sequence with the value "banana" is in the list.print("pineapple" not in x)
returnsTrue
because a sequence with the value "pineapple" is not in the list.
Python Operator Precedence
- Describes the order in which operations are performed; determine the order of operations by priority when there is more than one operator in an expression.
- Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-).
- As in traditional mathematics, multiplication is done first:
print(100 + 50 * 3)
results in 250. - When using parentheses, operations inside the parentheses are computed first:
print((100 + 50) * 3)
results in 450. - Operations with the same precedence (like * and /) are computed from left to right:
print(100 / 50 * 3)
results in 6. - Precedence order (from highest to lowest):
- Parentheses
()
- Exponentiation
**
- Unary plus, unary minus, and bitwise NOT
+x, -x, ~x
- Multiplication, division, floor division, and modulus
*, /, //, %
- Addition and subtraction
+, -
- Bitwise left and right shifts
<<, >>
- Bitwise AND
&
- Bitwise XOR
^
- Bitwise OR
|
- Comparisons, identity, and membership operators
==, !=, >, >=, <, <=, is, is not, in, not in
- Logical NOT
not
- Logical AND
and
- Logical OR
or
- Parentheses
- Parentheses have the highest precedence, and need to be evaluated first:
print((6 + 3) - (6 + 3))
is equal to9 - 9 = 0
. - Example involving bitwise right shift and subtraction: first the subtraction must be completed, followed by bitwise right shift
print(8 >> 4 - 2)
is equivalent toprint(8 >> 2)
=2
.
Python Booleans
- Booleans represent one of two values:
True
orFalse
. - In programming, it's often necessary to know if an expression is
True
orFalse
. - Expressions in Python can be evaluated, returning one of these two answers.
- Comparison of two values results in a Boolean answer.
- Running a condition in an
if
statement returnsTrue
orFalse
. - The
bool()
function evaluates any value and returnsTrue
orFalse
. - Almost any value is evaluated to
True
if it has some sort of content. - Any string is
True
, except empty strings. - Any number is
True
, except0
. - Any list, tuple, set, and dictionary are
True
, except empty ones. - Example:
bool("abc")
returnsTrue.
bool(123)
returnsTrue.
bool(["apple", "cherry", "banana"])
returnsTrue.
- 10 > 9 is
True
. - 10 == 9 is
False
. - 10 < 9 is
False
.
Python Exercises
- Division operator (/) divides numbers.
- Modulus operator (%) returns the division remainder.
- Floor division (//) returns the division quotient.
- Example:
x = 12
y = 3
print(x / y)
gives4.0
.print(x % y)
gives0
.print(x // y)
gives4
.
- Arithmetic assignment operators provide a shorthand way of performing arithmetic operations and assigning the result to a variable in a single step;
+=
and-=
are common. - Given examples:
x = 5
,y = 3
print(x > y)
returnsTrue
because 5 is greater than 3.print(x= 3 and x < 10)
returnsTrue
because 5 is greater than 3 and less than 10.print(x > 3 or x < 4)
returnsTrue
because one of the conditions is met (5 is greater than 3).print(not(x > 3 and x < 10))
returnsFalse
becausenot
is used to reverse the result.
- With zero fill left shift, the
<<
operator inserts the specified number of 0s from the right and let the same amount of leftmost bits fall off (binary operations).- If you push
00
in from the right:3 = 0000000000000011
becomes12 = 0000000000001100
.
- If you push
- Exponentiation has higher precedence than subtraction, evaluated first.
print(100 - 3 ** 3)
is equal toprint(100 - 27)
giving73
.
- Bitwise right shift has a lower precedence than subtraction, requires completing subtraction first.
print(8 >> 4 - 2)
translates toprint(8 >> 2)
, meaning2
.- The
>>
operator moves each bit the specified number of times to the right, filling empty holes at the left with 0's.8 = 0000000000001000
becomes2 = 0000000000000010
if each bit is moved two times to the right.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.