Professional Documents
Culture Documents
There is no need specify the datatype while creating or defining a variable in Python. But we
do have different data types available in Python
Variables can hold values, and every value has a data-type. Python is a dynamically typed
language; hence we do not need to define the type of the variable while declaring it. The
interpreter implicitly binds the value with its type.
In Python, the data types are associated with the values and not with the variable. Every
value in Python has a data type.
The explicit values which we use in our python code (script or program) are known as
literals.
Examples like : 36, 75.25 and 'Science' are called literals
print(36)
print(75.25)
print('Science')
print(20+80)
For example the literals 36, 20 and 80 are of Integer data type, 75.25 is of Float data type
and the value 'Science' is of String data type
Everything in Python is an object. That means even the data types are classes and variables
are instances (objects) of these classes
There are various data types in Python.
Now you know how to interact with the Python interpreter and execute Python code and
Now let us see the basic data types that are built into Python.
Python Numbers
Integers, floating point numbers and complex numbers fall under Python numbers category.
We can use the type() function to know which class a variable or a value belongs to.
Similarly, the isinstance() function is used to check if an object belongs to a particular class.
a=5
print(a, "is of type", type(a))
a = 2.0
a = 1+2j
print(a, "is complex number?", isinstance(1+2j,complex))
Output
A floating-point number is accurate up to 15 decimal places. Integer and floating points are
separated by decimal points. 1 is an integer, 1.0 is a floating-point number.
Complex numbers are written in the form, x + yj, where x is the real part and y is the
imaginary part.
>>> a = 1234567890123456789
>>> a
1234567890123456789
>>> b = 0.1234567890123456789
>>> b
0.12345678901234568
>>> c = 1+2j
>>> c
(1+2j)
In Python, numeric data type represents the data that has a numeric value. The numeric
value can be an integer, floating number, or even complex number. These values are defined
as int, float, and complex classes in Python.
1. Integers :
It consists of positive or negative whole numbers (without fraction or decimal).
Integers are zero, positive or negative whole numbers without a fractional part and having
unlimited precision, e.g. 0, 100, -10. The followings are valid integer literals in Python.
Integers in Python 3 are of unlimited size. There is no maximum limit or bounds for the
integer datatype in Python. So effectively Integer has no limit as how large an integer value
can be. The integer type is unbounded
>>> sys.maxsize
2147483647
That does not mean that the maximum int is limited to 2 billion! It means that the size of the
object containing the integer has a maximum size of 2 billion bytes. I.e. a very very large
number
>>> sys.maxsize
9223372036854775807
Python can handle arbitrarily large integer numbers in computation
numx=958731963267543298132764509873506365343428250233984398382362323623265
3625212798963
print(num)
print(type(num))
In Python, integer variables are defined by assigning a whole number to a variable. Python's
type() function can be used to determine the data type of a variable.
Examples : 2, 0, -12
>>> 0
0
>>> 100
100
>>> -10
-10
>>> 1234567890
1234567890
>>> y=5000000000000000000000000000000000000000000000000000000
5000000000000000000000000000000000000000000000000000000
Python interprets a sequence of decimal digits without any prefix as a decimal value number
Integers can be binary, octal, and hexadecimal values.
216
>>> 0o12 # octal
10
All integer literals or variables are objects of the int class. Use the type() method to get the
class name
>>>type(100)
<class 'int'> # type of x is int
>>> x=1234567890
>>> type(x)
>>> y=5000000000000000000000000000000000000000000000000000000
>>> type(y) # type of y is int
<class 'int'>
Leading zeros in non-zero integers are not allowed e.g. 000123 is invalid number, 0000 is 0.
>>> x=01234567890
SyntaxError: invalid token
Python does not allow comma as number delimiter. Use underscore _ as a delimiter instead.
>>> x=1_234_567_890
>>> x
1234567890
Note that integers must be without a fractional part (decimal point). It it includes a fractional
then it becomes a float.
>>> x=5
>>> type(x)
<class 'int'>
>>> x=5.0
>>> type(x)
<class 'float'>
>>> int('100')
100
>>> int('-10')
-10
>>> int('5.5')
5
Binary
A number having 0b with eight digits in the combination of 0 and 1 represent the binary
numbers in Python. For example, 0b11011000 is a binary number equivalent to integer 216.
>>> x=0b11011000
>>> x
216
>>> x=0b_1101_1000
>>> x
216
>>> type(x)
<class 'int'>
Octal
A number having 0o or 0O as prefix represents an octal number. For example, 0O12 is
equivalent to integer 10.
>>> x=0o12
>>> x
10
>>> type(x)
<class 'int'>
Hexadecimal
A number with 0x or 0X as prefix represents hexadecimal number. For example, 0x12 is equivalent to
integer 18.
>>> x=0x12
>>> x
18
>>> type(x)
<class 'int'>
Float :
In Python, floating point numbers (float) are positive and negative real numbers with a
fractional part denoted by the decimal symbol . or the scientific notation E or e, e.g. 1234.56,
3.142, -1.55, 0.23.
The decimal point in a real number is called floating point because it can be placed
anywhere. The decimal point is not fixed at a particular position. It can be floating at any
position
Integer numbers and floating pointing numbers are differentiated by the presence of
decimal point. 1 is an integer number and 1.0 is a floating-point number
Optionally the character 'e' or 'E' followed by a positive number or a negative number can be
appended to specify scientific notation (or exponential notation)
>>> f=1.2
>>> f
1.2
>>> type(f)
<class 'float'>
>>> f=123_42.222_013
>>> f
12342.222013
Scientific notation is used as a short representation to express floats having many digits. For
example: 345.56789 is represented as 3.4556789e2 or 3.4556789E2
>>> f=1e3
>>> f
1000.0
>>> f=1e5
>>> f
100000.0
>>> f=3.4556789e2
>>> f
345.56789 # 3.4556789 x 100
>>> f=1e-2
>>> f
0.01 # 1/100
>>> float('5.5')
5.5
>>> float('5')
5.0
-5.0
>>> float('1e3')
1000.0
The float beyond its maximum size referred as "inf", "Inf", "INFINITY", or "infinity".
>>> float('-Infinity')
-inf
>>> float('inf')
inf
We found out the maximum value that a float variable can hold with the sys.float_info.max
The output shows that the upper limit of a float variable is 1.7976931348623157e+308
1.7976931348623157e+308
>>>
>>> sys.float_info.min
2.2250738585072014e-308
>>> 1.79e308
1.79e+308
>>> 1.8e308
inf
>>> 5e-324
5e-324
>>> 1e-325
0.0
3. Complex Number
Python is a very versatile language for dealing with numerical data. It also supports working
on both real and imaginary numbers.
A complex number is a number with real and imaginary components. For example, 5 + 6j is a
complex number where 5 is the real component and 6 multiplied by j is an imaginary
component.
In mathematics, Complex numbers are the numbers that are expressed in the form of a+ib
where, a and b are real numbers and ‘i’ is an imaginary number called “iota”.
>>> cn = 4+2j
>>> cn
(4+2j)
>>> print(cn)
(4+2j)
>>> type(cn)
<class 'complex'>
myNum= 3+2j
print(type(myNum))
>>> 2 + 3j
(2+3j)
>>> 4j
4j
>>> 0 + 4j
4j
>>> 2 + 0j
(2+0j)
>>> 8 - 2j
(8-2j)
>>> -2 + 3j
(-2+3j)
>>> 1j * 1j
(-1+0j)
>>> 2j * 2j
(-4+0j)
>>> c = 3 +6j
>>> print(type(c))
<class 'complex'>
>>> print(c)
(3+6j)
>>> c1 = complex(3,6)
>>> print(type(c1))
<class 'complex'>
>>> print(c1)
(3+6j)
You must use j or J as imaginary component. Using other character will throw syntax error.
>>> a=5+2k
>>> a=5+j
The complex() method returns a complex number when real and imaginary parts are
provided, or it converts a string to a complex number.
complex([real[, imag]])
It takes two numeric parameters, the first one is a real number which is mandatory and the
second one is an imaginary number which is an optional paramter
If the string passed to this method is not a valid complex number, ValueError exception is
raised.
Example:
>>> complex(4)
(4+0j)
>>> complex(5,3)
(5+3j)
>>> r=8
>>> i=2
>>> c=complex(r,i)
>>> print(c)
(8+2j)
Extracting real and imaginary part of a complex number
In a complex number (a+bj), “a” is called the real part and “b” is called the imaginary part.
We can extract real part of the complex number using an attribute named “real” which
contains value “a” and imaginary part using the attribute “imag” which contains the value
“b” as follows.
To access the real part of a complex number, we can use the built-in real attribue function
and similarly use the imag attribute to access the imaginary part
>>> c = (3 + 6j)
>>> #Real Part of complex number
myNum= complex(3,2)
print(myNum.real)
print("Imaginary part of the complex Number is:")
print(myNum.imag)
Output
(3+2j)
Real part of the complex Number is:
3.0
2.0
Boolean Data Type:
In programming, you often want to check if a condition is true or not and perform some
actions based on the result.
To represent true and false, Python provides you with the boolean data type. The boolean
value has a technical name as bool.
The boolean data type has two values: True and False.
Note that the boolean values True and False start with the capital letters (T) and (F).
is_active = True
is_admin = False
To find out if a value is True or False, you use the bool() function. For example:
>>> bool('Hi')
True
>>> bool('')
False
>>> bool(100)
True
>>> bool(0)
False
As you can see clearly from the output, some values evaluate to True and the others
evaluate to False.
Falsy and Truthy values
When a value evaluates to True, it’s truthy. And if a value evaluates to False, it’s falsy.
False
None
An empty list []
An empty tuple ()
An empty dictionary {}
The truthy values are the other values that aren’t falsy.
String Data Type:
A triple quoted string can be enclosed in three single quotes ‘’’ or three double quotes “””
my_string = 'Hello'
print(my_string)
my_string = "Hello"
print(my_string)
my_string = '''Hello'''
print(my_string)
print(my_string)
>>> type(uname)
<class 'str'>
>>> type('Hello World')
<class 'str'>
<class 'str'>
>>> msg="Jun 29th is India's National Statistics Day"
>>> print(msg)
>>> print(quote)
The famous quote "Time is money" is from Benjamin Franklin
>>> message='''Our chef Manohar's special dish is "Paneer Afgani" is available today'''
>>> print(message)
>>> mesg="""Our chef Manohar's special dish is "Paneer Afgani" is available today"""
>>> print(mesg)