An operator is used to perform specific mathematical or logical operation on values.
Operand
For example,
>>> 10 + num,
Arithmetic Operator
Assignment Operator
Relational Operator
Logical Operator
Bitwise Operator
Note: bin( ) returns binary representation number.
Membership Operator
Identity Operator
Operator Precedence :
Execution of operator depends on the precedence of operator.
In Python, An expression is a valid combination of operators, literals and variables.
There are three types of Expression in Python, These are-
The conversion of one type to specific type explicitly, called Type Casting / Explicit Type Conversion.
With explicit type conversion, there is a risk of loss of information since we are forcing an expression to be of a specific type.
Syntax: <type>( )
Example x = int ( “123”)
y = float(“5369.36”)
z = str(598698.36)
Functions in Python that are used for explicitly converting an expression or a variable to a different types are –
Program : Explicit type conversion from int to float
num1 = 10
num2 = 20
num3 = num1 + num2
print(num3)
print(type(num3))
num4 = float(num1 + num2)
print(num4)
print(type(num4))
Output:
30
<class 'int'>
30.0
<class 'float'>
Program : Explicit type conversion from float to int
num1 = 10.2
num2 = 20.6
num3 = (num1 + num2)
print(num3)
print(type(num3))
num4 = int(num1 + num2)
print(num4)
print(type(num4))
Output:
<class 'float'>
30.8
<class 'int'>
30
Implicit conversion, also known as coercion, happens when data type conversion is done automatically by Python and is not instructed by the programmer.
Program to show implicit conversion from int to float.
num1 = 10 # num1 is an integer
num2 = 20.0 # num2 is a float
sum1 = num1 + num2
# sum1 is sum of a float and an integer
print(sum1)
print(type(sum1))
Output:
30.0
<class 'float'>
Q. Why was the float value not converted to an integer instead?
This is due to type promotion that allows performing operations (whenever possible) by converting data into a wider-sized data type without any loss of information.
Errors occurring in programs can be categorised as:
1. Syntax Errors:
2. Logical Errors
3. Runtime Error
20 videos|20 docs|5 tests
|
|
Explore Courses for Humanities/Arts exam
|