Sitemap

Python Tutorial | Python Numbers and Type Conversion

4 min readMar 22, 2023

--

Python is a versatile programming language that provides built-in support for various numeric types and type conversion functions. Understanding Python numbers and type conversion is essential for performing mathematical operations, data manipulation, and data validation. This comprehensive guide will provide you with an in-depth understanding of Python numbers and type conversion, complete with coding examples and explanations.

Python Numeric Types

Python supports three primary numeric types: integers, floating-point numbers, and complex numbers.

Integers

Integers are whole numbers, both positive and negative, without any decimal points. In Python, integers can be written in decimal, binary, octal, or hexadecimal notation.

Example:

a = 42  # Decimal integer
b = 0b101010 # Binary integer
c = 0o52 # Octal integer
d = 0x2A # Hexadecimal integer

print(a, b, c, d) # Output: 42 42 42 42

Floating-Point Numbers

Floating-point numbers, or floats, are real numbers that contain a decimal point. They can be written in standard decimal notation or scientific notation.

Example:

a = 3.14  # Standard decimal notation
b = 2.5e-2 # Scientific notation

print(a, b) # Output: 3.14 0.025

Complex Numbers

Complex numbers are numbers with a real part and an imaginary part, written as x + yi, where x is the real part, y is the imaginary part, and i is the imaginary unit (the square root of -1).

a = 3 + 4j
b = complex(3, 4)

print(a, b) # Output: (3+4j) (3+4j)

Arithmetic Operations with Python Numbers

Python supports various arithmetic operations for numeric types, such as addition, subtraction, multiplication, division, exponentiation, and more.

Example:

a = 10
b = 3.0

add = a + b # Addition
sub = a - b # Subtraction
mul = a * b # Multiplication
div = a / b # Division
exp = a ** b # Exponentiation
mod = a % b # Modulus
floor_div = a // b # Floor division

print(add, sub, mul, div, exp, mod, floor_div)
# Output: 13.0 7.0 30.0 3.3333333333333335 1000.0 1.0 3.0

Python Type Conversion

Type conversion, or type casting, is the process of converting a value from one data type to another. Python provides several built-in functions for type conversion, such as int(), float(), and complex().

Converting Between Numeric Types

You can utilize the int(), float(), and complex() functions to perform conversions between Python numeric types.

Get Nilimesh Halder, PhD’s stories in your inbox

Join Medium for free to get updates from this writer.

Example:

a = 3.14
b = 42

int_value = int(a) # Convert float to int (decimal part is truncated)
float_value = float(b) # Convert int to float
complex_value = complex(b) # Convert int to complex (adds an imaginary part of 0)
print(int_value, float_value, complex_value) # Output: 3 42.0 (42+0j)

Converting Strings to Numeric Types

You can also convert string representations of numbers to their corresponding numeric types using the same functions. However, the string must be a valid representation of the desired numeric type.

Example:

num_str1 = "123"
num_str2 = "45.6"

int_value = int(num_str1) # Convert string to int
float_value = float(num_str2) # Convert string to float
complex_value = complex(num_str1) # Convert string to complex
print(int_value, float_value, complex_value) # Output: 123 45.6 (123+0j)

Note that attempting to convert a string with an invalid format will result in a ValueError.

Converting Numeric Types to Strings

To convert a numeric value to a string, you can use the str() function.

Example:

a = 42
b = 3.14
c = 3 + 4j

str_a = str(a) # Convert int to string
str_b = str(b) # Convert float to string
str_c = str(c) # Convert complex to string
print(str_a, str_b, str_c) # Output: '42' '3.14' '(3+4j)'

Rounding and Formatting Numbers

Rounding Numbers

You can use the round() function to round a floating-point number to the nearest integer or a specified number of decimal places.

Example:

a = 3.14159

rounded_int = round(a) # Round to the nearest integer
rounded_float = round(a, 2) # Round to 2 decimal places

print(rounded_int, rounded_float) # Output: 3 3.14

Formatting Numbers

Python’s string formatting capabilities allow you to display numeric values in various formats, such as fixed-point, scientific, or percentage notation.

Example:

a = 1234567.89

fixed_point = "{:.2f}".format(a) # Format as fixed-point with 2 decimal places
scientific = "{:.2e}".format(a) # Format as scientific notation with 2 decimal places
percentage = "{:.1%}".format(a / 10000000) # Format as percentage with 1 decimal place

print(fixed_point, scientific, percentage) # Output: '1234567.89' '1.23e+06' '12.3%'

Summary

Understanding Python numbers and type conversion is essential for performing various mathematical operations and data manipulations in your programs. This comprehensive guide has provided you with an in-depth understanding of Python’s numeric types, arithmetic operations, type conversion functions, and number formatting techniques. With this knowledge, you can confidently work with numbers in Python and create more efficient and versatile programs.

If you like this article, please have a look at SETScholars and WACAMLDS. Thanking you very much for your time. Cheers!

--

--

Nilimesh Halder, PhD
Nilimesh Halder, PhD

Written by Nilimesh Halder, PhD

Principal Analytics Specialist - AI, Analytics & Data Science. Find my Machine Learning Case Notes from: https://nilimesh.substack.com/