<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: OLADEJOBI7</title>
    <description>The latest articles on DEV Community by OLADEJOBI7 (@oladejobi7).</description>
    <link>https://dev.to/oladejobi7</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F825846%2F5ccdc710-9886-4875-96e6-9c5c087cd2ce.png</url>
      <title>DEV Community: OLADEJOBI7</title>
      <link>https://dev.to/oladejobi7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/oladejobi7"/>
    <language>en</language>
    <item>
      <title>Python Dictionary with examples</title>
      <dc:creator>OLADEJOBI7</dc:creator>
      <pubDate>Sat, 05 Mar 2022 14:03:04 +0000</pubDate>
      <link>https://dev.to/oladejobi7/python-dictionary-with-examples-1bop</link>
      <guid>https://dev.to/oladejobi7/python-dictionary-with-examples-1bop</guid>
      <description>&lt;p&gt;Dictionary is a mutable data type in Python. A python dictionary is a collection of key and value pairs separated by a colon (:), enclosed in curly braces {}.&lt;br&gt;
Python Dictionary&lt;br&gt;
Here we have a dictionary. Left side of the colon(:) is the key and right side of the : is the value.&lt;br&gt;
mydict = {'StuName': 'Ajeet', 'StuAge': 30, 'StuCity': 'Agra'}&lt;br&gt;
Points to Note:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Keys must be unique in dictionary, duplicate values are allowed.&lt;/li&gt;
&lt;li&gt;A dictionary is said to be empty if it has no key value pairs. An empty dictionary is denoted like this: {}.&lt;/li&gt;
&lt;li&gt;The keys of dictionary must be of immutable data types such as String, numbers or tuples.
Accessing dictionary values using keys in Python
To access a value we can can use the corresponding key in the square brackets as shown in the following example. Dictionary name followed by square brackets and in the brackets we specify the key for which we want the value.
mydict = {'StuName': 'Ajeet', 'StuAge': 30, 'StuCity': 'Agra'}
print("Student Age is:", mydict['StuAge'])
print("Student City is:", mydict['StuCity'])
Output:
Student Age is: 30
Student City is: Agra
specify a key which doesn’t exist in the dictionary then you will get a compilation error. For example. Here we are trying to access the value for key ‘StuClass’ which does not exist in the dictionary mydict, thus we get a compilation error when we run this code.
mydict = {'StuName': 'Ajeet', 'StuAge': 30, 'StuCity': 'Agra'}
print("Student Age is:", mydict['StuClass'])
print("Student City is:", mydict['StuCity'])
Output:
Traceback (most recent call last):
File "./prog.py", line 2, in 
KeyError: 'StuClass'
Change values in Dictionary
Here we are updating the values for the existing key-value pairs. To update a value in dictionary we are using the corresponding key.
mydict = {'StuName': 'Ajeet', 'StuAge': 30, 'StuCity': 'Agra'}
print("Student Age before update is:", mydict['StuAge'])
print("Student City before update is:", mydict['StuCity'])
mydict['StuAge'] = 31
mydict['StuCity'] = 'Noida'
print("Student Age after update is:", mydict['StuAge'])
print("Student City after update is:", mydict['StuCity'])
Output:
Student Age before update is: 30
Student City before update is: Agra
Student Age after update is: 31
Student City after update is: Noida
Adding a new entry (key-value pair) in dictionary
We can also add a new key-value pair in an existing dictionary. Lets take an example to understand this.
mydict = {'StuName': 'Steve', 'StuAge': 4, 'StuCity': 'Agra'}
mydict['StuClass'] = 'Jr.KG'
print("Student Name is:", mydict['StuName'])
print("Student Class is:", mydict['StuClass'])
Output:
Student Name is: Steve
Student Class is: Jr.KG
Loop through a dictionary
We can loop through a dictionary as shown in the following example. Here we are using for loop.
mydict = {'StuName': 'Steve', 'StuAge': 4, 'StuCity': 'Agra'}
for e in mydict:
print("Key:",e,"Value:",mydict[e])
Output:
Key: StuName Value: Steve
Key: StuAge Value: 4
Key: StuCity Value: Agra
Python delete operation on dictionary
We can delete key-value pairs as well as entire dictionary in python. Lets take an example. As you can see we can use del following by dictionary name and in square brackets we can specify the key to delete the specified key value pair from dictionary.
To delete all the entries (all key-value pairs) from dictionary we can use the clear() method.
To delete entire dictionary along with all the data use del keyword followed by dictionary name as shown in the following example.
mydict = {'StuName': 'Steve', 'StuAge': 4, 'StuCity': 'Agra'}
del mydict['StuCity']; # remove entry with key 'StuCity'
mydict.clear();     # remove all key-value pairs from mydict
del mydict ;        # delete entire dictionary mydict&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>azuretrialhack</category>
    </item>
    <item>
      <title>Python Dictionary with examples</title>
      <dc:creator>OLADEJOBI7</dc:creator>
      <pubDate>Sat, 05 Mar 2022 13:49:12 +0000</pubDate>
      <link>https://dev.to/oladejobi7/python-dictionary-with-examples-4ldj</link>
      <guid>https://dev.to/oladejobi7/python-dictionary-with-examples-4ldj</guid>
      <description>&lt;p&gt;Dictionary is a mutable data type in Python. A python dictionary is a collection of key and value pairs separated by a colon (:), enclosed in curly braces {}.&lt;br&gt;
