Python 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

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?

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

  • **
  • % (correct)
  • /
  • //

Assignment operators are used to perform mathematical calculations directly on values.

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

Which assignment operator is equivalent to 'x = x * y'?

<p>x *= y (B)</p> Signup and view all the answers

What is the primary use of comparison operators in Python?

<p>To determine equality or difference between variables (C)</p> Signup and view all the answers

The operator != in Python means ______.

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

Logical operators in Python are used to perform calculations on numeric values.

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

Which logical operator returns True only if both statements are True?

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

What is the main function of bitwise operators?

<p>Comparing binary numbers (B)</p> Signup and view all the answers

Before a bitwise operation is performed, what conversion does Python apply to numbers?

<p>Python converts numbers to 32-bit signed integers.</p> Signup and view all the answers

Match the bitwise operator with its description:

<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 do identity operators in Python compare?

<p>If the objects are the same, with the same memory location (A)</p> Signup and view all the answers

The identity operator is not returns True if both variables ______ the same object.

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

If two lists have the same elements in the same order, the 'is' operator will always return True when comparing them.

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

What is the purpose of membership operators in Python?

<p>To test if a sequence is present in an object (B)</p> Signup and view all the answers

Which operator checks if a sequence is NOT present in an object?

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

The 'in' operator returns True if an element with the specified value is present in the object being checked.

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

What does operator precedence describe?

<p>The order in which operations are performed (C)</p> Signup and view all the answers

According to operator precedence, ______ and division have higher precedence than addition and subtraction.

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

What happens when there are operations with the same precedence?

<p>Operations with the same precedence are computed from left to right.</p> Signup and view all the answers

Which of the following operators has the highest precedence?

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

Booleans in Python represent one of three values: True, False, or Maybe.

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

In Python, what is the result of comparing two values using comparison operators?

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

When a condition is run in an if conditional statment, Python returns either ______ or False.

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

What is the purpose of the bool() function in Python?

<p>To evaluate a value and give you True or False in return (D)</p> Signup and view all the answers

In Python, bool(0) will return True.

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

Which of the following values will return False when passed to the bool() function?

<p>&quot;&quot; (B)</p> Signup and view all the answers

