Hello Technotizers, in this article we will dive deep into the different data types offered by Python. In programming, variables are used to store data of different data types.
Following are the built-in data types in Python of different categories:
Text Type: string
Numeric Types: integer, float, complex
Sequence Types: list, tuple, range
Mapping Type: dictionary
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
Ø Int data type:
Int, or integer is a whole number which is positive or negative, without decimals of unlimited length.
type() method is used to get the type of the data.
n = 10
print(type(n))
The output is
obtained as followed:
<class 'int'>
Ø Float data type:
Float, or floating point number is a positive or negative number with one or more decimal places.
f1 = 1.53
f2 = -25.52
print(type(f1))
print(type(f2))
The output is obtained as followed:
<class 'float'>
<class 'float'>
Ø Complex data type:
Complex numbers have a real part and an imaginary part as “j”. For e.g. 4+5j is a complex number.
c1 = 2+6j
c2 = -7j
print(type(c1))
print(type(c2))
The output is obtained as followed:
<class
'complex'>
<class
'complex'>
Python
can handle complex numbers and its associated functions using the file “cmath”.
Complex numbers have their uses in many applications related to mathematics and
python provides useful tools to handle and manipulate them.
· Finding the real and imaginary part:
The methods real() and imag() are used for extracting the real and imaginary parts of the complex number.
c = 4+5j
print("Real part:", c.real)
print("Imaginary part:", c.imag)
The output is obtained as followed:
Real part: 4.0
Imaginary part: 5.0
·
Converting real numbers to complex number:
A complex number is represented by “x + yi “. Python converts the real numbers x and y into complex using the function complex(x,y).
import
cmath
x = 3
y = 7
z = complex(x,y)
print(z)
The output is obtained as followed:
(3+7j)
·
Phase of complex number:
Geometrically, the phase of a complex
number is the angle between the positive real
axis and the vector representing the complex number. This is
also known as the argument of
complex number. Phase is returned using phase(), which
takes complex number as argument. The range of phase lies from -pi to +pi. i.e from -3.14 to +3.14.
import cmath
x = 3
y = 5
z = complex(x,y);
c=cmath.phase(z)
print("The phase of complex number is :",c)
The output is obtained as followed:
The
phase of complex number is : 1.0303768265243125
Ø Type conversion in Python:
Python type conversions are used to
convert one data type into another using certain built-in functions which is
useful in day to day and competitive programming.
There are two types of type
conversions in python:
1)
Implicit type conversion:
In this, the Python interpreter automatically converts one
data type to another without any user involvement. Let’s see an example below:
a = 5
print("a is of type:",type(a))
b = 4.5
a = a+b
print("Addition is:", a)
print("a is of type:",type(a))
The output is obtained as followed:
a is of
type: <class 'int'>
b is of
type: <class 'float'>
Addition
is: 9.5
a is of
type: <class 'float'>
So it is
observed that ‘a’ got converted into float type form integer type automatically
after getting added with ‘b’ which was a floating number.
2) Explicit type conversion:
In this, the user has to
manually change the type of the data by using some functions as per their
requirement.
x = 1
y = 2.8
a = float(x)
b = int(y)
c = complex(x)
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))
The output is obtained as follows:
1.0
2
(1+0j)
<class
'float'>
<class
'int'>
<class
'complex'>
1. int(a, base): This function converts any data type to integer. ‘Base’ specifies
the base in which string is if the data
type is a string.
2. float(): This
function is used to convert any data type to a floating-point number.
3. ord() : This
function is used to convert a character to integer.
4. hex() : This
function is to convert integer to hexadecimal string.
5. oct() : This
function is to convert integer to octal string.
Note: Complex numbers cannot be
converted into other data types.
Here, we end this article. Hope it was
helpful. See you then!!
Keep coding and exploring new techs!!
0 comments:
Post a Comment