An important idea in the Python programming language is the concept of different data types in python. Each value in Python has a unique data type. Data types refer to the categorization of data items or the placement of the data value into a certain data category. Knowing what operations can be carried out on a value is useful. If you are just starting out and want to learn more about data science, have a look at our selection of top colleges’ data science certifications. Every element in the Python programming language is an object. In Python, the classes are represented by different data types in python. Variables refer to the objects or instances of these classes. Let’s now talk about the various Python data types.
“Ready to take your python skills to the next level? Sign up for a free demo today!”
Example
Code
var1 = 20
var2 = 20.65
var3 = "Hello!, World "
print( type(var1) );
print( type(var2) );
print( type(var3) );
Output:
“Experience the power of our web development course with a free demo – enroll now!”
Built-in Data Types in Python
- Binary Types: memoryview, bytearray, bytes
- Boolean Type: bool
- Set Types: frozenset, set
- Mapping Type: dict
- Sequence Types: range, tuple, list
- Numeric Types: complex, float, int
- Text Type: str
1. Python Numbers
Python Numbers is a category that includes complex numbers, floating point numbers, and integers. In Python, floating point numbers are specified as a float, complex numbers as a complex class, and integers as an int. In this category, long is another datatype type that is present. To store longer integers, use this. This datatype was only present in Python 2.x and was later eliminated in Python 3.x.
The “Type()” method is used to determine a value or variable’s class. The “isinstance()” method is used to determine the value for a certain class.
“Get hands-on with our python course – sign up for a free demo!”
Integers
The value of an integer can be as large as it wants to be. The integer’s length is unrestricted and can be as long as the system’s maximum memory capacity.
- Integers can look like this:
- >>> print(123123123123123123123123123123123123123123123123123 + 1)
123123123123123123123123123123123123123123123123124
Floating Point Number:
Decimal points are what separate floating points from integers. Integer numbers can be represented as “1,” whereas floating point numbers may be represented as “1.0.” Up to 15 decimal places are correct.
Complex Number:
-
- “x + yj” is the written form of the complex number. Here y is the imaginary part and x is the real part.
2. Python List
Lists are collections of items in a certain order. Python, it is an extremely adaptable data type. The values in the list don’t have to be of the same data type. The most popular data type in Python is called a list. The most exclusive datatype in Python for storing diverse data is the list datatype. Python makes it simple to store many sorts of data.
A list may be declared with ease. The elements in the list are separated by commas and the list is wrapped in brackets.
A list may seem like follows:
“Ready to take your python skills to the next level? Sign up for a free demo today!”
Code:
ls = ["apple", "a", 100, 20.78]
print (ls[1])
print (ls[1:])
print (ls[:3])
print (ls)
print (ls + ls)
print (ls * 3)
print (type(ls))
ls[1] = "banana"
print (ls)
Output:
3. Python Tuple
A Tuple is an ordered sequence of elements that cannot be changed once it has been created. Tuples and lists vary primarily in that a tuple is immutable, or cannot be changed. Due to the fact that they cannot be altered or updated, Python’s tuples data type is often quicker than the list data type. Tuples are mostly used to write-protect data. Parentheses () can be used to denote tuples, while commas are used to demarcate the components.
Tuples can appear as follows:
Code:
tp = ("apple", "a", 100, 20.78)
print (tp[1])
print (tp[1:])
print (tp[:3])
print (tp)
print (tp + tp)
print (tp * 3)
print (type(tp))
tp[1] = "banana"
print (tp)
Output:
4. Python Strings
A string is a collection of Unicode symbols. The name for String in Python is str. Single or double quotations are used to represent strings. The use of triple quotes “”” or “‘ to indicate multiple strings is acceptable. Between the quotations, every character is a part of the string.
The only restriction is the machine system’s memory resources, which one may use as many characters as they like. In the Python programming language, deleting or updating a string will result in an error. As a result, the Python programming language does not permit the alteration of strings.
A string may appear as follows:
>>> s = “Python String”
>> s = ”’a multi-string
Strings may be extracted using slicing operators and are similarly immutable to tuples.
Other sorts of quotations must be used to define the string at the beginning and the end if someone wishes to use quotes to represent something in the string.
like as
>>> print(“This string contains a single quote (‘) character.”)
This string contains a single quote (‘) character.
>>> print(‘This string contains a double quote (“) character.’)
This string contains a double quote (“) character.
5. Python Set
Set refers to a group of singular objects that are not in chronological sequence. A comma is used to demarcate values while braces are used to define sets. The objects of a predetermined data type are found to be unordered.
A set’s duplicates are removed, and the set only retains values that are unique. On two sets, operations like intersection and union can be carried out.
Python set will look like this:
“Experience the power of our web development course with a free demo – enroll now!”
Code:
st = {"apple", "banana", 100, 20.78}
# set cannot support indexing st[1]
# set cannot support slicing st[1:]
print (st)
print (st + st)# set cannot support concatenation
print (st * 2) # set cannot support repetition
print (type(st))
# set is immutable st[2] = "hi"
Output:
6. Python Dictionary
Dictionary is a sort of Python data type where values are in pairs called key-value pairs and collections are unordered. When there is a lot of data, this sort of data type is helpful. Retrieving the data for which the Dictionaries data type is optimized is one of its greatest features. Only those who are aware of the key can obtain the value.
In Python, dictionaries are defined as a data type using curly braces. A pair is an item that is represented as key:value in the dictionary data type.
Python Dictionary can look like this:
Code:
dc = {"fruits":["apple", "banana"],'qty':100} print("Fruits: ",dc['fruits']) print("Quantity: ", dc['qty']) print ("Dictionary: ",dc)# print all elements of the dictionary print ("Keys: ",dc.keys()) # print all the keys of the dictionary print ("values: ",dc.values()) # print all the values of the dictionary print ("key value pairs: ",dc.items()) # print all the key values pair elements of the dictionary
Output:
7. Boolean Type
There can be only two types of value in the Boolean data type of Python, and that is True or False.
It can look like this:
>>> type(True)
<class ‘bool’>
>>> type(False)
<class ‘bool’>
In a boolean setting, the true value is referred to as “truthy,” while the false value is referred to as “falsy.” The items in boolean that are equal to True define Truthy, whereas the objects that are equal to Falsy define Falsy. Non-Boolean items may likewise be evaluated in a Boolean context.