Data types in python with examples


 



Built-in Data Types

In programming, data type is an important concept.

Variables can store data of different types, and different types can do different things.

Python has the following data types built-in by default, in these categories:

Text Type:str
Numeric Types:intfloatcomplex
Sequence Types:listtuplerange
Mapping Type:dict
Set Types:setfrozenset
Boolean Type:bool
Binary Types:



                           
bytesbytearraymemoryview        






Getting the Data Type

You can get the data type of any object by using the type() function:


Example

Print the data type of the variable x:


x = 5

print(type(x))


Setting the Data Type

In Python, the data type is set when you assign a value to a variable:


ExampleData TypeTry it
x = "Hello World"strTry it »
x = 20intTry it »
x = 20.5floatTry it »
x = 1jcomplexTry it »
x = ["apple", "banana", "cherry"]listTry it »
x = ("apple", "banana", "cherry")tupleTry it »
x = range(6)rangeTry it »
x = {"name" : "John", "age" : 36}dictTry it »
x = {"apple", "banana", "cherry"}setTry it »
x = frozenset({"apple", "banana", "cherry"})frozensetTry it »
x = TrueboolTry it »
x = b"Hello"bytesTry it »
x = bytearray(5)bytearrayTry it »
x = memoryview(bytes(5))memoryviewTry it »


Setting the Specific Data Type

If you want to specify the data type, you can use the following constructor functions:

ExampleData TypeTry it
x = str("Hello World")strTry it »
x = int(20)intTry it »
x = float(20.5)floatTry it »
x = complex(1j)complexTry it »
x = list(("apple", "banana", "cherry"))listTry it »
x = tuple(("apple", "banana", "cherry"))tupleTry it »
x = range(6)rangeTry it »
x = dict(name="John", age=36)dictTry it »
x = set(("apple", "banana", "cherry"))setTry it »
x = frozenset(("apple", "banana", "cherry"))frozensetTry it »
x = bool(5)boolTry it »
x = bytes(5)bytesTry it »
x = bytearray(5)bytearrayTry it »
x = memoryview(bytes(5))memoryviewTry it »



python data types size
data types in python with examples
built-in data types in python
how many data types in python
python data types journaldev
python data type check
data types in python 3
python data types cheat sheet




Post a Comment

Thanks for comment.

Previous Post Next Post