Python Operators and Expressions: Statements vs. Expressions

0

Understanding the difference between expressions and statements is crucial for writing effective Python code. In this guide, you’ll learn how operators work, how expressions produce values, and how statements control program flow. Practice with exercises and workshops to solidify your skills.

Expressions vs. Statements

What is an Expression?

An expression is a piece of code that evaluates to a value. Examples include arithmetic operations, function calls, or comparisons.

2 + 3          # Evaluates to 5
len("Hello")   # Evaluates to 5
3 > 2          # Evaluates to True

What is a Statement?

A statement is an instruction that performs an action. Examples include loops, conditionals, and variable assignments.

if x > 0:       # if statement
    print(x)

for i in range(5):  # for loop statement
    print(i)

Python Operators

1. Arithmetic Operators

print(10 + 3)   # 13 (Addition)
print(10 - 3)   # 7 (Subtraction)
print(10 * 3)   # 30 (Multiplication)
print(10 ** 3)  # 1000 (Exponentiation)
print(10 / 3)   # 3.333... (Division)
print(10 // 3)  # 3 (Floor Division)
print(10 % 3)   # 1 (Modulus)

2. Comparison Operators

print(5 == 5)   # True
print(5 != 3)   # True
print(5 > 3)    # True
print(5 <= 5)   # True

3. Logical Operators

print(True and False)  # False
print(True or False)   # True
print(not True)        # False

4. Assignment Operators

x = 5
x += 3   # Equivalent to x = x + 3 (x becomes 8)

Practice Work

Exercise 1: Identify Expressions vs. Statements

Label the following as expression or statement:

5 * (10 - 3)
x = 25
print("Hello")
sum = 2 + 3
Solution:

  • 5 * (10 – 3) → Expression
  • x = 25 → Statement
  • print(“Hello”) → Statement
  • sum = 2 + 3 → Statement (assignment is a statement)

Exercise 2: Fix Invalid Expressions

Correct the invalid code below:

result = 10 + * 3
if 5 > 3 and < 10:
    print("Valid")
Solution:

result = 10 * 3
if 5 > 3 and 5 < 10:
    print("Valid")

Exercise 3: Evaluate Expressions

What do these expressions evaluate to?

15 % 4
(5 > 3) or (2 == 3)
not (10 <= 5)
Solution:

  • 15 % 4 → 3
  • (5 > 3) or (2 == 3) → True
  • not (10 <= 5) → True

Workshop: Real-World Applications

Workshop 1: Temperature Converter

Write a script that converts Celsius to Fahrenheit using the formula: F = (C * 9/5) + 32.

celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"{celsius}°C = {fahrenheit}°F")

Workshop 2: User Login Validation

Check if a user’s input meets these conditions:

  • Username is at least 5 characters long.
  • Password contains at least 8 characters.
username = input("Enter username: ")
password = input("Enter password: ")

is_valid = (len(username) >= 5) and (len(password) >= 8)
print("Valid credentials?" , is_valid)

Best Practices

  • Use Parentheses for Clarity: (5 + 3) * 2 is clearer than 5 + 3 * 2.
  • Avoid Complex One-Liners: Break complicated expressions into multiple lines.
  • Prefer Descriptive Variables: discount_rate = 0.1 instead of dr = 0.1.

Conclusion

Mastering operators and understanding the difference between expressions and statements will help you write efficient and readable Python code. Practice the exercises and workshops to reinforce these concepts.

Next Steps: Explore our posts on Python Control Flow and Python Functions.

 

Choose your Reaction!
Leave a Comment

Your email address will not be published.