0

Slicing in py

I honestly don't get the concept of slicing in my still

4th Dec 2024, 7:08 AM
Rachel Atia
Rachel Atia - avatar
5 odpowiedzi
+ 5
In python, Slice is a way to extract a subset of elements from a list, tuple or string. It can take 3 arguments:- my_list[start_value: stop_value: step_value] Here, start_value defines the index of first element to include(default is 0) stop_value defines the index of last element to include(default is end of the list) step_value is the increament between elements(default is 1). Example:- my_list = [2,4,6,8,10] print(my_list[1:4:1]) #Output:- [4,6,8] You can use the same with tuple and string. Use the python course to understand it perfectly couse name Introduction to python, module name working with lists, lesson name Slicing. Some threads related to slicing:- https://www.sololearn.com/discuss/3285183/?ref=app https://www.sololearn.com/discuss/3276780/?ref=app https://www.sololearn.com/discuss/1575179/?ref=app https://www.sololearn.com/discuss/1660860/?ref=app https://www.sololearn.com/discuss/3285183/?ref=app
4th Dec 2024, 10:36 AM
Gulshan Mahawar
Gulshan Mahawar - avatar
+ 3
Rachel Atia , just some very basic info: you may know how to use indexing in python: > indexing allows you to access just *1* element / object from an iterable. (index starts with `0`) name = 'paul' # to get the first letter from the name you can do: print(name[0]) # this will result in `p` > slicing allows you to access a *range* of elements / objects from an iterable. (index starts with `0`, last index of range is not included) name = 'paul' # to get the first 2 letters from the name you can do: print(name[0:2] # this will result in `pa` the best would be to go through a tutorial, then do practice as much as you can afford.
4th Dec 2024, 12:06 PM
Lothar
Lothar - avatar
+ 1
Jan , your statement: ...slicing in Python is basically the same as using substring in a static language like java or c#... this can lead to a misunderstanding. > slicing can work on lists, tuples, dictionaries to extract objects. to compare this with getting substrings is a description that is not quite suitable.
4th Dec 2024, 4:12 PM
Lothar
Lothar - avatar
+ 1
Lothar I know that, and that's also why slicing has extended features compared to normal substring. Python has one trillion extra features compared to other languages. There is no substring method in Python, but slicing do the job on ordinary strings, and that is comparable to substring, even though you can slice lists, tuples etc........
4th Dec 2024, 4:34 PM
Jan
Jan - avatar
0
Slicing in Python is basically the same as using substring in a static language like Java or C#. It's just more dynamic in Python with extra smart features, such as the use of brackets and steps, instead of having a method called substring.
4th Dec 2024, 1:11 PM
Jan
Jan - avatar