Introduction to Operations
What are operations?
Operations are actions you can perform on values and variables in your program. You can think of them as the “verbs” of programming - they tell the computer what to do with data (values and variables).
Example:
age = 25
next_year = age + 1
print(next_year) # Output: 26
Here, + is an operation that adds two numbers together.
← Back to where you were | → Start the course from the beginning
Operators
An operator is a symbol that performs an operation. It’s like a mathematical symbol that tells the computer what action to take.
Examples of operators:
+(addition operator)-(subtraction operator)==(equality operator)*(multiplication operator)
← Back to where you were | → Start the course from the beginning
Arithmetic Operations
These operations work with numbers (integers and floats):
1. Addition (+)
Adds two numbers together.
score1 = 85
score2 = 92
total = score1 + score2
print(total) # Output: 177
2. Subtraction (-)
Subtracts one number from another.
price = 50
discount = 10
final_price = price - discount
print(final_price) # Output: 40
3. Multiplication (*)
Multiplies two numbers.
length = 5
width = 3
area = length * width
print(area) # Output: 15
4. Division (/)
Divides one number by another (always gives a float result).
total_cookies = 20
people = 4
cookies_per_person = total_cookies / people
print(cookies_per_person) # Output: 5.0
5. Integer Division (//)
Divides and rounds down to the nearest whole number.
total_cookies = 23
people = 4
whole_cookies = total_cookies // people
print(whole_cookies) # Output: 5
6. Modulo (%)
Gives the remainder after division.
total_cookies = 23
people = 4
leftover_cookies = total_cookies % people
print(leftover_cookies) # Output: 3
7. Exponentiation (**)
Raises a number to a power.
base = 2
power = 3
result = base ** power
print(result) # Output: 8 (2 to the power of 3)
← Back to where you were | → Start the course from the beginning
Comparison Operations
These operations compare values and give True or False results:
1. Equal to (==)
Checks if two values are the same.
age = 18
is_adult = age == 18
print(is_adult) # Output: True
2. Not equal to (!=)
Checks if two values are different.
password = "secret"
is_wrong = password != "password123"
print(is_wrong) # Output: True
3. Greater than (>)
score = 85
passed = score > 60
print(passed) # Output: True
4. Less than (<)
temperature = 15
is_cold = temperature < 20
print(is_cold) # Output: True
5. Greater than or equal to (>=)
age = 18
can_vote = age >= 18
print(can_vote) # Output: True
6. Less than or equal to (<=)
speed = 50
within_limit = speed <= 60
print(within_limit) # Output: True
← Back to where you were | → Start the course from the beginning
String Operations
1. Concatenation (+)
Joins strings together.
first_name = "Alice"
last_name = "Smith"
full_name = first_name + " " + last_name
print(full_name) # Output: Alice Smith
2. Repetition (*)
Repeats a string multiple times.
laugh = "ha"
big_laugh = laugh * 3
print(big_laugh) # Output: hahaha
← Back to where you were | → Start the course from the beginning
Order of Operations
Python follows mathematical order of operations (PEMDAS):
- Parentheses
() - Exponents
** - Multiplication
*and Division/ - Addition
+and Subtraction-
result = 2 + 3 * 4
print(result) # Output: 14 (not 20!)
result_with_parentheses = (2 + 3) * 4
print(result_with_parentheses) # Output: 20
← Back to where you were | → Start the course from the beginning
Remember
- Operators are symbols that perform operations
- Use parentheses
()to make your calculations clear - Remember that
/always gives a float, even for whole number results - Comparison operations are useful for making decisions in your programs
- String operations help you build messages and format text
← Back to where you were | → Start the course from the beginning
