Python Essentials: a Fast Track to Key Concepts — Chapter 2: Numbers

Sajjad Hadi
5 min readJun 14, 2023

--

This image is generated by for the 2nd chapter of Python Essentials.

In the previous chapter we learned about the basics of variables in Python. In this chapter we’re going to learn about Number. Numbers play a vital role in programming, allowing us to perform calculations and manipulate numerical data. In Python, we have different types of numbers, such as integers and floating-point numbers, along with various arithmetic operations to work with them. Additionally, Python provides convenient ways to convert between different number representations. In this chapter, we will explore these concepts with real-world examples to deepen our understanding of numbers in Python.

Chapters of This Series

  1. Chapter 1: Variable Basics
  2. Chapter 2: Numbers
  3. Chapter 3: Strings
  4. Chapter 4: String Methods
  5. Chapter 5: Booleans
  6. Chapter 6: Lists

1. Integers

Integer numbers are whole numbers without any fractional parts. In Python, we can work with integers to perform various calculations or represent quantities. Let’s consider a real-world example to illustrate integer numbers.

Real-world Example

Imagine you are developing a program to manage inventory quantities. You can represent the number of available items using integer numbers:

# Inventory quantity example
available_items = 50

In this example, available_items is an integer variable that represents the quantity of items available in the inventory.

2. Floating-Point Numbers

Floating-point numbers, commonly referred to as floats, are numbers that can have fractional parts. They are useful when dealing with calculations involving decimal values. Let’s explore a practical application of floating-point numbers.

Real-world Example

Suppose you are developing a financial application that calculates the interest earned on investments. Floating-point numbers can accurately represent the interest rate and calculate the interest amount:

# Interest calculation example
interest_rate = 0.05
investment_amount = 10000.00
interest_earned = investment_amount * interest_rate

In this example, interest_rate and investment_amount are floating-point variables, and interest_earned stores the calculated interest amount.

3. Basic Arithmetic Operations

Python provides a wide range of arithmetic operations to manipulate numbers. These operations include addition, subtraction, multiplication, division, and more. Let’s see how these operations work in a practical context.

Real-world Example

Consider a scenario where you are building a shopping cart application that calculates the total price of items. You can use arithmetic operations to perform the calculations:

# Shopping cart total calculation
item_price = 19.99
quantity = 3
total_price = item_price * quantity

In this example, item_price represents the price of a single item, quantity denotes the number of items, and total_price stores the calculated total.

Now, let’s delve into the remaining arithmetic operations to further expand our understanding of numerical computations in code.

Let’s consider a real-world example of calculating monthly expenses. Imagine you are managing your personal budget and want to determine your monthly expenses based on various categories.

Assume the following expense categories:

  • Rent: $1200
  • Groceries: $300
  • Utilities: $150
  • Transportation: $200

Multiplication

Calculate the total yearly rent expense by multiplying the monthly rent by 12:

# Rent expense calculation
monthly_rent = 1200
yearly_rent = monthly_rent * 12

In this example, monthly_rent represents the cost of rent per month, and yearly_rent stores the total yearly rent expense.

Subtraction

Calculate the remaining budget after deducting the expenses from your monthly income:

# Remaining budget calculation
monthly_income = 3000
expenses = rent + groceries + utilities + transportation
remaining_budget = monthly_income - expenses

Here, monthly_income represents your total monthly income, and expenses represents the sum of all expenses. The remaining_budget variable stores the difference between your income and expenses.

Division

Calculate the average monthly expenses by dividing the total expenses by the number of months:

# Average monthly expenses calculation
total_expenses = 1850
num_months = 6
average_expenses = total_expenses / num_months

In this example, total_expenses represents the sum of all expenses over a period of time, num_months represents the number of months, and average_expenses stores the calculated average monthly expenses.

Power and Square Root

Calculate the squared and square root values of a particular expense amount:

# Expense amount calculations
expense_amount = 500
squared_expense = expense_amount ** 2
square_root_expense = expense_amount ** 0.5

Here, expense_amount represents a specific expense value. squared_expense stores the result of squaring the expense amount, while square_root_expense stores the square root of the expense amount.

4. Number Conversion

Python provides built-in functions to convert numbers from one type to another. This capability is useful when you need to manipulate or format numbers for specific purposes. Let’s explore a common use case for number conversion.

Real-world Example

Suppose you are developing an application that reads input from a user as a string and needs to convert it into a numeric representation. You can use the int() or float() functions for this conversion:

# User input conversion example
user_input = input("Enter a number: ")
converted_number = int(user_input)

In this example, user_input captures the input from the user as a string, and converted_number stores the converted integer value.

!! Pay attention to the following !!

  1. When using the input() function in Python, keep in mind that it always retrieves data as a string. This means that if you need to perform numeric calculations or comparisons with the input, you will need to convert the string to the appropriate data type (e.g., int or float) using functions like int() or float().
  2. Be cautious when converting between floats and integers using the int() and float()functions. If you convert a float to an integer, the result will discard any fractional part, potentially leading to loss of precision. On the other hand, converting an integer to a float will result in a floating-point number with ".0" appended to the integer value.

5. Conclusion

Understanding numbers is fundamental in Python programming. In this article, we explored integer and floating-point numbers, basic arithmetic operations, and number conversion. By applying these concepts to real-world examples, you can see how numbers are essential for calculations, financial applications, inventory management, and user input processing. Embrace the versatility of numbers in Python, and keep practicing to strengthen your skills in working with numerical data.

If you found this course helpful and would like to explore more free courses, I invite you to follow my account on Medium and connect with me on LinkedIn. I regularly share valuable content on these platforms.

--

--