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…
Syntax and logical errors
Two types of errors can occur in Python:
1. Syntax errors – usually the easiest to spot, syntax errors occur when you make a typo. Not ending an if statement with the colon is an example of an syntax error, as is misspelling a Python keyword (e.g. using whille instead of while). Syntax error usually appear at compile time and are reported by the interpreter.
Here is an example of a syntax error:
x = int(input('Enter a number: ')) whille x%2 == 0: print('You have entered an even number.') else: print('You have entered an odd number.')
Notice that the keyword whille is misspelled. If we try to run the program, we will get the following error:
C:Python34Scripts>python error.py File "error.py", line 3 whille x%2 == 0: ^ SyntaxError: invalid syntax
2. Logical errors – also called semantic errors, logical errors cause the program to behave incorrectly, but they do not usually crash the program. Unlike a program with syntax errors, a program with logic errors can be run, but it does not operate as intended.
Consider the following example of an logical error:
x = float(input('Enter a number: ')) y = float(input('Enter a number: ')) z = x+y/2 print('The average of the two numbers you have entered is:',z)
The example above should calculate the average of the two numbers the user enters. But, because of the order of operations in arithmetic (the division is evaluated before addition) the program will not give the correct answer:
>>> Enter a number: 3 Enter a number: 4 The average of the two numbers you have entered is: 5.0 >>>
To rectify this problem, we will simply add the parentheses: z = (x+y)/2
Now we will get the correct result:
>>> Enter a number: 3 Enter a number: 4 The average of the two numbers you have entered is: 3.5 >>>
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