Data types in python
Shivam Chikara
Data Analyst skilled in Python || Power BI || Tableau || Machine Learning || Deep Learning || SQL || Git || R programming
What is data type in python?
Data types are the classification or categorization of data items. Everything is an object in Python programming, data types are classes and variables are instances (objects) of these classes. The following are the standard or built-in data types in Python:
·??????? Numeric
·??????? Sequence Type
·??????? Boolean
·??????? Set
·??????? Dictionary
Numeric Data Types in Python
The numeric data type in Python represents the data that has a numeric value. A numeric value can be an integer, a floating number, or even a complex number.
ex:- int:- a = 5 float:- b = 5.0, complex:- c = 2 + 4j
Sequence Data Type in Python
The sequence Data Type in Python is the ordered collection of similar or different data types. Sequences allow storing of multiple values in an organized and efficient fashion. There are several sequence types in Python –
·??????? Python String
·??????? Python List
·??????? Python Tuple
?
?? String Data Type
Strings in Python are arrays of bytes representing Unicode characters. A string is a collection of one or more characters put in a single quote, double-quote, or triple-quote.
ex:- String1 = 'This is my PC', "This is my PC", '''This is my PC'''.
?? Accessing elements of String
In Python programming, individual characters of a String can be accessed by using the method of Indexing. Negative Indexing allows negative address references to access characters from the back of the String.
?? List Data Type
Lists are just like arrays, declared in other languages which is an ordered collection of data. It is very flexible as the items in a list do not need to be of the same type.?
?? Creating a List in Python
Lists in Python can be created by just placing the sequence inside the square brackets [].
?? Access List Items
In order to access the list Items, refer to the index number. Use the index operator [] to access an item in a list. In Python, negative sequence indexes represent positions from the end of the array.
?? Tuple Data Type
Just like a list, a tuple is also an ordered collection of Python objects. The only difference between a tuple and a list is that tuples are immutable i.e. tuples cannot be modified after it is created. It is represented by a tuple class.?
ex:- Tuple1 = ()
领英推荐
?? Creating a Tuple in Python
In Python, tuples are created by placing a sequence of values separated by a ‘comma’ with or without the use of parentheses for grouping the data sequence.
?? Access Tuple Items
In order to access the tuple items, refer to the index number. Use the index operator [] to access an item in a tuple. The index must be an integer. Nested tuples are accessed using nested indexing.
?
?? Boolean Data Type in Python
Data type with one of the two built-in values, True or False. Boolean objects that are equal to True are truthy (true), and those equal to False are falsy (false).
Note – True and False with capital ‘T’ and ‘F’ are valid booleans otherwise python will throw an error.
Set Data Type in Python
In Python, a Set is an unordered collection of data types that is iterable, mutable, and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements.
ex:-set1 = set()
?? Create a Set in Python
Sets can be created by using the built-in set() function with an iterable object or a sequence by placing the sequence inside curly braces, separated by a ‘comma’.
?? Access Set Items
Set items cannot be accessed by referring to an index, since sets are unordered the items have no index. But you can loop through the set items using a for loop, or ask if a specified value is present in a set, by using the in the keyword.
?
Dictionary Data Type in Python
A dictionary in Python is an unordered collection of data values, used to store data values like a map, unlike other Data Types that hold only a single value as an element, a Dictionary holds a key: value pair. Key-value is provided in the dictionary to make it more optimized. Each key-value pair in a Dictionary is separated by a colon : , whereas each key is separated by a ‘comma’.
ex:- Dict = {}, Dict = {1: 'abc', 2: 'jkl', 3: 'mno'}
?? Create a Dictionary in Python
In Python, a Dictionary can be created by placing a sequence of elements within curly {} braces, separated by ‘comma’.
?? Accessing Key-value in Dictionary
In order to access the items of a dictionary refer to its key name. Key can be used inside square brackets. There is also a method called get() that will also help in accessing the element from a dictionary.
print(Dict['Key_value'])?
What is Python type()? Function?
To define the values of various data types and check their data types we use the type() function.
ex:- a=6
print(type(a))