Data Types, Errors Flashcards
1
Q
Integer Data Type
A
- Referred to as ‘int’, holds integer values.
- Integers are numbers without a fractional or decimal part.
- ex.) age = 25
2
Q
Float Data Type
A
- ‘float’
- Real numbers with decimal point.
- ex.) PI = 3.14159
3
Q
String Data Type
A
- ‘str’
- Enclosed in either (‘ ‘) or (“ “) quotation marks.
- ex.) name = ‘Alice’
4
Q
How to check the type of variable
A
type(variable_name)
5
Q
What are the types of errors?
A
Syntax, runtime, logic.
6
Q
Syntax Errors
A
- Syntax refers to the structure of a program and the rules about that structure.
- Definition: Syntax errors are errors in your code that the computer can’t interpret In python these errors include:
- Spelling errors
- The omission of important characters (eg. missing colon, bracket, mismatched quotes)
- Wrong indentation
- When does a syntax error occur?:
Occurs during the compilation or interpretation phase of the program, which analyzes the structure of the code. - Effect:
Program cannot be executed until the syntax error is fixed.
7
Q
Runtime Errors
A
- Definition: Errors that are not detected until run time. Occur when a statement is executed, and something unexpected or invalid happens during runtime.
Often caused by:
- Invalid inputs
- Division by zero
- Can’t find some data because it doesn’t exist. eg.) IndexError: out of range index.
- Mismatched data types: performing operations between incompatible data types can result in a ‘TypeError’. ex.) result = “5” + 2 # TypeError: can only concatenate str (not “int”) to str
Effect:
- Program may terminate
- May produce unexpected or undesirable behavior.
- Program may crash
8
Q
Logic Errors
A
- Definition: Do not produce error messages, instead they do not do what you intended the code to do.
- Produces incorrect results, like miscalculations, wrong decisions, unpredictable behavior.
Causes:
- Incorrect conditional statements, etc
9
Q
Type Conversion (Casting)
A
10
Q
A