Professional Documents
Culture Documents
UNIT-1(PART-2)
UNIT-1(PART-2)
(part-2 topics)
Prepared By
B.JAYARAM
Mtech(PhD)
CSD dept
Introduction to Python
• Python is a general purpose, dynamic, high-level,
interpreted, Object Oriented programming
language
•.
• It was created by Guido van Rossum ,started
implementation in 1989 and released in 1991 at CWI
in Netherland.
• Easiest than c,c++,java.
• Python supports multiple programming , pattern
including object-oriented imperative and functional or
procedure programming styles .
• What is the difference between compile and
• Interpreter
• Complier the program source code execute at a time -----
• Interpreter the program source code is execute line by line (or one
line after another )
OR
• Python is a general purpose programming
language that is often applied in scripting roles.
• Conditionals and Loops-if, else, elif, for, while, break, continue, pass, List
comprehensions, Generator expressions
TOPIC:1 Python Numbers (or) Numeric
• Numbers data types store numeric values. They are immutable data types,
which means that changing the value of a number data type result in a
newly allocated object.
• Examples :
• Var1 =10
• X=20
TOPIC 2: PYTHON OPERATORS
What is meant by an Operator in Python?
• These are the special symbols in python and are used to execute an
Arithmetic or Logical computation.
• An operator alone cannot perform an activity, it needs an Operand.
• What is an operand?
• An Operand is a value that the operator needs to complete a task.
To understand it clearly, let’s consider an example over here.
>>>10/2 5
>>> 5.0 / 2
Division that results into whole number 2.5
// Floor division
adjusted to the left in the number line. >>> 5.0 /2
2.0
x > y is False
4) Python (<) Less Than Operator
This Operator compares the right hand operator with the left hand one and if the right hand
one is greater than the left hand one, it returns True statement.
Ex: 10 is < 12, then the answer becomes True
10 is < 8, then the answer becomes False
Program:
x = 20
y = 25
# Output: x < y is True
print('x < y is',x<y)
Run:
x < y is True
5) Python >= Greater Than or Equal to operator
Greater than operator is used in python to evaluate to True
if the left hand operand is >= (greater than or equal) to right hand one
Program:
x = 20
y = 25
# Output: x >= y is False
print('x >= y is',x>=y)
Run:
x >= y. Hence it is False
6) Python <= Less than or equal to operator
Less than or equal to operator returns true if the right hand operator is greater than or equal to left hand one.
Program:
x = 20
y = 25
# Output: x <= y is True
print('x <= y is',x<=y)
Run:
x <=y is True
• III) Python Assignment Operators
• Assignment operator is used to assign value to the event, property, or variable. We
use this operator in python to assign values to variables.
• We have multiple Assignment operators.
• Let’s list them down and know things better.
• = Assign Value
• += Add AND
• -= Subtract AND
• *= Multiply AND
• /= Divide AND
• %= Modulus AND
• **= Exponent AND
• //= Floor Division
• 1) Python (=) Assign value Operator
• This operator assigns values to the left hand operand from the right side
operand.
• Example: 5 = 2+3
• In the above example, the right hand operands are 2 & 3, and have
contributed to construct value 5.
• 2) Python (+=) Add AND Operator
• It adds the value of right operand to the value of left hand operand.
• Let's consider an
• example;
• x = 10,
• x + = 5,
• which will return the value as 15 by adding the right side value to the left
hand one.
• Therefore, the answer is x= 15.
3) Python(-=) Subtract AND Operator
Subtract And operator (-=) subtracts the value of left operand from the right side operand and
returns the output.
Example
x = 10,
y -= 7,
the answer is x = 3
4) Python (*=) Multiply AND Operator
It multiplies the left hand operand with the right hand one and returns the value as output.
Example;
x = 10,
x*=5,
the answer is x = 50.
5) Python (/=) Divide AND Operator
Divide And operator divides the left hand operand with the right hand operand value and gives the result
Example;
x = 10,
x/= 5
the result is x = 2
6) Python (%=) Modulus AND Operator
It divides the value of left hand operand with the right hand one and the reminder that we get through this
task will be placed as a value of x.
Example;
x = 10,
x%= 5
the result is x = 0
7) Python **= Exponent AND operator
It gives us the exponential value of the left hand operand when raised to the power of the value found
in the right hand side.
Example;
x = 10, x**= 5 the result is x = 1,00,000
8) Python (//=) Floor Division
Floor division divides the value found in the left hand side with the value of right hand operand. The
integer value becomes the left hand Operand value.
Example;
x = 7, x//= 4 the result is x = 1
• IV) Python logical Operators
• Logical operators are used in any programming language to make decision
based on multiple conditions.
• In python, we use Logical operators to determine whether a condition is
True or False by taking Operand values as base.
• Let’s consider different logical operators that are used in python
programming.
• And - Logical AND
• Or - Logical OR
• Not - Logical NOT
1) Python and Logical AND operator
AND operator returns the logical value ‘True’ when two Operands are true;
otherwise, it will return False.
Ex: variable A = 10, B = 20,
x = 10
y = 20
# Output: x and y is True
print('x and y is',x and y)
Output:
Ans: A and B is True
2) Python or Logical OR operator
In this Logical OR operator, if any one of the two operands are non zero, then it returns the
True condition. .
Ex: variable A = 10, B = 20,
Program
x = 10
y = 20
# Output: x or y is True
print('x or y is',x or y)
Output:
Ans: a or b is True
3) Python not Logical NOT
Logical NOT is used to reverse the logical state of its operands.
Program
x = 10
y = 20
# Output: not x is False
print('not x is',not x)
Output:
Ans: Not (a and b) is false.
• V) Python Bitwise operators
• Bitwise operators are used in python to perform the operations on
binary numerals or bit patterns. It operates bit by bit.
• & Binary AND
• | Binary OR
• ^ Binary XOR
• ~ Binary Ones Complement
• << Binary Left Shift
• >> Binary Right Shift
1)Python & Binary AND Bitwise Operator
Description: This operator performs bit by bit AND operations on the
values on either side of the operator.
Example Program
a = 10
b = 4
# Print bitwise AND operation
print(a & b)
Output:
0
2) Python | Binary OR Bitwise Operator
Description: This operator performs bit by bit OR operations on
the values on either side of the operator.
Example Program
a = 10
b = 4
# Print bitwise OR operation
print(a | b)
Output: 14
3) Python ^ Binary XOR Bitwise Operator
Description: This operator performs bit by bit exclusive OR
operations on the values on either side of the operator.
Example Program
a = 10
b=4
# print bitwise XOR operation
print(a ^ b)
Output: 14
4) Python ~ Binary Ones Complement Bitwise Operator
Example Program:
a = 10
b=4
# Print bitwise NOT operation
print(~a)
Output: -11
5) Python << Binary Left shift Bitwise Operator
Description: The value to the left operator is shifted to the left as many
times as the value on the right side of the operator.
Example Program
a = 10
b=4
# print bitwise left shift operation
print(a << 2)
Output: 40
6) Python >> Binary Right shift Bitwise Operator
Output:
True
False
True
• 2) Python Is not Identity operator
• It will return false if either side of the operator pointing towards the
same thing, otherwise it returns true.
x = ["apple", "banana"]
y = ["apple", "banana"]
z=x
print(x is not z)
print(x is not y)
print(x != y)
OUTPUT:
False
True
False
TOPIC 3: PYTHON CONTROL STATEMENTS
PYTHON CONDITIONAL
STATMENTS
• Conditional statements are also known as decision-making
statements. We need to use these conditional statements to
execute the specific block of code if the given condition is
true or false.
• The conditional statements are
1. if statements
2. if-else statements
3. elif statements
4. Nested if and if-else statements
5. elif ladder
• 1) if statements:
• Python if statement is one of the most commonly used
conditional statements in programming languages.
• It decides whether certain statements need to be executed or
not.
• It checks for a given condition, if the condition is true, then
the set of code present inside the ” if ” block will be executed
otherwise not.
• The if condition evaluates a Boolean expression and executes
the block of code only when the Boolean expression becomes
TRUE.
Syntax:
if expression:
statement(s)
____________________________________________________________
If the boolean expression evaluates to TRUE, then the block of
statement(s) inside the if statement is executed.
If boolean expression evaluates to FALSE, then the first set of
code after the end of the if statement(s) is executed.
if Statement Flowchart:
PROGRAM :
a=3
if a > 2:
print(a, "is greater")
print(“Simple if ")
OUTPUT:
3 is greater
2) if-else statements
• The statement itself says if a given condition is true then execute the
statements present inside the “if block” and if the condition is false
then execute the “else” block.
• The “else” block will execute only when the condition becomes
false. It is the block where you will perform some actions when the
condition is not true.
• if-else statement evaluates the Boolean expression. If the condition
is TRUE then, the code present in the “ if “ block will be executed
otherwise the code of the “else“ block will be executed
Syntax:
If (EXPRESSION == TRUE):
Statement (Body of the block)
else:
Statement (Body of the block)
flowchart of if-else
Program:-1
num = 5
if(num > 10):
print(“number is greater than 10”)
else:
print(“number is less than 10”)
Output:
Output:
a is greater than b
Program 3:
• Write a python program to print give a number is odd or even
3) elif statements (ladder if)
• In Python, we have one more conditional statement called
“elif” statements. “elif” statement is used to check multiple
conditions only if the given condition is false.
• It’s similar to an “if-else” statement and the only difference is
that in “else” we will not check the condition but in “elif” we
will check the condition.
• “elif” statements are similar to “if-else” statements but “elif”
statements evaluate multiple conditions.
Syntax:
if (condition):
#Set of statement to execute if condition is true
elif (condition):
#Set of statements to be executed when if condition is false and
elif condition is true
else:
#Set of statement to be executed when both if and elif conditions
are false
Flowchart of if – elif - else:
Program1:
num = 10
if (num == 0):
print(“Number is Zero”)
else:
print(“Number is smaller than 5”)
Output:
Output:
Number is negative
• Program 3:
• Write a python program to implement students grades.
4) Nested if-else statements
• Nested “if-else” statements mean that an “if” statement or “if-
else” statement is present inside another if or if-else block.
Python provides this feature as well, this in turn will help us to
check multiple conditions in a given program.
• An “if” statement is present inside another “if” statement
which is present inside another “if” statements and so on.
Syntax:
if(condition):
#Statements to execute if condition is true
if(condition):
#Statements to execute if condition is true
#end of nested if
#end of if
• The above syntax clearly says that the if block will contain another if
block in it and so on.
• If block can contain ‘n’ number of if block inside it.
Program:
num = 5
if(num >0):
print(“number is positive”)
if(num<10):
print(“number is less than 10”)
Output:
number is positive
OUTPUT:
Mlritm college
Mlritm college
Mlritm college
Mlritm college
Mlritm college
Mlritm college
2)For loop:
• The for loop in Python is used to iterate over a sequence
(list, tuple, string) or other iterable objects. Iterating over a
sequence is called traversal.
SYNTAX:
OUTPUT:
1
2
2
• 2) for loop use tuple:
tuple=(1,2,3)
for x in tuple:
print(x)
Output:
1
2
3
• 3) for loop using dictionary :
x={"csda":204,"csdb":205,"csdc":206}
print("the keys are")
for keys in x:
print(keys)
print("the values are")
for value1 in x.values():
print(value1)
Output:
the keys are
csda
csdb
csdc
the values are
204
205
206
• 4) for loop using String :
x="MLRITM"
for s in x:
print(s)
OUTPUT:
M
L
R
I
T
M
Note: range() function
• The range() function can take 1 to 3 of the following arguments:
• Stop: The integer value up to which the sequence will get generated but
won't include this number. This is a required argument which must be passed.
Output:
0
1
2
3
4
5
• 2)
for i in range(6):
print(i)
OUTPUT:
0
1
2
3
4
5
• 3)
for i in range(30,0,-5):
print(i)
OUTPUT:
30
25
20
15
10
5
• 4)
for letter in "Python":
print(letter)
OUTPUT:
P
y
t
h
o
n
5)
for letter in "Python":
print(letter, end=" ")
Output:
Python
3) Nested For loop:
• A nested loop is a loop inside the body of the outer loop. The
inner or outer loop can be any type, such as a while loop or for
loop.
Syntax:
# outer for loop
for element in sequence
# inner for loop
for element in sequence:
body of inner for loop body of outer for loop
Example Programs:
• 1)
x = [1, 2]
y = [4, 5]
for i in x:
for j in y:
print(i, j)
OUTPUT:
14
15
24
25
2)
# Running outer loop from 2 to 3
If the break statement is inside a nested loop (loop inside another loop),
the break statement will terminate the innermost loop.
Syntax:
break
Flow chart:
The working of break statement in for loop and while loop is shown below.
Program:
• 1)
• # Use of break statement inside the loop
for val in "string":
if val == "i":
break
print(val)
print("The end")
OUTPUT:
s
t
r
The end
• 2) Python continue statement :
• The continue statement is used to skip the rest of the code inside
a loop for the current iteration only.
• Loop does not terminate but continues on with the next iteration.
• Syntax:
• continue
Flow chart:
• Programs:
• The working of the continue statement in for and while loop is shown
below.
• 1) # Program to show the use of continue statement inside loops
for val in "string":
if val == "i":
continue
print(val)
print("The end")
OUTPUT:
s
t
r
n
g
The end
TOPIC :6
Python - List Comprehension
• List comprehension in Python is an easy and compact
syntax for creating a list from a string or another list.
• It is a very concise way to create a new list by performing an
operation on each item in the existing list.
• List comprehension is considerably faster than processing a
list using the for loop.
Syntax:
[expression for element in iterable if condition]
• The above syntax, the list comprehension syntax contains
three parts:
• An expression, one or more for loop, and optionally, one or
more if conditions.
• The list comprehension must be in the square brackets [].
• The result of the first expression will be stored in the new
list.
• The for loop is used to iterate over the iterable object
that optionally includes the if condition.
Example Programs:
1) Create List of Even Numbers with List Comprehension
even_nums = [x for x in range(21) if x%2 == 0]
print(even_nums)
Output:
Output:
['Ramu', 'Jai', 'Sai']
a) List Comprehension using Nested Loops
• It is possible to use nested loops in a list comprehension expression.
• In the following example, all combinations of items from two lists
in the form of a tuple are added in a third list object.
• Program :
nums1 = [1, 2, 3]
nums2 = [4, 5, 6]
nums=[(x,y) for x in nums1 for y in nums2]
print(nums)
Output:
[(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]
b)List Comprehension with Multiple if Conditions
• We can use nested if conditions with a list comprehension.
• Program:
nums = [x for x in range(21) if x%2==0 if x%5==0]
print(nums)
Output:
[0, 10, 20]
c) List Comprehension with if-else Condition
• The following example demonstrates the if..else loop with a list comprehension.
• Program:
odd_even_list = ["Even" if i%2==0 else "Odd" for i in range(5)]
print(odd_even_list)
odd_even_list = [str(i) + '=Even' if i%2==0 else str(i) + "=Odd" for i in range(5)]
print(odd_even_list)
OUTPUT:
['Even', 'Odd', 'Even', 'Odd', 'Even']
['0=Even', '1=Odd', '2=Even', '3=Odd', '4=Even']
TOPIC 7: Python-Generator Expressions
• In Python, to create iterators, we can use both regular
functions and generators.
• Generators are written just like a normal function but we
use yield() instead of return() for returning a result.
• It is more powerful as a tool to implement iterators.
• It is easy and more convenient to implement because it
offers the evaluation of elements on demand.
• Another great advantage of the generator over a list is that it
takes much less memory.
Program:
def simple():
for i in range(10):
if(i%2==0):
yield i
8
TOPIC 8 : PASS STATEMENT
• In Python programming, the pass statement is a null statement
which can be used as a placeholder for future code.
• Suppose we have a loop or a function that is not implemented yet,
but we want to implement it in the future.
• In such cases, we can use the pass statement.
syntax of pass statement :
PASS
PROGRAM 1 :
x= {"csdc", "csdb", "Pass", "csda"}
for value in x:
if value == "Pass":
pass # leaving an empty if block using the pass keyword
else:
print("Not reached pass keyword: ", value)
• output :
• Not reached pass keyword: csdb
• Not reached pass keyword: csdc
• Not reached pass keyword: csda
PROGRAM 2:
n = 10
# use pass inside if statement
if n > 10:
pass
output:
x=1
print(eval('x + 1'))
output:
2