Python Operators: Arithmetic and Assignment

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

What is the primary purpose of arithmetic operators in Python?

  • To compare two values
  • To determine the logic between variables
  • To perform common mathematical operations (correct)
  • To assign values to variables

In Python, the 'is' operator checks if two variables are equal in value, whereas the '==' operator checks if they refer to the same object in memory.

False (B)

Explain the difference between the / and // operators in Python.

The / operator performs division and returns a floating-point result, while the // operator performs floor division and returns an integer result.

The Python operator that returns True if a sequence with the specified value is not present in the object is called ______.

<p>not in</p>
Signup and view all the answers

Match the following Python operators with their descriptions:

<p>&amp; = Sets each bit to 1 if both bits are 1 | = Sets each bit to 1 if one of two bits is 1 ^ = Sets each bit to 1 if only one of two bits is 1 ~ = Inverts all the bits</p>
Signup and view all the answers

What will be the output of the expression print(2 * 3 ** 2 + 4 / 2 - 1) in Python, considering operator precedence?

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

In Python, any non-empty string will evaluate to True when converted to a boolean value.

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

Explain how Python converts numbers into binary before preforming bitwise operations?

<p>Before a bitwise operation is performed, Python converts numbers to 32 bits signed integers.</p>
Signup and view all the answers

The Python operator that will shift left by pushing zeros in from the right and let the leftmost bits fall off is called ______.

<p>&lt;&lt;</p>
Signup and view all the answers

Match the following assignment operators with their equivalent long forms:

<p>x += y = x = x + y x -= y = x = x - y x *= y = x = x * y x /= y = x = x / y</p>
Signup and view all the answers

What is the output of print(not(True and False) or (True and True))?

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

The result of the expression 5 >> 1 is 2?

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

Illustrate an example of 'Zero fill left shift'.

<p>The &lt;&lt; operator inserts the specified number of 0's (in this case 2) from the right and let the same amount of leftmost bits fall off: If you push 00 in from the left: 3 = 0000000000000011 Becomes 12 = 0000000000001100</p>
Signup and view all the answers

______ operators are used to test if a sequence is presented in an object.

<p>Membership</p>
Signup and view all the answers

Match the name to the bitwise operator.

<p>AND = &amp; OR = | XOR = ^ NOT = ~</p>
Signup and view all the answers

What will be the output of the following code snippet?

x = ['apple', 'banana'] y = ['apple', 'banana'] print(x is y)

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

The expression bool(0) will evaluate in True.

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

