ERROR HANDLING & LISTS
from PadhAI Class
padhai_error_handling

Exception Handling

In [ ]:
def input_single_float(prompt):
    user_input = input(prompt)
    output = float(user_input)
    return output
In [ ]:
input_single_float('Enter the value of PI')
Enter the value of PI12
Out[ ]:
12.0
In [ ]:
def input_single_float_01(prompt):
    user_input = input(prompt)
    try:
        output = float(user_input)
    except ValueError:
        print("Input value is not as expected - enter numeric value")
        output = None
    except UnboundLocalError:
        output = None
    return output
In [ ]:
input_single_float_01("Enter any number")
Enter any number12
Out[ ]:
12.0

Lists

  1. List is a compound datatype, meaning, it can contain any datatype in the same contaner.
  2. Lists are indicated by [ ] square braces
  3. to access individual items, we use the index of the item, index starts from 0
  4. to get group of items we use : as seperator eg: word[:]
  5. is inclusive and is exclusive.
  6. to get the last item, use index as -1
  7. like wise, to get items in reverse order, use -2, -3 etc...
  8. expressions can be used with lists. for example, lst[1] + 10 returns item in 2nd index + 10
  9. len() is used to get the length of the list
  10. Lists are mutable, meaning, they can be changed, appended, deleted, added, inserted and the others
  11. Individual items in the list of lists need not be the same, ie., list(list[1]) need not be the same as list(list[2])
  12. if there exists two lists A and B, and if B is assigned to A like B = A, then, changing element in list A will also be changed in list B, because, both the lists A and B point to the same data - if needed to keep A and B to be mutually independent, then copy list A to B
In [ ]:
course = "Foundations of Data Science"
In [ ]:
words = course.split()
In [ ]:
print(words)
# content within a square bracket is a list type
['Foundations', 'of', 'Data', 'Science']
In [ ]:
words[1]
Out[ ]:
'of'
In [ ]:
words[0]
Out[ ]:
'Foundations'
In [ ]:
words[1:3]
Out[ ]:
['of', 'Data']
In [ ]:
words[2:5]
Out[ ]:
['Data', 'Science']
In [ ]:
words[3:34]
Out[ ]:
['Science']
In [ ]:
print(type(words))
<class 'list'>
In [ ]:
print(words[0])
Foundations
In [ ]:
print(words[-1])
Science
In [ ]:
print(words[-2])
Data

List with integers - Fibonacci Series

In [ ]:
fibo_list = [1,1,2,3,5,8,13,21,34]
In [ ]:
print(type(fibo_list))
<class 'list'>
In [ ]:
print(fibo_list[2])
2
In [ ]:
print(fibo_list[2] + 10)
12
In [ ]:
print(fibo_list[2:4])
[2, 3]
In [ ]:
print(type(fibo_list[3:6]))
<class 'list'>
In [ ]:
print(fibo_list[3:6])
[3, 5, 8]

printing list of items in the list - fibo_list

In [ ]:
for i in fibo_list:
    print(i)
1
1
2
3
5
8
13
21
34
In [ ]:
print(len(fibo_list))
9
In [ ]:
length = len(fibo_list)
for i in range(length):
    print(i)
0
1
2
3
4
5
6
7
8
In [ ]:
for i in range(length):
    print(i + 1.52)
1.52
2.52
3.52
4.52
5.52
6.52
7.52
8.52
9.52

List with multiple data types

In [ ]:
bm_record = ['Abel', 12, 23.34, False]
In [ ]:
print(type(bm_record))
<class 'list'>
In [ ]:
print(bm_record[2])
23.34
In [ ]:
print(bm_record[1])
12
In [ ]:
print(bm_record[1:3])
[12, 23.34]

working with lists

In [ ]:
bm_record.append('abel@idontknow.com')
In [ ]:
bm_record
Out[ ]:
['Abel', 12, 23.34, False, 'abel@idontknow.com']
In [ ]:
print(bm_record)
['Abel', 12, 23.34, False, 'abel@idontknow.com']
In [ ]:
for i in bm_record:
    print(i, type(i))
