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.
- 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
- 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
- Logical Operators
Logical operators are used to combine conditional statements.
- and Returns True if both statements are true
- or Returns True if one of the statements is true
- not Reverse the result, returns False if the result is true python
x = True
y = False
print(x and y) # False
print(x or y) # True
print(not x) # False
- 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
- 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
- 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:
-
Arithmetic Expressions: These are expressions that involve mathematical operations among numerical values and evaluate to a numerical value. Example: 4 + 5 * 10 / 2 evaluates to 29.0.
-
String Expressions: These involve string operations and typically evaluate to modified strings. Example: “Hello " + “World!” evaluates to “Hello World!”.
-
Logical Expressions: These involve Boolean values (True or False) and evaluate to a Boolean value based on the logical operators used (and, or, not). Example: True and False evaluates to False.
-
Comparison Expressions: These compare values using comparison operators and evaluate to Boolean values. Example: 5 > 3 evaluates to True.
-
Membership Expressions: These test whether a value is found in a sequence (like a list or string). Example: ‘a’ in ‘cat’ evaluates to True.
-
Identity Expressions: These compare the memory locations of two objects to check if they refer to the same object. Example: a is b evaluates based on whether a and b are the same object.
Features of Expressions
Expressions can be simple or complex and are fundamental to any Python program. They have several key characteristics:
- Evaluate to a Value: Every expression results in a value, which can be any of Python’s data types, like numbers, strings, lists, etc.
- Side Effects: Some expressions do more than just return a value; they can also cause side effects. For example, print(“Hello”) prints to the console (a side effect) and returns None.
- Composability: Expressions can be nested. This means the result of one expression can be used as part of another expression. For example, 3 * (4 + 5) uses the result of 4 + 5 as part of another arithmetic operation.
- Practical Examples
- To see how expressions fit into larger Python constructs, consider them in the context of statements:
# 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