0% found this document useful (0 votes)
17 views

Expressions and Assignment Statements

The document explains expressions and assignment statements in Python, highlighting their importance in programming for logic, calculations, and data management. It details various types of expressions, including arithmetic, relational, logical, and bitwise, along with examples of assignment statements and augmented assignment operators. A sample program is provided to demonstrate the application of these concepts in Python coding.

Uploaded by

Hezrone Ocampo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Expressions and Assignment Statements

The document explains expressions and assignment statements in Python, highlighting their importance in programming for logic, calculations, and data management. It details various types of expressions, including arithmetic, relational, logical, and bitwise, along with examples of assignment statements and augmented assignment operators. A sample program is provided to demonstrate the application of these concepts in Python coding.

Uploaded by

Hezrone Ocampo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

IT2409

Expressions and Assignment Statements


In Python, expressions and assignment statements are fundamental to programming. An expression produces
a value, while an assignment statement stores that value in a variable. These concepts are essential for writing
logic, performing calculations, and managing data effectively.
Expressions
An expression is a combination of values, variables, operators, and function calls that result in a computed
value. Expressions are crucial because they form the building blocks of logic in programming. Every expression
returns a value that can be used within a program.
An expression is any valid combination of operands (values or variables) and operators (such as +, -, *, /). It is
evaluated by Python to produce a value. Expressions can be as simple as a single number (10) or as complex
as a mathematical formula involving multiple operations and function calls.
Example (Simple Expression):
x = 10 + 5 # 10 and 5 are values, + is an operator
print(x) # Output: 15
Example (Complex Expression):
x = (10 + 5) * 2 - 3 / 1.5 # Uses parentheses, arithmetic operators, and division
print(x) # Output: 27.0
Expressions can also involve function calls:
def square(n):
return n * n
result = square(4) + 10 # Calls the square function and adds 10
print(result) # Output: 26
Types of Expressions
Arithmetic Expressions
These involve mathematical operations such as addition (+), subtraction (-), multiplication (*), division (/), and
exponentiation (**). These expressions are used in scenarios requiring numerical computations.
Examples:
result = (5 + 3) * 2
print(result) # Output: 16

total_cost = item_price * quantity + shipping_fee


Relational Expressions (or Comparison Expressions)
These evaluate to True or False using comparison operators. These utilize comparison operators like equal
to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal
to (<=). These expressions are essential in decision-making processes, such as conditional statements and
loops.
Examples:
print(10 > 5) # Output: True
print(4 == 5) # Output: False

if user_age >= 18:


print("Eligible to vote")

06 Handout 1 *Property of STI


Page 1 of 3
IT2409

Logical Expressions (or Boolean Expressions)


These use logical operators (and, or, not) to combine Boolean values. These are pivotal in constructing
complex conditional statements.
Examples:
x = (5 > 3) and (10 > 8)
print(x) # Output: True

if is_member and has_paid_fee:


grant_access()
Bitwise Expressions
These manipulate bits directly using bitwise operators (&, |, ^, ~, <<, >>). These perform operations at the
binary level using operators like AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>). These
expressions are commonly used in low-level programming, such as setting flags or manipulating binary data.
Example:
a = 5 # Binary: 0101
b = 3 # Binary: 0011
print(a & b) # Output: 1 (Binary: 0001)
Assignment Statements
An assignment statement assigns a value to a variable using the = operator. Assignment statements allow the
storage of values in named memory locations (variables). The left-hand side of the assignment operator (=) is
the variable name, and the right-hand side is the value to be stored.
Example (Simple Assignment):
x = 5 # x is assigned the value 5
name = "Alice" # name is assigned the string "Alice"
Example (Multiple Assignments):
a, b, c = 1, 2, 3
print(a, b, c) # Output: 1 2 3
Values can also be swapped without using a temporary variable:
x, y = 10, 20
x, y = y, x # Swapping values
print(x, y) # Output: 20 10
Augmented Assignment Operators
Python provides augmented assignment operators that simplify modifying variables. It provides a concise
syntax to update variable values by combining an arithmetic operation with an assignment operator, reducing
redundancy in code. For example:
x = 10
x += 5 # Equivalent to: x = x + 5
print(x) # Output: 15

x *= 2 # Equivalent to: x = x * 2
print(x) # Output: 30

x -= 10 # Equivalent to: x = x - 10
print(x) # Output: 20

06 Handout 1 *Property of STI


Page 2 of 3
IT2409

Here is the list of augmented assignment operators:


Operator Description Example Equivalent To
+= Addition a += b a = a + b
-= Subtraction a -= b a = a - b
*= Multiplication a *= b a = a * b
/= Division a /= b a = a / b
//= Floor Division a //= b a = a // b
%= Modulus (Remainder) a %= b a = a % b
**= Exponentiation a **= b a = a ** b
&= Bitwise AND a &= b a = a & b
|= Bitwise OR a |= b a = a | b
^= Bitwise XOR a ^= b a = a ^ b
<<= Bitwise Left Shift a <<= b a = a << b
>>= Bitwise Right Shift a >>= b a = a >> b

A sample program incorporating expressions and assignment statements.


# Assigning values to variables
x = 10
y = 5

# Performing arithmetic operations


sum_result = x + y
product_result = x * y

# Using a relational expression


is_x_greater = x > y

# Using a logical expression


is_x_positive_and_greater = (x > 0) and (x > y)

# Using an augmented assignment


x += 3

# Printing results
print("Sum:", sum_result)
print("Product:", product_result)
print("Is x greater than y?", is_x_greater)
print("Is x positive and greater than y?", is_x_positive_and_greater)
print("Updated x:", x)
Output:
Sum: 15
Product: 50
Is x greater than y? True
Is x positive and greater than y? True
Updated x: 13

References:
Downey, A. (2020). Think Python: How to think like a computer scientist (2nd ed.). O'Reilly Media.
Tenkanen, H., Heikinheimo, V., & Whipp, D. (2020). Introduction to Python for geographic data analysis.

06 Handout 1 *Property of STI


Page 3 of 3

You might also like