
Arithmetic Operations
Python's arithmetic operations on numeric values follow conventional mathematical calculations. Beyond numbers, programming also extends arithmetic operators to other types of values, such as strings and lists. Let's explore how they interact!
Numbers
In python, numbers include both integers and floats. We'll first explore arithmetic operations with these data types.
Arithmetic operators
- summation:
+
- deduction:
-
- multiplication:
*
- division:
/
- modulo:
%
- It is used to find the remainder of the division of the left operand by the right operand.
- e.g.
7%2 = 1
- exponential:
**
- e.g.
3**2 = 9
Example of Summation and Deduction
# Create variablesa = 10b = 2.5
# summationprint(a + b)
# deductionprint(a - b)
Output:
12.5
7.5
Example of Multiplication and Division
# Create variablesa = 20b = 2.5
# multiplicationprint(a * b)
# divisionprint(a / b)
Output:
50.0
8.0
Example of Modulo and Exponential
# Create variablesa = 7b = 2
# Example for modulo print("Example for modulo: 7%2")print(a % b)
# Example for exponentialprint("Example for exponetial: 3**2")print(a**b)
Output:
Example for modulo: 7%2
1
Example for exponetial: 3**2
49
Strings
Arithmetic operators in Python can be used with various data types, not just numbers. Let's see how they function with strings.
Operators Used in Strings:
- Concatenation using
+
- Repetition using
*
other arithmetic operators like -, /, and %
are not applicable to strings in Python
Concatenation of strings
String concatenation, crucial in data preprocessing and web development, effectively merges separate strings. This can be done using the +
operator to combine two or more strings.
# creating string variablestext1 = 'Hello'text2 = "World"
# Add strings together and print the resultprint(text1 + text2)
Output:
HelloWorld
The output string is a concatenation of text1
and text2
, yet they lack a space in between. Let's fix that by placing a space string ' '
in between.
print(text1 + ' ' + text2)
Output:
Hello World
String repetition
String repetition, achieved with the *
operator, efficiently creates repetitive strings without manual typing.
str1 = "Repeat me! "repeated_str = str1 * 3print(repeated_str)
Output:
Repeat me! Repeat me! Repeat me!
Note that a space is included at the end of str1
, so the repeated_str
has proper space in between.
Lists
In Python, arithmetic operators can be used on lists to perform various operations, such as concatenation, repetition, and element-wise addition or multiplication.
Operators Used in Lists:
- Concatenation using
+
- Repetition using
*
other arithmetic operators like -, /, and %
are not applicable to lists in Python
Combination of lists
Lists combination effectively merges separate lists. This can be done using the +
operator to combine two or more lists.
# Create listslist1 = [1, 2, 3]list2 = [4, 3, 6]
# Combine two listsresult = list1 + list2print(result)
Output:
[1, 2, 3, 4, 3, 6]
List repetition
You can repeat a list by using the * operator.
list1 = [1, 2, 3]
# Repeat the list repeated_list = list1 * 3print(repeated_list)
Output:
[1, 2, 3, 1, 2, 3, 1, 2, 3]