Python Dictionary&lt;br&gt;
Here we have a dictionary. Left side of the colon(:) is the key and right side of the : is the value.&lt;br&gt;
mydict = {'StuName': 'Ajeet', 'StuAge': 30, 'StuCity': 'Agra'}&lt;br&gt;
Points to Note:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Keys must be unique in dictionary, duplicate values are allowed.&lt;/li&gt;
&lt;li&gt;A dictionary is said to be empty if it has no key value pairs. An empty dictionary is denoted like this: {}.&lt;/li&gt;
&lt;li&gt;The keys of dictionary must be of immutable data types such as String, numbers or tuples.
Accessing dictionary values using keys in Python
To access a value we can can use the corresponding key in the square brackets as shown in the following example. Dictionary name followed by square brackets and in the brackets we specify the key for which we want the value.
mydict = {'StuName': 'Ajeet', 'StuAge': 30, 'StuCity': 'Agra'}
print("Student Age is:", mydict['StuAge'])
print("Student City is:", mydict['StuCity'])
Output:
Student Age is: 30
Student City is: Agra
specify a key which doesn't exist in the dictionary then you will get a compilation error. For example. Here we are trying to access the value for key 'StuClass' which does not exist in the dictionary mydict, thus we get a compilation error when we run this code.
mydict = {'StuName': 'Ajeet', 'StuAge': 30, 'StuCity': 'Agra'}
print("Student Age is:", mydict['StuClass'])
print("Student City is:", mydict['StuCity'])
Output:
Traceback (most recent call last):
File "./prog.py", line 2, in 
KeyError: 'StuClass'
Change values in Dictionary
Here we are updating the values for the existing key-value pairs. To update a value in dictionary we are using the corresponding key.
mydict = {'StuName': 'Ajeet', 'StuAge': 30, 'StuCity': 'Agra'}
print("Student Age before update is:", mydict['StuAge'])
print("Student City before update is:", mydict['StuCity'])
mydict['StuAge'] = 31
mydict['StuCity'] = 'Noida'
print("Student Age after update is:", mydict['StuAge'])
print("Student City after update is:", mydict['StuCity'])
Output:
Student Age before update is: 30
Student City before update is: Agra
Student Age after update is: 31
Student City after update is: Noida
Adding a new entry (key-value pair) in dictionary
We can also add a new key-value pair in an existing dictionary. Lets take an example to understand this.
mydict = {'StuName': 'Steve', 'StuAge': 4, 'StuCity': 'Agra'}
mydict['StuClass'] = 'Jr.KG'
print("Student Name is:", mydict['StuName'])
print("Student Class is:", mydict['StuClass'])
Output:
Student Name is: Steve
Student Class is: Jr.KG
Loop through a dictionary
We can loop through a dictionary as shown in the following example. Here we are using for loop.
mydict = {'StuName': 'Steve', 'StuAge': 4, 'StuCity': 'Agra'}
for e in mydict:
print("Key:",e,"Value:",mydict[e])
Output:
Key: StuName Value: Steve
Key: StuAge Value: 4
Key: StuCity Value: Agra
Python delete operation on dictionary
We can delete key-value pairs as well as entire dictionary in python. Lets take an example. As you can see we can use del following by dictionary name and in square brackets we can specify the key to delete the specified key value pair from dictionary.
To delete all the entries (all key-value pairs) from dictionary we can use the clear() method.
To delete entire dictionary along with all the data use del keyword followed by dictionary name as shown in the following example.
mydict = {'StuName': 'Steve', 'StuAge': 4, 'StuCity': 'Agra'}
del mydict['StuCity']; # remove entry with key 'StuCity'
mydict.clear();     # remove all key-value pairs from mydict
del mydict ;        # delete entire dictionary mydict&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>What is set in python ?</title>
      <dc:creator>OLADEJOBI7</dc:creator>
      <pubDate>Sat, 05 Mar 2022 13:25:02 +0000</pubDate>
      <link>https://dev.to/oladejobi7/what-is-set-in-python-and-it-usefull--3cf6</link>
      <guid>https://dev.to/oladejobi7/what-is-set-in-python-and-it-usefull--3cf6</guid>
      <description>&lt;p&gt;Set is an unordered and unindexed collection of items in Python. Unordered means when we display the elements of a set, it will come out in a random order. Unindexed means, we cannot access the elements of a set using the indexes like we can do in list and tuples.&lt;br&gt;
