Python Numeric Types

PythonPythonBeginner
Practice Now

Introduction

Welcome to the future Metatropolis, a super-advanced technological city where life thrives through the power of code and robotics. In Metatropolis, your role as a resident coder is about to get exciting! The city is calling on its skilled inhabitants to help optimize the energy distribution network. To do this, you must harness the core programming elements of Python, focusing specifically on numeric types and operations.

The city's energy council has set up a virtual lab where you can experiment and develop your mastery of Python numeric types. Your mission, should you choose to accept it, is to write efficient Python code that can perform complex calculations, convert energy units, and balance the city's power grid using numbers and mathematical operations.

Your goal is to ensure that energy flows consistently to all corners of the city, avoiding blackouts and ensuring a green, efficient distribution of power. Get ready to crunch numbers and use your knowledge to contribute to Metatropolis's shining future.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") subgraph Lab Skills python/numeric_types -.-> lab-271576{{"`Python Numeric Types`"}} end

Understanding Basic Numeric Types

In this step, you will familiarize yourself with Python's basic numeric types: integers and floating-point numbers. Let's write some code to perform basic arithmetic operations and see how Python handles different numeric types.

Now, add the following code to /home/labex/project/numeric_types.py:

## Basic arithmetic operations

## Addition
addition_result = 5 + 3
print("5 + 3 =", addition_result)

## Subtraction
subtraction_result = 10 - 2
print("10 - 2 =", subtraction_result)

## Multiplication
multiplication_result = 7 * 3
print("7 * 3 =", multiplication_result)

## Division (float result)
division_result = 15 / 3
print("15 / 3 =", division_result)

## Floor division (integer result)
floor_division_result = 15 // 2
print("15 // 2 =", floor_division_result)

## Modulus operation (remainder of division)
modulus_result = 16 % 5
print("16 % 5 =", modulus_result)

## Exponents (power of)
exponent_result = 2 ** 3
print("2 ** 3 =", exponent_result)

In the script above, we perform addition, subtraction, multiplication, and various types of division. Note how division results in a floating-point number even when both operands are integers.

Execute your code in the terminal and observe the results:

python3 /home/labex/project/numeric_types.py

Your terminal should show:

5 + 3 = 8
10 - 2 = 8
7 * 3 = 21
15 / 3 = 5.0
15 // 2 = 7
16 % 5 = 1
2 ** 3 = 8

Dealing with Floats and Type Casting

Floats, or floating-point numbers, are numbers with decimal points. Python automatically converts integers to floats in mixed arithmetic operations involving both types. In this step, you will also learn how to cast between integers and floats to achieve the desired numeric type.

Add the following code to the /home/labex/project/numeric_types.py after your previous code:

## Working with floats and casting

## Mixing integers and floats
mix_result = 10 + 3.14
print("10 + 3.14 =", mix_result)

## Casting an integer to a float
int_to_float = float(8)
print("float(8) =", int_to_float)

## Casting a float to an integer (note that this truncates rather than rounds)
float_to_int = int(3.99)
print("int(3.99) =", float_to_int)

In this code, you'll see how Python deals with operations involving both ints and floats. When you execute this script, note the conversion that takes place from int to float and vice versa.

Execute your code in the terminal and view the outputs:

python3 numeric_types.py

Your terminal should show:

10 + 3.14 = 13.14
float(8) = 8.0
int(3.99) = 3

Complex Numbers and More Operations

Python also supports complex numbers, which are of the form a + bj, where a is the real part and b is the imaginary part. Let's do some operations on complex numbers.

Add the following to /home/labex/project/numeric_types.py:

## Working with complex numbers

## Creating a complex number
complex_number = 3 + 4j
print("Complex number:", complex_number)

## Accessing real and imaginary parts
print("Real part:", complex_number.real)
print("Imaginary part:", complex_number.imag)

## Conjugate of a complex number
print("Conjugate:", complex_number.conjugate())

This code demonstrates creating a complex number and accessing its real and imaginary parts.

Execute the code:

python3 /home/labex/project/numeric_types.py

Your terminal should show:

Complex number: (3+4j)
Real part: 3.0
Imaginary part: 4.0
Conjugate: (3-4j)

Summary

In this lab, you embarked on a journey through the numerical world of Python. Starting with the basics of integers and floats, you explored arithmetic operations, experienced type conversions and delved into the realm of complex numbers.

By the end of this lab, you should have a solid understanding of Python's numeric types and the confidence to apply this knowledge in real-world applications.

Not only have you gained technical skills, but you've also contributed to the wonderful Metatropolis by ensuring an efficient and sustainable energy flow. Well done, and keep coding for a brighter future!

Other Python Tutorials you may like