Python: Arithmetic and Assignment Operators

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

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?

  • `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?

  • 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?

  • `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)?

<p>There is no difference; they both perform the same operation and yield identical results. (D)</p>
Signup and view all the answers

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)?

<p>Check if <code>x &amp; (x - 1)</code> equals 0 and <code>x</code> is not zero. (B)</p>
Signup and view all the answers

Analyze the expression not (a is b) in Python. In what scenario does this expression return True?

<p>When <code>a</code> and <code>b</code> are equal in value but are different objects. (C)</p>
Signup and view all the answers

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?

<p><code>x</code> is <code>[1, 2, 3, 4]</code>, <code>y</code> is <code>[1, 2, 3, 4]</code> (B)</p>
Signup and view all the answers

Given a list my_list = [1, 2, 3, 4, 5], which expression correctly checks if the value 3 is not present in the list?

<p>Both A and C (D)</p>
Signup and view all the answers

If x = True and y = False, evaluate the expression (x or y) and not (x and y). What is the result?

<p><code>True</code> (D)</p>
Signup and view all the answers

How does Python handle the result of a 'right shift' bitwise operation on a signed integer?

<p>It fills the leftmost bits with zeros if the number is positive and ones if the number is negative, preserving the sign. (A)</p>
Signup and view all the answers

What is the outcome of the expression (False == False) in [True]?

<p><code>False</code> (C)</p>
Signup and view all the answers

Evaluate the Python expression 10 // 3 + 5 % 2. What is the result and why?

<p><code>4</code>, because the modulus operation is performed before floor division. (C)</p>
Signup and view all the answers

What determines whether two variables in Python are considered identical by the is operator?

<p>If they occupy the same memory location. (B)</p>
Signup and view all the answers

Predict the output of the following snippet:

a = [1, 2, 3]
b = a[:]
b[0] = 5
print(a[0])

<p>1 (D)</p>
Signup and view all the answers

Given the expression False or True and False, what is its evaluated Boolean value in Python?

<p><code>False</code> (D)</p>
Signup and view all the answers

What is the result of the following Python code?

x = 8
y = 2
z = x >> y << y
print(z)

<p>8 (C)</p>
Signup and view all the answers

Under what condition will the Python expression x is not y evaluate to False?

<p>When <code>x</code> and <code>y</code> are the same object. (C)</p>
Signup and view all the answers

Analyze the following Python code. What will be the output?

x = True
y = not not x
print(x is y)

<p><code>True</code> (B)</p>
Signup and view all the answers

Given the Python code a = [1, 2, 3] and b = a. What will be the output after executing a += [4, 5]?

<p><code>a</code> is <code>[1, 2, 3, 4, 5]</code>, <code>b</code> is <code>[1, 2, 3, 4, 5]</code> (B)</p>
Signup and view all the answers

Predict the output of the following Python code:

list1 = [1, 2, 3]
list2 = list1
list2[0] = 4
print(list1)

<p>[4, 2, 3] (B)</p>
Signup and view all the answers

If x = 10 and y = 5, what does the expression x is not y evaluate to?

<p><code>True</code> (B)</p>
Signup and view all the answers

Consider two variables a = [1, 2, 3] and b = [1, 2, 3]. What is the difference between a is b and a == b?

<p><code>a is b</code> checks for object identity, while <code>a == b</code> checks for value equality. (A)</p>
Signup and view all the answers

What is the result of 5 & 3?

<p>1 (C)</p>
Signup and view all the answers

If you have two lists, list1 = [1, 2, 3] and list2 = list1[:], what does list1 is list2 evaluate to?

<p><code>False</code>, because <code>list2</code> is a new object in memory, even though it has the same elements as <code>list1</code>. (D)</p>
Signup and view all the answers

What output will this code produce?

print(bool(0) or bool("False"))

<p>True (D)</p>
Signup and view all the answers

What is the result of the following expression: 'a' in ['a', 'b', 'c'] is False?

<p><code>False</code> (A)</p>
Signup and view all the answers

What is the result of 10 % 3 and 5 // 2?

<p>2 (A)</p>
Signup and view all the answers

Flashcards

Arithmetic Operators

Operators used to perform calculations with numeric values.

Assignment Operators

Assigns values to variables.

Comparison Operators

Determine equality or difference between values.

Logical Operators

Determine logic between variables; result is True/False.

Signup and view all the flashcards

Bitwise Operators

Compare data in binary format.

Signup and view all the flashcards

Identity Operators

Compare if objects are at the same memory location.

Signup and view all the flashcards

Membership Operators

Test if a sequence is present in an object.

Signup and view all the flashcards

Operator Precedence

Order in which operations are performed.

Signup and view all the flashcards

Boolean

Represents 'True' or 'False'.

Signup and view all the flashcards

Most Values are True

Almost any value is True if it has some sort of content.

Signup and view all the flashcards

Division operator

Divides numbers.

Signup and view all the flashcards

Modulus operator

Returns the division remainder.

Signup and view all the flashcards

Floor division

Returns the division quotient

Signup and view all the flashcards

Zero fill left shift

Insert the specified number of 0's from the right.

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 to x = x + y
  • Subtraction assignment (-=) subtracts a value and assigns it to the variable, e.g., x -= y is equivalent to x = x - y
  • Multiplication assignment (*=) multiplies a value and assigns it to the variable, e.g., x *= y is equivalent to x = x * y
  • Division assignment (/=) divides a value and assigns it to the variable, e.g., x /= y is equivalent to x = x / y
  • Modulus assignment (%=) calculates the modulus and assigns it, e.g., x %= y is equivalent to x = x % y
  • Floor division assignment (//=) performs floor division and assigns it, e.g., x //= y is equivalent to x = x // y
  • Exponentiation assignment (**=) raises to a power and assigns it, e.g., x **= y is equivalent to x = 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 to 9-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 returns True or False
  • 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.

Quiz Team

Related Documents

More Like This

Arithmetic Operators and Constants Quiz
17 questions
C Operators: Arithmetic and Assignment
10 questions
Python Operators
39 questions

Python Operators

BetterThanExpectedLearning9144 avatar
BetterThanExpectedLearning9144
Use Quizgecko on...
Browser
Browser