List
* List is a collection of data,
* List will have a name like variable and stores multiple data with comma separate.
* it is ordered manner.
* It has index, starts from 0.
* Lists are mutable, means can change the list post define
Ex: Even_numbers=[2,4,8,10]
Even_numbers[2]=6
print(Even_numbers[2])
Output: 6
* List can store different data types in single list
Ex: Number= [90,"Ram",0.90]
Index of 90 is 0
Index of Ram is 1
Index of 0.90 is 2
* String is a collection of characters, however string is immutable
name = "Mobile"
print(name[0]) --output= M
* Slicing in list
F_names=["Banana","Mango","Grapes","Apple"]
F_names[1:3]
Output: ["Mango","Grapes"]
Note: for slicing in List python F_name[1:3] - only 1, 2 index data will out
Note: On strings also can perform slicing in python
* Slicing can do in three different ways on List and Strings in Python
First way - To get the elements from very first element in List
Flowers=["Lilly","Rose","Sunflower"]
print(Flowers[:2])
Output: ["Lilly","Rose"]
Note: Flowers[:2], this will result start from very first element of List untill first index element
Second way - To get the elements untill last element index in List
Flowers=["Lilly","Rose","Sunflower"]
print(Flowers[1:])
Output: ["Rose","Sunflower"]
Note: Flowers[1:], this will result untill last element of the list from index number 1
Third way- To get all the elements of List
Flowers=["Lilly","Rose","Sunflower"]
print(Flowers[:])
Output:["Lilly","Rose","Sunflower"]
Note: Flowers[:], this will result all the elements of the list
* Negative Indexing in List - to get from Last elements in strings and List
Ex: Vowel_letters=["a","e","i","o","u"]
print(Vowel_letters[-1]
Output: u
Fruit= "Apple"
print(Fruit[-1])
Output- e
Slicing with both negative and positive indexes
Ex: Vowel_letters=["a","e","i","o","u"]
print(Vowel_letters[2:-1])
Output: ["i","o"]
* Check whether element available in List
Vowel_letters=["a","e","i","o","u"]
print( 'a' in Vowel_letters)
Output: True
Fruit= "Apple"
print('A' in Fruit)
Output- True
Tuple
* Used for ordered collection
* Tuples can have data with different data types
* Defined data in Parentheses ().
* The difference between list and tuple is tuple is immutable and also append function won't work for tuple
That is we can't change the value once the tuple created
Example:
Tu_flower=(1,2,6)
Tu_flower[1]=5 //Error
* To count number of times integer repeated in a tuple, for finding this we can use count function
For instance,
Marks=(7,9,7,8,6,5,4,3,4)
print(Marks.count(7)) //2
* variables can be assigned the data available in a tuple
Example: R=("SS",8)
Name,ID=R
print (Name) //SS
print (ID) //8
In the scenario where the tuple length is not know then we can go with the asterisk symbol along with variable so that all the data will be copied to the variable but it will be converted to list format
Example: R=("SS",8,7,6,9)
Name,*IDs=R
print (Name) //SS
print (IDs) // [8,7,6,9]
Here IDs is List format
Dictionaries
* Dictionaries are one of the python collection
* Each dictionary has key: value pair
Example:
Student= {
"Name":"DailyUpdates234",
"Course studying": "Python"
}
Here Name, Course studying are keys and DailyUpdates234 and Python are the values
* Keys should be unique else overwritten with new value
For instance,
Student= {
"Name":"DailyUpdates234",
"Course studying": "Python",
"Name":"DailyUpdates"
}
print(student["Name"])
** When trying to extract Name key's value then DailyUpdates will be the output
* Function used for doing operations on the dictionaries are
-> update()
For instance,
Student= {
"Name":"DailyUpdates234",
"Course studying": "Python"
}
student.update({"Name":"DailyUpdates"})
-> items()
To get all the key value pairs of a dictionary
For example,
Student.items()
-> pop()
To delete an data use this
Student.pop("Name")
Comments
Post a Comment