-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors_exceptions.py
78 lines (45 loc) · 1.58 KB
/
errors_exceptions.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
# coding: utf-8
# # Syntax Errors
# ### It occurs when python can't interpret our code, since we didn't follow the current syntax for python. These are errors you're likely to get when you make a typo, or you're first starting to learn Python.
# # Exceptions
# ### It occurs when unexpected things happen during execution of a program, even if the code is syntactically correct. There are different types of built-in exceptions in python, and you can see which exception is thrown in the error message.
# # ValueError Exception
# ### It occurs when a built-in operation or function is given in argument with the correct type but an inappropriate value.
# In[1]:
x = int(input("Enter a Number: "))
x += 20
print(x)
# # NameError Exception
# ### It occurs when it can't find the value for this variable when it tries to run it.
# In[3]:
print(nonexistent_variable)
# # Assertion Error
# ### An assert statement fails.
# In[2]:
# AssertionError with error_message.
x = 1
y = 0
assert y != 0, "Invalid Operation" # denominator can't be 0
print(x / y)
# # Index Error
# ### A sequence subscript is out of range.
# In[4]:
j = [1, 2, 4]
print(j[4])
# # Key Error
# ### A key can't be found in a dictionary.
# In[6]:
# Creating a Dictionary
subjects = {'Sree': 'Maths',
'Ram': 'Biology',
'Shyam': 'Science',
'Abdul': 'Hindi'}
# Printing the subject of Fharan
print(subjects['Fharan'])
# # Type Error
# ### An object of an unsupported type is passed as input to an operation or function.
# In[18]:
string = "dams"
num = 4
print(string + num + string)