GATE | UGC-NET
NET | HPSC | KVS | DSSSB | TGT COMPUTER SCIENCE | PGT COMPUTER SCIENCE
Python Programming Notes Data Mining Hub
Introduction to Python (PART#2)
DATA TYPES
A Variable can hold different types of values. For example rollno of a student must be an
integer whereas name of student must be a string.
Data type identifies the type of data values a variable can hold and the operations that can be
performed on that data.
Since Python is dynamically typed language therefore we do not need to define the type of a
variable during declaring a variable.
a=5
The variable a holds integer value five and we did not define its type. Python interpreter will
automatically interpret variables a as an integer type.
Python provides various standard data types that define the storage method on each of them. The data types
defined in Python are given below.
1. Numeric: Number data type stores numerical values only. It is further classified into three
different types: int, float and complex
complex.
Type/ Class Description Examples
int integer numbers –12,
12, –3, 0, 125, 2
float real or floating point numbers –2.04,
2.04, 4.0, 14
complex complex numbers 3 + 4j, 2 – 2j
Boolean data type (bool) is a subtype of integer. It is a unique data type, consisting of two constants,
True and False.
2. Sequence : A Python sequence is an ordered collection of items, where each item is indexed
by an integer. Sequence
equence data types available in Python are Strings, Lists and Tuples.
a. String : A string is a collection of one or more characters put in a single quote, double-
double
quote or triple quote. These characters may be alphabets, digits or special characters
including spaces.
Example :
Str=”Data Mining Hub “ #It is a string
Str1=”Welcome 2023” #It is a string
1 SUBSCRIBE https://youtube.com/@datamininghub
DataMiningHub
GATE | UGC-NET
NET | HPSC | KVS | DSSSB | TGT COMPUTER SCIENCE | PGT COMPUTER SCIENCE
Python Programming Notes Data Mining Hub
b. List : List is a sequence of items separated by commas and the items are enclosed in square
brackets [ ].
Exapmle:
List=[10,20,”Ram”,True]
Print(List)
Output : [10,20,”Ram”,True]
c. Tuple: Tuple is a sequence of items separated by commas and items are enclosed in
parenthesis ( ).
Example:
T=(10,20,30, “Data”,False)
print(T)
Output: (10,20,30, “Data”,False)
d. Set: Set is an unordered collection of items separated by commas and the items are enclosed
in curly brackets { }.
Example :
set1 = {10,20,3.14,"New Delhi"}
print(set1)
Output : {10, 20, 3.14, "New Delhi"}
3. Dictionary: Dictionary in Python holds data items in key key-value pairs. Items in a dictionary are
enclosed in curly brackets { }.
Example:
dict1 = {'Fruit':'Apple','Climate':'Cold', 'Price(kg)':120}
print(dict1)
Output: {'Fruit': 'Apple', 'Climate': 'Cold','Price(kg)': 120}
Mutable and Immutable Data Types
Sometimes we need to change the value of a variables used in a progr
program
am.
Variables whose values can be changed after they are created and assigned are called
mutable.
Variables whose values cannot be changed after they are created and assigned are called
immutable.
Python data types can be classified into mutable and immutable as shown
Mutable Data Types Immutable Data Types
List Integers
Set Float
Dictionary String
Tuple
2 SUBSCRIBE https://youtube.com/@datamininghub
DataMiningHub
GATE | UGC-NET
NET | HPSC | KVS | DSSSB | TGT COMPUTER SCIENCE | PGT COMPUTER SCIENCE
Python Programming Notes Data Mining Hub
OPERATORS
An operator is used to perform specific mathematical or logical operation
tion on values. The values that
the operators work on are called operands.
For example,
In the expression
a+b-50
The value 50, and the variable a , b are operands and the + (plus) and - (Subtraction) sign is an
operator.
Python divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
1. Arithmetic Operators:
Arithmetic operators are used to perform mathematical operations like addition, subtraction,
multiplication, etc. Arithmetic Operators are shown below in table
table:
Operator Description Syntax
+ Addition: adds two operands x+y
– Subtraction: subtracts two operands x–y
* Multiplication: multiplies two operands x*y
/ Division (float): divides the first operand by the second returns x/y
the quotient
// Division (floor): divides the first operand by the second and x // y
returns the quotient by removing the decimal part. It is
sometimes also called integer division.
% Modulus: returns the remainder when the first operand is x%y
divided by the second
** Power: Returns first raised to power second x ** y
Example:
a=7
b=2
print ('Sum: ', a + b)
print ('Subtraction: ', a - b)
print ('Multiplication: ', a * b)
print ('Division: ', a / b) PRECEDENCE:
print ('Floor Division: ', a // b) P – Parentheses
print ('Modulo: ', a % b) E – Exponentiation
print ('Power: ', a ** b) M – Multiplication (Multiplication and
division have the same precedence)
D – Division
OUTPUT:
A – Addition (Addition and
Sum: 9
subtraction have the same precedence)
Subtraction: 5
S – Subtraction
Multiplication: 14
Division: 3.5
Floor Division: 3
Modulo: 1
Power: 49
3 SUBSCRIBE https://youtube.com/@datamininghub
DataMiningHub
GATE | UGC-NET
NET | HPSC | KVS | DSSSB | TGT COMPUTER SCIENCE | PGT COMPUTER SCIENCE
Python Programming Notes Data Mining Hub
2. Assignment Operators
Assignment operator assigns or changes the value of the variable on its left.
Assignment operators in Python are shown below:
Operator Description Syntax
= Assign value of right side of expression to left side x=y+z
operand
+= Add right-side
side operand with left side operand and then a+=b a=a+b
assign to left operand
-= Subtract right operand from left operand and then assign to a-=b
=b a=a-b
left operand
*= Multiply right operand with left operand and then assign to a*=b a=a*b
left operand
/= Divide left operand with right operand and then assign to a/=b a=a/b
left operand
%= Takes modulus using left and right operands and assign the a%=b a=a%b
result to left operand
//= Divide left operand with right operand and then assign the a//=b a=a//b
value(floor) to left operand
**= Calculate exponent(raise power) value using opera
operands and a**=b a=a**b
assign value to left operand
Example:
a = 10
b=5
a += b #a=a+b
print(a)
Output: 15
3. Comparison operators / Relational operators
Comparison operators (Relational operators) compares the values on LHS and RHS. It either
returns True or False according to the condition.
Operator Description Syntax
> Greater than: True if the left operand is greater than the right x>y
< Less than: True if the left operand is less than the right x<y
== Equal to: True if both operands are equal x == y
!= Not equal to – True if operands are not equal x != y
>= Greater than or equal to True if the left operand is greater than or equal to x >= y
the right
<= Less than or equal to True if the left operand is less than or equal to the x <= y
right
is x is the same as y x is y
is not x is not the same as y x is not y
Example:
a = 13
b = 33
print(a > b)
print(a < b)
print(a == b)
4 SUBSCRIBE https://youtube.com/@datamininghub
DataMiningHub
GATE | UGC-NET
NET | HPSC | KVS | DSSSB | TGT COMPUTER SCIENCE | PGT COMPUTER SCIENCE
Python Programming Notes Data Mining Hub
print(a != b)
print(a >= b)
print(a <= b)
OUTPUT:
False
True
False
True
False
True
4. Logical operators
There are three logical operators supported by Python. These operators (and, or, not) are to
be written in lower case only. The logical operator evaluates to either True or False based on
the logical operands on either side.
Operator Description Syntax
and Logical AND: True if both the operands are true x and y
or Logical OR: True if either of the operands is true x or y
not Logical NOT: True if the operand is false not x
Example:
a=5
b=6
print(a > 2 and b >= 6)
print(a>2 or b>7)
OUTPUT:
True
False
5. Identity operators
Identity
dentity operators compare the memory locations of two objects. There are two Identity
operators explained below –
Operator Description Syntax
is Evaluates to true if the variables on either side of the x is y
operator point to the same object and false otherwise.
is not Evaluates to false if the variables on either side of the x is not y
operator point to the same object and true otherwise.
Example:
a = 10
b = 20
c= a
print(a is not b)
print(a is c)
OUTPUT:
True
True
5 SUBSCRIBE https://youtube.com/@datamininghub
DataMiningHub
GATE | UGC-NET
NET | HPSC | KVS | DSSSB | TGT COMPUTER SCIENCE | PGT COMPUTER SCIENCE
Python Programming Notes Data Mining Hub
6. Membership operators
Membership operators are used to check if a value is a member of the given sequence or not.
operator Description Example
in Returns True if the variable/value is a = [1,2,3]
found in the specified sequence and print( 2 in a)
False otherwise OUTPUT: True
not in Returns True if the variable/value is a = [1,2,3]
not found in the specified sequence print( 2 not in a)
and False otherwise OUTPUT: False
7. Bitwise operators
Bitwise operator works on bits and performs bit by bit operation. To perform bitwise
operations first we have to convert decimal values into binary values.
Operator Name Example
& Binary AND Sets each bit to 1 if both bits are 1
| Binary OR Sets each bit to 1 if one of two bits is 1
^ Binary XOR Sets each bit to 1 if only one of two bits is 1
~ Binary Ones Inverts all the bits
Complement
<< Binary Left Shift Shift left by pushing zeros in from the right and let
the leftmost bits fall off
>> Binary Right Shift Shift right by pushing copies of the leftmost bit in
from the left, and let the rightmost bits fall off
# Examples
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
6 SUBSCRIBE https://youtube.com/@datamininghub
DataMiningHub
GATE | UGC-NET
NET | HPSC | KVS | DSSSB | TGT COMPUTER SCIENCE | PGT COMPUTER SCIENCE
Python Programming Notes Data Mining Hub
Precedence of Operators
Evaluation of the expression is based on precedence of operators. rs. When an expression contains
different kinds of operators, precedence determines which operator should be applied first. Higher
precedence operator is evaluated before the lower precedence operator.
The following table lists precedence of all operators from highest to lowest:
Sr.No. Operator & Description
1
**
Exponentiation (raise to the power)
2
~ + -
Complement, unary plus and minus
(method names for the last two are +@
and -@)
3
/ % //
Multiply, divide, modulo and floor division
4
+ -
Addition and subtraction
5
>> <<
Right and left bitwise shift
6
&
Bitwise 'AND'
7
^ |
Bitwise exclusive `OR' and regular `OR'
8
<= < > >= <> == !=
Comparison operators
9
= %= /= //= -= += *= **=
Assignment operators
10
is , is not
Identity operators
11
in , not in
Membership operators
12
not Logical operator
and Logical operator
or Logical operator
7 SUBSCRIBE https://youtube.com/@datamininghub
DataMiningHub
GATE | UGC-NET
NET | HPSC | KVS | DSSSB | TGT COMPUTER SCIENCE | PGT COMPUTER SCIENCE
Python Programming Notes Data Mining Hub
Note:
a) Parenthesis can be used to override the precedence of operators. The expression within ( ) is evaluated first.
b) For operators with equal precedence, the expression is evaluated from left to right except ** (Exponential
Exponential operator) which is
solved from right to left.
Q.1 How will the following expression be evaluated in Python?
15.0 / 4 + (8 + 3.0)
Solution:
= 15.0 / 4 + (8.0 + 3.0)
= 15.0 / 4.0 + 11.0
= 3.75 + 11.0
= 14.75
Q. 2 How will Python evaluate the following expression?
(20 + 30) * 40
Solution:
= (20 + 30) * 40
= 50 * 40
= 2000
CLASS 11 COMPUTER SCIENCE WITH PYTHON NCERT SOLUTION
SOLUTION(CBSE)
CHAPTER-5 5 GETTING STARTED WITH PYTHON
Question 3:
Write logical expressions corresponding to the following statements in Python and evaluate the expressions
(assuming variables num1, num2, num3, first, middle, last are already having meaningful values):
a) The sum of 20 and –10 10 is less than 12.
b) num3 is not more than 24
c) 6.75 is between the values of integers num1 and num2.
d) The string ‘middle’ is larger than the string ‘first’ and smaller than the string ‘last’
e) List Stationery is empty
ANSWER:
a) (20 + (-10)) < 12
b) num3 <= 24 or not(num3 > 24)
c) (6.75 >= num1) and (6.75 <= num2)
d) (middle > first) and (middle < last)
e) len(Stationery) == 0
Question 4:
Add a pair of parentheses to each expression so that it evaluates to True.
a) 0 == 1 == 2
b) 2 + 3 == 4 + 5 == 7
c) 1 < -1 == 3 > 4
ANSWER:
a) ( 0 == (1==2))
b) (2 + (3 == 4) + 5) == 7
c) (1 < -1) == (3 > 4 )
Question 5:
Write the output of the following.
8 SUBSCRIBE https://youtube.com/@datamininghub
DataMiningHub
GATE | UGC-NET
NET | HPSC | KVS | DSSSB | TGT COMPUTER SCIENCE | PGT COMPUTER SCIENCE
Python Programming Notes Data Mining Hub
a) num1 = 4
num2 = num`1 + 1
num1 = 2
print (num1, num2)
b) num1, num2 = 2, 6
num1, num2 = num2, num1 + 2
print (num1, num2)
c) num1, num2 = 2, 3
num3, num2 = num1, num3 + 1
print (num1, num2, num3)
ANSWER:
a) 2, 5
b) 6, 4
c) Error as num3 is used in RHS of line 2 (num3, num2 = num1, num3 + 1) before defining it earlier.
Question 7:
Give the output of the following when num1 = 4, num2 = 3, num3 = 2
a) num1 += num2 + num3
print (num1
b) num1 = num1 ** (num2 + num3)
print (num1)
c) num1 **= num2 + num3
d) num1 = '5' + '5'
print(num1)
e) print(4.00/(2.0+2.0))
f) num1 = 2+9*((3*12)-8)/10
print(num1)
g) num1 = 24 // 4 // 2
print(num1)
h) num1 = float(10)
print (num1)
i) num1 = int('3.14')
print (num1)
j) print('Bye' == 'BYE')
k) print(10 != 9 and 20 >= 20)
l) print(10 + 6 * 2 ** 2 != 9//4 -3
3 and 29>= 29/9)
m) print(5 % 10 + 10 < 50 and 29 <= 29)
n) print((0 < 6) or (not (10 == 6) and (10<0)))
ANSWER:
a) num1 += 3 + 2
Therefore, num1 = num1 + 3 + 2 = 4 + 3 + 2 = 9
Output: 9
b) num1 = num1 ** (num2 + num3)
num1 = 4 ** (3 + 5) = 4 ** 5 = 1024
output: 1024.
c) num1 **= num2 + num3
num1 **= 5
9 SUBSCRIBE https://youtube.com/@datamininghub
DataMiningHub
GATE | UGC-NET
NET | HPSC | KVS | DSSSB | TGT COMPUTER SCIENCE | PGT COMPUTER SCIENCE
Python Programming Notes Data Mining Hub
num1 = num1 ** 5
num1 = 4 ** 5
num1 = 1024
output: 1024.
d) num1 = '5' + '5'
#String concatenation
output: 55
e) print(4.00/(2.0 + 2.0))
print(4.00/(2.0 + 2.0))
print(4.0/4.0)
output: 1.0.
f) num1 = 2 + 9 * ((3 * 12) - 8) /10
# The expression within inner brackets will be evaluated first
num1 = 2 + 9 * (36 - 8) /10
# The expression within outer brackets will be evaluated next
num1 = 2 + 9 * 28/10
# * and / are of same precedence, hence, left to rig
right order is followed
num1 = 2 + 252/10
num1 = 2 + 25.2
num1 = 27.2
output : 27.2
g) num1 = 24 // 4 // 2
#When the operators are same, left to right order will be followed for operation
num1 = 6 // 2
#When floor division is used, return value will be int data type
num1 = 3
output : 3
h) num1 = float(10)
float(10) will convert integer
nteger value to float value.
output : 10.0
i) num1 = int('3.14')
This will result in an error as we cannot pass string representation of float to an int function.
j) print('Bye' == 'BYE')
output : False
k) print(10 != 9 and 20 >= 20)
print(True and True)
print(True)
output : True
l) print(10 + 6 * 2 ** 2 != 9//4 -3
3 and 29>= 29/9)
Taking the above statement in two separate parts
LHS:
10 SUBSCRIBE https://youtube.com/@datamininghub
DataMiningHub
GATE | UGC-NET
NET | HPSC | KVS | DSSSB | TGT COMPUTER SCIENCE | PGT COMPUTER SCIENCE
Python Programming Notes Data Mining Hub
10 + 6 * 2 ** 2 != 9//4 - 3
10 + 6 * 4 != 2 - 3
10 + 24 != -1
34 != -1
True
RHS:
29 >= 29/9
True
Now the complete equation can be written as
print(True and True)
output : True
m) print(5 % 10 + 10 < 50 and 29 <= 29)
Taking the above statement in two separate parts
LHS :
5 % 10 + 10 < 50
5 + 10 < 50
15 < 50
True
RHS:
29 <= 29
True
Now, the complete equation can be written as
print(True and True)
output : True
n) print( (0 < 6) or (not (10 == 6) and (10<0) ) )
print(True or (not False and False))
print(True or (True and False))
# not will be evaluated before and/or.
print(True or False)
print(True)
output : True
References:
Class 11 Computer Science NCERT
https://www.w3schools.com/python/
11 SUBSCRIBE https://youtube.com/@datamininghub
DataMiningHub