Operators and Expressions in Python

Operators and Expressions in Python

In Python, operators are used to perform operations on variables and values.

24 January 2023

Operators and Expressions in Python

In Python, operators are used to perform operations on variables and values. There are several types of operators in Python, including:

Arithmetic Operators: These operators are used to perform mathematical operations, such as addition, subtraction, multiplication, and division. Comparison Operators: These operators are used to compare values and determine their relation, such as equal to, greater than, less than, etc. Logical Operators: These operators are used to combine conditions and determine the truth or falsehood of a statement. Assignment Operators: These operators are used to assign values to variables, such as =, +=, -=, *= etc Membership Operators: These operators are used to test whether a value is a member of a sequence, such as lists, strings, or tuples. An expression is a combination of values, variables, and operators that results in a single value. For example, 2 + 3 is an expression that evaluates to 5.

Here are a few examples of how to use operators and expressions in Python:

Example 1: Using Arithmetic Operators


x = 5
y = 3
z = x + y
print(z)

In this example, x is assigned the value 5, y is assigned the value 3, and z is assigned the value of x + y (8). The program then prints the value of z (8).

Example 2: Using Comparison Operators


x = 5
y = 3
print(x > y)
print(x == y)

In this example, x is assigned the value 5, y is assigned the value 3. The program then uses the greater than operator (>) to compare the values of x and y. The first comparison will return True, and the second will return False.

Example 3: Using Logical Operators


x = 5
y = 3
z = 2
print((x > y) and (x > z))

In this example, x is assigned the value 5, y is assigned the value 3 and z is assigned the value 2. The program then uses the logical operator and to check if x is greater than both y and z. The expression evaluates to true.

Example 4: Using Assignment Operators


x = 5
x += 2
print(x)

In this example, x is assigned the value 5 and then the value of x is incremented by 2 using the += operator. The program then prints the new value of x (7)

Example 5: Using Membership Operators


colors = ["red", "green", "blue"]
print("green" in colors)
print("orange" not in colors)

In this example, the variable colors is assigned a list of strings [“red”, “green”, “blue”]. The program then uses the in operator to check if “green” is present in the list, which returns True. Then it uses the not in operator to check if “orange” is not present in the list which also returns True.

In conclusion, operators and expressions are fundamental building blocks of any programming language and in python as well, they play a crucial role in performing operations and making decisions in the programs. Understanding their usage and how to combine them to create expressions is a key step in becoming a proficient Python programmer.

© day1.study