You are on page 1of 96

Language

A way to communicate with others.

Program
Set of Commands/Instructions.

Programming Language
A way to write set of Instructions.
Ex- Python, C, C++, Java, C#.........

BY:- PUNEET MOHAN


Python is a very popular
programming language among
beginners as well as developers.
Python is an Multi-paradigm
language.

PYTHON LANGUAGE
Python was
designed for
Socket
Python was developed by Programming
Guido Van Rossum in 1990s.
Python was named after the BBC
comedy series ‘Flying Circus’ of
Monty Python. BY:- PUNEET MOHAN
Tokens/Lexical unit
The smallest individual unit in a program is
known as a token.
Python has following tokens:
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Punctuators

BY:- PUNEET MOHAN


Operators
Arithmetic operators
+(Addition), -(Subtraction),
*(Multiplication), //(Floor Division),
/(divide), %(Modulus), **(Power)

Ex- A=10 ,B=3


A+B=13,
A-B=7,
A*B=30, A**B=1000
A//B=3, A/B=3.33 , A%B=1
BY:- PUNEET MOHAN
Relational operators
>(Greater than), <(Less than),
>=(Greater than equal to),
<=(Less than equal to),
==(Equal to), !=(Not Equal to)
Ex- A=10, B=20
A>B (False)
A<B (True)
A>=B (False)
A<=B (True)
A==B (False), A!=B(True) BY:- PUNEET MOHAN
Logical operators
and , or, not
Ex-
A B A and B A or B not B
T T T T F
T F F T T
F T F T F
F F F F T

BY:- PUNEET MOHAN


Membership operators
In, not In
Ex-
10 in [10,20,30,40,50]
True

10 not in [10,20,30,40,50]
False

BY:- PUNEET MOHAN


DATA TYPES

Numbers
String
List
Complex
Tuples
Integer Dictionary
Floating point
BY:- PUNEET MOHAN
Immutable Mutable
▪ Tuple ▪ List
▪ String ▪ Dictionary
▪ Number
Python Screen

Interactive mode Script Mode

BY:- PUNEET MOHAN


Print() Statement
Use to give output or show message on screen.
Ex-
Print(“Welcome to Python”)

Input() Statement
Use to get data/information/string from user.
Ex-
A=input(“Enter your name”)
Or
A=int(input (“Enter a number”))
BY:- PUNEET MOHAN
Variable
Labeled storage location, whose value can be
manipulated during program run.
But python variables are not storage containers,
rather python variables are lie memory references.
Ex- A=10 # Integer value
Name=“Rohan” # String value
A=B=C=10 # Multiple Assignment
A,B,C=10,20,30 #Multiple value to multiple
variable.
Comments in Python
Single line = # Single line comment
Multiple line = ””” Multi line comment”””
BY:- PUNEET MOHAN
Rules to declare Identifier/Variable
•It Cannot start with a digit
•It can contain letters , digits or
underscore
•Punctuation and blanks are not allowed
•Python is case sensitive. The identifier
total is different form Total.
•Keywords cannot be used as a variable.

BY:- PUNEET MOHAN


KeyWords
These are reserved words that have a pre-
defined meaning to the interpreter. They are
Easy to recognize in IDLE because they are
automatically colored orange.
Keywords in Python:
and def for is return
as del from lambda try
assert elif global not while
break else if or with
class except import pass yield
continue finally IN raise print
BY:- PUNEET MOHAN
#Write a program to enter two numbers
from user and find sum of them.

a=int(input("Enter first number"))


b=int(input("Enter second number"))
c=a+b
print("Sum=",c)

BY:- PUNEET MOHAN


Programs to practice
•Write a program to enter two numbers from user and
find sum of them.
•Write a program to enter two numbers from user and
find difference of them.
•Write a program to enter a number from user and find
area of square.
•Write a program to enter two numbers from user and
find area of rectangle.
•Write a program to enter Height and Radius from user
and find area of triangle.
•Write a program to enter two numbers from user and
swap values.

BY:- PUNEET MOHAN


#Write a program to enter a number
two numbers from user and swap
values.

a=int(input("Enter first number"))


