Podcast
Questions and Answers
Given the variables x = [1, 2, 3]
and y = [1, 2, 3]
, evaluate the expressions x is y
and x == y
. What will be the result?
Given the variables x = [1, 2, 3]
and y = [1, 2, 3]
, evaluate the expressions x is y
and x == y
. What will be the result?
- `x is y` returns `True`, `x == y` returns `False`
- `x is y` returns `False`, `x == y` returns `False`
- `x is y` returns `True`, `x == y` returns `True`
- `x is y` returns `False`, `x == y` returns `True` (correct)
Consider the expression a and b or not c
, where a
, b
, and c
are Boolean variables. According to Python's operator precedence, how is this expression evaluated?
Consider the expression a and b or not c
, where a
, b
, and c
are Boolean variables. According to Python's operator precedence, how is this expression evaluated?
- Evaluated with `and` having the highest precedence: `a and (b or (not c))`
- The expression will raise a syntax error due to ambiguous precedence without parentheses.
- Evaluated with `not` having the highest precedence: `(a and b) or ((not c))` (correct)
- Evaluated from left to right: `(a and b) or (not c)`
What is the result of the expression 10 > 5 > 3
in Python, and why?
What is the result of the expression 10 > 5 > 3
in Python, and why?
- `False`, because the expression is evaluated as `(10 > 5) and (5 > 3)`, and while the first comparison is true, the interpreter does not proceed to the second comparison.
- `True`, because Python chains comparison operators, evaluating pairwise from left to right. (correct)
- `False`, because Python does not support chained comparison operators.
- `True`, because 10 is greater than both 5 and 3.
Given x = 5
, what is the difference between x << 2
and x * (2**2)
?
Given x = 5
, what is the difference between x << 2
and x * (2**2)
?
Using only bitwise operators, how can you determine if an integer x
is a power of 2 (i.e., $2^n$ for some non-negative integer n
)?
Using only bitwise operators, how can you determine if an integer x
is a power of 2 (i.e., $2^n$ for some non-negative integer n
)?
Analyze the expression not (a is b)
in Python. In what scenario does this expression return True
?
Analyze the expression not (a is b)
in Python. In what scenario does this expression return True
?
Consider the following Python code:
x = [1, 2]
y = x
x += [3, 4]
What are the values of x
and y
after executing this code?
Consider the following Python code:
x = [1, 2]
y = x
x += [3, 4]
What are the values of x
and y
after executing this code?
Given a list my_list = [1, 2, 3, 4, 5]
, which expression correctly checks if the value 3
is not present in the list?
Given a list my_list = [1, 2, 3, 4, 5]
, which expression correctly checks if the value 3
is not present in the list?
If x = True
and y = False
, evaluate the expression (x or y) and not (x and y)
. What is the result?
If x = True
and y = False
, evaluate the expression (x or y) and not (x and y)
. What is the result?
How does Python handle the result of a 'right shift' bitwise operation on a signed integer?
How does Python handle the result of a 'right shift' bitwise operation on a signed integer?
What is the outcome of the expression (False == False) in [True]
?
What is the outcome of the expression (False == False) in [True]
?
Evaluate the Python expression 10 // 3 + 5 % 2
. What is the result and why?
Evaluate the Python expression 10 // 3 + 5 % 2
. What is the result and why?
What determines whether two variables in Python are considered identical by the is
operator?
What determines whether two variables in Python are considered identical by the is
operator?
Predict the output of the following snippet:
a = [1, 2, 3]
b = a[:]
b[0] = 5
print(a[0])
Predict the output of the following snippet:
a = [1, 2, 3]
b = a[:]
b[0] = 5
print(a[0])
Given the expression False or True and False
, what is its evaluated Boolean value in Python?
Given the expression False or True and False
, what is its evaluated Boolean value in Python?
What is the result of the following Python code?
x = 8
y = 2
z = x >> y << y
print(z)
What is the result of the following Python code?
x = 8
y = 2
z = x >> y << y
print(z)
Under what condition will the Python expression x is not y
evaluate to False
?
Under what condition will the Python expression x is not y
evaluate to False
?
Analyze the following Python code. What will be the output?
x = True
y = not not x
print(x is y)
Analyze the following Python code. What will be the output?
x = True
y = not not x
print(x is y)
Given the Python code a = [1, 2, 3]
and b = a
. What will be the output after executing a += [4, 5]
?
Given the Python code a = [1, 2, 3]
and b = a
. What will be the output after executing a += [4, 5]
?
Predict the output of the following Python code:
list1 = [1, 2, 3]
list2 = list1
list2[0] = 4
print(list1)
Predict the output of the following Python code:
list1 = [1, 2, 3]
list2 = list1
list2[0] = 4
print(list1)
If x = 10
and y = 5
, what does the expression x is not y
evaluate to?
If x = 10
and y = 5
, what does the expression x is not y
evaluate to?
Consider two variables a = [1, 2, 3]
and b = [1, 2, 3]
. What is the difference between a is b
and a == b
?
Consider two variables a = [1, 2, 3]
and b = [1, 2, 3]
. What is the difference between a is b
and a == b
?
What is the result of 5 & 3
?
What is the result of 5 & 3
?
If you have two lists, list1 = [1, 2, 3]
and list2 = list1[:]
, what does list1 is list2
evaluate to?
If you have two lists, list1 = [1, 2, 3]
and list2 = list1[:]
, what does list1 is list2
evaluate to?
What output will this code produce?
print(bool(0) or bool("False"))
What output will this code produce?
print(bool(0) or bool("False"))
What is the result of the following expression: 'a' in ['a', 'b', 'c'] is False
?
What is the result of the following expression: 'a' in ['a', 'b', 'c'] is False
?
What is the result of 10 % 3 and 5 // 2
?
What is the result of 10 % 3 and 5 // 2
?
Flashcards
Arithmetic Operators
Arithmetic Operators
Operators used to perform calculations with numeric values.
Assignment Operators
Assignment Operators
Assigns values to variables.
Comparison Operators
Comparison Operators
Determine equality or difference between 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
Boolean
Boolean
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
Floor division
Signup and view all the flashcards
Zero fill left shift
Zero fill left shift
Signup and view all the flashcards
Study Notes
Python Operators Overview
- Python operators are categorized into arithmetic, assignment, comparison, logical, bitwise, identity, and membership groups
- Operator precedence dictates the order in which operations are performed, with multiplication and division taking priority over addition and subtraction
Arithmetic Operators
- These operators perform common mathematic operations using numeric values
- Addition (+) adds two values, e.g.,
x + y
- Subtraction (-) subtracts one value from another, e.g.,
x - y
- Multiplication (*) multiplies two values, e.g.,
x * y
- Division (/) divides one value by another, e.g.,
x / y
- Modulus (%) gives the remainder of a division, e.g.,
x % y
- Floor division (//) returns the integer part of the division, e.g.,
x // y
- Exponentiation (**) raises a number to a power, e.g.,
x ** y
Assignment Operators
- Assignment operators assign values to variables
- The assignment operator (=) assigns a value, e.g.,
x = y
- Addition assignment (+=) adds a value and assigns it to the variable, e.g.,
x += y
is equivalent tox = x + y
- Subtraction assignment (-=) subtracts a value and assigns it to the variable, e.g.,
x -= y
is equivalent tox = x - y
- Multiplication assignment (*=) multiplies a value and assigns it to the variable, e.g.,
x *= y
is equivalent tox = x * y
- Division assignment (/=) divides a value and assigns it to the variable, e.g.,
x /= y
is equivalent tox = x / y
- Modulus assignment (%=) calculates the modulus and assigns it, e.g.,
x %= y
is equivalent tox = x % y
- Floor division assignment (//=) performs floor division and assigns it, e.g.,
x //= y
is equivalent tox = x // y
- Exponentiation assignment (**=) raises to a power and assigns it, e.g.,
x **= y
is equivalent tox = x ** y
Comparison Operators
- Comparison operators determine the equality or difference between variables or values in logical statements
- Equal (==) tests if two values are equal, e.g.,
x == y
- Not equal (!=) tests if two values are not equal, e.g.,
x != y
- Greater than (>) tests if one value is greater than another, e.g.,
x > y
- Less than (<) tests if one value is less than another, e.g.,
x y
- Left shift (
<<
) shifts bits to the left, adding zeros from the right, e.g.,5 << 1
- Right shift (
>>
) shifts bits to the right, dropping rightmost bits, e.g.,5 >> 1
Identity Operators
-
Identity operators compare the identity of objects, checking if they are the same object with the same memory location
-
is
returns True if both variables are the same object, e.g.,x is y
x = ["apple", "banana"]
y = ["apple", "banana"]
z = x
print(x is z) # True, z is the same object as x
print(x is y) # False, x and y are different objects, even with the same content
is not
returns True if both variables are not the same object, e.g.,x is not y
x = ["apple", "banana"]
y = ["apple", "banana"]
z = x
print(x is not z) # False, z is the same object as x
print(x is not y) # True, x and y are different objects
Membership Operators
- Membership operators test if a sequence is present in an object
x = ["apple", "banana"]
print("banana" in x) # True
print("pineapple" not in x) # True
in
returns True if a sequence with the specified value is present, e.g.,"banana" in x
not in
returns True if a sequence with the specified value is not present, e.g.,"pineapple" not in x
Operator Precedence
- Parentheses
()
have the highest precedence and are evaluated first,print((6 + 3) - (6 + 3))
evaluates to 0 because it calculates to9-9
- Exponentiation
**
is next - 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 i.e.
==
,!=
,>
,>=
,<
,<=
,is
,is not
,in
,not in
- Logical NOT
not
- Logical AND
and
- Logical OR
or
Python Booleans
- Booleans represent True or False Values
- In programming you often need to know if an expression is True or False
- You can evaluate any expression in Python, and get one of two answers, True or False
- When you compare two values, the expression is evaluated and python returns the boolean answer
Boolean Values and Variables
- The
bool()
function evaluates a value and returnsTrue
orFalse
- Most values are True
- 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, except 0
- Any list, typle, set, and dictionary are True, exceot empty ones
bool("abc") #True bool(123) #True bool(["apple", "cherry", "banana"]) #True
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.