Podcast
Questions and Answers
What is the purpose of the multiplication operator (*
) in Python?
What is the purpose of the multiplication operator (*
) in Python?
What result does the modulus operator (%
) return?
What result does the modulus operator (%
) return?
Which operator would you use to check if two operands are not equal?
Which operator would you use to check if two operands are not equal?
What is the effect of the floor division operator (//
)?
What is the effect of the floor division operator (//
)?
Signup and view all the answers
Which operator checks if the left operand is greater than or equal to the right operand?
Which operator checks if the left operand is greater than or equal to the right operand?
Signup and view all the answers
Study Notes
Python Operators
1. Arithmetic Operators
-
Addition (
+
): Adds two operands.
Example:a + b
-
Subtraction (
-
): Subtracts the second operand from the first.
Example:a - b
-
Multiplication (
*
): Multiplies two operands.
Example:a * b
-
Division (
/
): Divides the first operand by the second (returns float).
Example:a / b
-
Floor Division (
//
): Divides and returns the largest whole number.
Example:a // b
-
Modulus (
%
): Returns the remainder of division.
Example:a % b
-
Exponentiation (
**
): Raises the first operand to the power of the second.
Example:a ** b
2. Comparison Operators
-
Equal to (
==
): Checks if two operands are equal.
Example:a == b
-
Not equal to (
!=
): Checks if two operands are not equal.
Example:a != b
-
Greater than (
>
): Checks if the left operand is greater than the right.
Example:a > b
-
Less than (
<
): Checks if the left operand is less than the right.
Example:a < b
-
Greater than or equal to (
>=
): Checks if the left operand is greater than or equal to the right.
Example:a >= b
-
Less than or equal to (
<=
): Checks if the left operand is less than or equal to the right.
Example:a <= b
3. Logical Operators
-
AND (
and
): Returns True if both operands are True.
Example:a and b
-
OR (
or
): Returns True if at least one operand is True.
Example:a or b
-
NOT (
not
): Returns True if the operand is False (negates the operand).
Example:not a
4. Bitwise Operators
-
AND (
&
): Performs bitwise AND operation.
Example:a & b
-
OR (
|
): Performs bitwise OR operation.
Example:a | b
-
XOR (
^
): Performs bitwise XOR operation.
Example:a ^ b
-
NOT (
~
): Performs bitwise NOT operation (inverts bits).
Example:~a
-
Left Shift (
<<
): Shifts bits to the left, filling with zeros.
Example:a << 1
-
Right Shift (
>>
): Shifts bits to the right.
Example:a >> 1
5. Assignment Operators
-
Assign (
=
): Assigns the value on the right to the variable on the left.
Example:a = 5
-
Add and assign (
+=
): Adds right operand to left operand and assigns to left.
Example:a += b
-
Subtract and assign (
-=
): Subtracts right operand from left operand and assigns to left.
Example:a -= b
-
Multiply and assign (
*=
): Multiplies right operand with left operand and assigns to left.
Example:a *= b
-
Divide and assign (
/=
): Divides left operand by right and assigns to left.
Example:a /= b
-
Floor divide and assign (
//=
): Performs floor division and assigns to left.
Example:a //= b
-
Modulus and assign (
%=
): Applies modulus and assigns to left.
Example:a %= b
-
Exponent and assign (
**=
): Raises left operand to the power of right and assigns to left.
Example:a **= b
-
Bitwise AND and assign (
&=
): Performs bitwise AND and assigns to left.
Example:a &= b
-
Bitwise OR and assign (
|=
): Performs bitwise OR and assigns to left.
Example:a |= b
-
Bitwise XOR and assign (
^=
): Performs bitwise XOR and assigns to left.
Example:a ^= b
-
Left shift and assign (
<<=
): Shifts bits left and assigns to left.
Example:a <<= 1
-
Right shift and assign (
>>=
): Shifts bits right and assigns to left.
Example:a >>= 1
6. Identity Operators
-
Is (
is
): Checks if two operands refer to the same object.
Example:a is b
-
Is not (
is not
): Checks if two operands do not refer to the same object.
Example:a is not b
7. Membership Operators
-
In (
in
): Checks if a value exists in a sequence (like a list, tuple, or string).
Example:a in list
-
Not in (
not in
): Checks if a value does not exist in a sequence.
Example:a not in list
Arithmetic Operators
- Addition (
+
) performs the sum of two operands, e.g.,a + b
. - Subtraction (
-
) computes the difference by subtracting the second operand from the first, e.g.,a - b
. - Multiplication (
*
) yields the product of two operands, e.g.,a * b
. - Division (
/
) divides the first operand by the second and returns a floating-point result, e.g.,a / b
. - Floor Division (
//
) divides and returns only the integer part of the quotient, discarding the fractional part, e.g.,a // b
. - Modulus (
%
) calculates the remainder after division, e.g.,a % b
. - Exponentiation (
**
) raises the first operand to the power specified by the second operand, e.g.,a ** b
.
Comparison Operators
- Equal to (
==
) checks for equality between two operands. It evaluates to true if they are equal, e.g.,a == b
. - Not equal to (
!=
) evaluates whether two operands are different, returning true if they are not equal, e.g.,a != b
. - Greater than (
>
) checks if the left operand exceeds the right operand, returning true if it does, e.g.,a > b
. - Less than (
<
) assesses if the left operand is smaller than the right either, returning true when it is, e.g.,a < b
. - Greater than or equal to (
>=
) checks if the left operand is greater than or equal to the right one, returning true if conditions are met, e.g.,a >= b
. - Less than or equal to (
<=
) verifies if the left operand is less than or equal to the right, returning true on satisfaction, e.g.,a <= b
.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
This quiz covers the essential operators in Python, focusing on both arithmetic and comparison types. Participants will learn about various operator functions through examples and understand their usage in programming. Test your knowledge and enhance your Python skills with this convenient guide.