Level 1
0 / 100 XP

Numbers

In Python there are three types of numbers that you will encounter:

  • Int (Integer): Positive or negative whole number (no decimal place).
  • Float: Positive or negative number written in decimal format
  • Complex: A real and imaginary number

Here are examples of what each number could look like:

Text
x = 1 # int y = 4.7 # float z = 1j # complex

We could inspect the type of any variable using the type() function:

Python
print( type(x) ) print( type(y) ) print( type(z) )

Here are some examples of how you can interact with the different types of numbers in Python:

Integer

Integer (int) numbers: These are whole numbers, such as 1, 2, or 3. You can create int variables and perform basic arithmetic operations on them, such as addition, subtraction, multiplication, and division. For example:

Python
# Create an int variable and assign it a value a = 10 # Perform arithmetic operations on the variable b = a + 5 c = a - 5 d = a * 5 e = a / 5 # Print the results print(b) # Output: 15 print(c) # Output: 5 print(d) # Output: 50 print(e) # Output: 2.0

Float

These are numbers with decimal points, such as 1.5, 2.3, or 3.14. You can create float variables and perform the same basic arithmetic operations as with ints. The main difference is that the result of these operations will also be a float. For example:

Python
# Create a float variable and assign it a value a = 10.5 # Perform arithmetic operations on the variable b = a + 5 c = a - 5 d = a * 5 e = a / 5 # Print the results print(b) # Output: 15.5 print(c) # Output: 5.5 print(d) # Output: 52.5 print(e) # Output: 2.1

Complex

These are numbers with a real and imaginary part, such as 3 + 2j or 4 - 5j. You can create complex variables and perform basic arithmetic operations on them, such as addition, subtraction, multiplication, and division. For example:

Python
# Create a complex variable and assign it a value a = 3 + 2j # Perform arithmetic operations on the variable b = a + 5 c = a - 5 d = a * 5 e = a / 5 # Print the results print(b) # Output: (8+2j) print(c) # Output: (-2+2j) print(d) # Output: (15+10j) print(e) # Output: (0.6+0.4j)

When would you use this? Unless you're dealing in heavy math, electrical engineering, or radiowaves (to name a few), you probably won't. So don't stress if you don't understand what an imaginary number is. You don't necessarily need to understand this to complete IT administrative tasks or software development.

An imaginary number is a number that cannot be represented in the real world. It is a number that exists only in the world of math. Imaginary numbers are often represented by the letter "i" in equations.

For example, the square root of -1 is an imaginary number. In math, we usually write it as "i". So, if you see an equation that has the letter "i" in it, it probably means that it involves an imaginary number.

Imaginary numbers can be difficult to understand because they don't represent anything we can see or touch in the real world. But even though they are difficult to understand, they are still very useful in math and science. For example, imaginary numbers are often used in equations to describe things like electricity, radio waves, and other types of energy.

Anyway, great job getting through all of that! See you in the next lecture!

Run your code and pass validations to complete this lesson.
Loading editor…

Run your code to see output here.