Understanding Python Operators and Expressions
This article provides a comprehensive overview of Python operators and expressions, covering arithmetic, comparison, logical, assignment, membership, and identity operators, their syntax, examples, and precedence rules, helping readers master how to construct and evaluate expressions for various programming tasks.
1. What are Operators and Expressions?
Operators are symbols that perform specific operations, such as + for addition, - for subtraction, and == for equality comparison. Expressions are code fragments composed of operators and operands that evaluate to a value; for example, 3 + 5 yields 8 .
2. Types of Operators in Python
Arithmetic Operators
Arithmetic operators perform basic mathematical calculations: addition ( + ), subtraction ( - ), multiplication ( * ), division ( / ), modulus ( % ), exponentiation ( ** ), and floor division ( // ). Example: 3 + 5 = 8 , 2 ** 3 = 8 .
a = 10
b = 3
print(a + b) # output 13
print(a ** b) # output 1000Comparison Operators
Comparison operators compare two values and return a Boolean ( True or False ). They include == , != , > , < , >= , and <= . Example: 3 == 5 evaluates to False .
x = 10
y = 20
print(x > y) # output False
print(x == y) # output FalseLogical Operators
Logical operators combine Boolean expressions: and , or , and not . Example: True and False results in False , while True or False results in True .
a = True
b = False
print(a and b) # output False
print(a or b) # output True
print(not a) # output FalseAssignment Operators
Assignment operators assign values to variables and can be combined with arithmetic for compound assignments, such as = , += , -= , *= , and /= . Example: x = 10 then x += 5 makes x equal to 15 .
x = 10
x += 5 # x is now 15
print(x)Membership Operators
Membership operators test whether a value exists in a sequence: in and not in . Example: 3 in [1, 2, 3] returns True .
nums = [1, 2, 3, 4, 5]
print(3 in nums) # output True
print(6 not in nums) # output TrueIdentity Operators
Identity operators compare the memory addresses of objects: is and is not . Example: a = [1, 2, 3]; b = a; a is b yields True .
a = [1, 2, 3]
b = a
print(a is b) # output True
print(a is not b) # output False3. Operator Precedence
Operator precedence determines the order of evaluation in complex expressions. From highest to lowest: parentheses () , exponentiation ** , multiplication/division/modulus/floor division *, /, %, // , addition/subtraction +, - , comparison operators, and finally logical operators not, and, or . Example: result = 10 + 3 * 2 ** 2 evaluates to 22 .
result = 10 + 3 * 2 ** 2 # first 2**2, then 3*4, then 10+12
print(result) # output 224. Summary
Operators and expressions are indispensable in Python programming. By mastering the various operator types and their precedence, developers can write concise, efficient, and logically correct code for a wide range of tasks.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.