The elements of a set are defined inside curly brackets and are separated by commas. For example –&lt;br&gt;
myset = {1, 2, 3, 4, "hello"}&lt;br&gt;
Python Set Example&lt;/p&gt;

&lt;h1&gt;
  
  
  Set Example
&lt;/h1&gt;

&lt;p&gt;myset = {"hi", 2, "bye", "Hello World"}&lt;br&gt;
print(myset)&lt;br&gt;
Output:&lt;br&gt;
{‘Hello world’, 2, ‘hi’, ‘bye’}&lt;br&gt;
Checking whether an item is in the set&lt;br&gt;
We can check whether an item exists in Set or not using “in” operator as shown in the following example. This returns the boolean value true or false. If the item is in the given set then it returns true, else it returns false.&lt;/p&gt;

&lt;h1&gt;
  
  
  Set Example
&lt;/h1&gt;

&lt;p&gt;myset = {"hi", 2, "bye", "Hello World"}&lt;/p&gt;

&lt;h1&gt;
  
  
  checking whether 2 is in myset
&lt;/h1&gt;

&lt;p&gt;print(2 in myset)&lt;/p&gt;

&lt;h1&gt;
  
  
  checking whether "hi" is in myset
&lt;/h1&gt;

&lt;p&gt;print("hi" in myset)&lt;/p&gt;

&lt;h1&gt;
  
  
  checking whether "BeginnersBook" is in myset
&lt;/h1&gt;

&lt;p&gt;print("BeginnersBook" in myset)&lt;br&gt;
Output:&lt;br&gt;
True&lt;br&gt;
True&lt;br&gt;
False&lt;br&gt;
Loop through the elements of a Set in Python&lt;br&gt;
We can loop through the elements of a set in Python as shown in the following elements. As you can see in the output that the elements will appear in random order each time you run the code.&lt;/p&gt;

&lt;h1&gt;
  
  
  Set Example
&lt;/h1&gt;

&lt;p&gt;myset = {"hi", 2, "bye", "Hello World"}&lt;/p&gt;

&lt;h1&gt;
  
  
  loop through the elements of myset
&lt;/h1&gt;

&lt;p&gt;for a in myset:&lt;br&gt;
    print(a)&lt;br&gt;
Output:&lt;br&gt;
2&lt;br&gt;
bye&lt;br&gt;
Hello World&lt;br&gt;
hi&lt;br&gt;
Python — Add or remove item from a Set&lt;br&gt;
We can add an item in a Set using add() function and we can remove an item from a set using remove() function as shown in the following example.&lt;/p&gt;

