Day 3: Operators and Expressions
Operators and expressions are fundamental concepts in any programming language, including Python. An operator is a symbol that performs an operation on one or more operands. An operand is a value that an operator operates on. An expression is a combination of operators, operands, and variables that produces a value.
Python supports several types of operators, including arithmetic, comparison, logical, assignment, and bitwise operators.
Arithmetic Operators
Arithmetic operators are used in Python to perform mathematical operations such as addition, subtraction, multiplication, and division on numerical values. Here are the arithmetic operators in Python:
- Addition (+): Adds two or more numerical values and returns their sum.
- Subtraction (-): Subtracts two or more numerical values and returns their difference.
- Multiplication (*): Multiplies two or more numerical values and returns their product.
- Division (/): Divides two or more numerical values and returns their quotient.
- Modulus (%): Divides two values and returns the remainder.
- Exponentiation (**): Raises the first value to the power of the second value.
- Floor Division (//): Divides two values and returns the integer value of the quotient (rounded down).
Here are some examples of how these operators can be used:
x = 5
y = 2
# Addition
print(x + y) # Output: 7
# Subtraction
print(x - y) # Output: 3
# Multiplication
print(x * y) # Output: 10
# Division
print(x / y) # Output: 2.5
# Modulus
print(x % y) # Output: 1
# Exponentiation
print(x ** y) # Output: 25
# Floor Division
print(x // y) # Output: 2
In addition to these basic operators, Python also has a set of shorthand operators that can be used to perform arithmetic operations and assign the result to a variable in one step. For example:
x = 5
y = 2
x += y # Equivalent to x = x + y
print(x) # Output: 7
x *= y # Equivalent to x = x * y
print(x) # Output: 14
Arithmetic operators are a fundamental concept in programming and are used in many mathematical calculations and algorithms.
Comparison Operators
Comparison operators compare two values and return a Boolean value (True or False). Here are the comparison operators in Python:
- Equal to (==)
- Not equal to (!=)
- Greater than (>)
- Less than (<)
- Greater than or equal to (>=)
- Less than or equal to (<=)
Here’s an example of using comparison operators in Python:
x = 5
y = 3
print(x == y) # Output: False
print(x != y) # Output: True
print(x > y) # Output: True
print(x < y) # Output: False
print(x >= y) # Output: True
print(x <= y) # Output: False
Logical Operators
In Python, logical operators are used to perform logical operations on Boolean values. The logical operators in Python are and, or, and not.
The and operator returns True if both operands are True, otherwise it returns False. For example:
x = 5
y = 10
z = 15
if x < y and y < z:
print("Both conditions are true")
else:
print("At least one condition is false")
In this example, the and operator is used to combine two conditions. The code checks whether x is less than y and whether y is less than z. Since both conditions are true, the output is “Both conditions are true”.
The or operator returns True if at least one of the operands is True, otherwise it returns False. For example:
x = 5
y = 10
z = 15
if x < y or y > z:
print("At least one condition is true")
else:
print("Both conditions are false")
In this example, the or operator is used to combine two conditions. The code checks whether x is less than y or whether y is greater than z. Since at least one condition is true (x is less than y), the output is “At least one condition is true”.
The not operator is used to reverse the Boolean value of an expression. If the expression is True, the not operator returns False. If the expression is False, the not operator returns True. For example:
x = 5
y = 10
if not x > y:
print("x is not greater than y")
else:
print("x is greater than y")
In this example, the not operator is used to reverse the Boolean value of the expression x > y. Since x is not greater than y, the output is “x is not greater than y”.
Logical operators are often used in conditional statements to control the flow of a program. By using logical operators, you can create complex conditions that check multiple expressions at once.
Assignment Operators
Assignment operators in Python are used to assign values to variables. The most basic assignment operator is the equal sign (=), which assigns the value on the right side to the variable on the left side. For example:
x = 5
In this example, we’re assigning the value 5 to the variable x.
Compound assignment operators are a shorthand way to perform an operation and assign the result to a variable. For example:
x += 5
This is equivalent to writing:
x = x + 5
In this example, we’re adding 5 to the current value of x and then assigning the result back to x.
Here are the compound assignment operators in Python:
+=: Add and assign-=: Subtract and assign*=: Multiply and assign/=: Divide and assign%=: Modulo and assign**=: Exponentiate and assign//=: Floor divide and assign
These operators can be used with numeric variables, and they combine an arithmetic operation with the assignment operation. For example:
x = 5
x += 2 # x is now 7
x -= 3 # x is now 4
x *= 2 # x is now 8
x /= 4 # x is now 2.0
x %= 2 # x is now 0.0
x **= 3 # x is now 0.0
x //= 1 # x is still 0.0
In this example, we’re performing a series of compound assignments on the variable x, modifying its value with each operation.
Assignment operators are a powerful tool for working with variables in Python, and they can help simplify your code and make it more concise.
Exercise
- Declare two variables,
xandy, and assign them values of your choice. - Using arithmetic operators, create a new variable
zthat is the sum ofxandy. - Using arithmetic operators, create a new variable
athat is the difference ofxandy. - Using arithmetic operators, create a new variable
bthat is the product ofxandy. - Using arithmetic operators, create a new variable
cthat is the result of dividingxbyy. - Using arithmetic operators, create a new variable
dthat is the remainder of dividingxbyy. - Using arithmetic operators, create a new variable
ethat is the result ofxraised to the power ofy. - Print the values of all the variables to the console, with appropriate labels.
- Using comparison operators, create a new variable
fthat is True ifxis greater thany, and False otherwise. - Using comparison operators, create a new variable
gthat is True ifxis less than or equal toy, and False otherwise. - Using logical operators, create a new variable
hthat is True if bothfandgare True, and False otherwise. - Print the values of
f,g, andhto the console, with appropriate labels.
Here’s an example solution to the exercise:
pythonCopy code# 1. Declare two variables, x and y, and assign them values of your choice.
x = 10
y = 5
# 2. Using arithmetic operators, create a new variable z that is the sum of x and y.
z = x + y
# 3. Using arithmetic operators, create a new variable a that is the difference of x and y.
a = x - y
# 4. Using arithmetic operators, create a new variable b that is the product of x and y.
b = x * y
# 5. Using arithmetic operators, create a new variable c that is the result of dividing x by y.
c = x / y
# 6. Using arithmetic operators, create a new variable d that is the remainder of dividing x by y.
d = x % y
# 7. Using arithmetic operators, create a new variable e that is the result of x raised to the power of y.
e = x ** y
# 8. Print the values of all the variables to the console, with appropriate labels.
print("x =", x)
print("y =", y)
print("z =", z)
print("a =", a)
print("b =", b)
print("c =", c)
print("d =", d)
print("e =", e)
# 9. Using comparison operators, create a new variable f that is True if x is greater than y, and False otherwise.
f = x > y
# 10. Using comparison operators, create a new variable g that is True if x is less than or equal to y, and False otherwise.
g = x <= y
# 11. Using logical operators, create a new variable h that is True if both f and g are True, and False otherwise.
h = f and g
# 12. Print the values of f, g, and h to the console, with appropriate labels.
print("f =", f)
print("g =", g)
print("h =", h)