Collection in Python

Collection is a group of elements stored with a single Name.  Python has following collections

  1. List
  2. Tuple
  3. Dictionary
  4. Set

List

  1. Contains all types of values enclosed with the [ ] brackets
  2. Each value is called as Element or Item which will be identified based its position called Index.
  3. Index starts with 0.

Example List Creation

# List creation
empList = [ 2001, 'Shivani',2005, 'Sanjay', 2007, 'Babu']
print ("employee Details : \n", empList)

#specific element from List - Indexing
print("2nd Position : ", empList[1])

#printing Range of element - Slicing
print("1st to 4th element : ", empList[0:4])

#printing from 2nd element to last element
print("2nd to last element : ", empList[1:])

#printing the list two times - Repeation
print(empList*2)

List Operations

  • Indexing – Retrieving the specific Element from the List.
  • Slicing – Retrieving group of element from the List.
  • Repeation – Printing the list to specified no. of times.

Win Corporate Training
website: http://www.wincorptrg.com
View My Profile