Data Types and Operators
Data Types and Operators
EG:
>>> A=15>2
>>> PRINT(A)
OUTPUT: TRUE
>>> SALARY=NONE
>>>TYPE((SALARY)
OUTPUT: <CLASS ’NONE TYPE’>
MAPPING
Telephone Directory
A key is there to map a particular data item.
In python dictionary is a mapping data type, where key is mapped to its
corresponding value.
Values in dictionary can be of any data type and can be duplicated.
Keys of dictionary must be unique.
They (keys) are case sensitive.
Syntax: <dictionary name>={‘item1’,’item2’,…….}
Output:
{'Name': 'Madhav', 'Roll no': '12'}
d={1:"Monday",2:"Tuesday"}
print(d)
Output:
{1: 'Monday', 2: 'Tuesday'}
SEQUENCE DATA TYPE
• A sequence is an ordered collection of similar or different data elements.
OUTPUT:
Value: 124.23
Data Type: <class 'float'>
IMPLICIT TYPE CONVERSION
• We get TypeError, if we try to add str and int.
• For example, hi + 23.
• Eg:
a='hi'
b=123
c=a+b
OUTPUT:
TypeError: can only concatenate str (not "int") to str
• Python is not able to use Implicit Conversion in such conditions.
• Python has a solution for these types of situations which is known as EXPLICIT
CONVERSION.
EXPLICIT TYPE CONVERSION
• This type of conversion is done manually.
• It can be done by using int( ), float( ) and str( ) BUILT-IN FUNCTIONS.
• This type of conversion is also called typecasting because the user casts
(changes) the data type of the objects.
• Eg:
a= '12' OUTPUT:
b = 23 35
<class 'int'>
c = int(a)
sum = b + c
print("Sum:",sum)
print("Data type of num_sum:",type(sum))
ERRORS AND DEBUGGING
• An error in a program is an unexpected and unintended by the programmer.
• Error is also called as BUG, DEFECT , FAILURE or FAULT.
• Debugging: It is the process of locating and fixing error(s) in a computer
program code.
• Executing the instruction of the program is known as RUNNING.
• An error can occur during compile time or at run time.
• Type of errors are:
a) Syntax Error
b) Logical Error
c) Linking Error
d) Run Time Error
SYNTAX ERROR
• It occurs due to wrongly typed statements or grammatical mistakes in
programming language.
• It is detected at the time of compilation process.
• Eg:
pint(“HELLO”)
OUTPUT:
NameError: name 'pint' is not defined. Did you mean: 'print'?
print("HELLO)
OUTPUT:
SyntaxError: incomplete input
LOGICAL ERROR
• They are the most difficult to debug.
• It gives wrong or undesired output.
• Eg:
a=2
b=2
c=a+b
print("subtraction value is:",c)
OUTPUT:
subtraction value is: 4
LINKING ERROR
• This error occurs during linking process after compilation.
• Eg:
import math
a=2
a=4
b=3
b=3
c=math.sqrt(a)
c=math.sqrt(a)
print(c)
print(c)
OUTPUT: OUTPUT:
NameError: name 'math' is not defined 2
RUN TIME ERROR
• A runtime error in a program is one that occurs after the program has been
successfully compiled.
• The Python interpreter will run a program if it is free of syntax errors.
• However, if the program encounters a runtime error - a problem that was not
detected when the program was parsed and is only revealed when a specific line
is executed - it may exit unexpectedly during execution.
• When a program crashes due to a runtime error, we say it has crashed.
RUN TIME ERROR
• Some examples of Python Runtime errors
i. Division by zero
ii.Performing an operation on incompatible types
iii.Trying to access a file that doesn’t exist
iv.If there is square root of negative number.