As we know that python is Dynamically typed programming language, than why do we need to learn about data type at all?
Although python automatically understands the data type, but it is important to know about data types so that we can utilize the power of data structures in future.
Basic Data Types available in python are:
Numeric - int, float, complex (Immutable) Strings (Immutable) List (Mutable, mostly used to store homogeneous data types) Tuple (Immutable, faster compared to List) Set (Unordered collection of items, mutable, removes duplicates) Dictionary (Unordered collection of Key-Value Pairs, Mutable, Keys are Unique - values may not be unique)
string_var = 'ThatAIGuy'
- String is a sequence of characters.
- Use index to access characters in a string
- If we try to access index out of range or use decimal numbers we will get an error
- Python supports both +ve and -ve index
- Strings are immutable. Elemets of strings can't be changed once it has been assigned. We can simply reassign different strings to the same name
- Strings support indexing as well as slicing
list_var = ['Checkout', 'ThatAIGuy', '.', 'com']
- Sequence Data Structure.
- Used to store collection of items.
- Order is preserved in a list
- Mutable
- Indexing and slicing
- Dynamic i.e. increase or decrease in size
tuple_var = ('Checkout', 'ThatAIGuy', '.', 'com')
- Similar to list but immutable.
- Sequence data type.
- Supports indexing and slicing.
set_var = {1, 11, 0, 1, 2}
- Unordered collection of items i.e. they can't be indexed
- No duplicates allowed
- Mutable
- Used to perform mathematical operations like Union, intersection, etc..
dict_var = {'key_1': 'val_1', 'key_2': 'val_2'}
- Unordered collection of items i.e. can't be indexed
- Other compound data tupes have only values as an element, whereas a dictionary has a KEY:VALUE pair
- Keys are unique, values may not be unique
- Values can be of any type, but key must be immutable