Python
/
Language
- 1 Language 9
-
Hello World S
-
Variables S
-
Functions S
-
Conditional A S
-
Operators S
-
While S
-
Turtle S
-
Script Mode S
-
Debugging S
- 2 Strings 7
-
Slice S
-
Raw Strings S
-
Regex A S
-
Validation S
-
Config S
-
Security S
-
Encrypt A S
- 3 Collections 6
-
Lists S
-
Dictionaries S
-
Efficiency S
-
Tree S
-
Tuples S
-
References S
- 4 Functions 5
-
Recursion S
-
Factorial S
-
Modulus S
-
Reassignment S
-
Approximate S
- 5 Storage 8
-
Files S
-
Databases S
-
Pipes S
-
With open S
-
Shelve A S
-
Zip S
-
Csv S
-
Json S
- 6 Class 4
-
Definition S
-
Attributes S
-
Functional S
-
Methods S
- 7 Goodies 5
-
Conditional Expression S
-
List Comprehension A S
-
Generator S
-
Named Tuple S
-
Modules S
- 8 Applications 5
-
Pythagora A S
-
Palindrome A S
-
Binary Search A S
-
Conway Game A S
-
Coin Flip A S
- 9 Scheduler 4
-
Time S
-
Multithreading A S
-
Subprocess S
-
Logging S
- 10 Packages 6
-
Clipboard A S
-
Ocr A S
-
Socket S
-
Image S
-
Virtualenv S
-
Jupyter S
S
R
Q
Python Language Debugging
Use try/except statements to catch errors try: print(c) except Exception as e: print(e) # c not definedExceptions
Syntax
1 p41 Syntax error refers to the structure and the rules.
>>> (1 + 2) # correct
3
>>> 2) # incorrect
# SyntaxError: invalid syntax
Runtime
Runtime errors does not appear until after the program has started (also called exceptions).
>>> first = "Hello"
>>> second = "World"
>>> first + " " + secoend
# NameError: name 'secoend' is not defined
Semantic
Semantic errors will not generate errors but it will not do the right thing.
# Semantic error
#
# The program performs concatenation instead of addition
# The programmer failed to convert the inputs to integers
num1 = input('Enter number 1: ')
num2 = input('Enter number 2: ')
sum = num1 + num2
if sum != int(num1) + int(num2):
print("Sum = " + sum + " / Incorrect - Semantic error")
num1 = input('Enter number 1: ')
num2 = input('Enter number 2: ')
sum = int(num1) + int(num2)
print("Sum = " + str(sum) + " / Correct")
Except
Use try/except statements to catch errors.
# Errors can be handled with ...
# try and except statements.
def calc(number, divider):
try:
return number/divider
except ZeroDivisionError:
print('Error: Division by zero')
except:
print('Error: Invalid argument')
assert calc(10, 2) == 5
assert calc(10, 0) == None # pass
# Error: Division by zero
➥ Questions