Hello All,
It is the continuation part from Python 2 : All Basic Concepts Covered-One Spot for Python Basics
Concept 11: Lists in Python
Lists store an ordered collection of items which can be of different data types.
Lists are written within square brackets []
Here is the simple list....
names = ['appu','chakrapani','upadhyaya',23,'TRUE']
Accessing all values in list
for name in names: print(name)
Output:
PS D:\Python_Programming> & "C:/Program Files/Python310/python.exe"
d:/Python_Programming/09_list_explained.py appu chakrapani upadhyaya 23 TRUE
Access the first item of a list at index 0
print(names[0])
Output:
appu
Python also supports negative indexing. Negative indexing starts from the end. It can be more convenient at times to use negative indexing to get the last item in the list because you don’t have to know the length of the list to access the last item.
Slicing a Python List:
When you want only a part of a Python list, you can use the slicing operator [].
print(names[2:4])
output:
['upadhyaya', 23]
Inserting data to Python:
Insert New Data using insert method
insert method helps us to insert the records in specific location
below we are inserting the data after index 2
names.insert(2,"Shraddha") print(names)
output:
['appu', 'chakrapani', 'Shraddha', 'upadhyaya', 23, 'TRUE']
Insert New Data using append method:
append method helps us to insert the records at last
names.append("Shreshta") print(names)
output:
['appu', 'chakrapani', 'Shraddha', 'upadhyaya', 23, 'TRUE', 'Shreshta']
Note:List allows duplicate data insertion, i mean we can hold duplicate data in list.
names.append("Shreshta") print(names)
Output:
['appu', 'chakrapani', 'Shraddha', 'upadhyaya', 23,
'TRUE', 'Shreshta', 'Shreshta']
Updating data in List:
With the help of index we can update the data in list.
names[2]="Updated" print(names)
Output:
['appu', 'chakrapani', 'Updated', 'upadhyaya', 23, 'TRUE', 'Shreshta', 'Shreshta']
Deleting the data in List :
Delete the data with remove key word, it will revove the data based on value and it does not return any deleted data
names.remove("Updated") print(names)
Output:
['appu', 'chakrapani', 'upadhyaya', 23, 'TRUE', 'Shreshta', 'Shreshta']
And another way of delete is, delete the data with pop() method, it will remove the data based on index and it will return deleted data
rec = names.pop(2) print(rec) print(names)
Output:
upadhyaya ['appu', 'chakrapani', 23, 'TRUE', 'Shreshta', 'Shreshta']
Concept 12: Sets in Python
- sets are used to hold the different types of data in one variable
- sets do not allow duplicate values
- sets is mutable, we can add/remove the data in it
- sets will be declared with {} symbol
- Set items are unchangeable, meaning that we cannot change the items after the set has been created.
- Set items can appear in a different order every time you use them, and cannot be referred to by index or key.
s = {"Chakrapani","Shraddha",100,10.5} print(s)
output
{'Chakrapani', 10.5, 100, 'Shraddha'}
Adding data to sets:
using add method we can add the data to sets.
s.add("Shreshta") print(s)
output:
{'Chakrapani', 100, 10.5, 'Shraddha', 'Shreshta'}
Adding duplicate data to sets, it will not have duplicate data
s.add("Shreshta") print(s)
output:
{'Chakrapani', 100, 10.5, 'Shraddha', 'Shreshta'}
Deleting the records from sets:
Delete the data with remove key word, it will revove the data based on value. It does not return any deleted data
s.remove("Shreshta") print(s)
output:
{'Chakrapani', 100, 10.5, 'Shraddha'}
Removing the data with pop, pop deletes last items, it will return deleted data
deleted = s.pop() print(deleted) print(s)
output:
Chakrapani {100, 10.5, 'Shraddha'}
Concept 13: Tuple in Python
Tuples is similar to list, we can have multiple data with different data type, But it is completely mutable, we cannot change the value or insert/update/delete. Like list it will have duplicate value and Tuples will be declared with () symbol
t = ("chakrapani","chakrapani",100,100,10.5,20.5) print(t)
output:
('chakrapani', 'chakrapani', 100, 100, 10.5, 20.5)
Getting specific data:
print(t[0])
output:
chakrapani
Concept 14: Dictionary in Python
As like other programming dictionary, Every dictionary has two parts 1. Key 2. Value
On the left hand side is key and right hand side is value
Syntax:
{"Key":"Value"}
Example:
users_dict = { "Id": 100, "FullName": "Chakrapani U", "EmailId": "Chakrapan@Test.com", "Phone": "9099999999", "Status": "Active", }
print(users_dict)
{'Id': 100, 'FullName': 'Chakrapani U', 'EmailId': 'Chakrapan@Test.com',
'Phone': '9099999999', 'Status': 'Active'}
users_dict["DateCreated"]="10-10-2021" print(users_dict)
{'Id': 100, 'FullName': 'Chakrapani U', 'EmailId': 'Chakrapan@Test.com', 'Phone': '9099999999', 'Status': 'Active', 'DateCreated': '10-10-2021'}
print(users_dict["EmailId"])
Chakrapan@Test.com
print(users_dict["EmailId1"])
Traceback (most recent call last): File "e:\Python Programming\Python\12_dictionary.py", line 23, in <module> print(users_dict[0]) KeyError: 0
users_dict["EmailId"] = "chakra24@test.com" print(users_dict)
{'Id': 100, 'FullName': 'Chakrapani U', 'EmailId': 'chakra24@test.com', 'Phone': '9099999999', 'Status': 'Active', 'DateCreated': '10-10-2021'}
users_dict.pop("DateCreated") print(users_dict)
{'Id': 100, 'FullName': 'Chakrapani U', 'EmailId': 'chakra24@test.com', 'Phone': '9099999999', 'Status': 'Active'}
users_dict.popitem() print(users_dict)
{'Id': 100, 'FullName': 'Chakrapani U', 'EmailId': 'chakra24@test.com', 'Phone': '9099999999'}