2. String Data Type
Strings are arrays of bytes in Python that represent Unicode characters. A string is made up of one or more characters enclosed in a single, double, or triple quotation. Multiline strings can be created using triple quotation marks. Python has no character data type; a character is a one-length string. The str class is used to represent it.
Strings can be of any length. The only limitation is the memory of the system.
Strings are immutable in Python, which means that string does not support modification or deletion. If you try to modify the string, Python will throw an exception.
Individual characters of the string can be accessed through indexing in Python.
s = "This is a string"
print("This is a normal string:",s)
s = '''This is
another string'''
print("This is a multiline string:",s)

You can also try this code with Online Python Compiler
Run Code
Output:
This is a normal string: This is a string
This is a multiline string: This is
another string

You can also try this code with Online Python Compiler
Run Code3. List Data Type
The list is a collection of items in an ordered sequence. It's one of Python's most popular data types, and it's pretty versatile. All items in a list don’t have to be of the same type. One of the best properties of the List is that it can hold the number of multiple elements simultaneously while still maintaining their order.
The difference between list and array in Python is that lists can store elements of different data types, while the data types of elements in an array should be the same.
List can be created by placing the sequence inside square brackets.
l1 = [1,2,3]
print("List containing integers:\n",l1)
l2 = ["string1", "string2", "string3"]
print("List containing strings:\n",l2)
l3 = ["string1",2,"string3"]
print("List containing both integers and string:\n",l3)
List containing integers:
[1, 2, 3]
List containing strings:
['string1', 'string2', 'string3']
List containing both integers and string:
['string1', 2, 'string3']

You can also try this code with Online Python Compiler
Run Code
The index number can be used to access the list items. To go to a specific item in a list, use the index operator []. Negative sequence indexes in Python represent positions from the array's end. It is sufficient to write List[-2] instead of needing to compute the offset as in List[len(list)-2]. Negative indexing indicates starting at the end, with -1 being the last item, -2 denoting the second-to-last item, and so on.
Also see, Python Operator Precedence
4. Tuple Data Type
The tuple is an ordered collection of Python objects, similar to a list. The sole difference between a tuple and a list is that tuples are immutable, meaning that once generated, they cannot be changed. The tuple class is used to represent it.
They can be created by placing the sequence of elements separated by a comma inside a parenthesis. They can contain any data type or combination of data types.
The element inside a tuple can be accessed with an indexing method, similar to the list.
t1 = (1,2,3)
print("tuple of integers:",t1)
t1 = (1,"string2",3)
print("tuple of integers and string:",t1)
print("type of t1:",type(t1))

You can also try this code with Online Python Compiler
Run Code
Output:
tuple of integers: (1, 2, 3)
tuple of integers and string: (1, 'string2', 3)
type of t1: <class 'tuple'>

You can also try this code with Online Python Compiler
Run Code5. Boolean Data Type
Boolean data types in Python can have two values: True or False. Non boolean objects can also be evaluated and determined to be True or False in a boolean context.
a = True
print(type(a))
a = False
print(type(a))

You can also try this code with Online Python Compiler
Run Code
Output:
<class 'bool'>
<class 'bool'>

You can also try this code with Online Python Compiler
Run Code6. Set Data Type
Set is an unordered data type collection that is iterable, mutable, and has no duplicate elements in Python. The sequence in which elements of a set appear is undefined.
Set can be created by placing the sequence of elements inside curly braces {}, or by passing an iterable inside the built-in set() function.
The set elements cannot be indexed by indexing since sets are an unordered collection of items.
s = set([1,2,3])
print("set:",s)
s = {1,2,3,4}
print("set:",s)
print(type(s))

You can also try this code with Online Python Compiler
Run Code
Output:
set: {1, 2, 3}
set: {1, 2, 3, 4}
<class 'set'>

You can also try this code with Online Python Compiler
Run Code7. Dictionary Data Type
In Python, a dictionary is an unordered collection of data values, similar to a map, used to store data values. Unlike other Data Types, which only contain a single value as an element, Dictionary holds a key-value pair. A colon separates each key-value pair in a Dictionary, whereas a comma separates each key.
Dictionary can be created by placing a sequence of key-value pairs separated by colon within curly braces. It can also be made using the built-in function dict().
The value of the key in the dictionary can be updated or indexed using square brackets. Get function can also be used to get the value of a key.
d = {1:2,2:3,3:4}
print(type(d))
d = dict()
d[1]=2
print(d.get(1))
>>>
<class 'dict'>
2

You can also try this code with Online Python Compiler
Run CodeYou can try it on online python compiler.
Check out Escape Sequence in Python
Frequently Asked Questions
What is data type category in Python?
Data type categories in Python include numeric types (integers, floats), sequence types (strings, lists, tuples), mapping types (dictionaries), set types (sets), and boolean types.
How many data types are there in Python?
Python has several built-in data types, including integers, floats, strings, lists, tuples, sets, dictionaries, and booleans. This core set covers a wide range of data representations.
What is the largest data type in Python?
The largest data type in Python is int, which can handle arbitrarily large values, limited only by available memory. Python integers are not bound by fixed sizes.
What is the smallest data type in Python?
The smallest data type in Python is bool, which represents boolean values True and False. It occupies minimal space compared to other data types.
Conclusion
In conclusion, Python's diverse data types provide flexibility and efficiency for various programming needs. From basic types like integers and floats to complex types like lists and dictionaries, Python supports both simple and sophisticated data management. The language's dynamic typing system and rich set of built-in types facilitate robust and adaptable coding, making it suitable for a wide range of applications.
Recommended Reading: