In this article you will learn about Python data types.
Python Data Types – Before moving ahead let’s know a little bit about Python.
Introduction – Python is open-source and free programming language. It is easy to use and learn as compared to other languages like C ++, Java, C# and JavaScript. It is highly recommended and allows itself to get integrated with other languages like C ++, C#, etc., One of the most important key factors of Python is it is Object-Oriented Language and had been used by many giant companies for so many purposes such as handling file data, web app, Desktop application, Machine learning and so on.
Python data types – In programming a data type is an attribute of data which tells the interpreter how the programmer intends to use the data. Like most languages Python also supports data types. It includes data types such as numeric or non-numeric and boolean etc. Every different types of data works in their different way.
List of Data Types in Python –
Text Type | str |
Numeric Type | int, float, complex |
Sequence Type | list, tuple, range |
Mapping Type | dict |
Set Type | set, frozenset |
Boolean Type | Bool |
Binary Type | Bytes, bytearray, memoryview |
Let’s get started with an example.
Text Type – A data type which represents written text inside quotation mark.
Example –
String = 'Hello world' # quotation mark can be both single or double print(String)
Numeric – A data type which represents numeric value known as numeric data type. It includes data type of int, float and complex.
Example –
int = 1 float = 1.2 (Decimal number in mathematical term) Complex = 5+8j (syntax = a+bj. Where, a, b = integer and j = alphabet)
Int or integer – An integer is a data type that includes number of some range of mathematical Integers. An integers support negative and positive both types of number.
Example –
x = 10 z = -7 print(x) print(z)

Float – In Python or computer lauguage decimal numbers calls float. It is as same as decimal number in mathematics. Float can be positive or negative both.
Example –
x = 5.2 y = -7.8 print(x) print(y)

Float can also be scientific numbers with an “e” to indicate the power of 10.
Example –
x = 1e7 y = 2E10 z = -5.7e6 print(x) print(y) print(z)

Complex – A number which includes real and imaginary component represented as x+yj. x and y are floats and j is -1(square root of -1 called an imaginary number).
Example –
z = 1+1j y = 2j print(z) print(y)

To know about Python object type.
Example –
x = 10 #int y = 1.1 #float z = 1+1j #complex print(type(x)) print(type(y)) print(type(z))

Sequence Type – A data type which represents how to manage a list of things (numbers, alpha-numeric) that are in order or not.
Example –
List = list(['red', 'yellow', 'green']) Tuple = tuple(['red', 'yellow' , 'green']) Range = range(6) print(List) print(Tuple) print(Range)

Mapping Type – A data type which represents storing data values in key:value pairs.
Example –
Dict = {'name' : 'John', 'age' : 20, 'country' : 'NYC'} print(Dict)

Set Type – A data type which represents to store values in set.
Set = {'red', 'yellow', 'green'} print(Set) x = frozenset(('red', 'yellow' , 'green')) print(x)

Bytes – A method which represents byte size of given integer.
Example –
x = bytes(5) print(x)
bytearray – A method which represents a bytearray object which is an array of the given object.
Example –
x = bytearray(6) print(x)
memoryview = A method which represents Python code to access the internal data of an object that supports the buffer protocol without copying.
Example –
x = memoryview(bytes(8)) print(x)
Boolean – It represents function of True and False. It is used when one of the two conditions is either true or false.
Example –
x = 10 if x > 0: print(True) else: print(False)

Note: You will learn later about if…else loop.
name = 'codingstreets' if type(name) == str: print(True) else: print(False)

Note: You will learn later about type() function.
Type Conversion – It defined as conversion of one type of data into another type of data with the help of int(), float(), complex().
Example –
x = 12 y = 12.1 z = 5j print(x) print(y) print (z)

# conversion of int into float
x = float(x) print(x)
# conversion of float into int
y = int(y) print(y)
# conversion of int into complex
z = complex(x) print(z)

Note: Complex data type can’t be converted into another data type.
In Python, specific data type can be set to variables by using following constructor fuctions.
Examples –
x = str(‘I like to read articles on codingstreets’) x = int(1) x = float(1.2) x = complex(1) x = list((‘red’, ‘yellow’ , ‘green’)) x = tuple((‘red’, ‘yellow’ , ‘green’)) x = range(6) x = dict(name = ‘John’ , Add = ‘NYC’) x = set((‘red’, ‘yellow’ , ‘green’)) x = frozenset((‘red’, ‘yellow’ , ‘green’)) x = bool(1) x = bytes(2) x = bytearray(6) x = memoryview(bytes(8))
If you find anything incorrect in above discussed topic and have any further question, please comment down below.
Follow us