Python Complex data types
- List
- Tuple
- Dictionary
List
- Group of Values belong different data type enclosed in [ ] bracket
- Value is called as item or Element
- Each Element will be retrieved based on the position.
List Operation
- Indexing – Retrieving only one item based on its position
- Slicing – Retrieving group of elements sequentially
- Repeation – Printing the List specified no. of times.
List Example:
# List creation
empList = [ 2001, ‘Shivani’,2005, ‘Sanjay’, 2007, ‘Babu’]
print (“employee Details : \n”, empList)
#Indexing
print(“2nd Position : “, empList[1])
#Slicing
print(“1st to 4th element : “, empList[0:4])
#printing from 2nd element to last element
print(“2nd to last element : “, empList[1:])
#Repeation
print(empList*2)
Tuple
- Elements are Enclosed in the round brackets ()
- Read only – Values can’t be modified.
- Supports all the List operations.
Example – Tuple
# Tuple creation
empTuple = ( 2001, ‘Shivani’,2005, ‘Sanjay’, 2007, ‘Babu’)
print (“employee Details : \n”, empTuple)
#specific element from Tuple
print(“2nd Position : “, empTuple[1])
#printing Range of element
print(“1st to 4th element : “, empTuple[0:4])
#printing from 3nd element to Tuple element
print(“3rd position to Last : “, empTuple[2:])
#printing the Tuple two times
print(empTuple*2)
Dictionary
- Elements are stored in Key-Value Format
- Key is unique and value can be duplicated
- Value will be Retrieved based on the key value.
Example – Dictionary
# Dictionary Creation
empDList = {}
empDList[1002]=”Sanjay”
empDList[1003]=’Sania’
empDList[1005]=’Sowmya’
print (“employee Details : \n”, empDList)
print(empDList.keys()) # prints all the keys
print(empDList.values()) # prints all the values
Happy Learning
Win Corporate Training
website: http://www.wincorptrg.com