Skip to main content

IP / CS - Python Slicing

 

Python slicing

Python slicing is a technique that allows you to extract specific parts of sequences like lists, strings, or tuples. It works by specifying a start, stop, and an optional step, using the following syntax:

sequence[start:stop:step]

EG:#1

my_list = [0, 1, 2, 3, 4, 5

print(my_list[1:4]) # Output: [1, 2, 3]

#2

my_string = "Hello, World!" 

print(my_string[:5]) # Output: "Hello" 

print(my_string[7:]) # Output: "World!"

#3

my_list = [10, 20, 30, 40, 50

print(my_list[-4:-1]) # Output: [20, 30, 40]

#4

my_string = "Python" 

print(my_string[::-1]) # Output: "nohtyP"

#5

lst = list(range(10))

>>> print(lst)

>>> print(lst[2:6:2])

#6

a = list(range(10))

print(a[:-2])

#7

a = list(range(10))

>>> print(a[::4])

#8

a = list(range(10))

>>> print(a[2:-2])


a = list(range(10))

>>> print(a[2:3])  Ans:[2]


>>> a = list(range(10))

>>> print(a[:-2:-2])   

#9.

list1=[['Maths',100],['Science',87],['English',70]]

print(list1)

print(list1[1],[0])

ASSIGNMENT

Q1. MCQ

1. What is the output of the following list function?

sampleList = [10, 20, 30, 40, 50]

sampleList.pop()

print(sampleList)

sampleList.pop(2)

print(sampleList)

a. [20, 30, 40, 50]

[10, 20, 40]

b. [10, 20, 30, 40]

[10, 20, 30, 50]

c. [10, 20, 30, 40]

[10, 20, 40]


2. What is the output of the following code

list1 = ['xyz', 'zara', 'PYnative']

print (max(list1))

a. PYnative

b. zara


3. What is the output of the following code

my_list = ["Hello", "Python"]

print("-".join(my_list))

a. HelloPython-

b. Hello-Python

c. -HelloPython


4. In Python, list is mutable

a. False

b. True


5. What is the output of the following list assignment

aList = [4, 8, 12, 16]

aList[1:4] = [20, 24, 28]

print(aList)

a. [4, 20, 24, 28, 8, 12, 16]

b. [4, 20, 24, 28]


6. Select all the correct options to join two lists in Python

listOne = ['a', 'b', 'c', 'd']

listTwo = ['e', 'f', 'g']

a. newList = listOne + listTwo

b. newList = extend(listOne, listTwo)

c. newList = listOne.extend(listTwo)

d. newList.extend(listOne, listTwo) 

 

7. What is the output of the following list operation

aList = [10, 20, 30, 40, 50, 60, 70, 80]

print(aList[2:5])

print(aList[:4])

print(aList[3:])

a. [20, 30, 40, 50]

[10, 20, 30, 40]

[30, 40, 50, 60, 70, 80]

b. [30, 40, 50]

[10, 20, 30, 40]

[40, 50, 60, 70, 80]


8. What is the output of the following list function?

sampleList = [10, 20, 30, 40, 50]

sampleList.append(60)

print(sampleList)

sampleList.append(60)

print(sampleList)

a. [10, 20, 30, 40, 50, 60]

b. [10, 20, 30, 40, 50, 60]

c. [10, 20, 30, 40, 50, 60]

d. [10, 20, 30, 40, 50, 60, 60]


9. What is the output of the following code

aList = ["PYnative", [4, 8, 12, 16]]

print(aList[0][1])

print(aList[1][3])

a. P 8

Y 16

b. P

12

c. Y

16


10. What is the output of the following

aList = [5, 10, 15, 25]

print(aList[::-2])

a. [15, 10, 5]

b. [10, 5]

c. [25, 10]


12. What command is used to insert 6 in a list ‘‘L’’ at 3rd position ?

a. L.insert(2,6)

b. L.insert(3,6)

c. L.add(3,6)

d. L.append(2,6)

Q2.1 What will be the output of the following code snippet?

arr = [1, 2, 3, 4, 5, 6]

for i in range(1, 6):

arr[i - 1] = arr[i]

for i in range(0, 6):

print(arr[i], end = " ")

2. What will be the output of the following code snippet?

fruit_list1 = ['Apple', 'Berry', 'Cherry', 'Papaya']

fruit_list2 = fruit_list1

fruit_list3 = fruit_list1[:]

fruit_list2[0] = 'Guava'

fruit_list3[1] = 'Kiwi'

sum = 0

for ls in (fruit_list1, fruit_list2, fruit_list3):

if ls[0] == 'Guava':

sum += 1

if ls[1] == 'Kiwi':

sum += 20 

print (sum)


3. What will be the output of the following code snippet?

L = [2e-04, 'a', False, 87]

T = (6.22, 'boy', True, 554)

for i in range(len(L)):

if L[i]:

    L[i] = L[i] + T[i]

else:

    T[i] = L[i] + T[i]

    break


4. What will be the output of the following code snippet?

L1 = [1, 1.33, 'GFG', 0, 'NO', None, 'G', True]

val1, val2 = 0, ' '

for x in L1:

    if(type(x) == int or type(x) == float):

        val1 += x

    elif(type(x) == str):

        val2 += x

    else:

        break

print(val1, val2)


5. What will be the output of the following code snippet?

L = [1, 3, 5, 7, 9]

print(L.pop(-3), end = ' ')

print(L.remove(L[0]), end = ' ')

print(L) 

Comments

Popular posts from this blog

GRADE XII - Python Connectivity with MySQL

  Python Connectivity with MySQL In real-life applications, the data inputted by the user and processed by the application needs to be saved permanently, so that it can be used for future manipulation. Usually, input and output data is displayed during execution but not stored, as it resides in main memory , which is temporary — i.e., it gets erased once the application is closed. This limitation can be overcome by sending the data to be stored in a database , which is made accessible to the user through a Front-End interface . Key Concepts Database A database is an organized collection of data that is stored and accessed electronically from a computer system. DBMS (Database Management System) A DBMS is software that interacts with end-users, applications, and the database to capture and analyze data. Front-End The Front-End is the user interface of the application, responsible for input/output interaction with the user. Back-End The Back-End refe...

GRADE XII - CSV FILE

CSV FILE (COMMA SEPERATED VALUE) CSV NOTES LINK :  CSV NOTES - CLICK HERE Writing data to a CSV file involves the following steps: Import the csv module Open the CSV file in write mode ( "w" ) using open() Create the writer object Write the data into the file using the writer object Close the file using close() writerow() Method This method is used to write a single row to a CSV file. It takes a sequence (list or tuple) as its parameter, and writes the items as a comma-separated line in the file. You do not need to add a newline character (\n) — it automatically places the end-of-line marker.     EXAMPLE 1: import csv  # Importing the csv module # CSV file opened using relative path csv_fobj = open("emp.csv", "w")   # Writer object created wtr_obj = csv.writer(csv_fobj) # Record with field heading is written wtr_obj.writerow(["Empno", "Name", "Salary"]) # Records with data are...