Lecture

Arithmetic Operations in Python

In Python, you can perform basic arithmetic operations using the + (addition), - (subtraction), * (multiplication), and / (division) operators.


Addition

Use the + operator. a + b represents the sum of a and b.


Subtraction

Use the - operator. a - b represents the result of subtracting b from a.


Multiplication

Use the * operator. a * b represents the product of a and b.


Division

Use the / operator to divide one number by another. This operation always returns a float, even if the result is a whole number. a / b represents the value obtained by dividing a by b.


Let's look at a simple example to see how each operation works.

Example of Number Operations
a = 10 b = 5 # Addition print("Addition:", a + b) # Output: 15 # Subtraction print("Subtraction:", a - b) # Output: 5 # Multiplication print("Multiplication:", a * b) # Output: 50 # Division print("Division:", a / b) # Output: 2.0

Operator Precedence

Just like in standard arithmetic, operations inside parentheses are performed first in Python.

Additionally, multiplication and division have higher precedence than addition and subtraction in Python expressions.

Operator Precedence Example
a = 10 b = 5 print("Operator Precedence:", a + b * 2) # Output: 20 print("Operator Precedence:", (a + b) * 2) # Output: 30
Mission
0 / 1

What is the expression to add values a and b, then multiply by 2 in Python?

a + b * 2

a * b + 2

(a + b) * 2

(a * b) + 2

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result