Created
February 5, 2023 06:02
-
-
Save Sarthak-Sidhant/465bfb475f4f809e0ddf0302a54b3c3e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Edited by: @Sarthak-Sidhant (GitHub) and Copyrights Reserved By https://sarthaksidhant.me/ & https://decodificate.tech/ | |
| # No Parts Of This Text or Code Shall be Reproduced Without Permission, Unless Used For Educational Purposes. | |
| #in this program, we will have a basic idea of how to code lists in python | |
| #we will start off by creating a list of numbers | |
| list1=[1,2,3,4,5,6,7,8,9,10] | |
| #this list contains numbers from 1 to 10, all of these are integers, and we will print them out | |
| print(list1) | |
| #we will now understand how to access elements in a list | |
| #positive indexing: we will print the first element of list1, the indexing (counting) starts from 0 | |
| print(list1[0]) #this will print 1st element of list1 which is 1 | |
| #negative indexing: we will print the last element of list1, the indexing (counting) starts from -1 (from the last) | |
| print(list1[-1]) #this will print the last element of list1 which is 10 | |
| #we will now understand slicing in lists | |
| #slicing: we will print the first 3 elements of list1 | |
| print(list1[0:3]) #this will print the first 3 elements of list1 which are 1,2,3 | |
| #we will now understand how to add elements to a list | |
| #we will add an element to list1 | |
| list1.append(11) #this will add 11 to the end of list1 | |
| print(list1) #this will print the list1 | |
| #we will understand how to insert an element at a specific position in a list | |
| #we will insert an element at the 3rd position in list1 | |
| list1.insert(2,"[-]") #this will insert 11 at the 3rd position in list1 | |
| print(list1) #this will print the list1 | |
| #we will now understand how to remove elements from a list | |
| #we will remove the last element from list1 | |
| list1.pop(-1) #this will remove the last element from list1 | |
| print(list1) #this will print the list1 | |
| #we will remove the 3rd element from list1 | |
| list1.remove("[-]") | |
| print(list1) #this will print the list1 | |
| #reversing a list | |
| list1.reverse() #this will reverse the list1 | |
| print(list1) #this will print the list1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment