GeeksBro

FREE EXCLUSIVE TIPS

Python Tutorials, Tricks & Tips, Projects, etc..,

Python Data Types | Complete Tutorial | Data Type Conversion

In this tutorial, you are going to learn about Python Data Types. Python has mainly 7 different data types. You will learn about all of them in this tutorial.

Python Data Types

You can find a lot of data types in Python. Every value in any Programming language is a data type.

python data types

You can use the type() built-in function to check the type of a variable or a value in Python. See the example.....

>>> type('geeksbro')
<class 'str'>
>>> type(7)
<class 'int'>

Let's see some mainly using data type in Python.....

#1. Numbers

Numbers are one of the most important data types in Python programming language. Python has three different type of built-in number types. They are

int
float
complex

Integers can be of any length in Python. They don't have any limits if the memory was available. See the example.....

>>> integer = 12345
>>> integer.bit_length()
14
>>> integer = 12345677890
>>> integer.bit_length()
34
>>> integer = 12345678900987654321
>>> integer.bit_length()
64
>>> type(integer)
<class 'int'>

Floats can have decimals up to 15 places. Floats and Integers are separated by the decimals. 1 is an integer whereas 1.0 is a float. See the example.....

>>> float = 1.6
>>> type(float)
<class 'float'>
>>> integer = 1
>>> float = 1.0
>>> type(integer)
<class 'int'>
>>> type(float)
<class 'float'>

Complex numbers are in the form a + bj, where a is a real part and b is an imaginery part. See the example.....

>>> c = complex(2, 3)
>>> c
(2+3j)
>>> c.real
2.0
>>> c.imag
3.0

You can also create complex numbers without built-in complex function. See the example.....

>>> c = 2 + 3j
>>> c
(2+3j)

#2. Booleans

Boolean data types are of 2 types in Python. They are True and False. These can be used to assign or compare the boolean values.

In Python, logical expressions return the boolean values. In Python, True is equalled to 1 and False is equals to 0. Python turns the boolean values into integers explicitly during the arithmetic operations. See the example.....

>>> a = True
>>> b = False
>>> if a:
... print('It is True')
...
It is True
>>> string = 'geeksbro'
>>> string == 'geeksbro'
True
>>> string == 'geeks'
False
>>> a + b
1
>>> b - a
-1

#3. Strings

A Sequence of one or more characters enclosed in either single, double or triple quotes are refered as Strings in Python. See the example.....

>>> string = 'i am enclosed in single quotes'
>>> string
'i am enclosed in single quotes'
>>> string = "i am enclosed in double quotes"
>>> string
'i am enclosed in double quotes'
>>> string = """i
... am
... enclosed
... in triple quotes"""
>>> string
'i\nam\nenclosed\nin triple quotes'

#4. Lists

List is an ordered sequence of elements. Elements in the list need no to be same data type.

Lists are declared using square brackets [ ]. See the example.....

Program:-

l = [7, 7.0, 'geeksbro', True]
print(l[2])
print(l[:2])
print(l[2:])

Output:-

geeksbro
[7, 7.0]
['geeksbro', True]

Lists are mutable, meaning elements in the list can be changed. See the example.....

>>> l[2] = 'geeksbro'
>>> l
[1, 2, 'geeksbro', 4]

#5. Tuples

Tuple is an ordered sequence of elements like a list. The only difference between tuple an list is, tuples are immutable whereas lists are mutable.

Tuples are faster than lists. They are declared using parentheses ( ). See the example.....

>>> t = (7, 7.0, 'geeksbro', True)

You can use indexing same as lists to print the elements. See the example.....

Program:-

t = (7, 7.0, 'geeksbro', True)
print(t[2])
print(t[:2])
print(t[2:])

Ouput:-

geeksbro
(7, 7.0)
('geeksbro', True)

#6. Sets

Set is an unordered collection of unique elements. Sets are declared using the curly brackets { }. See the example.....

>>> s = {1, 2, 2, 3}
>>> s
{1, 2, 3}

Sets eliminate the duplicate values. Set doesn't support the indexing.

You can also perform operations like union, intersection, the difference between the two sets.

#7. Dictionaries

Dictionary is formed with the key-value pairs. You must use the key to retrieve the data from a dictionary.

Dictionaries use curly brackets { } to initialize. In Dictionary each item is in the form key : value. Key can be, any one of the data types int and str. Value can be any data type.

See the example.....

Program:-

d = {1 : 'one', 'one' : 1}
print(type(d))
print(d[1])
print(d['one'])

output:->


one
1

Type Conversion In Python

You can convert one data type to another data type in Python. Python ha built-in function like int(), str(), float(), list(), tuple(), set(), dict() for the conversions of data types.

#1. Float to Integer and Vice Versa

>>> a = 2.8
>>> int(a)
2
>>> b = -2.8
>>> int(b)
-2
>>> a = 2
>>> float(a)
2.0
>>> b = -2
>>> float(b)
-2.0

#2. String to Integer and Float and Vice Versa

String must contain a valid number. You can convert any integer or float to string.

>>> a = 1
>>> str(a)
'1'
>>> a = 1.0
>>> str(a)
'1.0'
>>> a = '2'
>>> int(a)
2
>>> float(a)
2.0
>>> a = '2g'
>>> int(a)
Traceback (most recent call last):
File "", line 1, in 
ValueError: invalid literal for int() with base 10: '2g'

#3. List to Set and Tuple

>>> l = [1, 2, 2, 3]
>>> set(l)
{1, 2, 3}
>>> tuple(l)
(1, 2, 2, 3)

#4. Set to Tuple and List

>>> s = {1, 2, 2, 3}
>>> list(s)
[1, 2, 3]
>>> tuple(s)
(1, 2, 3)

#5. Tuple to Set and List

>>> t = (1, 2, 'geeksbro')
>>> set(t)
{1, 2, 'geeksbro'}
>>> list(t)
[1, 2, 'geeksbro']

#6. List, Tuple to Dictionary

You must have pairs to convert list or tuple to a dict. See the example.....

>>> l = [['one', 1], ['two', 2]]
>>> dict(l)
{'one': 1, 'two': 2}
>>> l = [('one', 1), ('two', 2)]
>>> dict(l)
{'one': 1, 'two': 2}
>>> t = (['one', 1], ['two', 2])
>>> dict(t)
{'one': 1, 'two': 2}
>>> t = (('one', 1), ('two', 2))
>>> dict(t)
{'one': 1, 'two': 2}

EndNote

If you have any Doubts Comment below or Contact us at GeeksBro Facebook Page.


Post a Comment