Python Operators
Python Operators
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
Arithmetic operators are used with numeric values to perform common mathematical
operations:
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
== Equal x == y
!= Not equal x != y
not Reverse the result, returns False not(x < 5 and x < 10)
if the result is true
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:
>> Signed right shift Shift right by pushing copies of the leftmost bit in x >> 2
from the left, and let the rightmost bits fall off
Operator Precedence
Example
Parentheses has the highest precedence, meaning that expressions inside parentheses must be
evaluated first:
print((8 + 3) - (6 + 3))
2
The precedence order is described in the table below, starting with the highest precedence at the
top:
Operator Description
() Parentheses
^ Bitwise XOR
| Bitwise OR
== != > >= < <= is is not in not Comparisons, identity, and membership operators
in
And AND
Or OR
If two operators have the same precedence, the expression is evaluated from left to right.
Example
Addition + and subtraction - has the same precedence, and therefor we evaluate the expression
from left to right:
print(5 + 4 - 7 + 3)
You can get the data type of any object by using the type() function:
Example
>>>x = 5
>>>print(type(x))
Output
class<int>
In Python, the data type is set when you assign a value to a variable:
If you want to specify the data type, you can use the following constructor functions:
x = bool(5) bool
x = bytes(5) bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
x = None NoneType
There may be times when you want to specify a type on to a variable. This can be done with
casting. Python is an object-orientated language, and as such it uses classes to define data types,
including its primitive types.
int() - constructs an integer number from an integer literal, a float literal (by removing all
decimals), or a string literal (providing the string represents a whole number)
float() - constructs a float number from an integer literal, a float literal or a string literal
(providing the string represents a float or an integer)
str() - constructs a string from a wide variety of data types, including strings, integer
literals and float literals
Example
Integers:
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
Floats:
Strings:
Python allows for user input. That means we are able to ask the user for input. The method is a
bit different in Python 3.6 than Python 2.7.
The following example asks for the username, and when you entered the username, it gets
printed on the screen:
Example
username = input("Enter username:")
print("Username is: " + username)
Output
Enter username:Arun
Username is: Arun
Python stops executing when it comes to the input() function, and continues when the user has
given some input.
PYTHON OUTPUT
Output Variables
Example
x = "Python is awesome"
print(x)
Output
Python is awesome
Example
x = "Python"
y = "is"
z = "awesome"
print(x, y, z)
Output
Python is awesome
Example
x = "Python "
y = "is "
Notice the space character after "Python " and "is ", without them the result would be
"Pythonisawesome".
Example
x=5
y = 10
print(x + y)
Output
15
In the print() function, when you try to combine a string and a number with
the + operator, Python will give you an error:
Example
x=5
y = "John"
print(x + y)
Output
TypeError: unsupported operand type(s) for +: 'int' and 'str'
The best way to output multiple variables in the print() function is to separate them with
commas, which even support different data types:
Example
x=5
y = "John"
print(x, y)
Output
5 John