Dev.to · 5 min read

Introduction to Python Module Four Part Three: Slicing

Introduction to Python Module Four Part Three: Slicing

Sololearn’s Introduction to Python course is wrapping up today with a brand new topic. Today’s post is wrapping up module four by introducing. You’ll learn what slicing is and how to slice a list in Python. Sequences like lists and strings are ordered content. Ordered content is great for indexing and slicing. In addition to learning about slicing, Sololearn shares some advanced slicing and indexing tips to help you with your code. Slicing Slicing lets developers take portions from a list that they need. You’ll be slicing lists all the time in your Python code. At Coding with Kids, the students were slicing lists as they were building games. To slice a list, put the variable name of list followed by an opening square bracket. Put the index you want to start slicing at. Place a colon (:) after this. Put the index when you want to stop slicing at then place the closing square brackets. Here’s an example of slicing in action. I created a variable called pizza_toppings with a list of strings assigned to them. The starting index is inclusive while the stopping index is exclusive. pizza_toppings = ["pepperoni", "mushrooms", "onions", "peppers", "broccoli", "sausage"] print(pizza_toppings[0:3]) # print pepperoni, mushrooms, and onions The line underneath the list is slicing the first 3 toppings from the list. I’m printing them to the console to make sure these three are displaying. If you see the wrong toppings or too many items, double check the index you are starting and ending with. Slicing a String Strings can be sliced to in the same way we did the example above. Here’s a variable with a string assigned to it. If I want to slice certain letters in the string, I will put the indexes where I should start and stop at. pizza = "pepperoni" print(pizza[0:6]) # print pepper When I sliced this string, I was able to move data from a sequence. In this example, I’m able to create a brand new string. If you look at the the pizza topping example I created earlier made a brand new list. Advanced Indexing and Slicing Tips Now that you know what indexing and slicing works in Python, you are ready for the advanced tips. These tips will help you get better control of everything from each sequence you meet in Python. Omitting the starting index when slicing You don’t need to include the starting index in slicing if you are starting from the first index. If you just put the colon and the stopping index, the computer will know you are slicing from the first element. pizza_toppings = ["pepperoni", "mushrooms", "onions", "peppers", "broccoli", "sausage"] print(pizza_toppings[:3]) # print pepperoni, mushrooms, onions This example is using the same pizza_topping variable and list items from earlier. However, the second line of code doesn’t have the starting index. It just has the colon and the ending index, but the result is still the same as the example above. Omitting the stopping index when slicing. You don’t need the stopping index when you are slicing too. If you are starting somewhere and need everything until the end of the list, you put the starting index then a colon. When the computer read this line, it will know you want to slice from that point to the last list item. pizza_toppings = ["pepperoni", "mushrooms", "onions", "peppers", "broccoli", "sausage"] print(pizza_toppings[1:]) # print mushrooms, onions, peppers, broccoli, sausage This example is taking the pizza_topping variable I created early and lists items starting and the first index. When the computer runs this code, it will start slicing at that index and print everything in the list that comes after that. Negative Indexing Negative indexing is a way with working with lists that Python supports. This type of indexing is when developers index starting at the end. While indexing starts with a positive index, it is reverse when it is negative. You will start at the last value of a sequence so it will be index of -1. pizza_toppings = ["pepperoni", "mushrooms", "onions", "peppers", "broccoli", "sausage"] print(pizza_toppings[-1]) # print sausage This example is taking the list in the pizza_toppings variable and using negative indexing. If I put the index as [-1], the computer will print the last item in the list. Developers can use both positive and negative indexing when they slice items in the list. Here’s an example of how you might use both with the pizza_toppings variable. pizza_toppings = ["pepperoni", "mushrooms", "onions", "peppers", "broccoli", "sausage"] print(pizza_toppings[1:-1]) # print mushrooms, onions, peppers, broccoli Conclusion Another module is wrapped up. Now you know how to create lists in Python and why they are so important for developers to know. You learned what indexing and slicing are and how to do both of these skills in a list. The next module in this course is the last module in the course. This module is about functions. You’ll learn what a function is and why they are so important in Python. You’ll also learn how to create a function and working with functions in Python.

This is a summary aggregated from Dev.to. Read the complete article on the original site:

Read full article at Dev.to

More Programming & Dev News