b=int(input("Enter second number"))
c=a
a=b #a , b = b , a
b=c
print(“value swapped=",a,” ”,b)

BY:- PUNEET MOHAN


Type Conversion
Conversion of a type of value into another.
It is of two types.
•Implicit conversion
Conversion done by compiler itself. Also called
Automatic conversion. No data lose.
Ex- a=4.5+5
print(a) # 9.5
•Explicit conversion
Conversion done by user intervention. Also called
type casting. Data lose can occur.
Ex- a=int(4.5+5)
print(a) #9
BY:- PUNEET MOHAN
FLOW CONTROL

SEQUENCE

CONDITIONAL/ ITERATION/
SELECTION
STATEMENT LOOP
BY:- PUNEET MOHAN
CONDITIONAL/
SELECTION STATEMENT

•IF… ELSE
•IF…ELIF…ELSE

BY:- PUNEET MOHAN


Python IF…ELSE Statements

if expression:
statement(s)
else:
statement(s)

BY:- PUNEET MOHAN


#Write a program to enter a number from user and
check a number is Positive or Negative.

n=int(input("Enter a number"))
if(n>0):
print("Number is Positive")
else:
print("Number is Negative")

BY:- PUNEET MOHAN


Programs to practice
•Write a program to enter a number from user and
check a number is Positive or Negative.
•Write a program to enter a number from user and
check number is even or odd.
•Write a program to enter two numbers from user and
find maximum number .
•Write a program to enter two numbers from user and
find minimum number .
•Write a program to enter a number from user and
check a number end with 7 or not.
•Write a program to enter a number from user and
check 5 at once place of the number .
BY:- PUNEET MOHAN
Python IF…ELIF…ELSE Statements
if expression:
statement(s)
elif expression:
statement(s)
elif expression:
statement(s)
else:
statement(s)
BY:- PUNEET MOHAN
#Write a program to enter a number from user and
check a number is Positive or Negative or zero.

n=int(input("Enter a number"))
If(n==0):
print(“Number is Equal to zero”)
elif(n>0):
print("Number is Positive")
else:
print("Number is Negative")

BY:- PUNEET MOHAN


Programs to practice
•Write a program to enter a number from user and
check a number is Positive or Negative or zero.
•Write a program to enter two numbers from user and
check numbers are equal, If not find maximum.
•Write a program to enter week day number(1 to 7)
from user and Print day name accordingly, if
number is not in a day number than print invalid.
•Write a program to enter month number(1 to 12)
from user and Print month name accordingly, if
number is not in a month number than print invalid.
•Write a program to enter two numbers from user and
ask for choice(1 to 4) 1=A+B, 2=A-B,3=A*B,4=A/B
if choice other than 1 to 4 than print invalid choice.
BY:- PUNEET MOHAN
ITERATION/ LOOP
There are two type of loops in Python

•WHILE LOOP

•FOR LOOP

BY:- PUNEET MOHAN


WHILE Loop
While loop repeat as long as a certain Boolean
condition is met.

Initialize
While (Expression):
Statement
Step

BY:- PUNEET MOHAN


#Print 0 to 10.
N=0
While N<=10 :
print(N)
N=N+1

#Print table of a number.

N=1
A=int(input(“Enter a number”))
While N<=10 :
print(N*A)
N=N+1
BY:- PUNEET MOHAN
The break Statement
With the break statement we can stop the loop
even if the while condition is true:
Example
Exit the loop when i is 3:
i=1
while i < 6:
print(i)
if i == 3:
break
i += 1

BY:- PUNEET MOHAN


The continue Statement
With the continue statement we can stop the
current iteration, and continue with the next:
Example
Continue to the next iteration if i is 3:
i=0
while i < 6:
i += 1
if i == 3:
continue
print(i)

BY:- PUNEET MOHAN


The else Statement
With the else statement we can run a block of code
once when the condition no longer is true:
Example
i=1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")

BY:- PUNEET MOHAN


Nested While Loop
A nested loop is a loop inside a loop.
The "inner loop" will be executed one time for
each iteration of the "outer loop":
Example
i=1 Pattern
while i<5:
j=0
*
while j<i: **
print("*",end="")
j=j+1 ***
print()
i=i+1
****
BY:- PUNEET MOHAN
Programs to practice
•Write a program to print 1 to 100.
•Write a program to print 100 to 1.
•Write a program to print Even numbers upto N.
• Write a program to print table of 2.
•Write a program to print table of a number.
•Write a program to find factorial of a number
•Write a program to reverse a number entered by user.
•Write a program to print sum of digits of number
entered by user.
•Write a program to Fibonacci series up to N.
•Write a program to check number is palindrome or not.
BY:- PUNEET MOHAN
Programs to practice
•Write a program to check number is ArmStrong or not.
•Write a program to check a number is prime or not.
•Write a program to find ArmStrong numbers upto N.
•Write a program to print table 2 to 20.
•Write programs to print patterns:

1 1 12345 * *****
12 22 1234 ** ****
123 333 123 *** ***
1234 4444 12 **** **
12345 5555 1 ***** *
BY:- PUNEET MOHAN
range() Function
The range() function returns a sequence of numbers, starting
from 0 by default, and increments by 1 (by default), and
ends at a specified number.

range(start, stop, step)


Parameter Description

start Optional. An integer number specifying at which position to start. Default


is 0
stop Required. An integer number specifying at which position to end.

step Optional. An integer number specifying the incrementation. Default is 1

BY:- PUNEET MOHAN


FOR loop
For loops iterate over a given sequence.

number = [2, 3, 5, 7]
for num in number:
print(num)

BY:- PUNEET MOHAN


# Prints the numbers 0,1,2,3,4
for x in range(5):
print(x)

# Prints 3,4,5
for x in range(3, 6):
print(x)

# Prints 3,5,7
for x in range(3, 8, 2):
print(x)
BY:- PUNEET MOHAN
#Factorial of a Number

n=int(input("Enter a number"))
f=1
for i in range(1,n+1):
f=f*i
print("Factorial= ",f)

BY:- PUNEET MOHAN


The pass Statement
For loops cannot be empty but if you have
a For loop with no content put in the pass
statement to avoid getting an error.
Example
for x in [0, 1, 2]:
pass

BY:- PUNEET MOHAN


The continue Statement
With the continue statement we can stop the
current iteration of the loop, and continue with
the next:
Example
Do not print banana:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)