&lt;h1&gt;
  
  
  Set Example
&lt;/h1&gt;

&lt;p&gt;myset = {"hi", 2, "bye", "Hello World"}&lt;br&gt;
print("Original Set:", myset)&lt;/p&gt;

&lt;h1&gt;
  
  
  adding an item
&lt;/h1&gt;

&lt;p&gt;myset.add(99)&lt;br&gt;
print("Set after adding 99:", myset)&lt;/p&gt;

&lt;h1&gt;
  
  
  removing an item
&lt;/h1&gt;

&lt;p&gt;myset.remove("bye")&lt;br&gt;
print("Set after removing bye:", myset)&lt;br&gt;
Output:&lt;br&gt;
Original Set: {'bye', 2, 'Hello World', 'hi'}&lt;br&gt;
Set after adding 99: {'bye', 2, 99, 'Hello World', 'hi'}&lt;br&gt;
Set after removing bye: {2, 99, 'Hello World', 'hi'}&lt;br&gt;
Set Methods&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;add(): This method adds an element to the Set.&lt;/li&gt;
&lt;li&gt;remove(): This method removes a specified element from the Set&lt;/li&gt;
&lt;li&gt;discard(): This method works same as remove() method, however it doesn’t raise an error when the specified element doesn’t exist.&lt;/li&gt;
&lt;li&gt;clear(): Removes all the elements from the set.&lt;/li&gt;
&lt;li&gt;copy(): Returns a shallow copy of the set.&lt;/li&gt;
&lt;li&gt;difference(): This method returns a new set which is a difference between two given sets.&lt;/li&gt;
&lt;li&gt;difference_update(): Updates the calling set with the Set difference of two given sets.&lt;/li&gt;
&lt;li&gt;intersection(): Returns a new set which contains the elements that are common to all the sets.&lt;/li&gt;
&lt;li&gt;intersection_update(): Updates the calling set with the Set intersection of two given sets.&lt;/li&gt;
&lt;li&gt;isdisjoint(): Checks whether two sets are disjoint or not. Two sets are disjoint if they have no common elements.&lt;/li&gt;
&lt;li&gt;issubset(): Checks whether a set is a subset of another given set.&lt;/li&gt;
&lt;li&gt;pop(): Removes and returns a random element from the set.&lt;/li&gt;
&lt;li&gt;union(): Returns a new set with the distinct elements of all the sets.&lt;/li&gt;
&lt;li&gt;update(): Adds elements to a set from other passed iterable.&lt;/li&gt;
&lt;li&gt;symmetric_difference(): Returns a new set which is a symmetric difference of two given sets.&lt;/li&gt;
&lt;li&gt;symmetric_difference_update(): Updates the calling set with the symmetric difference of two given sets.&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>What is tuple in python programming what can we use it for and what are the different between tuple and list ?</title>
      <dc:creator>OLADEJOBI7</dc:creator>
      <pubDate>Sat, 05 Mar 2022 13:21:23 +0000</pubDate>
      <link>https://dev.to/oladejobi7/what-is-tuple-in-python-programming-what-can-we-use-it-for-and-what-are-the-different-between-tuple-and-list--5nc</link>
      <guid>https://dev.to/oladejobi7/what-is-tuple-in-python-programming-what-can-we-use-it-for-and-what-are-the-different-between-tuple-and-list--5nc</guid>
      <description>&lt;p&gt;In Python, a tuple is similar to List except that the objects in tuple are immutable which means we cannot change the elements of a tuple once assigned. On the other hand, we can change the elements of a list.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Different between tuple and list&lt;/li&gt;
