![Slicing in Python](https://i0.wp.com/learnwithmira.in/wp-content/uploads/2024/11/online-education-concept_52683-8596-1.jpg?resize=740%2C740&ssl=1)
![Slicing in Python](https://i0.wp.com/learnwithmira.in/wp-content/uploads/2024/11/online-education-concept_52683-8596-1.jpg?resize=740%2C740&ssl=1)
Slicing in Python : Handwritten Notes
Here are some Handwritten notes and questions based on slicing in Python. These questions relate to practical scenarios that students may encounter in real applications, helping them understand the utility of Python slicing.
[doc id=24731]
Slicing in Python with Strings : Video Explanation
One-Word/Short Answer Questions:
- What is the syntax for slicing a list in Python?
list[start:end:step]
- What is the result of the expression
list[2:5]
on the listlist = [10, 20, 30, 40, 50]
?[30, 40, 50]
- What does the slice
list[:3]
return on a list?- The first three elements of the list.
- What is the output of the following code? Python
lst = [1, 2, 3, 4, 5] print(lst[::2])
Use code with caution.[1, 3, 5]
- What will be the output of
s = "Python"; print(s[-1:])
?n
- What does the slice
list[::]
do in Python?- It returns a copy of the entire list.
- What is the default step size when slicing a list?
- 1
- In Python slicing, what does
list[::3]
do?- It returns every third element of the list.
- What is the result of the expression
str[::-1]
ifstr = "Hello"
?olleH
- What does
lst[0:5:-1]
do to the listlst = [10, 20, 30, 40, 50]
?
- It returns an empty list because the step size is negative and the start index is greater than the end index.
Output-Based Questions:
- What will be the output of the following code? Python
s = "CBSE" print(s[1:3])
Use code with caution.SB
- Given
lst = [1, 2, 3, 4, 5, 6, 7]
, what will be the output ofprint(lst[1:6:2])
?[2, 4, 6]
- What will be printed by the following code? Python
a = [10, 20, 30, 40, 50] print(a[::2])
Use code with caution.[10, 30, 50]
- What is the result of this Python slicing operation? Python
str = "Computer" print(str[-3:-1])
Use code with caution.ut
- What is the output of the following code snippet? Python
lst = [11, 22, 33, 44, 55] print(lst[4:1:-1])
Use code with caution.[55, 44, 33]
5-Mark Questions:
- Explain the slicing mechanism in Python with examples. How can you slice a string to extract specific characters?
- Slicing Mechanism: Slicing is a powerful technique in Python that allows you to extract specific portions of a sequence (like a list or string) using a simple notation. The general syntax for slicing is
sequence[start:end:step]
. - Extracting Specific Characters:
- To extract characters from a string, you can use the same slicing syntax. For example, to extract the first three characters of a string
s
, you can uses[:3]
. - To extract every other character, you can use a step size of 2:
s[::2]
- To reverse a string, you can use a negative step size:
s[::-1]
- To extract characters from a string, you can use the same slicing syntax. For example, to extract the first three characters of a string
- Slicing Mechanism: Slicing is a powerful technique in Python that allows you to extract specific portions of a sequence (like a list or string) using a simple notation. The general syntax for slicing is
- Write a Python program that takes a string as input and prints the reverse of the string using slicing. Python
string = input("Enter a string: ") reversed_string = string[::-1] print("Reversed string:", reversed_string)
Use code with caution. - What is the difference between
lst[start:end]
andlst[start:end:step]
in Python? Explain with an example.lst[start:end]
extracts a slice of the list from the indexstart
(inclusive) to the indexend
(exclusive), with a default step size of 1.lst[start:end:step]
extracts a slice with a specified step size. The step size determines how many elements to skip between each extracted element.
- Explain how negative indices work in slicing. Demonstrate with a Python code example.
- Negative indices are used to access elements from the end of the sequence. For example,
-1
refers to the last element,-2
to the second-to-last, and so on.
my_list = [10, 20, 30, 40, 50] print(my_list[-1]) # Output: 50 print(my_list[-3:]) # Output: [30, 40, 50]
Use code with caution. - Negative indices are used to access elements from the end of the sequence. For example,
- Write a Python program to demonstrate the use of slicing in lists, where you print a sublist from index 3 to index 7 (exclusive) and reverse the sublist using slicing. Python
my_list = [10, 20, 30, 40, 50, 60, 70] sublist = my_list[3:7] reversed_sublist = sublist[::-1] print("Sublist:", sublist) print("Reversed sublist:", reversed_sublist)
Real-World Example Questions:
- Extracting a Specific Range of Data from a List:
- You are working with a list of temperatures for a week:
temperatures = [30, 32, 31, 33, 34, 35, 36]
.
Write a Python code to extract the temperatures from the 2nd to the 5th day (inclusive).
Output the result.
- You are working with a list of temperatures for a week:
- Slicing a String for a Username:
- You have a string
username = "john_doe_2024"
.
Write a Python code to extract just the first part of the username (i.e.,john
) before the underscore (_
).
- You have a string
- Extracting Even Index Elements from a List:
- You have a list of student marks:
marks = [45, 78, 23, 56, 89, 92, 74, 61]
.
Write a Python code to extract all marks at even indices (0, 2, 4, 6) and print them.
- You have a list of student marks:
- Reversing a Date String:
- You are given a date string
date = "2024-11-24"
.
Write a Python program to reverse the string and print it as"42-11-2024"
.
- You are given a date string
- Slicing to Extract Year from Date:
- A string
date = "2024-11-24"
contains a date in the format YYYY-MM-DD.
Write a Python code to extract and print only the year (i.e.,2024
) from the string using slicing.
- A string
- Selecting Specific Characters from a String:
- You have the string
text = "MachineLearning"
.
Write a Python program to slice the string and print the characters starting from index 3 to index 10 (inclusive).
- You have the string
- Extracting Sublist from a List of Products:
- You are managing an inventory system with the following list of product names:
products = ['Apple', 'Banana', 'Carrot', 'Date', 'Eggplant', 'Fig', 'Grape']
.
Write a Python code to extract and print the products from index 2 to 5.
- You are managing an inventory system with the following list of product names:
- Extracting Odd Indexed Elements from a List:
- You have the list
numbers = [12, 17, 23, 29, 34, 41, 56, 63]
.
Write a Python program to extract and print the elements at odd indices (1, 3, 5, 7).
- You have the list
- Handling Date Formatting:
- You are given a date string
date = "2024-11-24"
.
Write a Python code to format the date to show just the month and day (e.g., “11-24”).
- You are given a date string
- Slicing for Pagination in a List:
- You have a list of 1000 items (e.g., pages of a book):
items = ['page_1', 'page_2', ..., 'page_1000']
.
Write a Python program to extract the items for the 10th page to the 20th page using slicing.
- You have a list of 1000 items (e.g., pages of a book):
- Reversing a List:
- You are working with a list of customer orders:
orders = [101, 102, 103, 104, 105]
.
Write a Python code to reverse the list using slicing and print the reversed list.
- You are working with a list of customer orders:
- Extracting Every Third Element from a List:
- You have a list of product prices:
prices = [100, 200, 300, 400, 500, 600, 700, 800, 900]
.
Write a Python code to extract every third price (i.e., prices at indices 0, 3, 6) and print them.
- You have a list of product prices:
- Slicing for Middle Elements in a List:
- You are analyzing a list of stock prices:
stock_prices = [210, 250, 270, 300, 290, 280, 350]
.
Write a Python code to extract and print the middle three elements from the list.
- You are analyzing a list of stock prices:
- Extracting a Range of Characters from a String:
- You have a string
sentence = "DataScienceIsFun"
.
Write a Python code to extract the word “Science” using slicing.
- You have a string
- Selecting the Last Few Elements from a List:
- You have a list of weekly sales:
sales = [100, 200, 300, 400, 500, 600, 700]
.
Write a Python program to extract the sales from the last 3 days of the week.
- You have a list of weekly sales:
- Slicing to Get a Subset of a String:
- You have a string
address = "1234 Elm St, Springfield"
.
Write a Python code to extract the street name (i.e., “Elm St”) using slicing.
- You have a string
- Accessing the Last Few Items of a List:
- You are working with a list of the top 10 books:
books = ['Book1', 'Book2', 'Book3', 'Book4', 'Book5', 'Book6', 'Book7', 'Book8', 'Book9', 'Book10']
.
Write a Python code to get the last 4 books from the list.
- You are working with a list of the top 10 books:
- Reversing a List and Removing Elements:
- You have a list of student names:
students = ['Alice', 'Bob', 'Charlie', 'David', 'Eve']
.
Write a Python code to reverse the list using slicing and remove the first element.
- You have a list of student names:
- Slicing with Negative Indices:
- You have the string
message = "Welcome to Python!"
.
Write a Python program to extract the last 6 characters of the string using negative indices.
- You have the string
- Generating a Sublist for Weekly Reports:
- You are working with a weekly report list:
reports = ['Report1', 'Report2', 'Report3', 'Report4', 'Report5', 'Report6', 'Report7']
.
Write a Python code to generate and print the reports for the 3rd, 4th, and 5th days of the week using slicing.
- You are working with a weekly report list:
Tags: String Slicing python , Strings in Python , Slicing in Python , CBSE 12th lectures , CBSE 11th String , String notes , CBSE 11th computer science