Show Menu
Cheatography

Different types of Number data types Cheat Sheet (DRAFT) by

Different types of Number data types

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Boolean data type in Python

Boolean type is one of the built-in data types provided by Python, which represents one of the two values i.e. True or False.
Generally, it is used to represent the truth values of the expres­sions. For example, 1==1 is True whereas 2<1 is False.

Python Boolean Type

The output <class ‘bool’> indicates the variable is a boolean data type.

Example: Boolean type

a = True
type(a)

b = False
type(b)
Output:

<class 'bool'>
<class 'bool'>

Evaluate Variables and Expres­sions

We can evaluate values and variables using the Python bool() function. This method is used to return or convert a value to a Boolean value i.e., True or False, using the standard truth testing procedure.
Syntax:

bool([x])

Example: Python bool() method

# Returns False as x is not equal to y
x = 5
y = 10
print(bool(x==y))
 
# Returns False as x is None
x = None
print(bool(x))
 
# Returns False as x is an empty sequence
x = ()
print(bool(x))
 
# Returns False as x is an empty mapping
x = {}
print(bool(x))
 
# Returns False as x is 0
x = 0.0
print(bool(x))
 
# Returns True as x is a non empty string
x = 'GeeksforGeeks'
print(bool(x))
Output
False
False
False
False
False
True
 

Integers and Floats as Booleans

Numbers have zero as a value is considered 
as False, while if they are having value as any 
positive or negative number then it is considered 
as True.

var1 = 0
print(bool(var1))

var2 = 1
print(bool(var2))

var3 = -9.7
print(bool(var3))
Output:

False
True
True

Int type

int (Integers) are the whole number, including negative numbers but not fractions. In Python, there is no limit to how long an integer value can be.

How Python represents integers

Python, however, doesn’t use a fixed number of bit to store integers. Instead, Python uses a variable number of bits to store integers.

The maximum integer number that Python can represent depends on the memory available.

Example 1: Creating int and checking type

num = -8

# print the data type
print(type(num))
Output:

<class 'int'>

Getting the size of an integer

from sys import getsizeof

counter = 100
size = getsizeof(counter)

print(size)
Output:

28

Converting a String to a Float in Python

# python code to convert string
# to float
string = "90"
result = float(string)

print(result)
Output:

90.0
 

Python integer operations

Python integers support all standard operations including:

Addition +
Subtraction –
Multiplication *
Division /

a = 10
b = 20
c = a + b
print(c)
print(type(c))

c = a - b
print(c)
print(type(c))

c = a * b
print(c)
print(type(c))

c =  b/a
print(c)
print(type(c))
30
<class 'int'>
-10
<class 'int'>
200
<class 'int'>
2.0
<class 'float­'>

Python float

Python uses the float class to represent the real numbers.

Python float uses 8 bytes (or 64 bits) to represent real numbers.
Unlike the integer type, the float type uses a fixed number of bytes.

Creating float and checking type

num = 3/4
 
# print the data type
print(type(num))

num = 6 * 7.0
 
print(type(num))
<class 'float­'>
<class 'float­'>

Converting an Integer to a Float in Python

# python code to convert int
# float
number = 90
result = float(number)

print(result)
Output:

90.0