Floor division (//) returns what part of the result?

<p>Floor division returns the division quotient (without any remainder).</p> Signup and view all the answers

What does the expression x += 5 do?

<p>Adds 5 to x and updates x (C)</p> Signup and view all the answers

The following two python expressions will result in the same boolean True or False output. x > y is the same as y < x

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

If x = 5, which of the following expressions will return False?

<p>not(x &gt; 3 and x &lt; 10) (B)</p> Signup and view all the answers

The << operator inserts the specified number of 0's from the ______ and let the same amount of leftmost bits fall off.

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

If operator precedence dictates that exponentiation is done before subtraction, what is the output of print(100 - 3 ** 3)?

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

Match the type of expression to the operator used:

<p>Arithmetic = +, -, *, /, %, //, ** Comparison = ==, !=, &gt;, &lt;, &gt;=, &lt;= Logical = and, or, not Assignment = =, +=, -=, *=, /=, %=, //=, **=</p> Signup and view all the answers

Given x = ["apple", "banana"], which expression evaluates to True?

<p><code>&quot;banana&quot; in x</code> (B)</p> Signup and view all the answers

How are operations with the same precedence computed in an expression, such as 100/50*3?

<p>Operations with the same precedence are computed from left to right, so 100/50*3 will first compute 100/50 and then the result will be multiplied by 3.</p> Signup and view all the answers

The bitwise operator ~ (NOT) inverts all the bits of a number.

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

What is the value of bool("abc")?

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

Assume these assignments are made: x = ["apple", "banana"], y = ["apple", "banana"]. Then x == y is ______ and x is y is ______.

<p>true, false</p> Signup and view all the answers

Flashcards

Arithmetic Operators

Operators used with numeric values for common math operations.

Assignment Operators

Assigns values to variables.

Comparison Operators

Determine equality or difference of variables/values.

Logical Operators

Determine the logic (true/false) between variables or values.

Signup and view all the flashcards

Bitwise Operators

Compare binary numbers.

Signup and view all the flashcards

Identity Operators

Compare if objects are the same (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

The order in which operations are performed

Signup and view all the flashcards

Booleans

Represents True or False.

Signup and view all the flashcards

Most Values are True

Any value is evaluated to True if it has content.

Signup and view all the flashcards

Division Operator (/)

Divides two numbers.

Signup and view all the flashcards

Modulus Operator (%)

Returns the remainder.

Signup and view all the flashcards

Floor Division Operator (//)

Returns the quotient

Signup and view all the flashcards

Arithmetic Assignment Operators

Combines arithmetic and assignment in one step (+=

Signup and view all the flashcards

Zero fill left shift

Inserts specified zeros on the right

Signup and view all the flashcards

Right shift

Moves each binary bit the specified amount to the right

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 as x = x + y.
  • Subtraction Assignment (-=) subtracts and assigns: x -= y is the same as x = x - y.
  • Multiplication Assignment (*=) multiplies and assigns: x *= y is the same as x = x * y.
  • Division Assignment (/=) divides and assigns: x /= y is the same as x = x / y.
  • Modulus Assignment (%=) gets modulus and assigns: x %= y is the same as x = x % y.
  • Floor Division Assignment (//=) performs floor division and assigns: x //= y is the same as x = x // y.
  • Exponentiation Assignment (**=) raises to a power and assigns: x **= y is the same as x = x ** y.

Python Comparison Operators

  • Used in logical statements to determine equality or difference.
  • Comparison and logical operators return True or False.
  • 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 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).

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 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.
  • Example:
    • x = ["apple", "banana"]
    • y = ["apple", "banana"]
    • z = x
    • print(x is z) returns True because z is the same object as x.
    • print(x is y) returns False because x is not the same object as y, even if they have the same content.
    • print(x == y) returns True because x is equal to y.
    • print(x is not z) returns False because z is the same object as x.
    • print(x is not y) returns True because x is not the same object as y.
    • print(x != y) returns False because x is equal to y.

Python Membership Operators

  • Used to test if a sequence is present 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.
  • Example:
    • x = ["apple", "banana"]
    • print("banana" in x) returns True because a sequence with the value "banana" is in the list.
    • print("pineapple" not in x) returns True 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 have the highest precedence, and need to be evaluated first: print((6 + 3) - (6 + 3)) is equal to 9 - 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 to print(8 >> 2) = 2.

Python Booleans

  • Booleans represent one of two values: True or False.
  • In programming, it's often necessary to know if an expression is True or False.
  • 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 returns True or False.
  • The bool() function evaluates any value and returns True or False.
  • 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, tuple, set, and dictionary are True, except empty ones.
  • Example:
    • bool("abc") returns True.
    • bool(123) returns True.
    • bool(["apple", "cherry", "banana"]) returns True.
    • 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) gives 4.0.
    • print(x % y) gives 0.
    • print(x // y) gives 4.
  • 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) returns True because 5 is greater than 3.
    • print(x= 3 and x < 10) returns True because 5 is greater than 3 and less than 10.
    • print(x > 3 or x < 4) returns True because one of the conditions is met (5 is greater than 3).
    • print(not(x > 3 and x < 10)) returns False because not 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 becomes 12 = 0000000000001100.
  • Exponentiation has higher precedence than subtraction, evaluated first.
    • print(100 - 3 ** 3) is equal to print(100 - 27) giving 73.
  • Bitwise right shift has a lower precedence than subtraction, requires completing subtraction first.
    • print(8 >> 4 - 2) translates to print(8 >> 2), meaning 2.
    • The >> operator moves each bit the specified number of times to the right, filling empty holes at the left with 0's. 8 = 0000000000001000 becomes 2 = 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.

Quiz Team

Related Documents

More Like This

Python Arithmetic and Comparison Operators
17 questions
Introduction to Python Programming
45 questions
Python: Operators, Truthiness, Exceptions & more
24 questions
Python Operators
30 questions

Python Operators

BetterThanExpectedLearning9144 avatar
BetterThanExpectedLearning9144
Use Quizgecko on...
Browser
Browser