&lt;li&gt;The elements of a list are mutable whereas the elements of a tuple are immutable.&lt;/li&gt;
&lt;li&gt;When we do not want to change the data over time, the tuple is a preferred data type whereas when we need to change the data in future, list would be a wise option.&lt;/li&gt;
&lt;li&gt;Iterating over the elements of a tuple is faster compared to iterating over a list.&lt;/li&gt;
&lt;li&gt;Elements of a tuple are enclosed in parenthesis whereas the elements of list are enclosed in square bracket.&lt;/li&gt;
&lt;li&gt;How to create a tuple in Python
To create a tuple in Python, place all the elements in a () parenthesis, separated by commas. A tuple can have heterogeneous data items, a tuple can have string and list as data items as well.
2.1 Example - Creating tuple
In this example, we are creating few tuples. We can have tuple of same type of data items as well as mixed type of data items. This example also shows nested tuple (tuples as data items in another tuple).
# tuple of strings
my_data = ("hi", "hello", "bye")
print(my_data)
# tuple of int, float, string
my_data2 = (1, 2.8, "Hello World")
print(my_data2)
# tuple of string and list
my_data3 = ("Book", [1, 2, 3])
print(my_data3)
# tuples inside another tuple
# nested tuple
my_data4 = ((2, 3, 4), (1, 2, "hi"))
print(my_data4)
Output:
('hi', 'hello', 'bye')
(1, 2.8, 'Hello World')
('Book', [1, 2, 3])
((2, 3, 4), (1, 2, 'hi'))
2.2 Empty tuple:
# empty tuple
my_data = ()
2.3 Tuple with only single element:
Note: When a tuple has only one element, we must put a comma after the element, otherwise Python will not treat it as a tuple.
# a tuple with single data item
my_data = (99,)
If we do not put comma after 99 in the above example then python will treat my_data as an int variable rather than a tuple.&lt;/li&gt;
&lt;li&gt;How to access tuple elements
We use indexes to access the elements of a tuple. Lets take few example to understand the working.
3.1 Accessing tuple elements using positive indexes
We can also have negative indexes in tuple, we have discussed that in the next section. Indexes starts with 0 that is why we use 0 to access the first element of tuple, 1 to access second element and so on.
# tuple of strings
my_data = ("hi", "hello", "bye")
# displaying all elements
print(my_data)
# accessing first element
# prints "hi"
print(my_data[0])
# accessing third element
# prints "bye"
print(my_data[2])
Output:
('hi', 'hello', 'bye')
hi
bye
Note:&lt;/li&gt;
&lt;li&gt;TypeError: If you do not use integer indexes in the tuple. For example my_data[2.0] will raise this error. The index must always be an integer.&lt;/li&gt;
&lt;li&gt;IndexError: Index out of range. This error occurs when we mention the index which is not in the range. For example, if a tuple has 5 elements and we try to access the 7th element then this error would occurr.
3.2 Negative indexes in tuples
Similar to list and strings we can use negative indexes to access the tuple elements from the end.
-1 to access last element, -2 to access second last and so on.
my_data = (1, 2, "Kevin", 8.9)
# accessing last element
# prints 8.9
print(my_data[-1])
# prints 2
print(my_data[-3])
Output:
8.9
2
3.3 Accessing elements from nested tuples
Lets understand how the double indexes are used to access the elements of nested tuple. The first index represents the element of main tuple and the second index represent the element of the nested tuple.
In the following example, when I used my_data[2][1], it accessed the second element of the nested tuple. Because 2 represented the third element of main tuple which is a tuple and the 1 represented the second element of that tuple.
my_data = (1, "Steve", (11, 22, 33))
# prints 'v'
print(my_data[1][3])
# prints 22
print(my_data[2][1])
Output:
v
22&lt;/li&gt;
&lt;li&gt;Operations that can be performed on tuple in Python
Lets see the operations that can be performed on the tuples in Python.
4.1 Changing the elements of a tuple
We cannot change the elements of a tuple because elements of tuple are immutable. However we can change the elements of nested items that are mutable. For example, in the following code, we are changing the element of the list which is present inside the tuple. List items are mutable that's why it is allowed.
my_data = (1, [9, 8, 7], "World")
print(my_data)
# changing the element of the list
# this is valid because list is mutable
my_data[1][2] = 99
print(my_data)
# changing the element of tuple
# This is not valid since tuple elements are immutable
# TypeError: 'tuple' object does not support item assignment
# my_data[0] = 101
# print(my_data)
Output:
(1, [9, 8, 7], 'World')
(1, [9, 8, 99], 'World')
4.2 Delete operation on tuple
We already discussed above that tuple elements are immutable which also means that we cannot delete the elements of a tuple. However deleting entire tuple is possible.
my_data = (1, 2, 3, 4, 5, 6)
print(my_data)
# not possible
# error
# del my_data[2]
# deleting entire tuple is possible
del my_data
# not possible
# error
# because my_data is deleted
# print(my_data)
Output:
(1, 2, 3, 4, 5, 6)
4.3 Slicing operation in tuples
my_data = (11, 22, 33, 44, 55, 66, 77, 88, 99)
print(my_data)
# elements from 3rd to 5th
# prints (33, 44, 55)
print(my_data[2:5])
# elements from start to 4th
# prints (11, 22, 33, 44)
print(my_data[:4])
# elements from 5th to end
# prints (55, 66, 77, 88, 99)
print(my_data[4:])
# elements from 5th to second last
# prints (55, 66, 77, 88)
print(my_data[4:-1])
# displaying entire tuple
print(my_data[:])
Output:
(11, 22, 33, 44, 55, 66, 77, 88, 99)
(33, 44, 55)
(11, 22, 33, 44)
(55, 66, 77, 88, 99)
(55, 66, 77, 88)
(11, 22, 33, 44, 55, 66, 77, 88, 99)
4.4 Membership Test in Tuples
in: Checks whether an element exists in the specified tuple.
not in: Checks whether an element does not exist in the specified tuple.
my_data = (11, 22, 33, 44, 55, 66, 77, 88, 99)
print(my_data)
# true
print(22 in my_data)
# false
print(2 in my_data)
# false
print(88 not in my_data)
# true
print(101 not in my_data)
Output:
(11, 22, 33, 44, 55, 66, 77, 88, 99)
True
False
False
True
4.5 Iterating a tuple
# tuple of fruits
my_tuple = ("Apple", "Orange", "Grapes", "Banana")
# iterating over tuple elements
for fruit in my_tuple:
 print(fruit)
