FUTURE OF TANZANIA
  • Home
  • Python
    • Python I >
      • Python Install
      • Python Intro
      • Python Syntax
      • Python Comments
      • Python Variables
      • Python User Input
      • Python Data Types
      • Python Numbers
      • Python Casting
      • Python Strings
      • Python Booleans
      • Python Operators
    • Python II >
      • Python Lists
      • Python Tuples
      • Python Sets
      • Python Dictionaries
      • Python If Else
      • Python While Loops
      • Python For Loops
      • Python Functions
      • Python Arrays
      • Python Scope
      • Python Math
      • Python String Formatting
      • Python File Handling
      • Python Read Files
      • Python Write/Create Files
      • Python Delete Files
  • Machine Learning
  • AI Project
  • Student's Work
    • Water
    • Infrastructure
    • Well-being
    • Economy
  • RESOURCES
  • Picture

Python Operators


Operators​
Arithmetic Operators
​Assignment Operators
Comparison Operators
Logical Operators
Identity Operators

13_python_operators.pdf
File Size: 79 kb
File Type: pdf
Download File

This was referenced from w3schools.com.

Python Operators

Operators are used to perform operations on variables and values.
​
In the example below, we use the + operator to add together two values:
print(10 + 5)

Python divides the operators in the following groups:
  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators

Python Arithmetic Operators​
​

Arithmetic operators are used with numeric values to perform common
mathematical operations:
​Operator

​+

-
​
*

/

%

**
​
​//
​Name
​
Addition

Subtraction

Multiplication

Division

Modulus

Exponentiation

​​Floor Division
Example

x + y

x - y

​x * y

​x / y

​x % y

​x ** y

​​x // y

Python Assignment Operators

Assignment operators are used to assign values to variables:
​Operator

=

​+=

-=
​
*=

/=

%=

**=
​
​//=

&=

|=

^=

​>>=

​<<=
Example
​
x = 5

x += 3

x -= 3

x *= 3

x /= 3

x %= 3

x **= 3

​x //= 3

x &= 3

​x |= 3

x ^= 3

x >>= 3

​x <<= 3
Same As

x = 5

x = x + 3

x = x - 3

x = x * 3


x = x / 3

x = x % 3

x = x ** 3

​x = x // 3

x = x & 3

x = x | 3

x = x ^ 3

x = x >> 3

​x = x << 3

Python Comparison Operators​

Comparison operators are used to compare two values:
Operator

==

!=

>

<

>=

<=
Name

Equal

Not equal

Greater than

Less than

Greater than or equal to

​Less than or equal to
Example

x == y

x != y

x > y

x < y

x >= y

x <= y

Python Logical Operators

Logical operators are used to combine conditional statements:
Operator

and


or


​not
Description

Returns True if both
statements are true

Returns True if one of
the statements is true

​Returns True if one of
the statements is true
Example

x < 5 and x < 10


x < 5 or x < 4


not(x < 5 and x < 10)

Python Identity Operators

Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:
Operator

&

|

^

~

<<

>>
Name

AND

OR

XOR

NOT

Zero fill left shift

Signed right shift
Example

Sets each bit to 1 if both bits are 1

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

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

Inverts all the bits

Shift left by pushing zeros in from the right and let the leftmost bits fall off

Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off
All curriculum credit goes to w3schools.com
Python Booleans
Python Lists
Powered by Create your own unique website with customizable templates.
  • Home
  • Python
    • Python I >
      • Python Install
      • Python Intro
      • Python Syntax
      • Python Comments
      • Python Variables
      • Python User Input
      • Python Data Types
      • Python Numbers
      • Python Casting
      • Python Strings
      • Python Booleans
      • Python Operators
    • Python II >
      • Python Lists
      • Python Tuples
      • Python Sets
      • Python Dictionaries
      • Python If Else
      • Python While Loops
      • Python For Loops
      • Python Functions
      • Python Arrays
      • Python Scope
      • Python Math
      • Python String Formatting
      • Python File Handling
      • Python Read Files
      • Python Write/Create Files
      • Python Delete Files
  • Machine Learning
  • AI Project
  • Student's Work
    • Water
    • Infrastructure
    • Well-being
    • Economy
  • RESOURCES
  • Picture