0% found this document useful (0 votes)
2 views

Data Types and Operators

The document covers various data types in Python, including numeric, boolean, set, mapping, sequence, string, list, tuple, and their characteristics. It also explains mutable and immutable data types, typecasting, and error handling in Python programming. Key concepts such as implicit and explicit type conversion, as well as different types of errors like syntax, logical, linking, and runtime errors are discussed.

Uploaded by

vedanttawde2007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Data Types and Operators

The document covers various data types in Python, including numeric, boolean, set, mapping, sequence, string, list, tuple, and their characteristics. It also explains mutable and immutable data types, typecasting, and error handling in Python programming. Key concepts such as implicit and explicit type conversion, as well as different types of errors like syntax, logical, linking, and runtime errors are discussed.

Uploaded by

vedanttawde2007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

CH NO 7:

DATA TYPES AND OPERATORS


GRADE 11
INDRAJA ALAND
DATA TYPES
NUMERIC DATA TYPE
int float complex
Numbers can Numbers can A pair of real
be either be integer or and imaginary
Numeric positive or fraction. numbers.
negative. Eg: Eg:
int float
Eg: >>> a= 1.8 >>> a=x+yj
complex
>>>a=21 >>> b= -2.1 x: real
>>>b=-34 y: complex
j: imaginary
>>> a=2+2j
BOOLEAN DATA TYPE
 A boolean data type in python is used to represent one of the two boolean
values i.e. True or false

EG:
>>> A=15>2
>>> PRINT(A)
OUTPUT: TRUE

a=bool(input(“Are u Indian? True or False:"))


if a==true:
print("namaste")
SET DATA TYPE
 A set is an unordered collection of unique elements.
 Syntax: <set name>=(‘item1’,’item2’,……..)
 Set cannot contain duplicate values.
 Even if they have duplicate values, it does not give an error, but removes
duplicate values/items from output.
 Eg:
s={1,3,6,5,6,"apple",4,5,3,4}
print(s)
OUTPUT:
{1, 3, 4, 5, 6, 'APPLE'}
SPECIAL LITERALS-NONE
 Python has one special literal, which is “none”.
 It indicates absence of value/ no value.
 Not same as 0 or false or empty.
 It is also known as null.
EG:
>>> BALANCE=NONE
>>>PRINT(BALANCE)
OUTPUT: NONE

>>> 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’,…….}

 Properties of keys in dictionary data type:


Name of key is case sensitive
Duplicate entry of key is not allowed
The values can be of any data types.
MAPPING
 Eg:
D={'NAME':"Madhav",'Roll no ':'12'}
print(D)

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.

String Data Type:


 It is collection of one or more characters enclosed in a single quotation (‘ ’) double quotation(“
”) or triple quotation(“““ ””” )marks.
 It is represented by str class
 Each characters is stored in memory and is referred through indexing.
 This characters are UNICODE characters.
STRING DATA TYPE:
String in memory:
 In python characters are stored in memory in a sequence.
 You can refer to them using INDEXING.
 Index means its position in sequence.
STRING DATA TYPE:
Accessing elements of String:

The index value associated with element is used.


Eg: S=“PBIS”
S=“Grade 11!”
s=“hello world” print(S[-1]) print(S[0])
Output: Output:
print(s[4]) P
!
OUTPUT:
O
LIST DATA TYPE:
• A list contains items of different data types.
• The items stored in a list are separated by comma(,) and are enclosed using
square brackets[ ].
• A list stores elements in a sequence one after another.
• Syntax: <list name>= [‘item1’,’item2’,…..]
• Indexing can be used for a list.
• Eg: s=["radha",12,'XI']
print(s[2])
s=["radha",12,'XI']
OUTPUT:
print(s) XI
OUTPUT:
TUPLE DATA TYPE:
• A tuple is also an ordered data type like a list.
• It can also contain items of different data types.
• The items stored in a list are separated by comma(,) and are enclosed using
parentheses ( ).
• Only difference between list and tuple is:
TUPLE IS READ ONLY DATA STRUCTURE.
CANNOT MODIFY SIZE & VALUE OF ITEMS OF A TUPLE.
• Syntax: <tuple name>= (‘item1’,’item2’,…..)
• Eg:
S=(“radha”,18,‘XI’)
type( ) and isinstance( ) Function
• The type ( ) function is used to know variable or a value
belongs to which data types.
• The isinstance( ) function is used to check whether an
object belongs to a particular class.
• Example:
a=10
a=10 print(a,isinstance(10,complex))
print(type(a)) 10 False
<class 'int'> a=10
print(a,isinstance(10,int))
10 True
MUTABLE & IMMUTABLE DATA TYPES:
Mutable Data Types:
• A mutable object can be modified after it is created.
• Eg:
a=['a','b','c','d'] OUTPUT:
print(a) ['a', 'b', 'c', 'd']
print(id(a)) 2792507041024
a[2]=4 ['a', 'b', 4, 'd']
print(a)
2792507041024
print(id(a))
MUTABLE & IMMUTABLE DATA TYPES:
Immutable Data Types:
• An immutable object can never be modified after it is
created.
• Eg: OUTPUT:
a=6 140707521946568
print(id(a)) 140707521946536
a=5
print(id(a))
MUTABLE & IMMUTABLE DATA TYPES:
Immutable Data Types:
Eg:
OUTPUT:
a="Hello world"
TypeError: 'str' object does not
a[2]='a'
support item assignment
print(a)
Immutable Data Type Mutable Data Type
int, float, complex List
String Set
Tuple Dictionary
TYPECASTING IN PYTHON
• The process of converting the value of one data type to another is
called as TYPE CONVERSION.
• For example: converting int data to str.
• In typecasting, loss of data may occur because python interpreter is
enforced to change the data.

• Two types of data type conversion are:


a)Implicit Type Conversion
b)Explicit Type Conversion
IMPLICIT TYPE CONVERSION
• Python automatically converts one data type to another.
• Eg:
a= 123
b = 1.23
c= a + b
print("Value:",c)
print("Data Type:",type(c))

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.

• Python deals with this errors by raising an EXCEPTION.


• An exception is an unforeseen and unexpected situation.
• It occurs during execution of a program on which the compiler has no control.

You might also like