BY:- PUNEET MOHAN


The break Statement
With the break statement we can stop the
loop before it has looped through all the
items:

Example:

fruits = ["apple", "banana", "cherry"]


for x in fruits:
if x == "banana":
break
print(x) BY:- PUNEET MOHAN
Else in For Loop
The else keyword in a for loop specifies a block
of code to be executed when the loop is
finished:
Example
Print all numbers from 0 to 5, and print a
message when the loop has ended:
for x in range(6):
print(x)
else:
print("Finally finished!“)
BY:- PUNEET MOHAN
Nested Loops
A nested loop is a loop inside a loop.
The "inner loop" will be executed one time for
each iteration of the "outer loop":
Example
Print each adjective for every fruit:
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)
BY:- PUNEET MOHAN
Programs to practice
•Write a program to print 1 to 100.
•Write a program to print 100 to 1.
•Write a program to print Even numbers upto N.
• Write a program to print table of 2.
•Write a program to print table of a number.
•Write a program to find factorial of a number.
•Write a program to reverse a number entered by user.
•Write a program to print sum of digits of number
entered by user.
•Write a program to Fibonacci series up to N.
•Write a program to check number is palindrome or not.
BY:- PUNEET MOHAN
STRING
Collection of Characters(A to Z, a to z,
0 to 9,!@#$%^*,().?/<>+=-_).
Each character can be individually accessed
using its index.
Ex- S=“HELLO” / ‘HELLO’
0 1 2 3 4
H E L L O
-5 -4 -3 -2 -1
BY:- PUNEET MOHAN
String Slicing
s="Hello"
print(s[0])
print(s[3]) Seq[start:stop:step]
print(s) s[0:5:1]
print(s[-1])
print(s[-2])

BY:- PUNEET MOHAN


s="Hello"
print(s[0:3])
print(s[1:4])
print(s[0: ])
print(s[1: -2])

Reverse String
print(s[-1:-6:-1])
print(s[-1: :-1])
BY:- PUNEET MOHAN
String by using ‘in’ operator
s="Hello"
for d in s:
print(d)

String using range()

s="Hello" s="Bye"
for d in range (0,5) : for d in range(-1,-4,-1):
print(s[d]) print(s[d])

BY:- PUNEET MOHAN


Basic String Operations
“+”
‘hello’+’Jatin’ = hellojatin
#Only string can join
“*”
’hello’*2 = hellohello

In
‘e’ in ‘Hello’ = True

BY:- PUNEET MOHAN


String Function
k= "lalit"

To convert into Upper Case


k.upper()
To convert into lower Case
k.lower()
To check upper Case
k.isupper()
To check lower Case
k.islower()
BY:- PUNEET MOHAN
To check only Alphabet
k.isalpha()
To check only digit
k.isdigit()
To check alphabet and number
k.isalnum()
To find length
len(k)
To Capitalize
k.capitalize()
To split string
k.split()
BY:- PUNEET MOHAN
To count character k.count(‘l’)
k.count(‘l’,2)
To find character/digit
k.find(‘l’,3)
To remove space from left
k.lstrip()
To remove space from right
k.rstrip()
To check string is space
k.isspace()
To replace string
k.replace(‘l’,’D’)
BY:- PUNEET MOHAN
To change case
k.swapcase()
To join sequence with string
k.join(seq)
To partition
k.partition (‘a’)
To replace
k.replace (“Python”, “Programming”)

BY:- PUNEET MOHAN


Programs to practice
•Write a program to enter string from user and find its
length.
•Write a program to enter string from user and convert
lower case into upper case and vice versa.
•Write a program to enter string from user and count
alphabets , digits and special characters.
•Write a program to enter string from user and reverse
it.
•Write a program to replace all A/a by Z/z in a string.
•Write a program to join two strings.
•Write a program to join a sequence by string .

BY:- PUNEET MOHAN


LIST
Sequence of comma separated values
enclosed in square brackets. Each item can
be individually accessed using its index.
Ex-
L=[‘H’,1,”hell”,65.8,’@’ ]
0 1 2 3 4
H 1 “hell” 65.8 “@”
-5 -4 -3 -2 -1
BY:- PUNEET MOHAN
List Slicing
L=[“English“,45,’maths’,123,’ok’]
print(L[0])
print(L[3])
print(L) Seq[start:stop:step]
print(L[-1]) L[0:5:1]
print(L[2:-2])
print(L[1: ])
BY:- PUNEET MOHAN
Reverse List
print(L[-1:-6:-1])
print(L[-1: :-1])

List by using ‘in’ operator


L=[“English“,45,’maths’,123,’ok’]
for d in L:
print(d)

BY:- PUNEET MOHAN


List using range()
s=[“English“,45,’maths’,123,’ok’]
for d in range (0,5) :
print(s[d])

s=[“English“,45,’maths’,123,’ok’]
for d in range(-1,-4,-1):
print(s[d])

BY:- PUNEET MOHAN


Basic List Operations
“+”
[1,2,3]+[4,5,6] = [1,2,3,4,5,6]
#Only list can join
“*”
[1,2,’hello’]*2 = [1,2,’hello’,1,2,’hello’]

In
10 in [1,10,30,40] = True

BY:- PUNEET MOHAN


List Function
L=[12,45,54,65,89]
L2=[12,45,54,65,89]

To find length
len(L)
To find max Built-in Functions
max(L)
To find min
min(L)

BY:- PUNEET MOHAN


To add element at last
L.append(“Himanshu”)
To count objects appearance
L.count(12)
To append content of sequence
L.extend(L1)
To return first objects index
L.index(45)
To insert object at index
L.insert(2,”Ram”)
To Remove last object
L.pop()
L.pop(2) # To delete 2nd element
BY:- PUNEET MOHAN
To remove first instance of the object
L.remove(45)
To reverse list
L.reverse()
To sort list in ASC/DESC
L.sort() / L.sort(reverse=True)
To clear list
L.clear()

BY:- PUNEET MOHAN


Programs to practice
•Write a program that reverse an array of integers.
•Write a program that input integer list from user and
print it.
•Write a program that inputs two lists and creates a third
that contains all elements of the first followed by all
elements of the second.
•Write a program create a new list(from a old list of
strings) that consists of those strings with their first
character removed.
•Write a program to enter a list containing number 1 to 12
then replace all of the entries in the list greater than 5
with 50.
•Write a program to count even ,odd, positive and
negative numbers in a list. BY:- PUNEET MOHAN
# Program entre a list from user
# creating an empty list
lst = []

# number of elemetns as input


n = int(input("Enter number of elements : "))

# iterating till the range


for i in range(0, n):
ele = int(input())
lst.append(ele) # adding the element
print(lst)
BY:- PUNEET MOHAN
#Create a new list(from a old list of strings)
that consists of those strings with their first
characters removed.

L=["hello","bye","chirag"]
#Creating empty list M[]
M=[]
for i in L:
M.append(i[1:])
#New list
print(M)
BY:- PUNEET MOHAN
TUPLE
Sequence of immutable objects comma
separated values enclosed in parenthesis.
Each item can be individually accessed
using its index.
Valid T=3, / T=(3,)
T=(‘H’,1,”hell”,65.8,’@’)
0 1 2 3 4
Ex-
H 1 “hell” 65.8 “@”
-5 -4 -3 -2 -1
BY:- PUNEET MOHAN
Tuple Accessing
T=(“English“,45,’maths’,123,’ok’)
print(T[0])
print(T[3])
print(T) Seq[start:stop:step]
print(T[-1]) T[0:5:1]
print(T[2:-2])
print(T[1: ])
BY:- PUNEET MOHAN
Reverse Tuple
print(T[-1:-6:-1])
print(T[-1: :-1])

Tuple by using ‘in’ operator


T=(“English“,45,’maths’,123,’ok’)
for d in T:
print(d)

BY:- PUNEET MOHAN


Tuple using range()
T=(“English“,45,’maths’,123,’ok’)
for d in range (0,5) :
print(T[d])

T=(“English“,45,’maths’,123,’ok’)
for d in range(-1,-4,-1):
print(T[d])

BY:- PUNEET MOHAN


Basic Tuple Operations
“+”
(1,2,3)+(4,5,6) = (1,2,3,4,5,6)
#Only tuple can join
“*”
(1,2,’hello’)*2 = (1,2,’hello’,1,2,’hello’)

In
10 in (1,10,30,40) = True

BY:- PUNEET MOHAN


Tuple Functions
T=(12,45,54,65,89)
T2=(12,45,54,65,89)
To find length To return first objects index
len(T) T.index(45)
To find max
max(T)
To find min
min(T)
To count objects appearance
T.count(12)
BY:- PUNEET MOHAN
Unpacking Tuple
T=(12,45,54,65,89)
A,B,C,D,E = T
Print(A)
Print(B)
Print(C)
Print(D)
Print(E)

BY:- PUNEET MOHAN


Programs to practice
•Write a program that reverse an array of integers.
•Write a program that input integer tuple from user and
print it.
•Write a program that inputs two tuple and creates a third
that contains all elements of the first followed by all
elements of the second.
•Write a program enter five numbers and print its power
till 4..
•Write a program to find second maximum value in tuple.
•Write a program to count even ,odd, positive and
negative numbers in a Tuple.
•Write a program to count numbers end with 5 or 0.
BY:- PUNEET MOHAN
# Program entre a Tuple from user
# creating an empty list and tuple
lst = []
Tup=()
# number of elemetns as input
n = int(input("Enter number of elements : "))
# iterating till the range
for i in range(0, n):
ele = int(input())
lst.append(ele) # adding the element
print(lst)
Tup=Tuple(lst) #converting list to tuple
Print(Tup) BY:- PUNEET MOHAN
#Program to enter five numbers and print
its power till 4.

for n in range(5):
x=int(input("Enter number"))
T=(x, x**2, x**3, x**4)
print(x ,"raised to power 1,2,3,4 :=",T)

BY:- PUNEET MOHAN


#Program to find second maximum value

T=(23,34,45,56,68,89)
maxval=max(T)
Len=len(T)
secmax=0
for a in range(Len):
if secmax < T[a] < maxval: #68<89<89
secmax=T[a] #68
print("Second maximum value ",secmax)

BY:- PUNEET MOHAN


Dictionary
Dictionary are Mutable, unordered
collections with elements in the form of a
Key : value pairs . Each element can be
individually accessed using its Key.

D={Key: value}

Immutable Mutable
Note- Dictionaries are also called Associative arrays,
mappings or hashes. BY:- PUNEET MOHAN
Characteristics of a Dictionary
➢Unordered set
➢Not a Sequence
➢Indexed by Keys, Not numbers
➢Keys must be unique
➢Mutable
➢Internally stored as Mappings.

BY:- PUNEET MOHAN


EX-
D={ “Eno”: 1,
“Teachs”:”Computer Science”,
”Name”:”Puneet Mohan”,
“Salary”:100000 }

Key
EnO Teaches Name Salary

1 Computer Puneet Mohan 100000


Science

Value
BY:- PUNEET MOHAN
Dictionary Accessing
D={ “Eno”: 1, “Teachs”:”Computer Science”,
”Name”:”Puneet Mohan”, “Salary”:100000 }

Print(D[“Eno”])
Print(D)
Print(“Employee No =”, D[“Eno”])
Print(“Emp Name =”, D[“Name”])

Dictionary operation that takes a key and finds the


corresponding value is called lookup. BY:- PUNEET MOHAN
Dictionary Traversing
for <item> in <Dict>:
process each item
Ex-
D={ “Eno”: 1, “Teachs”:”Computer Science”,
”Name”:”Puneet Mohan”, “Salary”:10000 }
for k in D:
print(K, ”:”, D[k])
BY:- PUNEET MOHAN
Dictionary Functions
D={ “Eno”: 1, “Teachs”:”Computer Science”,
”Name”:”Puneet Mohan”, “Salary”:100000 }

To find length
len(D)
To get keys
D.keys()
To get values
D.values()
To delete all elements
D.clear()
BY:- PUNEET MOHAN
D={ “Eno”: 1, “Teachs”:”Computer Science”,
”Name”:”Puneet Mohan”, “Salary”:100000 }
D2={ “Eno”: 2, “Teachs”:”Science”,
”Name”:”Suneet”, “Salary”:200000 }

To Add or Update dictionary


D.update(D2) #update add or replace values
To get value with key
D.get (“Eno”) /
D.get (“Eno”, ”Key not present”)
To delete specific key
del D[‘Eno’] /
D.pop(‘Eno’) / D.pop(‘Eno’,”Not found”)
BY:- PUNEET MOHAN
To get items as a sequence of(key,value)
D.items()
Ex-
D={ “Eno”: 1, “Teachs”:”Computer Science”,
”Name”:”Puneet Mohan”, “Salary”:100000 }
MyL = D.items()
for x in MyL:
print(x)

BY:- PUNEET MOHAN


Creating Dictionary
Initializing a dictionary
D={ “Eno”: 1, “Teachs”:”Computer Science”,
”Name”:”Puneet Mohan”, “Salary”:100000 }

Adding key:value pairs to a dictionary


Emp={ } or
Emp=dict ()
Emp[“Name”]=‘john’
Emp[‘salary’]=10000
Emp[‘age’]=24
BY:- PUNEET MOHAN
Creating a dictionary from name and values
D=dict (“Eno”= 1, “Teachs”=”Computer Science”,
”Name”=”Puneet Mohan”, “Salary”=100000 )

D=dict ([[“Eno”, 1], [“Teachs”, ”Computer Science”],


[”Name”=”Puneet Mohan”],[“Salary”=100000]])

By using Zip function


D=dict (zip((“Eno”, “Teachs”,”Name” ,“Salary”),
(1,”Comp Sci”, ”Puneet”,10000)))

BY:- PUNEET MOHAN


Checking existence of a key in dictionary
in , not in
D={ “Eno”: 1, “Teachs”:”Computer Science”,
”Name”:”Puneet Mohan”, “Salary”:100000 }
>>>“Name” in D >”100000” in D.values()
(True) (True)
>>>”Mobile” not in D
(True)

Pretty Printing(by using dumps())


Import json
Print(json.dumps(D,indent=2))
BY:- PUNEET MOHAN
Counting frequency of elements in a
list using Dictionary

❑Create a empty dictionary


❑Take up an element from the list
❑Check if this element exists as a key in the
dictionary:(If not then add)

BY:- PUNEET MOHAN


# program to count the frequency of a list-
elements using a dictionary.
Import json
Sen="This is a super idea This\
idea will change the idea of learning"
words=Sen.split()
d={}
for one in words:
key=one
if key not in d:
count=words.count(key)
d[key]=count
print("Counting frequency in list\n", words)
print(json.dumps(d, indent=1)) BY:- PUNEET MOHAN
SORTING
To arrange data in ascending or descending
order.
•Bubble sort
•Insertion Sort

BY:- PUNEET MOHAN


BUBBLE SORT

BY:- PUNEET MOHAN


BUBBLE SORT
l=[ ]
for j in range(5):
k=int(input("Enter element in Array"))
l.append(k)
N=len(l)
for I in range(n):
for m in range(n-1):
if(l[m]>l[m+1]):
Print(“Data sorted”)
temp=l[m]
For I in range(n):
l[m]=l[m+1]
print(i)
l[m+1]=temp

BY:- PUNEET MOHAN


BY:- PUNEET MOHAN
INSERTION SORT
l=[ ]
for j in range(5):
k=int(input("Enter element in Array"))
l.append(k)
N=len(l)
for i in range(1,n):
for m in range(i,0,-1):
if(l[m]<l[m-1]):
Print(“Data sorted”)
temp=l[m]
For i in range(n):
l[m]=l[m-1]
print(l[i])
l[m-1]=temp

BY:- PUNEET MOHAN


Part Two
To be Continue…….

Legal Warning: -
Unauthorized reproduction, retaining
and use of this material, process and
pattern shall be liable to legal action.
BY:- PUNEET MOHAN

You might also like