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

CS - SORTING/SEARCHING ALGORITHMS

  SORTING ALGORITHMS                       SORTING ALGORITHM PDF LINK #Bubble Sort          ·        The outer loop iterates through the entire array. ·        The inner loop compares adjacent elements and swaps them if they are out of order. ·        The outer loop runs n times, and each pass moves the largest element to its correct position. arr=[3,8,5,2,1] n = len(arr) print(n) for i in range(n):  #traverse through all the elements         # Last i elements are already sorted, no need to check them         for j in range(0, n-i-1):              # Swap if the element found is greater than the next element              if arr[j] > arr[j+1]:               ...

GRADE XI - NESTED FOR LOOP

                                                         NESTED FOR LOOP 1. for var1 in range(3):      print(var1,"OUTER LOOP")          # 0 outer loop       1 outer loop      2 outer loop          for var2 in range(2):                  print(var2+1,"INNER LOOP")    #1  2 inner loop     1  2  inner loop   1 2 inner loop  2. Print the following pattern using for loop: 1  1 2  1 2 3  1 2 3 4  Sol: for r in range(1,5):   #ROWS     for c in range(1,r+1):   #COLUMNS         print(c,end=" ")     print() 3. Print the following pattern using for loop: @  @ @  @ @ @...