Python — Common Errors and Methods :
Print() :
prints the result of expression to the console.
Eg : print(5+3) prints 8 to the console.
Type() :
Returns the data type of the variable/output .
Eg : print(type(9)) prints <class ‘int’> to the screen.
Note : We can convert float to int or int to float or to any datatype.
print(int(3.9)) returns 3
print(int(3.2)) returns 3
In the above example, we should notice that it will not round the value, it cuts the decimal part.
Whitespaces in Statements doesn’t matter Eg : print( 4 + 5 ) is same as print(4+5). 79 -99 characters has been set as a limit to increase readability in a statement.
Common Errors :
1)while True
print(“Hello World”)
This raises a Syntax Error because while statement should end in ‘:’ .
2) ZeroDivisionError :
print(5/0) — Whenever we try to divide a number by zero this type of error raises.
3) NameError :
print(4+spam) — This raises a NameError because the variable ‘spam’ is not defined.
4) TypeError :
print(5+ ‘5’) — It cannot convert ‘int’ to str implicitly.