Understanding Python Numbers#

In Python, numbers are a fundamental data type used for various calculations. There are two main types of numbers: integers (int) and floating-point numbers (float).

  1. Integers (int): These are whole numbers, which can be positive, negative, or zero. Examples include 5, -10, and 0. Integers are used for countable objects or discrete values.

  2. Floating-Point Numbers (float): These numbers have a decimal point or are written in scientific notation, like 3.14 or 2.5e-3. Floats are used for real numbers, including fractions and approximate calculations.

These two types are essential for handling a wide range of mathematical operations in Python.

Integers in Python#

Python provides various tools for working with integers, including creating them, checking their type, and performing standard math operations. Here’s how you can use these features:

Creating an Integer#

To create an integer variable, just assign a whole number to it:

Python 3.12.4 (tags/v3.12.4:8e8a4ba, Jun  6 2024, 19:30:16) [MSC v.1940 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> my_var = 22
>>> print(my_var)
22

In this example, my_var is assigned the integer value 22.

Checking the Type of an Integer#

You can check the type of a variable using the type() function:

>>> my_var = 22
>>> print(type(my_var))
<class 'int'>

This confirms that my_var is of type int.

Math Operations with Integers#

Python allows you to perform basic math operations on integers:

  • Addition: Use the + operator:

>>> result = 17 + 22
>>> print(result)
39
  • Subtraction: Use the - operator:

>>> result = 22 - 7
>>> print(result)
Output: 15
  • Multiplication: Use the * operator:

>>> result = 3 * 4
>>> print(result)
Output: 12
  • Division: Use the / operator:

>>> result = 4 / 7
>>> print(result)
0.5714285714285714

These basic operations are essential for working with integers, making Python a powerful tool for various mathematical computations and data manipulation tasks.

Floats in Python#

Working with floating-point numbers (floats) in Python is straightforward. Here’s how you can create, check the type, and perform basic math operations with floats:

Creating a Float#

To create a float variable, just assign a number with a decimal point to it:

>>> my_var = 3.3
>>> print(my_var)
3.3

In this example, my_var is assigned the float value 3.3.

Checking the Type of a Float#

You can check the type of a variable using the type() function:

>>> my_var = 3.3
>>> print(type(my_var))
<class 'float'>

This confirms that my_var is a float.

Math Operations with Floats#

Python allows you to perform basic math operations on floats:

  • Addition: Use the + operator:

>>> result = 3.3 + 2.2
>>> print(result)
5.5
  • Division: Use the / operator:

>>> result = 7 / 2
>>> print(result)
3.5
  • Multiplication: Use the * operator:

>>> result = 3.1 * 2.5
>>> print(result)
7.75

Rounding Numbers#

You can round float numbers using the round() function. For example, to round the result of 4 divided by 3 to the nearest integer:

>>> result = round(4 / 3)
>>> print(result)
1

The result variable now holds the integer value 1, which is the result of rounding 4/3.

Floats are essential for handling real numbers and approximate calculations, making Python a versatile language for various mathematical computations and scientific applications.

Numbers - Other Operators#

In addition to basic arithmetic operations, Python provides other operators for working with numbers. Here are two commonly used number operators:

Modulo Operator (%)#

The modulo operator, represented by %, calculates the remainder when one number is divided by another. For example:

>>> result = 9 % 2
>>> print(result)
1

The result variable will hold the value 1 because 9 divided by 2 leaves a remainder of 1.

Power Operator (**)#

The power operator, represented by **, raises a number to a specified exponent. For instance:

>>> result = 2 ** 3
>>> print(result)
8

The result variable will hold the value 8 because 2 raised to the power of 3 is 8.

These operators expand the range of mathematical operations you can perform in Python, allowing for tasks like finding remainders and calculating exponents in your numerical computations.

Incrementing Counters#

When working with counters in Python, you can increment or decrement their values in various ways. Here are some common methods:

Using Assignment Operator#

You can initialize a counter and then increment it using the assignment operator:

>>> i = 0       # Initialize i to 0
>>> i = i + 1   # Increment i by 1
>>> print(i)
1

After these operations, i holds the value 1.

Using Augmented Assignment#

A more concise way to increment a counter is to use the augmented assignment operator (+=):

>>> i = 0       # Initialize i to 0
>>> i += 1      # Increment i by 1
>>> print(i)
1

This achieves the same result, with i holding the value 1.

Decrementing a Counter#

Decrementing a counter is similar to incrementing, but you subtract a value instead:

>>> i = 10          # Initialize i to 10
>>> i = i - 1       # Decrement i by 1
>>> print(i)
9

You can also use the augmented assignment operator for decrementing:

>>> i = 10   # Initialize i to 10
>>> i -= 1   # Decrement i by 1
>>> print(i)
9

In both cases, i ends up with the value 9.

These techniques are commonly used for maintaining and updating counters in loops, tracking progress, and controlling the flow of your code when you need to count or iterate through a series of values.