Zed-King Study app · Free
O Level students across India practise here, free
Google login · no Play Store needed
About this PDF
Strings, lists, tuples, sets and dictionaries — with all their operations.
This is part of the free O Level M3-R5.1 study material in the Zed-King Study Zone. You can read every page above in your browser, or download the file and study offline. Zed-King Group of Institute is a NIELIT Study Centre in Kaithal, Haryana, and all of this material follows the current NIELIT syllabus.
- Course
- O Level — NIELIT Foundation Diploma
- Module
- M3 — M3-R5.1: Programming and Problem Solving through Python
- Section
- Chapterwise Notes
- Format
- PDF — read online or download
- Price
- Free, no sign-up required
Text from this PDF
The opening pages as plain text — open the reader above for tables, diagrams and the full file.
CHAPTER-5 SEQUENCE DATA TYPES List:- Python has 'List' data structure. List elements have a sequence and are mutable (changeable). The elements in Python's list are called 'items'. Every item in the list is separated with comma (,). And the entire items are closed inside square brackets ([]).
Mixed data types can also be used in the list.
Example:
print(type(l))
Note:- 1. List data type allow heterogeneous element
2. The list data type follows the insertion order, that is, the data will be printed in the order in which you input the data.
3. List data creates mutable object, that is, it allows modification in its content.
4. List data creates mutable object, that is, it allows modification in its content.
5. list support both positive & Negative indexing.
listl list2 list3 list4 #lnteger List #Float List #Mixed Data Types List #Nested List Creating List in Python There are many ways to prepare lists in Python.
1. Empty List:- To create an empty list, simply square bracket ([]) is used.
Example:- my_list = [l print(my_list)
2. List with Items:- # list To create a list with List with Items, items are placed in square brackets, separated by commas Example:-
3. Dynarnic list creation:— list elernent in square bracket ")
print(type(l))
4. Using List function:— List function is used to Separate character by character listl = elernent of list print(listl)
5. Using split function:— Split function is used to separate vvord by vvord.
print(list2)
ist2-split()
Accessing Element of List:- Two methods are used to access data from the list- By using index number By using slice operator
1. By using index number:- Listl Note:- Python supports both positive & negative indexing.
positive index means left to right print(listl [01)
print(list1[3])
Negative index means right to left print(listl[-l])
2. By using slice operator:- Syntax:- [start:stop:step] Start:- This indicates the starting index which has a default value of (O).
Stop:- It indicates the last index in the list whose by default value is one less than the maximum length in the list.
Step:- This is an increment value, its default value is Listl print(list1[O:6])
print(listl [2:4])
print(listl [:5:])
'Anil' 'ram', 60] 'ram'] 'Ram' # ['anil', Traversing Elements of List:- Sequential accessing of each element in a list is called traversing.
Using 'in' operator inside for loopz In operation is used to iterate each element through for loop in List sequence.
listl ,60] for x in listl:
color= ,'teal'] if 'purple' in color:
print("Yes")
Using range() Function:- This function provides the sequence between 2 integer numbers.
for i in range(5,15):
print(i)
Note:- You can convert it into a list by using the list() function.
print(list(range(1,5)))
Using while loope while i<6:
print(number[i],end=' ')
List Operations:- Many operations are performed on the list which are as follows-
1. Joining lists
2. Repeating lists
3. Slicing lists
4. Comparing lists Joining Lists:— Joining list means to concatenate (add) two lists. By using + operator, you can create a list by joining two lists together.
c=x+Y # [IO, 20, 30, •apple', •banana', •mango'] Note:- TO concatenate two lists, both the arguments Of + operator must be Of list type.
print(x+y)
# TypeError: can only concatenate list (not "int") to list Repeating List:- Repetition operation is used to repeat the elements of the list up to the given numbers.
b=a*3 Slicing List:- Slice operator also works on list print(a[1:3])
Comparison of list:- Comparison operator is used to compare list objects. To compare, number of element, order of element and list should be case sensitive.
x=['Dog', 'Cat', 'Rat'] y=['dog', 'cat', 'rat'] 'CAT', 'RAT'] print(x==z)
print(x!=z)
#False # False Note:- When comparing list objects between relational operators (>, < , > =, only the comparison of the first element of the list object is performed.
print(a > b)
print(a < b)
# True # False Aliasing List Object:- When the already available list is given to another reference variable, this process is called aliasing.
b=a When an element of one reference variable is changed in the aliasing list object, those changes are also reflected in the other reference variables.
More chapterwise notes for M3-R5.1
Run these Python programs yourself — free
Reading code is not the same as writing it. Type any program from these notes into our free online Python compiler (real Python 3 with NumPy, input() works), or study one of the explained programs with its output.
Prepare for O Level with expert guidance
Classroom training, practical labs and exam registration at Zed-King Institute, Kaithal.
