In Python, expressions and statements are fundamental concepts that form the building blocks of code. Let’s discuss each one:
Expressions:
An expression is a combination of values, variables, and operators that can be evaluated to a single value. In other words, it produces a result. Expressions can include literals, variables, and function calls.
Examples of expressions:
# Arithmetic expression
result = 2 + 3 * 4
# String concatenation expression
greeting = "Hello" + " " + "World"
# Function call expression
length = len("Python")
# Comparison expression
is_equal = (5 == 5)
In the examples above, each line represents an expression that produces a value. Expressions can be simple or complex, involving multiple operators and operands.
Statements:
A statement is a complete line of code that performs an action. Unlike expressions, statements do not necessarily produce a value. Python is an imperative programming language, meaning that most of the code consists of statements that instruct the computer to perform certain actions.
Examples of statements:
# Assignment statement
x = 10
# Conditional statement
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")
# Loop statement
for i in range(5):
print(i)
In the examples above:
- The assignment statement (
x = 10
) assigns the value10
to the variablex
. - The conditional statement (
if
andelse
) checks whetherx
is greater than 5 and prints a message accordingly. - The loop statement (
for i in range(5)
) iterates over the range of values and prints each value ofi
.
It’s important to note that expressions can be part of statements. For example, in the assignment statement x = 10
, the right side contains the expression 10
, which is evaluated and assigned to the variable x
.
Understanding the distinction between expressions and statements is crucial for writing effective and readable Python code. Expressions are the building blocks that produce values, and statements are the instructions that make your program do something.
Operators
In Python, operators are special symbols or reserved words that perform specific actions on operands (values or variables) to create expressions. Expressions are combinations of operators and operands that evaluate to a value. Here are some of the most commonly used operators and expressions in Python:
- Arithmetic Operators:
Arithmetic operators are used to perform mathematical operations on numerical values. The common arithmetic operators in Python are:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulus (%)
- Exponentiation (**)
- Floor Division (//)
Example:
a = 10
b = 3
print(a + b) # Output: 13
print(a - b) # Output: 7
print(a * b) # Output: 30
print(a / b) # Output: 3.3333333333333335
print(a % b) # Output: 1
print(a ** b) # Output: 1000
print(a // b) # Output: 3
- Comparison Operators:
Comparison operators are used to compare two values and return a Boolean value (True or False). The common comparison operators in Python are:
- Equal to (==)
- Not equal to (!=)
- Greater than (>)
- Less than (<)
- Greater than or equal to (>=)
- Less than or equal to (<=)
Example:
a = 10
b = 3
print(a == b) # Output: False
print(a != b) # Output: True
print(a > b) # Output: True
print(a < b) # Output: False
print(a >= b) # Output: True
print(a <= b) # Output: False
Difference between == and = Operator
= | == |
It is an assignment operator | It is a comparison operator |
It is used for assigning the value to a variable | It is used for comparing two values. It returns 1 if both the value is equal otherwise returns 0 |
Constant term cannot be place on left hand side Example: 1= x; is invalid | Constant term can be placed in the left-hand side. Example: 1 ==1 is valid and return 1 |
- Logical Operators:
Logical operators are used to combine multiple conditions and return a Boolean value. The common logical operators in Python are:
- and
- or
- not
Example:
a = 10
b = 3
print(a > 5 and b > 2) # Output: True
print(a > 5 or b > 5) # Output: True
print(not(a > 5)) # Output: False
- Assignment Operators:
Assignment operators are used to assign values to variables. The common assignment operators in Python are:
- = (Simple Assignment)
- += (Addition Assignment)
- -= (Subtraction Assignment)
- *= (Multiplication Assignment)
- /= (Division Assignment)
- %= (Modulus Assignment)
- **= (Exponentiation Assignment)
- //= (Floor Division Assignment)
Example:
a = 10
b = 3
a += b # equivalent to a = a + b
print(a) # Output: 13
a **= b # equivalent to a = a ** b
print(a) # Output: 2197
5. Bitwise Operators
In Python, bitwise operators perform operations on the binary representation of integer values. They operate on each bit of the binary representation separately. Here are the commonly used bitwise operators in Python:
- Bitwise AND (&):
The bitwise AND operator returns a 1 in each bit position where both corresponding bits in the operands are 1, otherwise, it returns a 0.
Example:
a = 60 # 0011 1100
b = 13 # 0000 1101
c = a & b # 0000 1100
print(c) # Output: 12
2. Bitwise OR (|):
The bitwise OR operator returns a 1 in each bit position where at least one corresponding bit in the operands is 1, otherwise, it returns a 0.
Example:
a = 60 # 0011 1100
b = 13 # 0000 1101
c = a | b # 0011 1101
print(c) # Output: 61
3. Bitwise XOR (^):
The bitwise XOR operator returns a 1 in each bit position where only one corresponding bit in the operands is 1, otherwise, it returns a 0.
Example:
a = 60 # 0011 1100
b = 13 # 0000 1101
c = a ^ b # 0011 0001
print(c) # Output: 49
4. Bitwise NOT (~):
The bitwise NOT operator returns the complement of the binary representation of the operand.
Example:
a = 60 # 0011 1100
b = ~a # 1100 0011
print(b) # Output: -61
Note that the result is a negative value because the bitwise NOT operator changes the sign bit of the binary representation of the operand.
- Left Shift (<<):
The left shift operator shifts the binary representation of the left operand to the left by the number of bits specified by the right operand.
Example:
a = 60 # 0011 1100
b = a << 2 # 1111 0000
print(b) # Output: 240
- Right Shift (>>):
The right shift operator shifts the binary representation of the left operand to the right by the number of bits specified by the right operand.
Example:
a = 60 # 0011 1100
b = a >> 2 # 0000 1111
print(b) # Output: 15
These are the common bitwise operators in Python. Understanding them will help you write more efficient and effective Python programs.
6. Identity Operators
In Python, identity operators are used to compare the memory locations of two objects. These operators return True
if the objects referred to by the variables on either side of the operator are the same object, otherwise, they return False
. Here are the two identity operators in Python:
- is Operator:
The is
operator returns True
if the variables on either side of the operator refer to the same object, otherwise, it returns False
.
Example:
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # Output: True
print(a is c) # Output: False
In the example above, the is
operator returns True
when comparing a
and b
because they both refer to the same object in memory, while it returns False
when comparing a
and c
because they refer to different objects in memory.
2. is not Operator:
The is not
operator returns True
if the variables on either side of the operator do not refer to the same object, otherwise, it returns False
.
Example:
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is not b) # Output: False
print(a is not c) # Output: True
In the example above, the is not
operator returns False
when comparing a
and b
because they both refer to the same object in memory, while it returns True
when comparing a
and c
because they refer to different objects in memory.
Identity operators are useful when you need to check whether two variables refer to the same object in memory or not.
7.Membership Operators
In Python, membership operators are used to test whether a value is a member of a sequence or not. The two membership operators in Python are:
in
Operator:
The in
operator returns True
if a value is found in a sequence, otherwise, it returns False
.
Example:
a = [1, 2, 3, 4, 5]
b = 3
c = 6
print(b in a) # Output: True
print(c in a) # Output: False
In the example above, the in
operator returns True
when checking if b
is in the list a
, while it returns False
when checking if c
is in the list a
.
2. not in
Operator:
The not in
operator returns True
if a value is not found in a sequence, otherwise, it returns False
.
Example:
a = [1, 2, 3, 4, 5]
b = 3
c = 6
print(b not in a) # Output: False
print(c not in a) # Output: True
In the example above, the not in
operator returns False
when checking if b
is not in the list a
, while it returns True
when checking if c
is not in the list a
.
Membership operators are useful when you need to check if a value is present or not in a sequence, such as a list, tuple, or string.
Order of Precedence
In Python, the order of precedence, also known as operator precedence, determines the order in which different operators are evaluated in an expression. Operators with higher precedence are evaluated before operators with lower precedence. If operators have the same precedence, their associativity (left-to-right or right-to-left) determines the order of evaluation.
Here’s a brief overview of some common operators in Python and their precedence:
- Parentheses
()
: Highest precedence. Expressions within parentheses are evaluated first. - Exponentiation
**
: Raises the left operand to the power of the right operand. - Unary plus
+
and unary minus-
: Positive and negative signs. - Multiplication
*
, division/
, and floor division//
: Multiplication, division, and floor division. - Modulus
%
: Returns the remainder of the division of the left operand by the right operand. - Addition
+
and subtraction-
: Addition and subtraction. - Bitwise shift operators
<<
and>>
: Shift bits to the left or right. - Bitwise AND
&
: Bitwise AND. - Bitwise XOR
^
: Bitwise exclusive OR. - Bitwise OR
|
: Bitwise OR. - Comparison operators (
<
,<=
,>
,>=
,==
,!=
) and membership operators (in
,not in
): Compare values and test membership. - Logical NOT
not
: Boolean NOT. - Logical AND
and
: Boolean AND. - Logical OR
or
: Boolean OR.
Let’s look at an example to illustrate operator precedence:
result = 2 + 3 * 4 ** 2 - (8 / 2) % 3
print(result)
In this example, the order of evaluation is as follows:
4 ** 2 is evaluated first (16).
8 / 2 is evaluated next (4.0).
(8 / 2) % 3 is evaluated next (1.0).
3 * 16 is evaluated (48).
2 + 48 is evaluated (50).
50 – 1 is evaluated (49).
So, the final result printed will be 49
.