Expressions in Python
What is Expression in Python?
The expression contains more than one operator as well as the operands with it. Expression helps us to produce some other values. In the Python programming language, expressions produce some other value and result after being interpreted with the help of a Python interpreter.
Let’s suppose, we have a=a+20. In this expression 20 is a value that will be added to the variable a. After the addition, we will get the result and it will be assigned to the variable a.
Example:
a = 25 # a statement
a = a + 20 # an expression
print(a)
Output:
45
An expression in Python can contain identifiers, operands, and operators.
What are Identifiers?
An identifier is a specific name used to identify the class or variables. The name that we give to the variable is known as an identifier.
What are Operands?
Operands are known as the objects of the operator in Python. On the other hand, the operator is a type of symbol that performs the arithmetic or logical operation on the operands.
Some Python operators are as follows:
Example: +, -, *, / etc.
Types of expressions in Python
In Python language, we have different types of expression. Some of them are as follows:
1. Constant expressions
A constant expression is a type of expression in Python that contains only values. In Python, the constant expression operator is always constant. The constant cannot be changed after the initialization of the value.
Example:
q = 456 + 9
# In this program 456 and 9 are constants.
print("The q is: ", q)
Output:
The q is: 465
2. Arithmetic expression
Arithmetic expressions are used to perform the mathematical problems with numeric values. The arithmetic expressions contain addition, multiplication, division, and subtraction in it.
(+) Operator
With the help of this operator, we can add two or more than two numbers.
Example:
#In this program we will show the working of the + operator
#taking the user input
p=int(input("Enter the p: "))
b=int(input("Enter the b: "))
# + operator functionality
add = p+b;
#printing the output
print("The sum is: ",add)
Output:
Enter the p: 25
Enter the b: 15
The sum is: 40
Explanation:
Here, we have initialized the two variables p and b. In those variables, we have taken the data from the user. Then we have created another variable to add them. Here, we have used the + operator so that we can add these two variables. Then we have printed the result.
(-) Operator
Using this operator, we can do the subtraction of the data. When we have two or more variables then we can do the subtraction with the help of the – operator.
Example:
#In this program, we will show the working of the - operator
#taking the user input
p=int(input("Enter the p: "))
b=int(input("Enter the b: "))
# - operator functionality
r = p-b;
#printing the output
print("The sub is: ",r)
Output:
Enter the p: 25
Enter the b: 15
The sub is: 10
Explanation:
Here, we have initialized the two variables p and b. In those variables, we have taken the data from the user. Then we have created another variable which is r. In that variable, we have used the (-) operator to do the subtraction of the two variables. Then we have printed the output.
(*) Operator:
With the help of this operator, we can do the multiplication of the data. When we have two or more variables then we can do the multiplication with the help of the * operator.
Example:
#In this program, we will show the working of the * operator
#taking the user input
p=int(input("Enter the p: "))
b=int(input("Enter the b: "))
# * operator functionality
r = p*b;
#printing the output
print("The multiplication is: ",r)
Output:
Enter the p: 25
Enter the b: 15
The multiplication is: 375
Explanation -:
Here, we have initialized the two variables p and b. In those variables, we have taken the data from the user. Then we have created another variable which is r. In that variable, we have used the (*) operator so that we can do the multiplication of the two variables. Then we have printed the output in the r variable.
(/) Operator:
This operator helps us to divide the two numbers. We can say that with the help of this operator, we can divide two or more than two numbers.
Example:
#In this program we will show the working of the ‘/ ’ operator
#taking the user input
p=int(input("Enter the p: "))
b=int(input("Enter the b: "))
# / operator functionality
r = p/b;
#printing the output
print("The division is: ",r)
Output:
Enter the p: 25
Enter the b: 15
The division is: 1.6666666666666667
Explanation:
Here, we have initialized the two variables p and b. In those variables we have taken the data from the user. Then we have created another variable which is r. In that variable, we have used the (/) operator so that we can divide the two variables. Then we have printed the output in the r variable.
(%) Operator
This operator is also known as the modulus operator. This operator works to return the remainder of that particular division.
Example:
#In this program, we will show the working of the % operator
#taking the user input
p=int(input("Enter the p: "))
b=int(input("Enter the b: "))
# % operator functionality
r = p%b;
#printing the output
print("The remainder is: ",r)
Output:
Enter the p: 25
Enter the b: 15
The remainder is: 10
Explanation:
Here, we have initialized the two variables p and b. In those variables, we have taken the data from the user. Then we have created another variable which is r. In that variable, we have used the (%) operator so that we can find the modulus. Then we have printed the data which is in the r variable.
(//) Operator
This operator is known as the quotient operator means with the help of this operator we can find the quotient. This is not a widely used operator.
Example:
#In this program, we will show the working of the // operator
#taking the user input
p=int(input("Enter the p: "))
b=int(input("Enter the b: "))
# // operator functionality
r = p//b;
#printing the output
print("The quotient is: ",r)
Output:
Enter the p: 25
Enter the b: 15
The quotient is: 1
Explanation:
Here, we have initialized the two variables p and b. In those variables, we have taken the data from the user. Then we have created another variable which is r. In that variable, we have used the (//) operator so that we can find the quotient. Then we have printed the data which is in the r variable.
(**) Operator
This operator is used to find the exponent of the number.
Example:
#In this program, we will show the working of the // operator
#taking the user input
p=int(input("Enter the p: "))
b=int(input("Enter the b: "))
# ** operator functionality
r = p**b;
#printing the output
print(p,"to the power",b," is: " ,r)
Output:
Enter the p: 5
Enter the b: 6
5 to the power 6 is: 15625
Explanation:
Here, we have initialized the two variables p and b. In those variables, we have taken the data from the user. Then we have created another variable which is r. In that variable, we have used the (**) operator so that we can find the exponent of the number. Then we have printed the data which is in the r variable.
All Operators Example:
#In this program, we will show the working of all operators
#taking the user input
p=int(input("Enter the p: "))
b=int(input("Enter the b: "))
# ( + ) operator program
add = p+b
# ( - ) operator program
sub = p-b
# ( / ) operator program
div = p/b
# ( * ) operator program
mul = p*b
# ( // ) operator program
que = p//b
# ( % ) operator program
mod = p%b
# ** operator functionality
exp = p**b;
#printing the output
print("Add is: ",add)
print("Sub is: ",sub)
print("Div is: ",div)
print("Mul is: ",mul)
print("Que is: ",que)
print("Rem is: ",mod)
print(p,"to the power",b,"is: " ,exp)
Output:
Enter the p: 5
Enter the b: 4
Add is: 9
Sub is: 1
Div is: 1.25
Mul is: 20
Que is: 1
Rem is: 1
5 to the power 4 is: 625
3. Integral expressions
Integral expressions are used to produce the integer result after the implementation of all the automatic and explicit type conversions. In other words, it is used for computation and type conversion that always produces an integer value as a result.
Example:
#In this program, we will show the working of the integral expression
#taking the user input
#integer value
p=int(input("Enter the p: "))
#Float value
b=float(input("Enter the b: "))
# integral expression functionality
r = p+int(b);
#printing the output
print("Add -: " ,r)
#printing the datatype of the answer
print(type(r))
Output:
Enter the p: 15
Enter the b: 21.4
Add -: 36
<class 'int'>
Explanation:
Here, we have initialized the two variables p and b. In those variables, we have taken the data from the user. But first, we have taken the integer value in the variable p and then we have taken the float value which we have stored in the b. Then we have created another variable which is r. In that variable, we have used the integral expression so that we can convert the float number into the integer number. Then we have printed the data which is in the r variable. Also, we have printed the type of the data which is in the r variable.
4. Floating expressions
Floating expressions are used to produce the float value result after the implementation of all the automatic and explicit type conversions. In other words, it is used for computation and type conversion that always produces a float value as a result in Python.
Program -:
#In this program, we will show the working of the floating expression
#taking the user input
#integer value
p=int(input("Enter the p: "))
#Float value
b=float(input("Enter the b: "))
# integral expression functionality
r = float(p)+b;
#printing the output
print("Add: " ,r)
#printing the datatype of the answer
print(type(r))
Output:
Enter the p: 25
Enter the b: 12.6
Add: 37.6
<class 'float'>
Explanation:
Here, we have initialized the two variables p and b. In those variables, we have taken the data from the user. But first, we have taken the integer value in the variable p and then we have taken the float value which we have stored in the b. Then we have created another variable which is r. In that variable, we have used the integral expression so that we can convert the integer number into the float number. Then we have printed the data which is in the r variable. Also, we have printed the type of the data which is in the r variable.
5. Relational Expression
In Python, a relational expression is a combination of more than two arithmetic expressions joined using the relational operation. It specifies if the overall expression result is either true or false.
In Python, we have the following types of relational expressions:
>, >=, <=, <,= ,!=
Greater than Relational Expression
Using this relational expression, we can find the greatest number from one or more numbers.
Example:
#In this program, we will show the working of the greater than relational expression
#taking the user input
p=int(input("Enter the p: "))
b=int(input("Enter the b: "))
# greater than relational expression functionality
if p>b:
print(p," is large")
else:
print(b,"is large")
Output:
Enter the p: 15
Enter the b: 36
36 is large
Explanation:
Here, we have initialized the two variables p and b. In those variables, we have taken the data from the user. Then we have to use the “if” condition in that we have given the condition that p is greater than b then print p is large. Otherwise print the b is large.
Less than Relational Expression
Using this relational expression, we can find the smallest value from one or more values.
Example:
#In this program, we will show the working of the less than relational expression
#taking the user input
p=int(input("Enter the p: "))
b=int(input("Enter the b: "))
# less than relational expression functionality
if p<b:
print(p,"is Small")
else:
print(b,"is Small")
Output:
Enter the p: 15
Enter the b: 36
15 is Small
Explanation:
Here, we have initialized the two variables p and b. In those variables, we have taken the data from the user. Then we have to use the if condition in that we have given the condition that p is smaller than b then print p is Small. Otherwise, print the b as Small.
Less than equal-to Relational Expression
Using this relational expression, we can find the smallest value from one or more values.
Example:
#In this program, we will show the working of the less than equal-to a relational expression
#taking the user input
p=int(input("Enter the p: "))
b=int(input("Enter the b: "))
# lessthan equal-to relational expression functionality
if p<=b:
print(p,"is Small")
else:
print(b,"is Small")
Output:
Enter the p: 16
Enter the b: 17
16 is Small
Explanation:
Here, we have initialized the two variables p and b. In those variables, we have taken the data from the user. Then we have to use the if condition in that we have given the condition that p is smaller than or equal to b then print p is Small. Otherwise, print the b as Small.
Greater than equal-to Relational Expression
Using this relational expression, we can find the greatest number from one or more numbers.
Example:
#In this program, we will show the working of the greater than equal to relational expression
#taking the user input
p=int(input("Enter the p: "))
b=int(input("Enter the b: "))
# greater than equal-to-relational expression functionality
if p>=b:
print(p,"is large")
else:
print(b,"is large")
Output:
Enter the p: 16
Enter the b: 15
16 is large
Explanation:
Here, we have initialized the two variables p and b. In those variables, we have taken the data from the user. Then we have to use the if condition in that we have given the condition that p is greater than equalto b then print p is large. Otherwise print the b is large.
Equal to Relational Expression
Using this relational expression, we can find an equal number.
Example:
#In this program, we will show the working of equal to a relational expression
#taking the user input
p=int(input("Enter the p: "))
b=int(input("Enter the b: "))
# equal to relational expression functionality
if p==b:
print("Equal")
else:
print("Not Equal")
Output:
Enter the p: 64
Enter the b: 64
Equal
Explanation:
Here, we have initialized the two variables p and b. In those variables, we have taken the data from the user. Then we have to use the “if” condition in that we have given the condition that p is equal-to b then print equally. Otherwise, print the not equal.
Not Equal-to Relational Expression
Using this relational expression we can find the numbers which are not equal.
Example:
#In this program, we will show the working of Not equal-to a relational expression
#taking the user input
p=int(input("Enter the p: "))
b=int(input("Enter the b: "))
# Not equal-to relational expression functionality
if p!=b:
print("Not Equal")
else:
print("Equal")
Output -:
Enter the p: 15
Enter the b: 16
Not Equal
Explanation:
Here, we have initialized the two variables p and b. In those variables, we have taken the data from the user. Then we have to use the “if” condition in that we have given the condition that p is not equalto b then print not equal. Otherwise, print the equal.
All Relational Operator Example:
#In this program, we will show the working of all relational expressions
#taking the user input
p=int(input("Enter the p: "))
b=int(input("Enter the b: "))
# ( < ) Expression working
if p<b:
print(p,"is Small")
else:
print(b,"is Small")
# ( > ) Expression working
if p>b:
print(p,"is large")
else:
print(b,"is large")
# ( == ) Expression working
if p==b:
print("Both are Equal")
# ( != ) Expression working
if p!=b:
print("Both are Not Equal")
Output:
Enter the p: 16
Enter the b: 54
16 is Small
54 is large
Both are Not Equal
Explanation:
Here, we have initialized the two variables p and b. In those variables, we have taken the data from the user. Then we have to use the different relational expressions like we have used the greater than equal to expression, and the less than equal to expression as well as the equal to expression working with it.