0% found this document useful (0 votes)
20 views

Operators (Cheatsheet)

Python cheatsheet

Uploaded by

jankoviid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Operators (Cheatsheet)

Python cheatsheet

Uploaded by

jankoviid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Python: Operators

Python Operators Contents

Review arithmetic, comparison, 1: Arithmetic operators


assignment, and other Python operators 4: Comparison operators
with this short e-book! 5: Assignment operators

6: Logic operators

7: Identity operators

8: Membership operators

Addition
Can be used to add numbers (integers or floats) or join (concatenate) strings.

You cannot add strings and numbers together!

Subtraction
Can be used mathematically, between integers and floats:

The Complete Python Course on Udemy Page 1 of 8


Python: Operators

You can also subtract sets to return elements of set_1 that aren’t in set_2 :

Multiplication
Has the typical arithmetic use.

Multiplying most data types usually


duplicates their contents.

Division
Can be true division, which always results in a float.

Or floor division, which always rounds down to the nearest whole number.

The Complete Python Course on Udemy Page 2 of 8


Python: Operators

Modulo
Based on Euclidean division (see ref #2 below)
Start with a dividend and a divisor
Used to get the remainder of the division between both operands.

Exponentiation
Raises the first operand to the power of the second operand.

Extra Resources
How do mathematics work in Python
Python’s modulo operator and floor divisio
Python’s divmod function

The Complete Python Course on Udemy Page 3 of 8


Python: Operators

Comparison operators
Equal
Used to compare two values. They can be numbers, strings, or
anything else.

Not equal
The opposite of == .

Greater than

Less than

Greater than or equal to

Less than or equal to

The Complete Python Course on Udemy Page 4 of 8


Python: Operators

Assignment operators
Operator Example Explanation
= var = 2 Makes var equal to the value 2.
+= var += 2 Adds 2 to var.
-= var -= 2 Subtracts 2 from var.
*= var *= 2 Multiplies var by 2.
**= var **= 2 Elevates var to the power of 2.
/= var /= 2 Divides var by 2.
//= var //= 2 Divides var by 2 using floor division.
%= var %= 2 Calculates the remainder of var / 2.

Extra Resources
Conditionals and Booleans

The Complete Python Course on Udemy Page 5 of 8


Python: Operators

Logic Operators
And
Returns the first value if it evaluates to False , otherwise it returns
the second value.
Statement Result
True and True True
True and False False
False and False False
False and True False

Or
Returns the first value if it evaluates to True , otherwise it returns
the second value.
Statement Result
True or True True
True or False True
False or False False
False or True True

Not
Returns True if the operand evaluates to False , or False if it
evaluates to True .
Statement Result
not True False
not False True

The Complete Python Course on Udemy Page 6 of 8


Python: Operators

Identity operators
is
Returns True if the operands have the same identity (i.e. are
exactly the same value).

Above, users_copy is users returns False because although the


two lists have the same values inside them, they are not the same list.

is not
Returns True if the operands don’t have the same identity.

Extra Resources
Logical comparisons in Python: and & or

The Complete Python Course on Udemy Page 7 of 8


Python: Operators

Membership operators
in
Returns True if an element exists inside an object.

not in
Returns True if an element does not exist inside an object.

The Complete Python Course on Udemy Page 8 of 8

You might also like