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 of the following is the most accurate definition of an operator in Python?

  • A function that performs a specific task.
  • A data type that stores numerical values.
  • A symbol used to perform operations on variables and values. (correct)
  • A keyword used to define variables.

In Python, the terms 'operator' and 'operand' are interchangeable.

False (B)

In Python, what is the primary function of the assignment operator (=)?

assigns a value to a variable

The _ operator is used to return the remainder of a division operation.

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

Which group of operators in Python is specifically designed for performing mathematical computations?

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

The // operator in Python performs standard division, resulting in a floating-point number.

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

What is the purpose of the ** operator in Python?

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

The x ______= y statement is an example of an assignment operator, which is equivalent to writing x = x _ y.

<ul> <li></li> </ul> Signup and view all the answers

Which of the following assignment operators is used to perform floor division and assign the result?

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

The comparison operators in Python can only be used with numerical values.

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

What do comparison operators in Python primarily evaluate?

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

The _ operator returns True if two operands are not equal.

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

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

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

The not operator can only be used with boolean values.

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

What is the primary function of logical operators in Python?

<p>determine the logic between variables or values</p> Signup and view all the answers

The _ operator inverts the bits of a number.

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

Which bitwise operator performs a bitwise AND operation?

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

Bitwise operators in Python operate on floating-point numbers directly without conversion.

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

Describe the operation performed by the left shift bitwise operator (<<).

<p>shifts bits to the left</p> Signup and view all the answers

The ^ operator performs a bitwise _ operation.

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

In Python, what will print(5 & 1) output?

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

Python directly supports a bitwise NOR operator.

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

Explain how the right shift operator (>>) affects a binary number.

<p>shifts bits to the right</p> Signup and view all the answers

Given x = 5, the expression x << 1 will result in _, due to the left shift operation.

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

What would be the output of print(~5) in Python?

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

The modulus operator (%) can only be used with integer operands.

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

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

<p>division vs floor division</p> Signup and view all the answers

In the expression x and y, if x is False, the expression will evaluate to _ regardless of the value of y.

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

What is the outcome of the comparison 5 >= 5 in Python?

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

Unlike arithmetic operators, assignment operators do not return any value.

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

Flashcards

What is an Operator?

A symbol used to perform operations on variables and values in Python.

What are Operands?

Values or variables on which an operator acts.

What is the Assignment Operator (=)?

The assignment operator assigns a value to a variable.

What are Arithmetic Operators?

These operators perform mathematical calculations.

Signup and view all the flashcards

What are Assignment Operators?

These operators assign values to variables.

Signup and view all the flashcards

What are Groups of Python Operators?

Categories include: Arithmetic, Assignment, Comparison, Logical, Bitwise, Membership and Identity Operators.

Signup and view all the flashcards

What is the + operator?

Operator that performs addition.

Signup and view all the flashcards

What is the - operator?

Operator that performs subtraction.

Signup and view all the flashcards

What is the / operator?

Operator that divides two values

Signup and view all the flashcards

What does % (modulus) do?

The operator that returns the remainder of a division.

Signup and view all the flashcards

