7.data Handling Notes
7.data Handling Notes
Example:
x = 1.10
y = 1.0
z = -35.59
a = 35e3
b = 12E4
c = -87.7e100
complex: Complex numbers are written with a “j” as the imaginary part.
Example:
>>>x = 3+5j
>>>y = 2+4j
>>>z=x+y
>>>print(z)
5+9j
>>>z.real
5.0
>>>z.imag
9.0
Real and imaginary part of a number can be accessed through the attributes real and imag.
Python allows for either pairs of single or double quotes. Example: ‘hello’ is the same as “hello” .
Python does not have a character data type, a single character is simply a string with a length of 1.
The python string store Unicode characters.
Each character in a string has its own index.
String is immutable data type means it can never change its value in place
List
Tuple
Set
Dictionary
3.2 Mutable & Immutable Data Type: Ch 03 – Data Handling
1. Arithmetic Operators
2. Relational Operator
3. Logical Operators
4. Bitwise operators
5. Assignment Operators
6. Other Special Operators
o Identity Operators
o Membership operators
Example:
>>>x= -5
>>>x**2
>>> -25
ii. Relational Operators: Relational operators compare the values. It either returns True or False
according to the condition.
iii. Logical operators: Logical operators perform Logical AND, Logical OR and Logical NOT operations.
0 and 0
0
>>> 0 and 6
0
>>> ‘a’ and ‘n’
’n’
>>> 6>9 and ‘c’+9>5 # and operator will test the second operand only if the first operand
False # is true, otherwise ignores it, even if the second operand is wrongThe or
operator: The or operator works in two ways:
X Y X or Y
False False False
False True True
True False True
True True True
In an expression X or Y, if first operand has true value, then return first operand X as a
result, otherwise returns Y.
X Y X or Y
False False Y
False True Y
True False X
True True X
>>>0 or 0 0
>>>0 or 6 6
>>>‘a’ or ‘n’ ’a’
>>>6<9 or ‘c’+9>5
# or operator will test the second operand only if the first operand
True
True True
iv. Bitwise operators: Bitwise operators acts on bits and performs bit by bit operation
Examples:
Let
a = 10
b=4
print(a & b)
print(a | b)
print(~a)
print(a ^ b)
print(a >> 2)
print(a << 2)
Output:
0
14
-11
14
2
40
v. Assignment operators: Assignment operators are used to assign values to the variables.
vi. Other Special operators: There are some special type of operators like-
a. Identity operators– is and is not are the identity operators both are used to check if
two values are located on the same part of the memory. Two variables that are equal
does not imply that they are identical.
Example:
Let
a1 = 3
b1 = 3
a2 = ‘PythonProgramming’
b2 = ‘PythonProgramming’
a3 = [1,2,3] b3 = [1,2,3] print(a1 is not b1)
print(a2 is b2) # Output is False, since lists are mutable.
print(a3 is b3)
Output:
False
True
False
Example:
>>>str1= “Hello”
>>>str2=input(“Enter a String :”)
Enter a String : Hello
>>>str1==str2 # compares values of string
True
>>>str1 is str2 # checks if two address refer to the same memory address
False
b. Membership operator – in and not in are the membership operators; used to test whether a value or variable
is in a sequence.
Example:
Let
x = ‘Digital India’
y = {3:’a’,4:’b’}
print(‘D’ in x)
print(‘digital’ not in x)
print(‘Digital’ not in x)
print(3 in y)
print(‘b’ in y)
Output:
True
True
False
True
False
Operator Precedence: It describes the order in which operations are performed when an
expression is evaluated. Operators with higher precedence perform the operation first.
Operator Associativity: whenever two or more operators have the same precedence, then
associativity defines the order of operations.
Strings … continued
Lists
• List is a more general sequence object that
allows the individual items to be of different
types.
• Equivalent to arrays in other languages.
• Lists have no fixed size and can be expanded
or contracted as needed.
• Items in list can be retrieved using the index.
• Lists can be nested just like arrays, i.e., you
can have a list of lists.
Lists
• Simple list:
• Nested list:
Dictionaries
• Dictionaries are unordered mappings of ’Name
: Value’ associations.
• Comparable to hashes and associative arrays
in other languages.
• Intended to approximate how humans
remember associations.
Files
• File objects are built for interacting with files on the
system.
• Same object used for any file type.
• User has to interpret file content and maintain
integrity.
Tuples
• Tuples are immutable lists.
• Maintain integrity of data during program
execution.
Sets
• Special data type introduced since Python 2.4
onwards to support mathematical set theory
operations.
• Unordered collection of unique items.
• Set itself is mutable, BUT every item in the set
has to be an immutable type.
• So, sets can have numbers, strings and tuples
as items but cannot have lists or dictionaries as
items.
Expressions
• Python has following types of expression –
• In Python, input () function is used to take input which takes input in the
form of string. Then it will be type casted as per requirement. For ex- to
calculate volume of a cylinder, program will be as-
DEBUGGING PROGRAMS
11.1 INTRODUCTION:
Errors in a program are known as ‘bugs’.
To remove the errors from a program, known as debugging.
a. Syntax Error
b. Semantics Error
3. Logical Error
1. Compile Time Error: Compile time errors are those errors that occur at the time
of compilation of the program.
There are two types of compile time errors:
for example:
a=10
b=20
Example:
a+b = c # expression cannot come on the left side of the assignment operator.
2. Run Time Error: Errors that occur during the execution or running the program
are known as run time errors. When a program “crashed” or “abnormally
terminated” during the execution, then this type errors are run time errors.
a=1
while a<5: # value of a is always less than 5, infinite loop
print(a)
a=a-1
3. Logical Error: These types of errors occur due to wrong logic. When a program
successfully executes but giving wrong output, then it is known as logical error.
Example:
a=10
b=20
c=30
average=a+b+c/3 #wrong logic to find the average
print(average)
OUTPUT:
40.0
Exception: The unusual and unexpected condition other than syntax or logical
errors,encountered by a program at the time of execution, is called exception.
Exception handling in python can be done using try and except blocks.
Syntax:
try:
# Code that may generate an exception
except:
# Code for handling the exception
Example:
num1=int(input("Enter first number :"))
num2=int(input("Enter second number: "))
try:
r=num1/num2
print("Result is :", r)
except:
print("Divided by zero")
OUTPUT:
END…..