Slice Function - Python Tips and Tricks #14
Skills:
AI Pair Programming80%
Key Takeaways
The video discusses the slice function in Python, a feature that enables unique slicing of collections. It is part of NeuralNine's Python Tips and Tricks series, focusing on beginner-level programming concepts.
Full Transcript
[Music] what is going on guys welcome back to python tips and tricks tutorial series in today's video we're going to talk about the slice function in python which is a fancy way of accessing elements from a collection so let us get right into it so you're probably already familiar with basic index slicing in python we have a list numbers and these numbers are sometimes 10 90 20 55 65 70 5 15 85 and so on just some list and what we can do in python is we can go ahead and print of course just single numbers we can say okay we want to have the index three or something and then we get the element at position four index three we can say okay i want all the elements from index three onwards so three to the end we got all these uh elements after and including index three i can say okay i want all up until index three without index three included uh i can say i want all from index three up until index six index six not included something like that um and i can do all kinds of things like that i can also say okay you give me all until the second last element so up until -2 and this is how we do basic index slicing in python we already know that this is not the trick the trick is that we can actually use the slice function in order to outsource this indexing to variables so we can say for example let's create a very simple variable here last four and last four means i'm interested in the last four elements of a list so what this essentially means is that i create an object that addresses the last four elements of a collection and then i don't have to slice anymore using the slicing that we just talked about but i can actually go ahead and pass this variable in order to access the last four elements so what i do here is i say slice and then i pass negative four because i'm interested in the last four and then i pass none which essentially means that we're just going to access the last four elements and now i can go ahead and print numbers last four and what happens then is you see that i got 75 15 and 85. what we passed here is the start index which is negative four which is the fourth last index and none means essentially go on until the list is over i can also pass a specific number here if i'm interested in a stop but since i'm not interested in a spot i start in the fourth last position and then go until the end this is the last four uh slice so to say i can also do that with first four uh but then of course i need to do it in a different way i need to just stop at four or i think either four or three uh so let's see print numbers first four and yeah as you can see the stop is four so four is no not included the index four is not included and that is what we can do here now there's also another thing that we can do here we can also specify for example a step size now instead of saying i'm interested in the last four in the first four and something in the middle i can also say i'm interested in every other element so i can create every other so i'm always skipping an element and what i basically say here is i say i start at zero i don't provide a stop so i don't say stop at any index and the step size is two and if i do that you can see print numbers every other and when i run this you can see i get 10 20 65 5 and 85 so 10 20 65 5 and 85 every other element i can do that with three as well so we don't have to do every other i can also do every third element so this this and this and theoretically if i have let's say 95 here i would also get 95. so we can specify a start an end or stop and a step size and we can do that also with just something you can you can name them whatever you want some things slice and you can say okay always start at index two and stop at index seven or something and the step size is two whatever and then you say okay something and when we print that you get 20 65 and five because we started index two this is this one here we take index two and we go up until index seven but we only take every other element so we take this one here but it is index uh four then we take this one here which is index six and then we have an x7 and the next one would be this one here but we're not going to take it because it's out of out of the limits okay so this is how you use the slice function in python now one little side note at the end of this video we can also do this on string so we can say i have my text here hello world how are you or something like that and i can also apply these here on the text i can also go ahead and say print my text and say every other even though it's actually every third and i would get every third letter so i would get hl wl uh space and then wry question mark um and this allows for some interesting string slicing as well if you have a certain pattern that you want to slice the string into or you have certain a certain section that you're interested in you can always do that with the slicing objects here so that's it for this video hope you enjoyed and hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below also make sure you subscribe to this channel in order to see more future videos for free and if you haven't done it yet click on the notification bell if you don't want to miss a video so you can turn on the notification and you get notified whenever a new video is released other than that thank you very much for watching see you in the next video and bye [Music] you
Original Description
Today we learn about the slice function in Python, which allows us to slice collections in a unique way.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
🐍 The Python Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: AI Pair Programming
View skill →
🎓
Tutor Explanation
DeepCamp AI