Expression
An expression is defined as a combination of constants, variables, and operators.
An expression always evaluates to a value.
A value or a standalone variable is also considered as an expression but a standalone operator is not an expression.
Some examples of valid expressions are given below.
(i) 100 (iv) 3.0 + 3.14
(ii) num (v) 23/3 -5 * 7(14 -2)
(iii) num – 20.4 (vi) "Global" + "Citizen"
The types of operators used in expression determine the type of expression.
Arithmetic Expression: involve numbers and arithmetic operators
Relational Expression: Expression with relational operators
Logical Expression: Expression with logical operators
String Expression : involve string with + and *
Type Conversion
We can change the data type of a variable in Python from one type to another. Such data type conversion can happen in two ways:
Explicit Type conversion(Type Casting) :
In Explicit Type conversion, the user or programmer converts the data type of an object to the required data type. In Python we use predefined functions like int(), float(), str(), bool() etc to perform explicit type conversion.
Implicit Type conversion:
Performed by the compiler automatically without programmer's intervention
in a mixed arithmetic expression, Python converts all operands up to the type of the largest operand(type promotion)
# Program to illustrate Implicit type conversion
# creating addition() function to add two numbers
def addition(a, b):
print("Type of first number(a) :", a, type(a))
print("Type of second number(b) :", b, type(b))
c = a + b
print("Type of resulting variable(c) :", c, type(c))
# addition() function calls with different inputs
addition(21, 23) # both integers
print('\n')
addition(21, 23.0) # second being float
print('\n')
addition(21.0, 23) # first being float
print('\n')
addition(21.0, 23.0) # both float