Python Complex Datatypes

Python Complex data types

  1. List
  2. Tuple
  3. Dictionary

List

  1. Group of Values belong different data type enclosed in [ ] bracket
  2. Value is called as item or Element
  3. Each Element will be retrieved based on the position.

List Operation

  1. Indexing – Retrieving only one item based on its position
  2. Slicing – Retrieving group of elements sequentially
  3. 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

  1. Elements are Enclosed in the round brackets ()
  2. Read only – Values can’t be modified.
  3. 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

  1. Elements are stored in Key-Value Format
  2. Key is unique and value can be duplicated
  3. 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
View My Profile

Advertisements