Python Basic Tutorials

Python is a high level general purpose programming language which has a clear/easy learning curve. Python programming language is massively used in various domains like Artificial Intelligence, Data Science, Web Development, Utilities Tools and Scripts and many more

Python is a Programming language, and the default Python we use is written in C programming language which is also referred to as CPython (Python implementation in C). There are various implementation of Python Programming language i.e Jython(in JAVA), Skulpt(in JS) e.t.c

To make everything easy: We refer CPython as Python

Python Lists

Python list is the collection of items in a particular order

The list can contain digits, letters, or strings too

In python, a list can be created using the big brackets in which individual elements are separated using a comma operator. The lists are mutable

Example

>>>list=[1,2,3,'hello','world']
>>>print(list)
   [1,2,3,'hello','world']
Accessing the list elements.

Elements in the list are stored in the proper order. This order is said to be indexing. With the help of indexing or position, we can directly access list elements.


fruit=['mango','apple','banana','pineapple']
print(list[0])
print(list[1])
print(list[2])
print(list[-1])
print(list[-2])
Output

mango
apple
banana
pineapple
banana
Modifying list element.

Modifying the list element means changing the individual element in the list. It can be done using the list index concept too

let us consider the list of fruits and try to change its individual elements


>>>fruit=['mango','apple','banana','pineapple']
>>>print(fruit)
   ['mango','apple','banana','pineapple']

>>>fruit[0]='apple' #changing the first element 'mango' with 'apple'
>>>print(fruit)
   ['apple','apple','banana','pineapple']

>>>fruit[1]='mango' #changing the second element 'apple' with mango
>>>print(fruit)
   ['apple','mango','banana','pineapple']
Adding elements to the list
  1. Using append() method
  2. We can insert elements to the list in any index or position using insert() method. This method takes two parameters, one is an index element and another is an element or items to be inserted

    The major advantage of this method in lists is that we can directly insert elements to whichever position or the index we want.

    Example
    
    >>>name=['ram','sita','hari','krishna']
    >>>print(name)
       ['ram','sita','hari','krishna']
    
    >>>name.append('radha') #adding the element at the end of the list using append() method
    >>>print(name)
       ['ram','sita','hari','krishna','radha']
        
  3. Inserting elements to the list.
  4. We can insert elements to the list in any index or position using insert() method. This method takes two parameters, one is an index element and another is an element or items to be inserted.

    The major advantage of this method in lists is that we can directly insert elements to whichever position or the index we want.

    Example
    
    >>>name=['ram','sita','hari','krishna']
    >>>print(name)
       ['ram','sita','hari','krishna']
    
    >>>name.insert(0,'bishal') #'bishal is added to first index
    >>>print(name)
    ['bushal','ram','sita','hari','krishna']
    
Removing elements from the list
  1. Using del statement
  2. If you know the index of the element in the list to be deleted or removed you can just simply use the del statement.

    Example
    
    >>>name=['ram','sita','hari','krishna']
    >>>print(name)
       ['ram','sita','hari','krishna']
    
    >>>del name[0] #removing elements using del statement from the first index
    >>>print(name)
       ['sita','hari','krishna']
    
    >>>del name[2] #removing the last elements using del statement
    >>>print(name)
       ['sita','hari']
    
  3. Using pop() method
  4. If you want to remove the list elements from the end of the list then the pop() method is used

    Example
    
    >>>laptops=['acer','lenovo','toshiba','macbook','dell']
    >>>print(laptops)
       ['acer','lenovo','toshiba','macbook','dell']
    
    >>>popped_item=laptops.pop()
    >>>print(popped_item)
       'dell'
    >>>print(laptops)
       ['acer','lenovo','toshiba','macbook']
    
    >>>popped_item=laptops.pop()
    >>>print(popped_item)
       'macbook'
    >>>print(laptops)
       ['acer','lenovo','toshiba']

    We can also remove elements from any position of the list using pop() method

      
    >>>laptops=['acer','lenovo','toshiba','macbook','dell']
    >>>print(laptops)
       ['acer','lenovo','toshiba','macbook','dell']
    
    >>>popped_item=laptops.pop(1) #removing the item from index 1
    >>>print(popped_item)
       'lenovo'
    >>>print(laptops)
       ['acer','toshiba','macbook','dell']
    
    
    >>>popped_item=laptops.pop(0) #removing the item from index 0
    >>>print(popped_item)
       'acer'
    >>>print(laptops)
       ['toshiba','macbook','dell']
        
  5. Using remove() method
  6. The remove() method is used to remove the element from the list using the element's name. If you know whatever value you want to delete from the list you can simply remove it using the remove() method

    Example
    
    >>>laptops=['acer','lenovo','toshiba','macbook','dell']
    >>>print(laptops)
       ['acer','lenovo','toshiba','macbook','dell']
    
    >>>laptops.remove('acer') #removing 'acer' from list
    >>>laptops.remove('toshiba') #removing 'toshiba' from list
    
    >>>print(laptops)
       ['lenovo','macbook','dell']
Sorting a list
  1. Using sort() method.
  2. Using the sort() method, a list can be sorted permanently. It will affect the original list of elements and sort it.

    Example
    
    >>>age=[12,34,21,4,12,10]
    >>>age.sort()
    >>>print(age)
       [4,10,12,12,21,34]
  3. Using sorted() built-in function
  4. Using the sorted() method, a list can be sorted temporarily. It will sort the elements of the list without affecting an original list.

    Example
    
    >>>age=[12,34,21,4,12,10]
    >>>print(sorted(age))
       [4,10,12,12,21,34]
    >>>print(age)
       [12,34,21,4,12,10]
    
Printing a list in reverse order.

We can print a list in reverse order or let's say we can reverse a list by using list slicing.


>>>age=[12,34,21,4,12,10]
>>>reversed_age=age[::-1] #list slicing for reversing a list
>>>print(reversed_age)
   [10,12,4,21,34,12]
Reversing a list using reverse() method

>>>age=[12,34,21,4,12,10]
>>>age.reverse()
>>>print(age)
   [10,12,4,21,34,12]
Finding a length of a list.

We can find the length of a list using len() function


>>>age=[12,34,21,4,12,10]
>>>length= len(age) #length of the list.
>>>print(length)
    6