What is 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: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
None Type: NoneType

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" str Try it »
x = 20 int Try it »
x = 20.5 float Try it »
x = 1j complex Try it »
x = ["apple", "banana", "cherry"] list Try it »
x = ["apple", "banana", "cherry"] tuple Try it »
x = range[6] range Try it »
x = {"name" : "John", "age" : 36} dict Try it »
x = {"apple", "banana", "cherry"} set Try it »
x = frozenset[{"apple", "banana", "cherry"}] frozenset Try it »
x = True bool Try it »
x = b"Hello" bytes Try it »
x = bytearray[5] bytearray Try it »
x = memoryview[bytes[5]] memoryview Try it »
x = None NoneType Try 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"] str Try it »
x = int[20] int Try it »
x = float[20.5] float Try it »
x = complex[1j] complex Try it »
x = list[["apple", "banana", "cherry"]] list Try it »
x = tuple[["apple", "banana", "cherry"]] tuple Try it »
x = range[6] range Try it »
x = dict[name="John", age=36] dict Try it »
x = set[["apple", "banana", "cherry"]] set Try it »
x = frozenset[["apple", "banana", "cherry"]] frozenset Try it »
x = bool[5] bool Try it »
x = bytes[5] bytes Try it »
x = bytearray[5] bytearray Try it »
x = memoryview[bytes[5]] memoryview Try it »

Test Yourself With Exercises

Exercise:

The following code example would print the data type of x, what data type would that be?

Start the Exercise



Data types 

Data types are the classification of data items. Data types represents a kind of value which determines what can be done to that data.


What are the different types of data in Python?

Data TypesExamplesExplanationMutable/Immutable?
Strings "Hello!", "23.34" Text - anything between
" "  becomes string
Immutable
Integers 5364 Whole numbers Immutable
Floats 3.1415 Decimal Numbers Immutable
Booleans True, False Truth values that represent Yes/No Immutable
Lists [1,2,3,4,5] A collection of data,
sits between  [ ]  
Mutable
Tuples [1,2,3,4,5] A collection of data,
sits between  [ ]  
Immutable
Dictionaries {"a":1, "b":2, "c":3} A collection of data, 
sits between  { }  
Mutable

How do you set data types?

Data types are set when you assign a value to a variable.

ExamplesTypeExplanation
ex_1 = "Hello World" string The data assigned sits in between  " " 
ex_2 = 254 integer The data assigned does not sit in between  " " , and is a whole number
ex_3 = 25.43 float The data assigned does not sit in between  " " , and is a decimal number
ex_4 = ["Anna", "Bella", "Cora"] list A list of strings - Data assigned sits in between  " " , within  [ ]  

How do you check data types?

Use the  type[ ]  function to check data types.

 type[x]  determines and returns what is the type of the input x


Click the triangle button to run the codes and see the output:

Type conversion

To convert variables from one type to another [i.e. integers to floats], we use type conversions as follows:

Data TypeSyntax
strings str[]
integer int[]
floats float[]
lists list[]


Application

What is data types explain with example?

data type.

What are the 5 data types in Python?

Python has five standard Data Types:.
Numbers..
String..
Tuple..
Dictionary..

What are the 4 types of data in Python?

Number stores numeric values. The integer, float, and complex values belong to a Python Numbers data-type. Python provides the type[] function to know the data-type of the variable. Similarly, the isinstance[] function is used to check an object belongs to a particular class.

What are the 5 data types and examples?

Common examples of data types.
Boolean [e.g., True or False].
Character [e.g., a].
Date [e.g., 03/01/2016].
Double [e.g., 1.79769313486232E308].
Floating-point number [e.g., 1.234].
Integer [e.g., 1234].
Long [e.g., 123456789].
Short [e.g., 0].

Chủ Đề