Sunday, 30 August 2020

Python Data Types

 

Python Data Types


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))
Try it Yourself »

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))

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home