Abel <class 'str'>
12 <class 'int'>
23.34 <class 'float'>
False <class 'bool'>
abel@idontknow.com <class 'str'>
In [ ]:
bm_record[0] = "Abel Ashwin"
In [ ]:
for i in bm_record:
    print(i)
Abel Ashwin
12
23.34
False
abel@idontknow.com

count returns the number of times, the given item is repeated in the list

In [ ]:
bm_record.count("Abel Ashwin")
Out[ ]:
1
In [ ]:
bm_record.extend?
In [ ]:
cm_record = ['Sameer', 25.254, 12.23]

with APPEND, new list becomes another list in the existing list, ie., list within a list

In [ ]:
bm_record.append(cm_record)
In [ ]:
bm_record
Out[ ]:
['Abel Ashwin',
 12,
 23.34,
 False,
 'abel@idontknow.com',
 ['Sameer', 25.254, 12.23]]
In [ ]:
print(bm_record)
['Abel Ashwin', 12, 23.34, False, 'abel@idontknow.com', ['Sameer', 25.254, 12.23]]

with EXTEND, the new list contents are added to the existing list

In [ ]:
bm_record.extend(cm_record)
In [ ]:
print(bm_record)
['Abel Ashwin', 12, 23.34, False, 'abel@idontknow.com', ['Sameer', 25.254, 12.23], 'Sameer', 25.254, 12.23]
In [ ]:
bm_record = ['Abel', 12, 23.34, False]
In [ ]:
print(bm_record.reverse())
None
In [ ]:
print(bm_record)
[False, 23.34, 12, 'Abel']
In [ ]:
print(fibo_list.reverse())
None
In [ ]:
fibo_list.reverse()
In [ ]:
print(fibo_list)
[1, 1, 2, 3, 5, 8, 13, 21, 34]
In [ ]:
fibo_list.sort()
In [ ]:
print(fibo_list.sort())
None
In [ ]:
fibo_list.remove?
# Remove first occurrence of value.
#Raises ValueError if the value is not present.
In [ ]:
fibo_list.pop?
# Remove and return item at index (default last).
# Raises IndexError if list is empty or index is out of range.

Lists Continued

In [ ]:
bm_record = [
             ['Abel', 75, 1.73, False],
             ['Ashwin', 120, 1.78, True]
]
In [ ]:
print(type(bm_record))
<class 'list'>
In [ ]:
bm_record
Out[ ]:
[['Abel', 75, 1.73, False], ['Ashwin', 120, 1.78, True]]
In [ ]:
bm_record[0]
Out[ ]:
['Abel', 75, 1.73, False]
In [ ]:
bm_record[1]
Out[ ]:
['Ashwin', 120, 1.78, True]
In [ ]:
bm_record[0][1]
Out[ ]:
75
In [ ]:
bm_record[1][2]
Out[ ]:
1.78
In [ ]:
bm_record[0][0] = "Abel brother of Cain"
In [ ]:
bm_record[0]
Out[ ]:
['Abel brother of Cain', 75, 1.73, False]
In [ ]:
bm_record[0].append("abel@idontknow.com")
In [ ]:
bm_record[0]
Out[ ]:
['Abel brother of Cain', 75, 1.73, False, 'abel@idontknow.com']
In [ ]:
bm_record
Out[ ]:
[['Abel brother of Cain', 75, 1.73, False, 'abel@idontknow.com'],
 ['Ashwin', 120, 1.78, True]]
In [ ]:
bm_record[1][1] = 125
In [ ]:
bm_record
Out[ ]:
[['Abel brother of Cain', 75, 1.73, False, 'abel@idontknow.com'],
 ['Ashwin', 125, 1.78, True]]
In [ ]:
identity_3 = [
              [1,0,0],
              [0,1,0],
              [0,0,1]
]
In [ ]:
identity_3[1][2]
Out[ ]:
0
In [ ]:
identity_3[1][1]
Out[ ]:
1
In [ ]:
identity_3[1, 0]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-83-11a278085da4> in <module>()
----> 1 identity_3[1, 0]

TypeError: list indices must be integers or slices, not tuple
In [ ]:
A = [1, 2]
In [ ]:
B = A
In [ ]:
A[0] = A[0] + A[1]
In [ ]:
print(A)
[3, 2]
In [ ]:
print(B)
[3, 2]
In [ ]:
A = [1, 2]

