×

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. 


Related Topics

Python for Data Analysis

Data analysis uses various techniques to read, illustrate, manipulate and evaluate a particular data. You can have access to the data and keep the data updated regularly. You can append...

4 minutes read.

Python Program to Print Sum of all Elements in an Array

Python program to print sum of all elements in an array A set of objects stored in contiguous memory locations is referred to as an array. The concept is to keep...

2 minutes read.

Python Program to check whether a given number is Armstrong or not

Program to check whether a given number is Armstrong or not A number is said to be an Armstrong if the sum of each digit's cube of a given number equals...

1 minute read.

Explain sklearn clustering in Python

Make a connection and patterns across datasets by using clustering, one of the unsupervised machine learning approaches. Grouping is crucial because it ensures unlabelled data's natural clustering. The sample from...

7 minutes read.

Python Num2words

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Python System Command

To execute a program in Python, we need to execute some shell commands to run our program on the computer. Python will provide some shell commands in our background to...

3 minutes read.

Python Setup guide and installation

Python Installation guide Step by Step Python is widely used high-level programming language. The first version of Python was launched in 1991. Since then, Python has been gaining popularity. It is considered as...

4 minutes read.

How To Print Colored Text in Python

Changing the colour of certain parts of a string when printing the output of a Python programme to the terminal may make it easier to read. We can approach this...

3 minutes read.

Assertion error in python

In this article, we will discuss assertions in the python programming language. Assertion: Assertions are a way of telling a program to test a certain condition and trigger an error if the...

3 minutes read.

Data Class in Python

Introduction In Python 3.7, the dataclass modules are introduced as a practical tool for creating organized classes designed specifically for data storage. These classes contain specific attributes and capabilities to deal...

6 minutes read.

Python Dictionary values() method

Python Dictionary values() method The dictionary.values() method in Python returns a view object that displays a list of all the values in the specified dictionary. Syntax dictionary.values() Parameter NA Return This method returns a view object. The...

1 minute read.

Python Program to Print all the Prime Number in an Interval

Python Program to Print all the Prime Number in an Interval What is Prime numbers? A prime number is referred to those numbers that can be divisible by them only. In simple words,...

4 minutes read.

Python bin() function

Python bin() function The bin() function in Python returns the binary version for the specified integer. Syntax bin(x) Parameter n: This parameter represents an integer or int value. Return This function returns a binary string for the specified integer...

1 minute read.

Python for Loop Increment

Introduction In general, loops are employed for sequential traversal. It belongs to the definite iteration category. Definite iterations imply that the number of iterations is explicitly set in advance.  In this article,...

4 minutes read.

Python String index() method

Python String index() method The string.index() method in Python finds the lower index of the first occurrence of the specified value or raise a ValueError if the substring is not found. Syntax index(sub[, start[, end]]) Parameter sub:...

2 minutes read.

Python hex() function

Python hex() function The hex() function in Python  converts the specified number into a hexadecimal value. Syntax hex(x) Parameter x: The parameter represents an Integer value. Return This function Returns hexadecimal string. Example 1 # Python Program explaining # the hex() function print("The...

1 minute read.

Python Program to Generate a Random String

The term "random" refers to a group of information or data that can be accessed in any chronological order. To create random strings, a Python program called random is utilized....

6 minutes read.

Amazon rekognition using python

Amazon recognition service is an AI service which is an image labelling API (Application programming interface). In this article, we shall learn to use the AWS python boto3 module to...

3 minutes read.

Django vs NodeJS

Django and NodeJS are open-source frameworks used to develop web applications. Both Django and NodeJS are cross-platform and robust technology used for developing versatile web applications and mobile applications. Django Django is...

2 minutes read.

IPython Display

A command shell, often known as IPython is a software application that makes an operating system's features accessible to users or other applications. Based on the purpose and specific functioning...

6 minutes read.