Control structures and loops in python

In Python, operators are special symbols that carry out arithmetic or logical computation. The value that the operator operates on is called the operand. Here’s an overview of the different types of operators in Python and how they are used in expressions.

  1. Arithmetic Operators

These operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.

+ Addition: Adds values on either side of the operator.
- Subtraction: Subtracts the right-hand operand from the left-hand operand.
* Multiplication: Multiplies values on either side of the operator.
/ Division: Divides the left-hand operand by the right-hand operand.
% Modulo: Returns the remainder of division.
** Exponentiation: Raises the left operand to the power of the right.

// Floor Division: Performs division and results in the integer quotient.

a = 10
b = 3
print(a + b)  # 13
print(a - b)  # 7
print(a * b)  # 30
print(a / b)  # 3.3333...
print(a % b)  # 1
print(a ** b) # 1000
print(a // b) # 3
  1. Comparison Operators

These operators compare the values on either side of them and decide the relation among them.

== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
x = 5
y = 8
print(x == y)  # False
print(x != y)  # True
print(x < y)   # True
print(x > y)   # False
print(x <= 5)  # True
print(x >= 8)  # False
  1. Logical Operators

Logical operators are used to combine conditional statements.

x = True
y = False
print(x and y)  # False
print(x or y)   # True
print(not x)    # False
  1. Assignment Operators

Used to assign values to variables.

= Assigns values from right side operands to left side operand

+= Adds right operand to the left operand and assign the result to left operand

-= Subtracts right operand from the left operand and assign the result to left operand, and similarly for *=, /=, %= etc.

python

Copy code

x = 10
x += 5  # Same as x = x + 5
print(x)  # 15
  1. Membership Operators

These operators test for membership in a sequence such as strings, lists, or tuples.

in Returns True if a sequence with the specified value is present in the object

not in Returns True if a sequence with the specified value is not present in the object

list = [1, 2, 3, 4, 5]
print(3 in list)        # True
print(6 not in list)    # True
  1. Identity Operators

These operators compare the memory locations of two objects.

is Returns true if both variables are the same object

is not Returns true if both variables are not the same object

x = [1, 2, 3]
y = [1, 2, 3]
z = x
print(x is z)       # True, same memory location
print(x is y)       # False, different objects, even if they are equal
print(x is not y)   # True

This tutorial covers the primary operators used in Python. Each operator type serves different purposes in Python programming, enabling developers to write efficient and effective code.

An expression in Python is a combination of values, variables, operators, and calls to functions that are constructed according to the syntax of the language, which can be evaluated to produce another value. Essentially, an expression is anything that evaluates to a value. For example, 2 + 3 is an expression that evaluates to 5.

Types of Expressions in Python

Python categorizes expressions based on the function they serve and the values they return. Here’s an overview of several common types of expressions:

Features of Expressions

Expressions can be simple or complex and are fundamental to any Python program. They have several key characteristics:

# This line is a statement
a = 6 * 7  # '6 * 7' is an expression that evaluates to 42

# This line is also a statement
if a > 20:  # 'a > 20' is an expression that evaluates to True
    print(a)  # 'print(a)' is a function call expression

In these examples, expressions are integrated into statements to perform actions. The Python interpreter evaluates these expressions to determine how to execute the statements.

quizes