0% found this document useful (0 votes)
0 views3 pages

Python Class4

The document outlines Python collections, specifically focusing on Lists, Tuples, Sets, and Dictionaries, detailing their characteristics and behaviors. It provides syntax examples for creating and manipulating Lists, including accessing elements, using ranges, and various predefined methods. Additionally, it explains how to create Lists using the list() function and highlights the mutable nature of Lists in Python.

Uploaded by

sachinv13307
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views3 pages

Python Class4

The document outlines Python collections, specifically focusing on Lists, Tuples, Sets, and Dictionaries, detailing their characteristics and behaviors. It provides syntax examples for creating and manipulating Lists, including accessing elements, using ranges, and various predefined methods. Additionally, it explains how to create Lists using the list() function and highlights the mutable nature of Lists in Python.

Uploaded by

sachinv13307
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Collections (Like Array)

1) List – It is a collection of values, which is ordered and changeable. (Allows Duplication)


2) Tuple – It is a collection of values, which is ordered and unchangeable (Allows Duplication)
3) Set – It is collection of values, which is unordered and unindexed. (Not allows Duplication)
4) Dictionary – It is collection of values, which is unordered, changeable and indexed. (Not
allows Duplication)

List:

 In Python List is enclosed within square bracket.

Syntax:
Variable = [value1,value2,value3,…]

Eg:
students = [“Ram”,”Siva”,”Mani”]
print(students) #[‘Ram’,’Siva’,’Mani’]

 In Python List allows different types of values in single list.

Eg:

emp =[“Selvam”,34,32000.00]

print(emp) # [‘Selvam’,34,32000.00]

 List items can be accessed using index number. (Note: Index start with 0)

Eg:
print(emp[0]) #Selvam

 Unlike other programming language, Python allow negative index. (-1 means last value, -2
means last before and so on)

Eg:
print(emp[-2]) #34
Range Behaviour:

 In Python we can access set of index using range of indexes option.

Syntax:
variable[start_index : end_index]
Note: Here range start with start_index, but end in end_index-1.

Eg:
vowels= [‘a’, ’e’, ’i’, ’o’, ’u’]
print(vowels[2:4]) #[‘i’,’o’]

 In Python we can omit start_index or end_index in range of indexes option.

Eg:
print(vowels[:3]) # [‘a’, ’e’, ’i’]
print(vowels[1:]) # [‘e’, ’i’, ‘o’, ’u’]

 In Python range of indexes option allows negative index also.

Eg:
print(vowels[-4:-2] #[‘e’, ’i’]
Note: here -4 is included and -2 is excluded

Eg:
print(vowels[-3:] #[‘i’,’o’,’u’]

Eg:
print(vowels[:-1] #[‘a’,’e’,’i’,’o’]
 In Python List in Mutable (Changeable), so we can change the value using index.

Eg:
vowels[1]=’E’
print(vowels) # [‘a’, ’E’, ’i’, ’o’, ’u’]

 In Python to get number of items in the list, there is the predefined function called len().

Eg:
Print(len(vowels)) #5

 In Python to join two list just use + sign to add them.

Eg:
a = [1,2,3]
b = [4,5,6]
c = a+b
print(c) # [1,2,3,4,5,6]

 To handle List in Python there are predefined methods available, they are

extend() – To join another list into current list


append() - To add new item at the end of list
copy() - To copy a list to new variable
clear() - To delete all items in the list
count() – Count number of occurrence of a particular item
index() – Find index of first occurrence of a particular item
insert() – Add new item into list at particular position
pop() - Remove item from the list using index position
remove() - Remove item in the list using item value
reverse() – Reverse item in the list
sort() – Sort item in the list (Default: Ascending order)

 Create List using list() function.

Eg:
a = list((1,2,3))
print(a) # [1,2,3]

Note: While creating list using list() function values should be given inside parenthesis

You might also like