Output:
Apple
Orange
Grapes
Banana&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>what is list in python programming and what can we use it for ?</title>
      <dc:creator>OLADEJOBI7</dc:creator>
      <pubDate>Sat, 05 Mar 2022 13:15:07 +0000</pubDate>
      <link>https://dev.to/oladejobi7/what-is-list-in-python-programming-and-what-can-we-use-it-for--558c</link>
      <guid>https://dev.to/oladejobi7/what-is-list-in-python-programming-and-what-can-we-use-it-for--558c</guid>
      <description>&lt;p&gt;A list is a data type that allows you to store various types data in it. List is a compound data type which means you can have different-2 data types under a list, for example we can have integer, float and string items in a same list.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a List in Python
Lets see how to create a list in Python. To create a list all you have to do is to place the items inside a square bracket [] separated by comma ,.
# list of floats
num_list = [11.22, 9.9, 78.34, 12.0]
# list of int, float and strings
mix_list = [1.13, 2, 5, "beginnersbook", 100, "hi"]
# an empty list
nodata_list = []
As we have seen above, a list can have data items of same type or different types. This is the reason list comes under compound data type.&lt;/li&gt;
&lt;li&gt;Accessing the items of a list
Syntax to access the list items:
list_name[index]
Example:
# a list of numbers
numbers = [11, 22, 33, 100, 200, 300]
# prints 11
print(numbers[0])
# prints 300
print(numbers[5])
# prints 22
print(numbers[1])
Output:
11
300
22
Points to Note:&lt;/li&gt;
&lt;li&gt;The index cannot be a float number.
For example:
# a list of numbers
numbers = [11, 22, 33, 100, 200, 300]
# error
print(numbers[1.0])
Output:
TypeError: list indices must be integers or slices, not float&lt;/li&gt;
&lt;li&gt;The index must be in range to avoid IndexError. The range of the index of a list having 10 elements is 0 to 9, if we go beyond 9 then we will get IndexError. However if we go below 0 then it would not cause issue in certain cases, we will discuss that in our next section.
For example:
# a list of numbers
numbers = [11, 22, 33, 100, 200, 300]
# error
print(numbers[6])
Output:
IndexError: list index out of range&lt;/li&gt;
&lt;li&gt;Negative Index to access the list items from the end
Unlike other programming languages where negative index may cause issue, Python allows you to use negative indexes. The idea behind this to allow you to access the list elements starting from the end. For example an index of -1 would access the last element of the list, -2 second last, -3 third last and so on.
3.1 Example of Negative indexes in Python
# a list of strings
my_list = ["hello", "world", "hi", "bye"]
# prints "bye"
print(my_list[-1])
# prints "world"
print(my_list[-3])
# prints "hello"
print(my_list[-4])
Output:
bye
world
hello&lt;/li&gt;
&lt;li&gt;How to get a sublist in Python using slicing
We can get a sublist from a list in Python using slicing operation. Lets say we have a list n_list having 10 elements, then we can slice this list using colon : operator. Lets take an example to understand this:
4.1 Slicing example
# list of numbers
n_list = [1, 2, 3, 4, 5, 6, 7]
# list items from 2nd to 3rd
print(n_list[1:3])
# list items from beginning to 3rd
print(n_list[:3])
# list items from 4th to end of list
print(n_list[3:])
# Whole list
print(n_list[:])
Output:
[2, 3]
[1, 2, 3]
[4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7]&lt;/li&gt;
&lt;li&gt;List Operations
There are various operations that we can perform on Lists.
5.1 Addition
There are several ways you can add elements to a list.
# list of numbers
n_list = [1, 2, 3, 4]
# 1. adding item at the desired location
# adding element 100 at the fourth location
n_list.insert(3, 100)
# list: [1, 2, 3, 100, 4]
print(n_list)
# 2. adding element at the end of the list
n_list.append(99)
# list: [1, 2, 3, 100, 4, 99]
print(n_list)
# 3. adding several elements at the end of list
# the following statement can also be written like this:
# n_list + [11, 22]
n_list.extend([11, 22])
# list: [1, 2, 3, 100, 4, 99, 11, 22]
print(n_list)
Output:
[1, 2, 3, 100, 4]
[1, 2, 3, 100, 4, 99]
[1, 2, 3, 100, 4, 99, 11, 22]
5.2 Update elements
We can change the values of elements in a List. Lets take an example to understand this:
# list of numbers
n_list = [1, 2, 3, 4]
# Changing the value of 3rd item
n_list[2] = 100
# list: [1, 2, 100, 4]
print(n_list)
# Changing the values of 2nd to fourth items
n_list[1:4] = [11, 22, 33]
# list: [1, 11, 22, 33]
print(n_list)
Output:
[1, 2, 100, 4]
[1, 11, 22, 33]
5.3 Delete elements
# list of numbers
n_list = [1, 2, 3, 4, 5, 6]
# Deleting 2nd element
del n_list[1]
# list: [1, 3, 4, 5, 6]
print(n_list)
# Deleting elements from 3rd to 4th
del n_list[2:4]
# list: [1, 3, 6]
print(n_list)
# Deleting the whole list
del n_list
Output:
[1, 3, 4, 5, 6]
[1, 3, 6]
5.4 Deleting elements using remove(), pop() and clear() methods
remove(item): Removes specified item from list.
pop(index): Removes the element from the given index.
pop(): Removes the last element.
clear(): Removes all the elements from the list.
# list of chars
ch_list = ['A', 'F', 'B', 'Z', 'O', 'L']
# Deleting the element with value 'B'
ch_list.remove('B')
# list: ['A', 'F', 'Z', 'O', 'L']
print(ch_list)
# Deleting 2nd element
ch_list.pop(1)
# list: ['A', 'Z', 'O', 'L']
print(ch_list)
# Deleting all the elements
ch_list.clear()
# list: []
print(ch_list)
Output:
['A', 'F', 'Z', 'O', 'L']
['A', 'Z', 'O', 'L']
[]&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
  </channel>
</rss>
