Python Arithmetic and Comparison Operators
17 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the output type of the input() function, regardless of the input given?

  • List
  • Float
  • Integer
  • String (correct)
  • The floor division operator (//) returns a float type result.

    False

    What is the result of the expression 5 ** 2?

    25

    The operator '==' checks if two values are ______.

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

    Match the following operators with their functions:

    <p>== = Checks if values are equal != = Checks if values are not equal and = Logical AND between two conditions or = Logical OR between two conditions</p> Signup and view all the answers

    What will be the output of the expression 7 % 3?

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

    The operator 'not' returns True if the input is True.

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

    What does the bitwise AND operator (&) do?

    <p>Calculates the logical AND of each bit in two operands.</p> Signup and view all the answers

    What is the result of the expression a ^ b given that a = 5 (0101 in binary) and b = 3 (0011 in binary)?

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

    The bitwise OR operation will result in 1 only if both input bits are 0.

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

    What does the bitwise negation operator (~) do to a bit?

    <p>It flips the bit; 0 becomes 1 and 1 becomes 0.</p> Signup and view all the answers

    The operator used to check if a value is not present in a data structure is called __________.

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

    Which of the following expressions would produce a 'True' result when evaluating x is y?

    <p>x = a; y = b when a and b are assigned the same string</p> Signup and view all the answers

    The expression '1 in [1, 2, 3]' returns false.

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

    Describe operator precedence in Python.

    <p>It determines the order in which operators are evaluated in expressions.</p> Signup and view all the answers

    Match the following operators with their descriptions:

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

    The expression 'x >> n' performs a __________ right shift operation.

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

    Study Notes

    Input Function

    • The input() function takes user input as a string.
    • The input is always a string, even if the user enters a number.

    Arithmetic Operators

    • + (Unary Positive): Used to indicate a positive value; has no effect on the operand.
    • + (Binary Addition): Adds two operands together.
    • - (Unary Negation): Changes the sign of the operand (makes it negative).
    • - (Binary Subtraction): Subtracts the second operand from the first.
    • * (Multiplication): Multiplies two operands.
    • / (Division): Divides the first operand by the second. The result is always a float.
    • % (Modulo): Returns the remainder of a division.
    • // (Floor Division): Performs division and rounds the result down to the nearest integer.
    • ** (Exponentiation): Raises the first operand to the power of the second.

    Comparison Operators

    • == (Equal to): Checks if two operands are equal; returns True if they are, False otherwise.
    • != (Not Equal to): Checks if two operands are not equal; returns True if they are not equal, False otherwise.
    • < (Less than): Checks if the first operand is less than the second; returns True if it is, False otherwise.
    • > (Greater than): Checks if the first operand is greater than the second; returns True if it is, False otherwise.
    • <= (Less than or equal to): Checks if the first operand is less than or equal to the second; returns True if it is, False otherwise.
    • >= (Greater than or equal to): Checks if the first operand is greater than or equal to the second; returns True if it is, False otherwise.

    Logical Operators

    • not (Logical Negation): Inverts the truth value of an operand; returns True if the operand is False, and False if the operand is True.
    • or (Logical OR): Returns True if at least one of the operands is True, False otherwise.
    • and (Logical AND): Returns True if both operands are True, False otherwise.

    Bitwise Operators

    • & (Bitwise AND): Performs a bitwise AND operation on each corresponding bit of the operands; returns 1 if both bits are 1, 0 otherwise.
    • | (Bitwise OR): Performs a bitwise OR operation on each corresponding bit of the operands; returns 1 if at least one bit is 1, 0 otherwise.
    • ~ (Bitwise Negation): Flips the bits of an operand (0 becomes 1, 1 becomes 0).
    • ^ (Bitwise XOR): Performs a bitwise XOR operation on each corresponding bit of the operands; returns 1 if the bits are different, 0 if they are the same.
    • >> (Right Shift): Shifts all bits in the first operand to the right by the number of positions specified by the second operand.

    Membership Operators

    • in: Checks if a value exists within another data structure (string, list, tuple, dictionary). Returns True if it exists, False otherwise.
    • not in: Checks if a value doesn't exist within another data structure. Returns True if it doesn't exist, False otherwise.

    Operator Precedence

    • Operator precedence determines the order in which operators are evaluated within an expression.
    • Operators with higher precedence are evaluated before operators with lower precedence.
    • Parentheses can be used to override precedence.

    Augmented Assignment Operators

    • Augmented assignment operators combine an arithmetic or bitwise operation with an assignment in one step.
    • For example, x += 5 is equivalent to x = x + 5.

    Studying That Suits You

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

    Quiz Team

    Related Documents

    Description

    Explore the fundamental arithmetic and comparison operators in Python through this quiz. Learn how to use operators like addition, subtraction, and comparison to manipulate data and evaluate expressions. Test your knowledge and deepen your understanding of Python syntax and functionality.

    More Like This

    Arithmetic Operators Quiz
    5 questions

    Arithmetic Operators Quiz

    AgreeableDarmstadtium avatar
    AgreeableDarmstadtium
    Python Basics Quiz
    45 questions
    Arithmetic Operations in Python
    18 questions

    Arithmetic Operations in Python

    InestimablePrairieDog2213 avatar
    InestimablePrairieDog2213
    Use Quizgecko on...
    Browser
    Browser