Introduction
Variables are used to store data and the data they store is related to some specific type like text, number, or sequence, in any programming language. Python Datatypes are similar to most of the other programming languages.
Python also has predefined datatypes.
During the declaration of any variable, we do not need to specify it’s a data type in Python, as Python is a dynamically-typed language and Python automatically specifies the datatype.
Data-Types
Data-Type | Categories |
---|---|
1. Text/string | str |
2. Number | int, float, complex |
3. Sequence | list, tuple |
4. Mapping | dict |
5. Set | set, frozenset |
6. Boolean | bool |
7. Binary | bytes, bytearray, memoryview |
type() in Python
Python uses “type()” to determine the type of any datatype, see the example below–
Example
x = 2 y = "violet-cat-415996.hostingersite.com" z = ["list", 1] print(type(x)) print(type(y)) print(type(z))
Output
<class 'int'> <class 'str'> <class 'list'>
Type Casting
Typecasting or type conversion is the conversion of a variable from one data type to another e.g. int to float or float to int.
Example
var1 = 20.3 var2 = int(var1) # to int type var3 = complex(var1) # to complex type var4 = str(var1) # to str type print(var1, "Type is", type(var1)) print(var2, "Type is", type(var2)) print(var3, "Type is", type(var3)) print(var4, "Type is", type(var4))
Output
20.3 Type is <class 'float'> 20 Type is <class 'int'> (20.3+0j) Type is <class 'complex'> 20.3 Type is <class 'str'>
From the next lecture, we will start learning all data types in detail.
Also Read: