- Python Basics
- Python Home
- Python History
- Python Applications
- Python Features
- Python Versions
- Python Environment Setup
- Python Basic Syntax
- Python end (end=)
- Python sep (sep=)
- Python Comments
- Python Identifiers
- Python Variables
- Python Operators
- Python Ternary Operator
- Python Operator Precedence
- Python Control & Decision
- Python Decision Making
- Python if elif else
- Python Loops
- Python for Loop
- Python while Loop
- Python break Statement
- Python continue Statement
- Python pass Statement
- Python break Vs continue
- Python pass Vs continue
- Python Built-in Types
- Python Data Types
- Python Lists
- Python Tuples
- Python Sets
- Python frozenset
- Python Dictionary
- List Vs Tuple Vs Dict Vs Set
- Python Numbers
- Python Strings
- Python bytes
- Python bytearray
- Python memoryview
- Python Misc Topics
- Python Functions
- Python Variable Scope
- Python Enumeration
- Python import Statement
- Python Modules
- Python operator Module
- Python os Module
- Python Date & Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes & Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class Vs Static Method
- Python @property Decorator
- Python Regular Expressions
- Python CGI Programming
- Python Network Programming
- Python Send E-mail
- Python Multi-threading
- Python XML Processing
- Python MySQL Database
- Python GUI Programming
- Python Event Handling
- Python Keywords
- Python All Keywords
- Python and
- Python or
- Python not
- Python True
- Python False
- Python None
- Python in
- Python is
- Python as
- Python with
- Python yield
- Python return
- Python del
- Python from
- Python lambda
- Python assert
- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python read()
- Python write()
- Python open()
- Python Examples
- Python Examples
- Python Test
- Python Online Test
- Give Online Test
- All Test List
Python Data Types
Data types in Python, is basically an attribute of data which tells the compiler or interpreter, how to interpret its value. For example, if we have a numeric types of data, means that, it can be used to perform arithmetic operations.
Python 7 Built-in Data Types
Data type represents the type of value and determines how the value can be used in a Python program. Here are the list of 7 built-in data types:
- Text Type
- Numeric Types
- Sequence Types
- Mapping Type
- Set Types
- Boolean Type
- Binary Types
Let's describe briefly about these 7 built-in data types, one by one along with example programs. But before starting, let's first understand how can we get the type of data, a variable holds.
How to Get the Type of Data, an Object (Variable) Holds ?
To find and print the type of data, a variable holds in Python, we need to use type method. Here is an example program, demonstrates it:
myvar = 10 print(type(myvar))
The output produced by above Python program, is shown in the snapshot given below:
As you can see from the above output, because the variable myvar holds a value 10 that is an integer value, therefore the output produced, saying the variable myvar is of the class int.
Python Text Data Type
The str class comes under the category of text data type in Python. This data type holds all the string values. Here is an example:
myvar = 'c' print(type(myvar)) myvar = "c" print(type(myvar)) myvar = "codescracker.com" print(type(myvar)) myvar = "Hey, What's Up!" print(type(myvar))
Here is its sample output:
The above program can also be created as:
a, b, c, d = 'c', "c", "codescracker.com", "Hey, What's Up!" print(type(a)) print(type(b)) print(type(c)) print(type(d))
You'll get the same output as of previous program's output.
Python Numeric Data Types
Under the category of numeric data types, we have:
- int - For example: 123
- float - For example: 3.49
- complex - For example: 95+4j. Here 95 is a real, whereas 4 is an imaginary part
Here is an example program, uses numeric data types. I've used three variables with three different numeric values such as int, float, and complex:
a, b, c = 123, 3.49, 95.4j print(type(a)) print(type(b)) print(type(c))
The output produced by above program will be:
<class 'int'> <class 'float'> <class 'complex'>
Python Sequence Data Types
This is another and important data types we must have to aware about. This data type deals with values in sequence. A sequence allows us to store multiple values of same/different types in a same variable. These are the list of sequence types:
- list - Set of multiple elements (values) separated by commas. All elements must be stored within square brackets, i.e. []. List can be changed, means list is mutable.
- tuple - Same as list, but all elements must be stored within round brackets, i.e. (). And tuple can not be changed, means tuple is immutable.
- range - It generates an immutable sequence. For example, range(5) generates or returns 0, 1, 2, 3, 4
Note - To learn about these three topics in detail, refer to its separate tutorial.
Let's take an example, uses all these three types of sequence in Python:
var = [1, "codescracker", True, 23.4, 234] print(type(var)) var = (1, "codescracker", True, 23.4, 234) print(type(var)) var = range(10) print(type(var))
The output produced by above program will be:
<class 'list'> <class 'tuple'> <class 'range'>
Python Mapping Data Type
The dictionary comes under the category of mapping data type in Python. In dictionary, each item are in the pair of key and value. Since, every item (element) are in the form of key and value pair, therefore it comes under the category of mapping data type.
Like list, dictionary is also mutable. Here is an example shows the mapping type in Python:
var = {'day': 'Thursday', 'month': 'September', 'year': '2021'} print(type(var)) var = {1: 'day', 2: 'month', 3: 'year'} print(type(var))
Now the output produced by above program will be:
<class 'dict'> <class 'dict'>
Python Set Data Types
Under this data type category, we have basically two types. Here are the list of these two types:
- set - It allows to store set of multiple elements, separated by commas. All elements must be stored within curly braces, i.e. {}. Set is mutable.
- frozenset - It also allows to store multiple elements. To create it, first create a list, then use frozenset() to freeze the list. The frozenset is immutable.
Here is an example, uses both type.
var = {"Liam", "Noah", "Oliver"} print(type(var)) var = ["Elijah", "William", "Benjamin", "Lucas"] var = frozenset(var) print(type(var))
Now, if you execute this program, then the output produced will exactly looks like:
<class 'set'> <class 'frozenset'>
Python Boolean Data Type
The boolean data type is used when we need to evaluated one of the two possible values say True and False. Here is an example, demonstrates the boolean data type in Python:
var = True print(type(var)) var = False print(type(var)) print(type(True)) print(type(False))
The output produced by above Python program, demonstrating boolean data type will be:
<class 'bool'> <class 'bool'> <class 'bool'> <class 'bool'>
Python Binary Data Types
Most of the time, binary data types in Python are used to improve the performance. Here are the list of three binary types:
- bytes - Values of bytes type are basically a sequence of bytes, that are ready to store in the memory or disk. Use either bytes() to convert anything like strings, number etc. into a sequence of bytes. Or just put strings or numbers etc inside b' and '. For example, b'codescracker.com' or bytes("codescracker.com"). Bytes object is immutable.
- bytearray - It is an array of bytes. So it is similar to bytes. But unlike bytes, it is mutable.
- memoryview - A memoryview object exposes the C level buffer interface as a Python object. That is, the memoryview() used to create memoryview object, of bytes type value passed as its argument.
Here is an example, demonstrates these three binary types.
var = b"codescracker" print(type(var)) var = bytes("codescracker", "utf-8") print(type(var)) var = bytearray(10) print(type(var)) var = memoryview(bytes(10)) print(type(var))
The snapshot given below shows the sample output produced by above Python program:
From above program, the following statement:
var = bytes("codescracker", "utf-8")
encodes the string codescracker using utf-8 encoding method.
Mega Program on Python Data Type
This is the last example program, covering all the data types in Python. First I've initialized all type of values to some variables, then printed the values and types. Let's take a look:
a = 'c' b = "c" c = "codescracker" d = 12 e = 43.93 f = 43j g = ["Henry", "Alexander"] h = ("Mason", "Michael", "Ethan") i = range(5) j = {"Name": "Daniel", "Age": 38} k = {"Owen", "Aiden"} l = frozenset(g) m = True n = False o = bytes(c, "utf-8") p = bytearray(b, "utf-8") q = bytearray(c, "utf-8") r = memoryview(bytes(d)) s = memoryview(bytes(c, "utf-8")) t = memoryview(bytearray(d)) u = memoryview(bytearray(c, "utf-8")) print("--------------------------------------------------------------------") print("Variable\t Value\t\t\t\t\t\t\t\t Type") print("--------------------------------------------------------------------") print("a\t\t\t", a, "\t\t\t\t\t\t\t\t\t", type(a)) print("b\t\t\t", b, "\t\t\t\t\t\t\t\t\t", type(b)) print("c\t\t\t", c, "\t\t\t\t\t\t", type(c)) print("d\t\t\t", d, "\t\t\t\t\t\t\t\t", type(d)) print("e\t\t\t", e, "\t\t\t\t\t\t\t\t", type(e)) print("f\t\t\t", f, "\t\t\t\t\t\t\t\t", type(f)) print("g\t\t\t", g, "\t\t\t", type(g)) print("h\t\t\t", h, "\t\t", type(h)) print("i\t\t\t", i, "\t\t\t\t\t\t", type(i)) print("j\t\t\t", j, "\t\t", type(j)) print("k\t\t\t", k, "\t\t\t\t\t", type(k)) print("l\t\t\t", l, "\t", type(l)) print("m\t\t\t", m, "\t\t\t\t\t\t\t\t", type(m)) print("n\t\t\t", n, "\t\t\t\t\t\t\t\t", type(n)) print("o\t\t\t", o, "\t\t\t\t\t", type(o)) print("p\t\t\t", p, "\t\t\t\t\t", type(p)) print("q\t\t\t", q, "\t\t", type(q)) print("r\t\t\t", r, "\t", type(r)) print("s\t\t\t", s, "\t", type(s)) print("t\t\t\t", t, "\t", type(t)) print("u\t\t\t", u, "\t", type(u))
Seriously!, I mean so many horizontal tabs (\t). The snapshot given below shows the sample output produced by this mega program on data types in Python:
More Examples
« Previous Tutorial Next Tutorial »