×

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

How to print in same line in python

How to Print in the Same Line in Python To print in Python, we use the print () function and the syntax of print () goes like this: print (values, sep =...

3 minutes read.

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal We know that the most widely used number system is a decimal system, but the computer only understands binary values. The...

2 minutes read.

List Subtract in Python

The List is one of the most unique Data Structures in Python. It is generally used to store multiple values in just one single variable. It is one of the...

4 minutes read.

any() Keyword in python

“any” keyword in python This page contains all the information about any () Keyword in python, how it is used with lists, tuples, dictionaries, sets, and what its function is? Python...

3 minutes read.

Python Simple Interest

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

3 minutes read.

Python List clear() method

Python List clear() method The list.clear () method in Python extends the list by appending all the items from the iterable. Syntax list.clear() Parameter NA Example 1 # Python Program explaining # the list.clear() method week_list = ["Monday", "Tuesday", "Wednesday","Thursday","Friday",...

1 minute read.

How to check the version of the Python Interpreter?

As we all know what an interpreter is, and how important it is. We should also be aware of the fact that it is important to have knowledge of the...

2 minutes read.

Crash Course on Python by Google

There is a new, free Python programming course offered by Google on Coursera. No programming experience is necessary. There are numerous Python courses available, but when you learn that Google is...

5 minutes read.

Python md5() function

Python md5() function The md5() function in PHP calculates the md5 hash of a string. Syntax md5 ( string $str [, bool $raw_output] ) Parameter str(required)- This parameter specifies the string. raw_output(optional)- This parameter specifies a hex or binary output format: TRUE - Raw 16 character binary...

1 minute read.

Introduction to Scratch programming

Scratch programming is generally designed for children who may create digital stories, games, and animations using the computer language Scratch, which has the largest kid-focused community in the world. Scratch...

3 minutes read.

Flutter with tensor flow in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The...

3 minutes read.

Convert String to Binary in Python

String to binary The strings can be defined as the array of Unicode code characters. Binary Binary is defined as the number system which consists two symbols 0 and 101. It is base-2...

2 minutes read.

Python String splitlines() method

Python String splitlines() method The string.splitlines() method in Python splits the specified string and returns a list of the lines in the string, breaking at line boundaries.  Syntax splitlines([keepends]) Parameter keepends(optional): This parameter specifies if...

1 minute read.

Python Recursion

Recursion is one of the most interesting yet important concepts of any programming language. If you want to be a good programmer or data scientist, then you should better have...

4 minutes read.

Python | Read csv using pandas.read_csv()

Python is an excellent language for performing information analysis, owing to the fantastic biological system of information-driven python packages. Pandas is one of those packages that make taking in and...

4 minutes read.

NOT IN operator in Python

This page contains all the information about “not in” operator in python. You can also know everything about what type of operator is “not in” in python language and where...

7 minutes read.

Python Set difference() Method

Python Set difference() Method The set.difference() method in Python returns the set difference of two sets(A-B). Syntax set.difference(set1) Parameter set- This argument represents a set (minuend) set1- This arguments represents a set(subtrahend) Return This method returns the difference of the two specified...

2 minutes read.

Google Chrome API in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Variables, Constants and Literals

Python is today's most valuable, easy-to-implement and sought-out programming language. Nowadays, developers want to focus on implementing the program rather than spending time with complex programs. So, for this reason,...

17 minutes read.

iobase Python

All I/O flow classes derive from this conceptual base class. Derived classes will have to execute several of the class's abstract data types. The loop method is supported by all members of...

4 minutes read.