Arithmetic expressions and numeric variables in Python (3/100 Days of Python)

Martin Mirakyan
3 min readJan 4, 2023

--

Day 3 of the “100 Days of Python” blog post series covering numbers, arithmetic expressions, and numeric variables in Python

Arithmetic expressions in Python can be used to perform mathematical operations on numerical values. In this tutorial, we will cover several topics:

  1. Basic arithmetic operators
  2. Order of operations
  3. Using parentheses to specify the order of operations
  4. Assigning values to variables
  5. Getting integer inputs

Basic arithmetic operators

In Python, you can use the following operators to perform basic arithmetic operations:

Basic arithmetic operations in Python with usage examples

Here is a program that demonstrates how to use the arithmetic operations in Python:

print(3 + 2)  # Output: 5
print(3 - 2) # Output: 1
print(3 * 2) # Output: 6
print(3 / 2) # Output: 1.5
print(3 // 2) # Output: 1
print(3 % 2) # Output: 1
print(3 ** 2) # Output: 9

It’s possible to combine arithmetic operations with more complex expressions, which will be demonstrated at the end of the post.

Order of operations

In Python, as in mathematics, the order of operations determines how arithmetic expressions are evaluated. Python follows the standard mathematical order of operations: parentheses, exponents, multiplications and divisions (performed from left to right), and additions and subtractions (performed from left to right).

Here’s an example of how the order of operations is applied in Python:

print(3 + 2 * 4 - 1)
# 3 + 2 * 4 - 1
# = 3 + 8 - 1
# = 11 - 1
# = 10

Using parentheses to specify the order of operations

You can use parentheses to specify the order of operations in an arithmetic expression. Parentheses are used to group operations and indicate that they should be performed before other operations.

Here’s an example of how you can use parentheses to specify the order of operations:

print((3 + 2) * 4 - 1)
# (3 + 2) * 4 - 1
# = 5 * 4 - 1
# = 20 - 1
# = 19

Assigning values to variables

You can assign values to variables in Python using the assignment operator =. You can then use these variables in arithmetic expressions:

x = 3
y = 2
print(x + y) # Output: 5
print(x * y) # Output: 6

Getting integer inputs

To get integers from the input in Python, you can use the int() function:

num = input('Enter an integer: ')  # Get input from the user
num = int(num) # Convert the input to an integer
print(num) # Print the integer

This will prompt the user to enter an integer, and then it will convert the input to an integer and print it. If the user enters a value that cannot be converted to an integer (e.g. a string that is not a number), it will raise a ValueError exception.

We can also get the input and convert it to an integer with one line:

age = int(input('Enter an integer: '))  # Get input from the user
print(2 * age) # Print twice the age

What’s next?

--

--

Martin Mirakyan
Martin Mirakyan

Written by Martin Mirakyan

Software Engineer | Machine Learning | Founder of Profound Academy (https://profound.academy)

No responses yet