What is Floor Division (//)?

It performs division and returns the integer quotient.

Signup and view all the flashcards

What are Arithmetic Assignment Operators?

A way to shorthand arithmetic operations.

Signup and view all the flashcards

What are Comparison Operators?

Test for equality or difference between values.

Signup and view all the flashcards

What are Logical Operators?

Determine logic between variables/values.

Signup and view all the flashcards

What does the 'and' operator do?

Tests if both statements are true.

Signup and view all the flashcards

What does the 'or' operator do?

Returns true if one of the statements is true.

Signup and view all the flashcards

What does the 'not' operator do?

Reverses the result; true becomes false and vice versa.

Signup and view all the flashcards

What are Bitwise Operators?

They compare binary numbers.

Signup and view all the flashcards

What does the '&' (AND) bitwise operator do?

Sets each bit to 1 if both bits are 1.

Signup and view all the flashcards

What does the ‘|’ (OR) bitwise operator do?

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

Signup and view all the flashcards

What does the '<<' (left shift) operator do?

Shifts bits to the left, adding zeros from the right.

Signup and view all the flashcards

What does the '>>' (right shift) bitwise operator do?

Shifts bits to the right.

Signup and view all the flashcards

Study Notes

Python Operators Overview

  • Week 3, Lesson 1 introduces Python Operators.

Learning Objectives

  • The concept of Python Operators can be explained
  • Types of Python Operators can be utilized

Definition and Categories

  • An operator is a symbol that performs operations on variables and values.
  • In arithmetic operations, numbers are called operands.
  • An operator defines the operation performed between two operands.
  • Python operators are used to perform operations on variables and values
  • The '+' (Addition) operator adds two values ​​together.
  • The Assignment Operator (=) assigns a value to a variable.
  • Example: x = 5 assigns the value 5 to the variable x.

Groups of Python Operators:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators
  • Membership operators
  • Identity operators

Arithmetic Operators

  • Used with numeric values ​​for common mathematical operations.
  • Addition: x + y
  • Subtraction: x - y
  • Multiplication: x * y
  • Division: x / y
  • Modulus (Division Remainder): x % y
  • Floor division: x // y
  • Exponentiation: x ** y

Arithmetic Operations

  • Typical arithmetic operations operate on two numbers.
  • The code print(x + y) adds numbers; for x = 5 and y = 3, Result: is 8
  • The code print(x - y) subtracts numbers; for x = 5 and y = 3, the Result: is 2
  • The code print(x * y) multiplies numbers; for x = 5 and y = 3, the Result: is 15
  • The codeprint(x / y) divides numbers; for x = 5 and y = 3, the Result: is 1.6666…
  • The code print(x % y) returns the division remainder; for x = 5 and y = 3, the Result: is 2
  • The code print(x // y) returns the division quotient; for x = 5 and y = 3, the Result: is 1
  • The code print(x ** y) returns the exponent; for x = 5 and y = 3, the Result: is 125

Concept of Assignment Operators

  • Assignment operators assign values to variables
  • Assignment:x = y
  • Addition Assignment: x += y is the same as x = x + y
  • Subtraction Assignment: x -= y which is the same as x = x - y
  • Multiplication Assignment: x *= y which is the same as x = x * y
  • Division Assignment: x /= y which is the same as x = x / y
  • Modulus Assignment: x %= y which is the same as x = x % y
  • Floor division Assignment: x //= y which is the same as x = x // y
  • Exponentiation Assignment: x **= y which is the same as x = x ** y
  • Assigning AND: x &= 3 which is the same as x = x & 3
  • Assigning OR: x |= 3 which is the same as x = x | 3
  • Assigning XOR: x ^= 3 which is the same as x = x ^ 3
  • Shift right assignment: x >>= 3 which is the same as x = x >> 3
  • Shift left assignment: For example print(x := 3) will print(x) and set x = 3

Examples of Assignment Operators

  • Arithmetic assignment operators are a shorthand way of performing arithmetic operations and assigning the result in one step.
  • += and -= are the two most common arithmetic assignment operators
  • For x = 10, x += 5 results in x = 15
  • For x = 10, x -= 5 results in x = 5
  • For x = 5, x *= 5 results in x = 50
  • The division assignment (/=) operator performs division on the two operands and assigns the result to the left operand
  • For x = 20, x /= 2 results in x = 10.0
  • For x = 20,x %= 7 results in x = 3.0
  • For x = 20, x //= 2 results in x = 1.0

Comparison Operators

  • Comparison operators are used in logical statements to determine equality or difference between variables or values.
  • Comparison and logical operators are used to test for true or false.
  • 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
  • If x = 5 and y = 3, the code print(x > y) returns True
  • If x = 5 and y = 3, the code print(x <= y) returns False
  • If x = 5 and y = 3, the codeprint(x == y) returns False
  • If x = 5 and y = 3, the code print(x != y) returns True

Logical Operators

  • Logical operators determine the logic between variables or values.
  • and: Returns True if both statements are true
  • or: Returns True if one of the statements is true
  • not: Reverses the result; returns False if the result is true
  • With x = 5, the code print(x > 3 and x < 10) returns True because 5 is greater than 3 AND 5 is less than 10
  • With x = 5, the code print(x > 3 or x < 4) returns True because one of the conditions are true; 5 is greater than 3
  • With x = 5, the code print(not(x > 3 and x < 10)) returns False because not is used to reverse the result

Concept of Bitwise Operators

  • Bitwise operators compare (binary) numbers.
  • & (AND): Sets each bit to 1 if both bits are 1
  • | (OR): Sets each bit to 1 if one of two bits is 1
  • ^ (XOR): Sets each bit to 1 if only one of two bits is 1
  • ~ (NOT): Inverts all the bits
  • << (left shift): Shifts left by pushing zeros in from the right, letting the leftmost bits fall off
  • >> (right shift): Shifts right by pushing copies of the leftmost bit in from the left, letting the rightmost bits fall off
  • Binary and decimal results of bitwise operations
  • 5 & 1: binary 0101 & 0001= binary 0001 = decimal 1
  • 5 | 1: binary 0101 | 0001 = binary 0101 = decimal 5
  • 5 ^ 1: binary 0101 ^ 0001= binary 0100 = decimal 4
  • ~5: binary ~0101 = binary 1010 = decimal 10
  • 5 << 1: binary0101 << 1 = binary 1010 = decimal 10
  • 5 >> 1: binary0101 >> 1 = binary 0010 = decimal 2

Examples of Bitwise Operators

  • Bitwise AND sets each bit to 1 if both bits are 1.
    • For print( 0 & 1 ) the Result: is 0
  • Bitwise OR sets each bit to 1 if one of two bits is 1
  • For print( 0 | 1 ) the Result: is 1
  • Bitwise Left Shift shifts left by pushing zeros in from the right
  • For x = 5, print( x << 1) results in 10
    • x's binary number '0101' becomes '1010' which equals 5 * 2
  • Bitwise Right Shift shifts right by pushing copies of the leftmost bit in from the left
  • For y = 8, print( y >> 1) results in 4
    • y's binary number ‘1000' becomes ‘0100' which is equal to 8 / 2

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 Precedence Quiz
16 questions
Python Arithmetic and Comparison Operators
17 questions
Python Operators: Arithmetic and Assignment
29 questions

Python Operators: Arithmetic and Assignment

BetterThanExpectedLearning9144 avatar
BetterThanExpectedLearning9144
Python: Arithmetic and Assignment Operators
27 questions

Python: Arithmetic and Assignment Operators

BetterThanExpectedLearning9144 avatar
BetterThanExpectedLearning9144
Use Quizgecko on...
Browser
Browser