Python Data Types
Data Types
Python Data Types
In computer programming, data types
specify the type of data that can be
stored inside a variable.
Python Numeric Data Type
Python numeric data type is used to hold numeric values like;
1. int - holds signed integers of non-limited length.
2. float- holds floating precision numbers and it’s accurate up to 15 decimal places.
3. complex- holds complex numbers.
In Python, we need not declare a datatype while declaring a variable like C or C++. We
can simply just assign values in a variable. But if we want to see what type of numerical
value is it holding right now, we can use type(), like this:
Numeric Data Types
Python String Data Type
List Data Type
Python List Data Type
The list is a versatile data type exclusive in Python. In a sense, it is the same as the array
in C/C++.
Variables store only one piece of data, whereas a list can store multiple data of different
types.
Formally list is an ordered sequence of some data written using square brackets([]) and
commas(,).
Python Lists
Python Lists
Python Lists
Lists - Slicing
Lists - append()
The append() method allows you to add items of any data type. This includes numbers,
strings, and even other lists or complex objects.
Lists - append() example
Appending vs. Extending Lists
The append() method adds its argument as a single item to the end of a list. By contrast,
the list extend() method unpacks the argument and adds each element to the list. This is
useful when you need to combine two lists or add multiple elements to an existing list at
once.
lists - insert()
In Python, insert() is a list method that allows you to add a single element to a specific
position in the list. insert() modifies the list without creating a new list.
As a list method, insert() requires a list to insert the element into and two parameters.
The parameters specify the index at which to insert the element and the element to
insert.
Some methods used in list data type
Append(): adds an element to the end of the list.
Extend(): adds all elements of one list to another list.
Insert(): inserts the desired element at a given index.
Pop(): deletes an index element and returns the deleted element.
Clear(): deletes all elements in the list.
Index(): returns the index of the searched element. If there is more than one of the same element, it sends the first
index it finds.
Count(): returns the number of the searched element in the list.
Sort(): sorts the list elements from descending to ascending.
Reverse(): reverses the order of the elements.
Copy(): copies the elements in the list.
Nested lists – Matrix
Matrix Summation
Matrix Transpose
Product of two Matrices
range() Function
Range() Function
The range function is used to iterate over elements in a list.
By executing a task repeatedly, the range generates indices for the data structure.
The range function is used to iterate over elements in a list. By executing a task
repeatedly, the range generates indices for the data structure.
Range() function
The start represents a starting value if, when omitted, the range starts from 0, while stop
is the number that indicates that the range should stop generating numbers.
The step being the last value, specifies the increment or step between each different
number in the sequence. The default value for this parameter is 1.
The range function returns an immutable series of numbers.
(NOTE: Immutable = its value cannot be changed after it has been created.)
range() function Examples
range() function Examples
Create Reverse Range
You can use the range() function to generate ranges in reverse by providing a negative
step. This can be particularly useful when you need to iterate over a sequence in the
opposite order.
Generate Custom Sequences with Range
Create Indices for Lists
NOTE: len() is a built-
in function that returns
the number of
elements (length) in an
iterator/object passed
to the function.
Tuples
Tuples
Tuple is a data type similar to a list. The difference of the tuple is that it cannot be changed,
added or removed after it is defined. So the size of the tuple cannot change after it is defined.
Dictionaries
Dictionary
A dictionary can be defined as a list containing the key and its value (key-value pairs). However, we
cannot directly pull elements by specifying index like a list. The location of the elements and therefore
their values can be accessed with the help of keys and we can change them after they are defined.
Note: list and set are not hashable so they cannot be used as keys. In the dictionary, the key must be of
a data type that cannot be changed later. (Ex: FrozenSet, Tuple, String, Integer)
Dictionary
Dictionary
Shortcut creation of Dictionary type with iterations
Search for keys
If-else statements in dictionaries
Example of iteration in nested structure
Sets
Sets
In Python, a set is a built-in data type that represents a collection of unique
elements with no particular order.
The elements in the set are immutable, but the set itself is mutable. Sets can be
defined using curly braces {} with comma-separated elements or by the set()
constructor. They gets used for mathematical operations like unions,
intersections, and differences.
Characteristics of Sets
● No defined order: In a set, there's no defined order because the elements are unordered.
● Uniqueness: Sets are unique in nature, because they do not allow duplicate elements.
● Mutability: Sets allow you to update either by adding or removing elements after creation.
● Elements Immutability: Mutable elements like lists cannot be an element of a set, because elements
within a set must be immutable. Therefore, immutable data types like floats, integers , tuples and
string can be used instead.
Set Operations
Frozen Sets
Frozen Sets
FrozenSet is a class with properties of a lumen, but its elements cannot be changed once assigned. It is defined
by the FrozenSet() function.
Note: Tuples are immutable lists, and FrozenSets are immutable sets.
Note2: Sets that are mutable cannot be shuffled, so they cannot be used as dictionary keys. On the other hand,
because FrozenSet is hashable, they can be used as a key to a dictionary.
The available methods for FrozenSet are as follows.
copy(), difference(), intersection(), isdisjoint(), issubset(), issuperset(), symmetric_difference(), union()