Python Operators to Know for Intro to Python Programming

Python operators are essential tools for performing various tasks in programming. They allow you to do math, compare values, and manipulate data efficiently, making your code more powerful and easier to read. Understanding these operators is key to mastering Python.

  1. **Arithmetic operators (+, -, *, /, //, %, )

    • Used to perform basic mathematical operations such as addition, subtraction, multiplication, and division.
    • The division operator (/) returns a float, while the floor division operator (//) returns the largest integer less than or equal to the result.
    • The modulus operator (%) returns the remainder of a division operation.
    • The exponentiation operator (**) raises a number to the power of another number.
  2. **Assignment operators (=, +=, -=, *=, /=, %=, =)

    • Used to assign values to variables, with the basic assignment operator being (=).
    • Compound assignment operators (e.g., +=, -=) combine an arithmetic operation with assignment for more concise code.
    • These operators modify the value of a variable in place, which can improve code readability and efficiency.
  3. Comparison operators (==, !=, <, >, <=, >=)

    • Used to compare two values and return a Boolean result (True or False).
    • The equality operator (==) checks if two values are equal, while the inequality operator (!=) checks if they are not equal.
    • Relational operators (<, >, <=, >=) determine the order of values, allowing for comparisons of size or magnitude.
  4. Logical operators (and, or, not)

    • Used to combine or modify Boolean expressions.
    • The 'and' operator returns True if both operands are True; otherwise, it returns False.
    • The 'or' operator returns True if at least one operand is True; it returns False only if both are False.
    • The 'not' operator negates the Boolean value of its operand.
  5. Identity operators (is, is not)

    • Used to check if two variables refer to the same object in memory.
    • The 'is' operator returns True if both variables point to the same object, while 'is not' returns True if they do not.
  6. Membership operators (in, not in)

    • Used to test for membership within a collection (like lists, tuples, or strings).
    • The 'in' operator returns True if a value exists in the specified collection, while 'not in' returns True if it does not.
  7. Bitwise operators (&, |, ^, ~, <<, >>)

    • Operate on the binary representations of integers.
    • The '&' operator performs a bitwise AND, while '|' performs a bitwise OR.
    • The '^' operator performs a bitwise XOR (exclusive OR).
    • The '~' operator inverts the bits (bitwise NOT).
    • The '<<' operator shifts bits to the left (multiplying by powers of two), and '>>' shifts bits to the right (dividing by powers of two).


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.