Python Numbers and Type Conversions (Casting)

Sovary June 2, 2022 319
3 minutes read

We will learn different numeric in Python and we will explain how to convert from string to numeric . Python have three built-in numeric types integer, float-point numbers and complex numbers. Python not completely strongly-type. It's data type is base on the value which is assign to variable.

Integers

An integer is a number which is not a fraction or contain decimal place and can be negative or positive. Example: 4 is integer but 4.0 or 4.5 are not.

In Python int is defined as integer data type. We can use type() function to check which value is belong to. Numeric types are created when we assign number value to them and data type will identified automatically.

Example #1:

# Assign value to variables
num1 = 5
num2 = -360

# Display result
print(type(num1))
print(type(num2))

Output #1:

<class 'int'>
<class 'int'>

In real world writing down in large number in group of three separated by comma is readable then nothing. For instance, we write 300000 is more confused than 300,000.

In Python will not understand when you assign to variable with group of three digits separated, but we can use underscore (_) instead of comma to distinct the numbers.

Example #2:

# Assign value to variables
num1 = 300000
num2 = 300_000 # Readable

# Display result
print("num1 is type", type(num1))
print("num2 is type", type(num2))

Output #2:

num1 is type  <class 'int'>
num2 is type  <class 'int'>

Floating-Point Numbers

An floating-point is a number which is contain decimal place and can be negative or positive. Example: 4.0 is floating-point. float is data type of floating-point in Python

Example #1:

num1 = 5.0
print(type(num1))

Output #1:

<class 'float'>

 For float data type we can define in three different ways as below

Example #2:

num1 = 100000.0 # 1
num2 = 10_000.0 # 2
# 1e4 = 1e+4
num3 = 1e4      # 3
num4 = -5.7e11
num5 = 3e-4

print(type(num1))
print(type(num2))
print(type(num3))
print(num4)
print(num5)

Output #2:

<class 'float'>
<class 'float'>
<class 'float'>
-570000000000.0
0.0003

Explain #2:

Line 1-2: num1 num2 are the same technique for human readable where place underscore(_) to group of three digits separated.

Line 3-4: we use scientific numbers (e notation) to indicate the power of 10 . e or seems like x10 power by number at its right side. Example: 1e4 = 1x104 = 10000

Math Explain:

E notation is from words exponential notation which means times ten raised to the power of (10n).
Why -5.7e11 equal to -570000000000.0 (zero ten times)?
In math for -5.7 we can write in -57x10-1 and e11 write in 1x1011 so in result -57x10-1 + 1x1011 by the formula xn + xm = xn+m. Finally, -57x1010

Complex Numbers

Complex numbers are create from real number part and imaginary part. We simply write imaginary part with letter j at the end of number.

Example:

comp = 5+3j
print (type(comp))

Output:

<class 'complex'>

Type Conversion (Casting)

We can convert number type to another or vise versa, but we can not convert complex number to other. We use built-in functions int(), float() and complex() to convert between numeric type. These functions can convert from strings as well, but the string must be format in numeric value.

Example:

num1 = 3     # integer
num2 = 3.2   # float
num3 = 32+8j # complex
num4 = "11"  # string

convert_num1 = float(num1)   # convert integer to float-points
convert_num2 = int(num2)     # convert float to integer
convert_num3 = complex(num2) # convert float to complex
convert_num4 = float(num4)     # convert string to float

print(convert_num1, type(convert_num1))
print(convert_num2, type(convert_num2))
print(convert_num3, type(convert_num3))
print(convert_num4, type(convert_num4))

 Output:

3.0 <class 'float'>
3 <class 'int'>
(3.2+0j) <class 'complex'>
11.0 <class 'float'>

Hope you learn and help you to understand basic Python numeric.

Python 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him