CARVIEW |
- CCNA online course
- Linux online course
- VMware ESXi online course
- Nmap online course
- MySQL online course
- Raspberry Pi online course
- Apache HTTP Server course
- VMware Player online course
- Splunk online course
- SQL online course
- Oracle VirtualBox online course
- Python online course
- Asterisk course
- VMware Workstation Player course
- Process Explorer course
- Pillow online course
- Create a web crawler in Python
- A short introduction to…
Types of errors
Errors in Python can be categorized into two types:
1. Compile time errors – errors that occur when you ask Python to run the application. Before the program can be run, the source code must be compiled into the machine code. If the conversion can not perfomed, Python will inform you that your application can not be run before the error is fixed. The most common errors of this type are syntax errors – for example, if you don’t end an if statement with the colon. Here is an example:
x = int(input('Enter a number: ')) if x%2 == 0 print('You have entered an even number.') else: print('You have entered an odd number.')
The code above checks if the number the user enters is an odd or an even number. However, notice how the if statement is missing the colon (:) at the end of the line. Because of it, the program won’t run and the interpreter will even inform use what the problem is:
C:\Python34\Scripts>python error.py File "error.py", line 3 if x%2 == 0 ^ SyntaxError: invalid syntax
2. Runtime errors – errors that occur after the code has been compiled and the program is running. The error of this type will cause your program to behave unexpectedly or even crash. An example of an runtime error is the division by zero. Consider the following example:
x = float(input('Enter a number: ')) y = float(input('Enter a number: ')) z = x/y print(x,'divided by',y,'equals: ',z)
The program above runs fine until the user enters 0 as the second number:
>>> Enter a number: 9 Enter a number: 2 9.0 divided by 2.0 equals: 4.5 >>> Enter a number: 11 Enter a number: 3 11.0 divided by 3.0 equals: 3.6666666666666665 >>> Enter a number: 5 Enter a number: 0 Traceback (most recent call last): File "C:/Python34/Scripts/error1.py", line 3, in <module> z = x/y ZeroDivisionError: float division by zero >>>
Python course
- Introduction
- Python overview
- Install Python on Windows
- Install Python on Linux
- Add Python to the Windows Path
- Run Python code
- Interactive prompt
- IDLE editor
- Command line
- Help mode
- Basic programs
- Write your first program
- Use comments
- What are variables?
- Variable data types
- Variable names
- Numeric variables
- Strings
- Get the current date and time
- Operators overview
- Arithmetic operators
- Comparison operators
- Logical operators
- Assignment operators
- Membership operators
- Identity operators
- Conditional statements
- The if statement
- Get user input
- The if...else statement
- The if...elif statement
- Nested if statements
- Use logical operators
- Loops
- The for loop
- Use for loop with the range() function
- The break statement
- The continue statement
- The pass statement
- Use else statement in loops
- The while loop
- Nested loop statements
- Errors
- Types of errors
- Syntax and logical errors
- The try...except statements
- The try...except...else statements
- The try...except...finally statements
- Catch specific exceptions
- Raise exception
- Nest exception handling statements
- Modules
- What are modules?
- Import modules
- Find files on disk
- Display module content
- Strings
- What are strings?
- Escape characters
- Access individual characters
- String functions
- Search strings
- Concatenating strings
- Lists, sets, tuples, dictionaries
- What are lists?
- Modify lists
- Loop through a list
- Check whether a value is in a list
- Sorting lists temporarily
- Sorting lists permanently
- Obtaining the list length
- What are sets?
- What are dictionaries?
- Add new key-value pair to a dictionary
- Modify a value in a dictionary
- Delete a key-value pair in a dictionary
- Loop through a dictionary
- What are tuples?
- Looping over a tuple
- Working with files
- How to read and write files
- Read a file
- Read and write – with statement
- Make a list of lines from a file
- Functions
- What are functions?
- Return statement
- Positional arguments
- Keyword arguments
- Default values for parameters
- Flexible number of arguments
- Variable scopes