Based on operator precedence, what is the output of print(100 // 50 * 3)?

<p>6</p>
Signup and view all the answers

The Python operator that returns True if both variables are the same object is called ______.

<p>is</p>
Signup and view all the answers

Match each logical operator with their correct description.

<p>and = Returns True if both statements are true or = Returns True if one of the statements is true not = Reverse the result, returns False if the result is true</p>
Signup and view all the answers

What will the print() statements output?

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

The following code snippet will print("b is greater than a")

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

Provide an example of an expression using comparison operators

<p>x &gt; y</p>
Signup and view all the answers

Floor division ______ returns the division quotient

<p>//</p>
Signup and view all the answers

Match each arithmetic operator with their example

<ul> <li>= x + y</li> </ul> <ul> <li>= x - y</li> </ul> <ul> <li>= x * y / = x / y</li> </ul>
Signup and view all the answers

Which of the following code snippets will return True? (Select all that apply)

<p>All of the above (D)</p>
Signup and view all the answers

The following code snippet will return True 5 < 5 and 5 < 10

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

What will this expression output based on operator precedence? print(8 >> 4 - 2)

<p>2</p>
Signup and view all the answers

The ______ function allows you to evaluate any value, and give you True or False in return

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

Flashcards

Arithmetic Operators

Operators that carries out arithmetic operations on numeric values.

Addition Operator (+)

An operator that performs addition.

Subtraction Operator (-)

Operator that performs subtraction.

Multiplication Operator (*)

Operator that performs multiplication.

Signup and view all the flashcards

Division Operator (/)

Operator that performs division.

Signup and view all the flashcards

Modulus Operator (%)

Returns the remainder of a division.

Signup and view all the flashcards

Floor Division Operator (//)

Performs floor division, returning the integer quotient.

Signup and view all the flashcards

Exponentiation Operator (**)

Raises a number to a power.

Signup and view all the flashcards

Assignment Operators

Operators that assign values to variables.

Signup and view all the flashcards

Assignment Operator (=)

Assigns value from right to left.

Signup and view all the flashcards

Addition Assignment Operator (+=

Adds and assigns in one step.

Signup and view all the flashcards

Subtraction Assignment Operator (-=)

Subtracts and assigns in one step.

Signup and view all the flashcards

Comparison Operators

Compares values and returns True or False.

Signup and view all the flashcards

Equal Operator (==)

Checks if two values are equal.

Signup and view all the flashcards

Not Equal Operator (!=)

Checks if two values are not equal.

Signup and view all the flashcards

Greater Than Operator (>)

Checks if left value is greater than right.

Signup and view all the flashcards

Logical Operators

Checks if a condition is true or false.

Signup and view all the flashcards

AND Operator

Returns true if both conditions are true.

Signup and view all the flashcards

OR Operator

Returns true if one condition is true.

Signup and view all the flashcards

NOT Operator

Reverses the result.

Signup and view all the flashcards

Bitwise Operators

Used to compare binary numbers.

Signup and view all the flashcards

AND Bitwise Operator (&)

Sets each bit to 1 if both bits are 1.

Signup and view all the flashcards

OR Bitwise Operator (|)

Sets each bit to 1 if one of two bits is 1.

Signup and view all the flashcards

Identity Operators

Compares objects in memory.

Signup and view all the flashcards

IS Operator

Returns True if both refer to same object.

Signup and view all the flashcards

Membership Operators

Tests if sequence exists in an object.

Signup and view all the flashcards

Operator Precedence

Determines order of operation execution.

Signup and view all the flashcards

Booleans

True or False values.

Signup and view all the flashcards

bool() function

Returns the Boolean value of a value.

Signup and view all the flashcards

Division operator(/)

Division (/) divides numbers.

Signup and view all the flashcards

Study Notes

Python Operators: An Overview

  • Python operators are categorized into arithmetic, assignment, comparison, logical, bitwise, membership, and identity operators.

Arithmetic Operators

  • Arithmetic operators perform common mathematical operations on numeric values.
  • Addition (+): x + y
  • Subtraction (-): x - y
  • Multiplication (*): x * y
  • Division (/): x / y
  • Modulus (%): Returns the remainder of a division: x % y
  • Floor Division (//): Returns the integer part of the division: x // y
  • Exponentiation (**): x ** y

Assignment Operators

  • Assignment operators assign values to variables.
  • Assignment (=): x = y
  • Addition Assignment (+=): x += y is equivalent to x = x + y
  • Subtraction Assignment (-=): x -= y is equivalent to x = x - y
  • Multiplication Assignment (*=): x *= y is equivalent to x = x * y
  • Division Assignment (/=): x /= y is equivalent to x = x / y
  • Modulus Assignment (%=): x %= y is equivalent to x = x % y
  • Floor Division Assignment (//=): x //= y is equivalent to x = x // y
  • Exponentiation Assignment (**=): x **= y is equivalent to x = x ** y

Comparison Operators

  • Comparison operators determine equality or difference between variables in logical statements.
  • Equal (==): x == y
  • Not Equal (!=): x != y
  • Greater Than (>): x > y
  • Less Than (<): x < y
  • Greater Than or Equal To (>=): x >= y
  • Less Than or Equal To (<=): x <= y

Logical Operators

  • Logical operators determine logic between variables.
  • AND: Returns True if both statements are true: x < 5 and x < 10
  • OR: Returns True if one of the statements is true: x < 5 or x < 4
  • NOT: Reverses the result, returning False if the result is true: not(x < 5 and x < 10)

Bitwise Operators

  • Bitwise operators compare binary numbers and operate on 32-bit signed integers.
  • Python converts numbers to 32-bit signed integers before a bitwise operation.
  • The result is converted back to a Python number after the operation.
  • AND (&): Sets each bit to 1 if both bits are 1; 5 & 1 results in 1
  • OR (|): Sets each bit to 1 if one of two bits is 1; 5 | 1 results in 5
  • XOR (^): Sets each bit to 1 if only one of two bits is 1; 5 ^ 1 results in 4
  • NOT (~): Inverts all the bits; ~5 results in 10
  • Left Shift (<<): Shifts left by pushing zeros in from the right, letting the leftmost bits fall off; 5 << 1 results in 10
  • Right Shift (>>): Shifts right by pushing copies of the leftmost bit in from the left, letting the rightmost bits fall off; 5 >> 1 results in 2

Identity Operators

  • Identity operators compare the objects, not if they are equal, but if they are actually the same object with the same memory location.
  • is: Returns True if both variables are the same object: x is y
  • is not: Returns True if both variables are not the same object: x is not y

Membership Operators

  • Membership operators test if a sequence is presented in an object.
  • in: Returns True if a sequence with the specified value is present in the object: x in y
  • not in: Returns True if a sequence with the specified value is not present in the object: x not in y

Operator Precedence

  • Operator precedence dictates the order operations are performed and multiplication and division have higher precedence than addition and subtraction.
  • Parentheses have the highest precedence.

Python Booleans

  • Booleans represent either True or False.
  • Expressions in Python can be evaluated to True or False.
  • Almost any value is evaluated to True if it has content.
  • A string is True, except empty strings.
  • Any number is True, except 0.
  • Any list, tuple, set, and dictionary are True, except empty ones.

Python Exercises

  • Division operator (/) divides numbers.
  • Modulus operator (%) returns the division remainder.
  • Floor division (//) returns the division quotient.
  • Arithmetic assignment operators are a shorthand way of performing arithmetic operations and assigning the result to a variable in a single step.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Python Arithmetic Operators Quiz
10 questions
Python Arithmetic and Comparison Operators
17 questions
Python Operators
30 questions

Python Operators

BetterThanExpectedLearning9144 avatar
BetterThanExpectedLearning9144
Python: Arithmetic and Assignment Operators
27 questions

Python: Arithmetic and Assignment Operators

BetterThanExpectedLearning9144 avatar
BetterThanExpectedLearning9144
Use Quizgecko on...
Browser
Browser