+ 1
Can someone help and explain better on slicing
I hardly get a lesson right while slicing
3 odpowiedzi
+ 2
peculiar benard
Try using search bar before posting a question:
https://www.sololearn.com/Discuss/3310243/?ref=app
+ 1
Slices allow us to output arrays and perform various operations on them. For example:
animals= ["cat", "dog", "bird", "bear", "fox"]
favourite_animals = animals[2:]
print(favourite_animals)
Output: ["bird", "bear", "fox"]
You can use slices starting from index 0.
favourite_animals = animals[:2]
print(favourite_animals)
Output: ["cat, "dog"]
And you can use the slices like this:
favourite_animals = animals[1:4]
print(favourite_animals)
Output: ["dog", "bird", "bear"]
So we understand that slices simplify our code.
0
Slicing is like cutting a piece of cake 🍰
In python we use slicing for accessing the specific element
Eg : We need 70,30 in the list only
We access the elements with their index number start from 0
Basically 20 at -> 0 index
list = [20,40,70,30,100]
list = list[2:4] #they ignore 4th element,access only 2 and 3rd element as a slice 🍕
print(list)