Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Numbers
course content

Course Content

Introduction to Python

NumbersNumbers

Let's dive into numbers first. Python has the following numerical types:

  • int - for integer numbers (e.g., 3, -1, 1003);
  • float - for decimal numbers (e.g., 2.8, 3.333, -3.0);
  • complex - for complex numbers (e.g., 3+2j).

We'll focus on the first two types since the complex type is typically reserved for scientific applications. Let's say we want to determine how many days are in 792 hours and how many seconds are in an hour. We'll crunch these numbers and identify their types.

Here's a quirky outcome! Even though both numbers were integers (of type int), their division resulted in a float type (yielding 33.0). But why? Isn't 33.0 essentially an integer? Well, in math, it is. But Python, being cautious, recognizes that dividing two integers won't always give an integer result (unlike multiplication, subtraction, or addition).

Note

If you need to switch between numerical types, use int() to convert to integer, float() for decimal, and complex() for complex number. When you convert a decimal to an integer, Python drops the decimal portion without rounding.

Note

When converting a floating-point number to an integer, the process truncates the number by removing the decimal portion, rather than rounding it mathematically.

Everything was clear?

Section 2. Chapter 5
some-alt