0% found this document useful (0 votes)
2 views

Slice_operatorUnit5Python

Chapter 5 discusses the concept of indexing in Python, explaining that indexes start at zero and can be negative to access elements from the end of a list. It covers list slicing, detailing the syntax and providing examples for accessing subsets of lists. Additionally, the chapter includes practice exercises for manipulating lists and using loops.

Uploaded by

vidhu.bhis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Slice_operatorUnit5Python

Chapter 5 discusses the concept of indexing in Python, explaining that indexes start at zero and can be negative to access elements from the end of a list. It covers list slicing, detailing the syntax and providing examples for accessing subsets of lists. Additionally, the chapter includes practice exercises for manipulating lists and using loops.

Uploaded by

vidhu.bhis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Chapter 5 Python

What Is an Index?

An index is a position of an individual character or element in a list, tuple, or string. The index
value always starts at zero and ends at one less than the number of items.

Negative indexes enable users to index a list, tuple, or other indexable containers from the end of
the container, rather than the start.

Python List Slicing

List slicing is the process of accessing a specified portion or subset of a list for some action while
leaving the rest of the list alone. Let us consider a Python list. To access a range of elements in a
list, you must slice it. One method is to utilize the simple slicing operator, i.e. colon (:) With this
operator, one can define where to begin slicing, and where to terminate slicing, and the step. List
slicing creates a new list from an old one.

Syntax

list[start: stop: step]

where,

 start – index position from where the slicing will start in a list
 stop – index position till which the slicing will end in a list
 step – number of steps, i.e. the start index is changed after every n steps, and list slicing is
performed on that index
Note – In Python, indexing starts from 0, and not 1.
Get all the Items from One Position to Another Position

Example:
my_list = [1, 3, 5, 7, 9, 11, 13, 15]
print('Items in List from 1st to 6th index are:', my_list[1:6])

Output
Items in List from 1st to 6th index are: [3, 5, 7, 9, 11]

If you wish to display all the elements between two specific indices, put them before and after the
‘:’ symbol. In the preceding example, my list[1:6] returns the elements between the first and sixth
positions. The beginning position (i.e. 1) is included, but the finishing position (i.e. 6) is not.

output: [3,4,5,6,7]

Solve these questions for practice:

1)Create a list in Python of children selected for science quiz with following names- Arjun,
Sonakshi, Vikram, Sandhya, Sonal, Isha, Kartik
 Print the whole list
 Delete the name “Vikram” from the list
 Add the name “Jay” at the end
 Remove the item which is at the second position.
 print the length of the list
 print the elements from second to fourth position using positive indexing
 print the elements from position third to fifth using negative indexing

2) Perform the following tasks on the list in sequence-


● Create a list num=[23,12,5,9,65,44]
● Create a list of first 10 even numbers, add 1 to each list item and print the final list.
● Create a list List_1=[10,20,30,40]. Add the elements [14,15,12] using extend function.
Now sort the final list in ascending order and print it.

Try these programs using If, For, While Loop


1. Program to check if a person can vote
2. To check the grade of a student
3. Input a number and check if the number is positive, negative or zero and display an
appropriate message
4. To print first 10 natural numbers
5. To print first 10 even numbers
6. To print odd numbers from 1 to n
7. To print sum of first 10 natural numbers
8. Program to find the sum of all numbers stored in a list

You might also like