English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Python has some simple built-in types, such as int, float, complex, str, bool. It also has some complex built-in types, such as List, Dict, Tuple, Set.
Lists-Lists are one of the data types in Python. Lists are collections of objects, which are ordered and mutable. In Python, it is written in square brackets [].
my_list=["car","bus","truck"] print(my_list)
We can access list items by referencing the index number:
Return position1the item at this position.
my_list=["car","bus","truck"] print(my_list[1)]
Using index numbers, we can change the value of the item.
my_list=["car","bus","truck"] my_list[2] = "van" # The values are mutable print(my_list)
We can use a for loop to traverse the items in the List.
my_list=["car","bus","truck"] for x in my_list: print(x)
Some methods in the list
Python has some built-in methods that we can use in lists.
Serial number | Methods and descriptions |
---|---|
1 | append() This method is used to add elements to the end of the list |
2 | clear() This method is used to remove all elements from the list |
3 | copy() This method is used to return a copy of the list |
4 | count() This method is used to return the number of elements with specified values |
5 | extend() This method is used to add elements to the end of the list (or any iterable) |
6 | index() This method is used to return the index of the first element with specified values |
7 | insert() This method is used to add elements at specified positions |
8 | pop() This method is used to delete elements at specified positions |
9 | remove() This method is used to delete items with specified values |
10 | reverse() This method is used to reverse the order of the list |
11 | sort() This method is used to sort lists |
Dictionaries-Dictionaries are unordered collections of elements, using keys instead of positions. Dictionaries are an abstract data type in Python. Dictionaries have two parameters, one is the key, and the other is the value. Each key is associated with a value, so we can say that dictionaries are associative arrays.
>>> student = {"Aadrika":001, "Adwaita":009, "Sakya":011, "Sanj":022}
Here, we use student records, and what we want to do is use the student name as the index.
>>> student = {"Aadrika":001, "Adwaita":009, "Sakya":011, "Sanj":022} >>> student["Adwaita"] 009
In these examples, our dictionary is the student, and there is an order in the dictionary. Just like the first element is "Aadrika", the second element is "Adwaita", and so on. But there is no order in the dictionary. This is why the output of the student dictionary does not reflect the "original order".
If you want to add an element.
>>> student["Krishna"] = 111 >>> student student = {"Aadrika":001, "Adwaita":009, "Sakya":011, "Sanj":022,"Krishna":111}
Therefore, the initial dictionary is empty, and then values are taken one by one in the incremental process.
Tuples-Tuples are a group of objects in Python. They are separated by commas (", "). In terms of indexing, tuples are similar to lists. Tuples are mainly immutable. They also have comparability and hashability, so we can easily sort them. In Python dictionaries, tuples are used as keys.
my_tuple={"car","bus","truck"} print(my_tuple)
We can access tuple items by referencing the index number.
Return item to position1.
my_tuple={"car","bus","truck"} print(my_tuple[1)]
If a tuple is created, then we will not be able to change its values. Tuples are immutable.
We cannot change the values in the tuple.
my_tuple={"car","bus","truck"} my_tuple[3] = "van" # The values are unchangeable print(my_tuple)
We can use a for loop to traverse the items in the tuple.
my_tuple={"car","bus","truck"} for x in my_tuple: print(x)
Python has two built-in methodscount()
andindex()
. We can use these methods in tuples.
count() | This method returns the number of times the specified value appears in the tuple. |
index() | This method searches for the specified value in the tuple and returns the position where it is found |
set-In mathematics, a set is a collection of different objects. For example, here it is assumed that3a number, when considered separately, the number2,4and6They are different objects, but when considered together, they form a set of size3A single set, denoted as {2,4,6}
In Python, set is very useful because set is a highly optimized method, because it is easy to check whether there is any specific element in the set or not.
Set methods
1.add(x) method: If the element is not in the list, it will be added to the list.
A = {"AA", "BB", "CC"} A.add("DD") -> add DD in A set.
2.union(s) method: This method returns the union of two sets. For union operations, please use the “ |” operator.
A = {"AA", "BB", "CC"} B = {"MM", "NN"} Z = A.union(B) OR Z = A|B -> Set Z will have elements of both A and B
3.intersection method: This method returns the intersection of two sets. In this case, the “&” operator can also be used.
S = A.intersection(B) -> Set S will contain the common elements of A and B
4.difference method: This method returns a set of elements that belong to the first group but not to the second group. We can use the “-”operator.
S = A.difference(B) OR S = A – B -> Set S will have all the elements that are in A but not in B
5.clear()
Method: Clear the entire set.
B.clear() -> Clears B set
Set operators
The set and frozen set support the following operators
Input s | #Check to prevent |
The key is not in s | #Non-restricted check |
s1 == s2 | #The two groups are equal |
s1!= s2 | #The two groups are not equal |
s1 <= s2 | #s1is s2The subset, s1 <s2The first group is a subset of the second, s1> = s2The first group is a superset of the second |
s1> s2 | The first group is a superset of the second group |
s1 | s2 | The union of the two sets |
s1and s2 | The intersection of the two sets |
s1 – s2 | The element set in the first group, not the second group |
s1 ˆ s2 | An element that is exactly in the first or second group |