# Essential Python Expressions You Should Master

Expression is one of the essential parts of any programming language. We need some sort of group of words to express what we want to perform or what our aim is. Here comes into the picture the different type of expressions we use while programming. However, defining an expression in programming is somewhat different from real. We have to care about the data types we are using and the types of output we want. Besides that, we also have to care about the precedence and associativity of operands on the operators. So, in this article, we will discuss what is the expression in Python and its type.

Contents

## What is an Expression?

Usually, expressions are how we express ourselves. Similarly, in programming, expressions are the group of operators and operands used to describe the operations we want to perform. However, as we have “BODMAS” while performing some arithmetic operation, pythons use concepts of precedence and associativity to specify the order in which operations go. Based on the types of operators we are using, various kinds of expressions are used. Let’s see each of them.

### Constant Expression

These are the expressions that have only constant values. There are no operators used in these kinds of expressions.

```a = 10
print(a)
```

Output:

``10``

### Arithmetic Expressions

The expression that is formed using arithmetic operators like addition, subtraction, multiplication is known as an arithmetic expression. In these kinds of expressions, we generally deal with numeric values.

```# Arithmetic Expressions

num1 = 10
num2 = 20
sum = num1+num2
print(sum)
```

Output:

``30``

### Integral Expressions

Expressions that result in integral values are known as integral expressions. Those results can either be integers themselves or typecast them into integer datatype.

```# integral Expression

num = 10.402
# produces integral output
num = int(num)
print(num)
```

Output:

``10``

### Floating Expressions

Floating expressions are those that create floating-point numbers after performing some operations. Those results can either be floating-point numbers themselves or typecast them into it.

```# integral Expression

num = 14/4
# produces floating-point output
num = num
print(num)
```

Output:

``3.5``

### Relational Expressions

These types of expressions are used to show the relation between two entities. These entities may be an integer, floating-point number, or something else. These expressions use operators like “<, >, <=, >=, ==,!=”. Return types of these expressions are often boolean values, and hence, are also known as boolean expressions.

```# Relational Expressions

a = 20
b = 40

c = a<b
print(c)
```

Output:

``True``

### Bitwise Expressions

To perform an operation on bit-level, we use these expressions. We make use of bitwise operators such as “>>” or “<<” to perform any operations.

```a = 12

bit = a>>2
print(bit)
```

Output:

``3``

In the above case, if we convert 12 into a binary number, then it is 1100. Applying the”>>” operator becomes 11 by shifting 2 digits to the right. In the decimal value of (11), base2 is 3, which is our output.

### Logical Expressions

We use these expressions to perform “logical and“, “logical or”, and “logical not” operations.

```p = 1
q = 0

# Logical operations

print(p and q)  # value of (1 /\ 0) is 0
print(p or q)   # value of (1 \/ 0) is 0
print(not q)    # negation of 0 is 1
```

Output:

``````0
1
True``````

### Combinational Expression

Expressions having more than one expression or combination of them is known as Combinational Expression.

```a = 2
b = 0
c = 0
d = 1
val = a+b and c+d
print(val)
```

Output:

``1``

### Generator Expressions

These are expressions that yield new generator objects. Its syntax is almost similar to list comprehension. The only difference is that it uses parentheses instead of brackets. Let’s see the example.

```z = (x*y for x in range(3) for y in range(x, x+3))
for i in z:
print(i)
print(i)
```

Output:

``````0
0
0
1
2
3
4
6
8
8``````

### Yield Expressions

This expression makes the function a generator function. We use the yield keyword when we want to return a value from the function without actually destroying its state. The actual execution of the function starts after the encounter of the yield keyword in the case of the generator function.

```def gen():  # defines a generator function
yield 123
```

### Await expression

This expression is used to suspend the execution of coroutine on an awaitable object.

```async def read_data(db):
data = await db.fetch('SELECT ...')
...
```

### Assignment Expressions

The expression is called so when we assign some expression to an identifier. For example:

```a = "This is an assignment expression"
print(a)
```

Output:

``This is an assignment expression``

### Conditional Expressions

These expressions contain some conditions to get a conditional check. We generally use “for”,”if-else”,”elif“, or “while” keyword to apply condition in our code.

```# using if else statement

a = 5
if(a==5):
print("This is printed if a is equal to 5")
else:
print("a is not equal to 5")

# using while loop
b = 0
while(b<2):
print(b)
b++
```

Output:

``````This is printed if a is equal to 5
0
1``````

Q1) What is the difference between an expression and a statement in Python?

The difference between an expression and a statement is that an expression returns some result while and statement may or may not return anything.

Q2) Are python generator expressions are call by value or by reference if iterating over a list?

Referencing a mutable object in Python is called by reference in Python as if the value of referenced object changes inside also reflects outside the function.

## Conclusion

So, today in this article, we learned about expressions in Python. We have seen what python expressions are and different types of python expressions. Then, we have witnessed demonstrations over them and learned to use them. I hope this article has helped you. Thank You.

Subscribe
Notify of