Copy contents of list A to list B so that when assigned B to A, the values doesnt change

In [ ]:
B = [item for item in A]
In [ ]:
print(B)
[1, 2]
In [ ]:
A[0] = A[0] + A[1]
In [ ]:
print(A)
[3, 2]
In [ ]:
print(B)
[1, 2]
In [ ]:
A = [
     [1, 1],
     [1, 0]
]
In [1]:
!pip install nbconvert
Requirement already satisfied: nbconvert in /usr/local/lib/python3.7/dist-packages (5.6.1)
Requirement already satisfied: traitlets>=4.2 in /usr/local/lib/python3.7/dist-packages (from nbconvert) (5.1.1)
Requirement already satisfied: nbformat>=4.4 in /usr/local/lib/python3.7/dist-packages (from nbconvert) (5.1.3)
Requirement already satisfied: mistune<2,>=0.8.1 in /usr/local/lib/python3.7/dist-packages (from nbconvert) (0.8.4)
Requirement already satisfied: entrypoints>=0.2.2 in /usr/local/lib/python3.7/dist-packages (from nbconvert) (0.4)
Requirement already satisfied: bleach in /usr/local/lib/python3.7/dist-packages (from nbconvert) (4.1.0)
Requirement already satisfied: pandocfilters>=1.4.1 in /usr/local/lib/python3.7/dist-packages (from nbconvert) (1.5.0)
Requirement already satisfied: testpath in /usr/local/lib/python3.7/dist-packages (from nbconvert) (0.5.0)
Requirement already satisfied: defusedxml in /usr/local/lib/python3.7/dist-packages (from nbconvert) (0.7.1)
Requirement already satisfied: pygments in /usr/local/lib/python3.7/dist-packages (from nbconvert) (2.6.1)
Requirement already satisfied: jupyter-core in /usr/local/lib/python3.7/dist-packages (from nbconvert) (4.9.1)
Requirement already satisfied: jinja2>=2.4 in /usr/local/lib/python3.7/dist-packages (from nbconvert) (2.11.3)
Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python3.7/dist-packages (from jinja2>=2.4->nbconvert) (2.0.1)
Requirement already satisfied: jsonschema!=2.5.0,>=2.4 in /usr/local/lib/python3.7/dist-packages (from nbformat>=4.4->nbconvert) (4.3.3)
Requirement already satisfied: ipython-genutils in /usr/local/lib/python3.7/dist-packages (from nbformat>=4.4->nbconvert) (0.2.0)
Requirement already satisfied: attrs>=17.4.0 in /usr/local/lib/python3.7/dist-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.4->nbconvert) (21.4.0)
Requirement already satisfied: importlib-metadata in /usr/local/lib/python3.7/dist-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.4->nbconvert) (4.11.0)
Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.4->nbconvert) (3.10.0.2)
Requirement already satisfied: importlib-resources>=1.4.0 in /usr/local/lib/python3.7/dist-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.4->nbconvert) (5.4.0)
Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in /usr/local/lib/python3.7/dist-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.4->nbconvert) (0.18.1)
Requirement already satisfied: zipp>=3.1.0 in /usr/local/lib/python3.7/dist-packages (from importlib-resources>=1.4.0->jsonschema!=2.5.0,>=2.4->nbformat>=4.4->nbconvert) (3.7.0)
Requirement already satisfied: webencodings in /usr/local/lib/python3.7/dist-packages (from bleach->nbconvert) (0.5.1)
Requirement already satisfied: six>=1.9.0 in /usr/local/lib/python3.7/dist-packages (from bleach->nbconvert) (1.15.0)
Requirement already satisfied: packaging in /usr/local/lib/python3.7/dist-packages (from bleach->nbconvert) (21.3)
Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /usr/local/lib/python3.7/dist-packages (from packaging->bleach->nbconvert) (3.0.7)
In [ ]:
%shell jupyter nbconvert --to html /content/testfile.ipynb
In [ ]:
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ END CELL ~~~~~~~~~~~~~~~~~~~~~~~~~~~~