23it211 Introduction To Python Programming
23it211 Introduction To Python Programming
An Autonomous Institution | Approved by AICTE | Affiliated to Anna University | Accredited by NAAC with A++ Grade |
Kuniamuthur, Coimbatore – 641008
I YEAR / II SEMESTER
SUBMITTED BY
Name : _____________________________
Class : ____________
SRI KRISHNA COLLEGE OF ENGINEERING AND TECHNOLOGY
An Autonomous Institution | Approved by AICTE | Affiliated to Anna University | Accredited by NAAC with A++ Grade |
Kuniamuthur, Coimbatore – 641008
BONAFIDE CERTIFICATE
Name : _____________________________
Class : ____________
7. Multiply matrices
AIM:
To write a program to calculate the GCD for two number.
ALGORITHM:
Step 1: Start
Step 3: To get input for two variables (a and b), we have use input function.
Step 4: By using if and else condition we can get the GCD value for the two
variables.
START
GET A,B
false
IF GCD=A%B
(B==0)
true
DISPLAY A DISPLAY
GCD
STOP
PROGRAM:
a=int(input("Enter the number A="));
b=int(input("Enter the number B="));
if (b==0):
print("GCD is:",a)
else:
gcd=a%b;
print("GCD is:",gcd)
OUTPUT:
RESULT:
Hence the GCD value of the two numbers is obtained through the above program.
RESULT:
Hence the Gain value is obtained through the above program.
SUB FUNCTIONS:
Step 3 : To get the input for three variables(a ,b and c),we have to use input
function.
Step 5 : By using if and else condition we can get the largest number among the
three numbers.
Step 7 : When this condition is true, we can that a is largest among the three
numbers.
Step 9 : When this condition is true, we can that b is largest among the three
numbers.
Step 11: when above two conditions if and elif is false it we can that c is largest
among the three numbers.
Step 12: It will display the output for the given program.
START
GET A,B,C
false
IF(A>=B)
AND
(A>=C)
true false
ELIF
DISPLAY A (B>=C)
true
DISPLAY B
DISPLAY C
STOP
PROGRAM:
OUTPUT:
RESULT:
Hence the largest value among the three numbers is obtained through the above program.
B. PRINT THE PATTERN:
AIM:
To write a program to print the pattern in python.
ALGORITHM:
Step 1 : Start
Step 2 : For printing the pattern we can use while loop and for loop to get the
desired output.
Step 4 : In while loop we have to declare the range of i it is less than or equal to
6,which means it will go up to five rows.
Step 5 : By using for loop for j we can initialize as 1 and its range goes up to i .
Step 6 : To display the output in star show we have given the star symbol in
double quote.
Step 7 : By using end function we can tell loop will stop that and continue next
iteration.
Step 8 : We have to increment the i value so that we can use i+=1 it means
i=i+1.
START
I=1
WHILE false
(I<=6):
true
FOR J IN
RANGE
(1,I):
true
PRINT(“*”,END=’’)
PRINT(END=’\N’)
I+=1
STOP
PROGRAM:
i=1
while(i<=6):
for j in range(1,i):
print("*",end=' ')
print(end='\n')
i+=1
OUTPUT:
RESULT:
Hence the print the pattern is obtained through the above program.
C. FIBONACCI SERIES
AIM:
To write a program to print the Fibonacci series in python.
ALGORITHM:
Step 1 : Start
Step 2 : At first, we have to how much value needed for the Fibonacci series
Step 3 : For getting input we have to use input function; we can declare that
variable as nterms.
Step 5 : We have to declare count value as 0 because it should start from 0 and
then it increments.
Step 6 : By using while and if ,elif and else condition we can get the Fibonacci
series.
Step 7 : In if condition we can declare that nterms is less than or equal to zero
,the Fibonacci series will not obtain because it will go in negative.
Step 8 : In elif condition we can declare that nterms equal to 1 and then it will
print the n1 term which is zero.
Step 9 : When above two conditions if and elif is false, then it will come to else
part.
Step 10 : By using while condition we can tell count less than nterms because
of this only it exists till the limit we said in input function.
Step 11 : we can print n1 and then we have to use the calculation as nth=n1+n2
and by using swapping n1=n2 and n2=nth.
Step 12 : Finally, we have to increment count value as count+=1.
Step 13: It will display the output for the given program.
Step 14: Stop
FLOWCHART:
START
GET NTERMS
N1=0
N2=1
COUNT=0
IF ELIF
(NTERMS<
false false
(NTERMS=
=0) =0)
true true
WHILE
(count < false
nterms)
true
DISPLAY
N1
nth=n1+n2
n1=n2
n2=nth
count+=1
STOP
PROGRAM:
nterms=int(input("Enter the no of terms needed: "))
n1,n2=0,1
count= 0
if nterms<=0:
print("Please enter a positive integer")
elif nterms==1:
print("Fibonacci sequence upto",nterms,":",n1)
else:
print("Fibonacci sequence:")
while count<nterms:
print(n1)
nth=n1+n2
n1=n2
n2=nth
count+=1
OUTPUT:
RESULT:
Hence the Fibonacci series is obtained through the above program
EX.NO: 02
FIND A SQUARE ROOT OF THE NUMBER
DATE: (NEWTON’S METHOD)
AIM:
To write a program to find the square root of the number using newton’s method.
ALGORITHM:
Step 1: Start
Step 3: We have declared a function name the function as “newton method”, in that
we have to give an argument as number and loop value is equal to 100.
Step 5: Using for loop we have to use the formula ( number = 0.5 * (number+a
/number)
Step 6: After this calculation it will display the square root of a number.
Step 7: Stop
FLOWCHART:
START
A=FLOAT(NUMBER)
FOR IN IN
RANGE(100)
NUMBER=0.5*(NUMBER+A/
NUMBER)
RETURN NUMBER
GET
NUMBER
DISPLAY
NEWTON_METHOD(NUMBER)
STOP
PROGRAM:
def newton_method(number,loop=500):
a = float(number);
for i in range(loop):
number = 0.5*(number+a/number)
return number
number=int(input("Enter the number :"));
print ("The square root value of",number,"is",newton_method(number));
OUTPUT:
RESULT:
Hence the square root of the number by the newton method obtained through the above program.
Perfect number
Write a python program to get a number from the user and find out whether the given number is a
perfect number or not
Explantion
A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the
number itself
input - 6
divisors - 1,2,3
sum - 6
If sum of divisors and original number are equal, it is a perfect number
PROGRAM:
num=int(input())
sum_v=0
for i in range(1,num):
if (num%i==0):
sum_v=sum_v+i
if(sum_v==num):
print("Perfect number")
else:
print("Not a perfect number")
OUTPUT:
RESULT:
Hence the number is perfect number or not a perfect number is obtained through the above program.
SUB PROGRAM 1:
AIM:
A) Write a python program to do basic calculator functions.
ALGORITHM:
Step 1: Start
Step 2: We have to get the two inputs for the variable a and b.
Step 3: After getting input ,we have to tell what are the operation are to be done and
there are five choices (1,2,3,4,5)
Step 4: And we have to get the input as choice, using if elif and else we can do the
operation
Step 5: In if condition, when they press 1 it will do the addition operation as a+b.
Step 6: In elif condition, when they press 2 it will do the subtraction operation as a-b.
Step 7: In elif condition, when they press 3 it will do the multiplication operation as
axb.
Step 8: In elif condition, when they press 4 it will do the division operation as a/b.
Step 9: In elif condition, when they press 5 it will do the modulus operation as a%b.
Step 10: In else condition, when they press some other number ,it will display
invalid number
START
GET A,B
GET CHOICE
IF true DISPLAY
(CHOICE
A+B
==1)
false
ELIF true
DISPLAY
(CHOIC
A-B
E==2)
false
true
ELIF DISPLAY
(CHOICE= A*B
=3)
false
false
true
ELIF DISPLAY
(CHOICE A%B
==5)
STOP
PROGRAM:
a=int(input("Enter the first number : "));
b=int(input("Enter the second number : "));
print("1.Addition");
print("2.Subtraction");
print("3.Multiplication");
print("4.Division");
print("5.Modulus");
choice=int(input("\nEnter the operation need to do: "));
if(choice==1):
c=a+b;
print("Addition of",a,"and",b,"is",c);
elif(choice==2):
c=a-b;
print("Subtraction of",a,"and",b,"is",c);
elif(choice==3):
c=a*b;
print("Multiplication of",a,"and",b,"is",c);
elif(choice==4):
c=a/b;
print("Division of",a,"and",b,"is",c);
elif(choice==5):
c=a%b;
print("Modulus of",a,"and",b,"is",c);
else:
print("Invalid number")
OUTPUT:
RESULT:
Hence the basic calculator functions are obtained through the above program.
SUB PROGRAM 2:
B) write a python program for the following Problem statement.
Students and Department Given N students name and department, print the X students
belonging to a specific department D.
Sample Input/Output:
Input:
5
Arun EEE
Bhuvana ECE
Ganesh MECH
Pavithra ECE
Rohit CSE
ECE
Output:
Bhuvana
Pavithra
AIM:
To write a python program to the students and department and to separate according to the
specific department.
ALGORITHM:
Step 1: Start
Step 2: We have to get the input for the number of student information needed.
Step 4: Using for loop we have to append the value to the empty list one by one ,in
the format of name and then department.
Step 5: After appending, we need to know which specific department student name to
be shown in output.
Step 7: Using for loop we have to separate the name and department into two list
Step 8: Using if condition, whether m=department when it’s true it will print the
students name or the iteration will be continued.
Step 9: Stop
FLOWCHART:
START
GET N
A=[ ]
FOR I IN
RANGE
(N)
GET M
A.APPEND()
FOR I IN
RANGE
(N)
IF M==D:
X,D=A[I].SPLIT()
DISPLAY X
STOP
PROGRAM:
OUTPUT:
RESULT:
Hence the students and department and they separate according to the specific department,
this was obtained through the above program.
EX.NO: 03
EXPONENTIATION
DATE: (POWER OF A NUMBER)
AIM:
To write a program to find exponentiation.
ALGORITHM:
Step 1: Start
Step 2: We have to get the input for the base and exponent.
Step 4: Using while loop we can repeat the same function again and again.
Step 5: Inside the loop we can tell answer*=base and then exponent will
be decremented.
Step 7: End.
FLOWCHART:
START
GET BASE
GET
EXPONENT
ANSWER=1
FOR I IN false
RANGE
(0,3):
true
ANSWER*=BASE DISPLAY
ANSWER
EXPONENT-=1
STOP
PROGRAM:
OUTPUT:
RESULT:
Hence the exponentiation obtained through the above program.
SUB PROGRAM 1:
A) Write a python program to do the following problem statements, write a python
program which makes use of function to display all such numbers which are divisible
by 7 but are not a multiple of 5, between 1000 and 2000.
AIM:
To write a python program to which makes use of function to display all such numbers which
are divisible by 7 but are not a multiple of 5, between 1000 and 2000.
ALGORITHM:
Step 1: Start
Step 3: Using for loop we can keep the range as 1000 to 2000.
Step 4: Using If condition we can tell the condition i%7 is equal to zero and
i%5 is not equal to zero.
Step 6: End
FLOWCHART:
START
I=1000
false FOR I IN
RANGE
(1000,2000):
true
true
IF (I%7==0 DISPLAY I
AND
I%5!=0):
false
STOP
PROGRAM:
i=1000
for i in range (1000,2000):
if(i%7==0 and i%5!=0):
print(i)
OUTPUT:
RESULT:
Hence the number which divisible by 7 but are not a multiple of 5, between 1000 and 2000
are obtained in the reverse order through the above program.
EX.NO: 04
FIND THE MAXIMUM IN A LIST OF NUMBERS
DATE:
AIM:
To write a program to find the maximum of a list of numbers.
ALGORITHM:
Step 1: Start
Step 4: Using for loop we know there are three number in the list ,so we tell
it starts in 0 and end in 3
Step 6: If the condition is true then maximum will be equal to the list
element. If it’s false it will move on to the next iteration.
Step 8: End
FLOWCHART:
START
LIST1=[2,52,6]
FOR I IN
RANGE
(0,3):
LIST1[i]=MAXIMU
IF LIST1 >
MAXIMUM M
:
I+=1
DISPLAY
MAXIMUM
STOP
PROGRAM:
list1= [2,52,6]
maximum=0
for i in range (0,3):
if list1[i]>maximum:
maximum=list1[i]
i+=1
print("Largest element is:", maximum)
OUTPUT:
RESULT:
Hence the to find the maximum of a list of numbers obtained through the above program.
SUB PROGRAM 1:
AIM:
To write a python program to display the elements of a list in reverse order.
ALGORITHM:
Step 1: Start
Step 3: Using the built-in function (.reverse) we can reverse the list1
Step 7: End
FLOWCHART:
START
LIST1=[10,20,30,40,50,60]
DEF
REVERSE(LIST1)
LIST1.REVERSE()
RETURN LIST1
DISPLAY
REVERSE(LIST1)
STOP
PROGRAM:
List1=[10,20,30,40,50,60]
def Reverse(list1):
list1.reverse()
return list1
print(Reverse(list1))
OUTPUT:
RESULT:
Hence the elements in the list are obtained in the reverse order through the above program.
EX.NO: 05
LINEAR SEARCH AND BINARY SEARCH
DATE:
AIM:
A) Write a python code to check the element is in the list or not by using Linear
Search method.
ALGORITHM:
Step 1: Start
Step 3: Using for loop we can keep the range as length of array.
Step 5: When the if condition is true, it will return the i value, if the
condition is false, it will return -1.
Step 6: We have to declare the array with value and it will display on the
output screen.
Step 7: We have to get the input which we need to find using input function.
Using the linear function, the output will be displayed on the screen
Step 8: End
FLOWCHART:
START
false
FOR I IN
RANGE RETURN -1
true (LEN(ARR)):
true IF
ARR[I]==X
RETURN I
ARR=[124,48,88,96,220,78,10,500]
DISPLAY ARR
GET X
DISPLAY
STR(LINEAR(ARR,
X))
STOP
PROGRAM:
def linear(arr,x):
for i in range(len(arr)):
if arr[i]==x:
return i
return -1
arr=[124,48,88,96,220,78,10,500]
print(arr)
x=int(input("Enter the number which one need to find: "))
print("The element is ",x," it index value is",str(linear(arr,x)))
OUTPUT:
RESULT:
Hence the python code to check the element is in the list or not by using Linear Search
method obtained through the above program.
Swapping elements
Write a python code to accept a list of elements from the user and perform the Swapping of
adjacent elements
PROGRAM:
n=int(input())
seat=[]
ele=input()
seat=ele.split()
i=0i=0
while(i<n-1):
t=seat[i]
seat[i]=seat[i+1]
seat[i+1]=t
i=i+2
for i in range(0,n):
print(seat[i],end=" ")
OUTPUT:
RESULT:
Hence the python code for swapping of the adjacent element is obtained through the above
program.
B. BINARY SEARCH
AIM:
B) Write a python program to check the element is in the list or not, by binary search
ALGORITHM:
Step 1: Start
Step 7: Using if else condition we find the index value for the element.
START
DEF
LINEAR_SEARCH(LIST1,N):
LOW=0
HIGH=LEN(LIST1)-1
MID=0
true
WHILE MID=(HIGH+LOW)//2
LOW<=HIGH:
false true
IF LOW=MID+1
LIST1[MID]<N:
false
true
ELIF HIGH=MID-1
LIST1[MID]>N:
false
RETURN MID
RETURN -1
RESULT=BINARY_SEARCH(LIST1,N):
IF RESULT!=-1:
true
DISPLAY
STR(RESULT) STOP
PROGRAM:
def binary_search(list1, n):
low = 0 ,high = len(list1) – 1,mid = 0
while low <= high:
mid = (high + low) // 2
if list1[mid] < n:
low = mid + 1
elif list1[mid] > n:
high = mid - 1
else:
return mid
return -1
list1 = [12, 24, 32, 39, 45, 50, 54];print(list1)
n=int(input("Enter the element need to be searched:"))
result = binary_search(list1, n)
if result != -1:
print("Element is present at index", str(result))
OUTPUT:
RESULT:
Hence the program to check the element is in the list or not, by binary search elements is
obtained through the above program.
EX.NO: 06
FIRST N PRIME NUMBERS
DATE:
AIM:
To write a python code to implement first N Prime Numbers using function.
ALGORITHM:
Step 1: Start
Step 4: Using if condition inside the for loop with a condition of number>1
Step 5: Again, we have to declare the for loop inside the if condition the for
loop range starts from 2 to the number
Step 6: Again, inside the for loop we have to declare the if condition of
number%i==0 and have to use break statement.
Step 7: In else part, the prime number will be displayed on the screen.
Step 9: we have to call the function prime(n) the output will be displayed on
the screen.
START
DEF PRIME(N):
true
FOR NUMBER IF
IN NUMBER>1:
RANGE(1,N+1):
FOR NUMBER IN
false
RANGE(2,NUMBE
R):
IF
NUMBER%1==0:
DISPLAY
NUMBER
BREAK
GET N
PRIME(N)
STOP
PROGRAM:
def prime(n):
for number in range(1,n+1):
if number>1:
for i in range(2,number):
if (number%i)==0:
break
else:
print(number)
n=int(input("Enter the first N prime numbers :"))
prime(n)
OUTPUT:
RESULT:
Hence the python code to implement first n prime numbers using function is obtained through
the above program.
EX.NO: 07
MULTIPLY MATRICES
DATE:
AIM:
To write a python code to implement the matrix multiplication using nested list and loops.
ALGORITHM:
Step 1: Start
Step 4: Inside the function we have to declare the for loop for i of range of
length of A matrix.
Step 5: Again, we have to declare the for loop for j in the range of length of
B[0] matrix.
Step 6: Again, we have to declare the for loop for k in the range of length of
B matrix
Step 8: Using for loop we can able to print the result, by the print function.
Step 10: We have to call the function multiply(A,B) the output will be
displayed on the screen
START
RESULT=[[0,0,0],[0,0,0],[0,0,0]]
DEF MULTIPLY(A,B):
false
FOR I IN
RANGE(LEN(A)
):
true
FOR J IN
RANGE(LEN(B[0])
):
true
FOR K IN
RANGE(LEN(B))
:
RESULT[I][J]+=A[I][K]*B[K][J]
FOR R IN DISPLAY R
RESULT:
DISPLAY A=[[1,2,3],[4,5,6],[7,8,9]]
RESULT B=[[1,2,3],[4,5,6],[7,8,9]]
MULTIPLY(A,B)
STOP
PROGRAM:
result=[[0,0,0],[0,0,0],[0,0,0]]
def Multiply(A,B):
for i in range(len(A)):
for j in range(len(B[0])):
for k in range(len(B)):
result[i][j]+=A[i][k]*B[k][j]
for r in result:
print(r)
return 0
A=[[1,2,3],[4,5,6],[7,8,9]]
B=[[1,2,3],[4,5,6],[7,8,9]]
print("Result:")
Multiply(A,B)
OUTPUT:
RESULT:
Hence the python code to implement the matrix multiplication using lists and loops is
obtained through the above program.
Dictionary
A vegetable vendor wishes to store the vegetables she sells in her shop along with
the price/kg. Write a python script to
achieve this.
She initially stored 5 Vegetables along with its price/kg. To this add a new vegetable with its
price and also print the cost of given
vegetable - print ‘Zero’ if not available. Finally update the cost of given vegetable.
Assume while updating the cost of given vegetable, the given vegetable is already included.
Refer Input format and Sample Input section for better understanding
Input Format
First 5 lines contains vegetable names (string) and its price/kg (float) separated by comma
Next line contains new vegetable name and its price separated by comma
Next line contains the name of the vegetable whose price needs to be displayed. If the given
vegetable is not available print 'Zero'
Next line contains vegetable name and its new price to be updated separated by comma.
Note:
Vegetable names are of data type string
Price/kg are of data type oat
Output Format
First line of the output consists of price of the given vegetable.
Second line of the output consists of updated price of the given vegetable name
PROGRAM:
#Create a dictionary with vegeatble details along with the name_price
veg_dict = {}
#Get 5 vegetable details
for i in range(0,5):
name_price = input().split(',')
#User input
veg_dict[name_price[0]] = float(name_price[1])
#To add new vegetables
new_veg = input().split(',')
#User input
veg_dict[new_veg[0]] = float(new_veg[1])
OUTPUT:
RESULT:
Hence the python code to implement dictionary is called and vegetable and price is founded
obtained through the above program.
EX.NO:08
PROGRAMS THAT TAKE COMMAND LINE
DATE: ARGUMENTS (WORD COUNT).
AIM:
To write a python code to show the number of command line arguments
ALGORITHM:
Step 1: Start
Step 6: Inside the for loop, we have to use print function sys.argv[i] the i
will get increment
Step 9: Inside the for loop, we have to increment the sum, with the form of
sum+=int(sys.argv[i])
START
GET
N=LEN(SYS.ARGV)
DISPLAY N
DISPLAY SYS.ARGV[0]
false
FOR I IN SUM=0
RANGE(1,N):
true
DISPLAY
SYS.ARGV[I]
false
FOR I IN
RANGE(1,N):
true
SUM+=INT(SYS.ARGV[I])
DISPLAY
SUM
STOP
PROGRAM:
Import sys
n=len(sys.argv)
print(“Total arguments passed:”)
print(“name the python script”,sys.argv)
print(“argument passed”)
for i in range(1,n):
print(sys.argv[i],end=””)
sum=0
for i in range(1,n):
sum+=int(sys.argv[i])
print(“Result”,sum)
OUTPUT:
RESULT:
Hence the python code to show the number of command line arguments is obtained through
the above program.
Reverse a sentence word by word
Write a program to reverse a sentence word by word. Use Python class to achieve the result.
Class name: python_string
method name: reverse_words
PROGRAM:
class python_string:
def __init__(self,inp_string):
self.s = inp_string
def reverse_words(self):
return ' '.join(reversed(s.split()))
s= input()
#object
a = python_string(s)
print(a.reverse_words())
OUTPUT:
RESULT:
Hence the python code to show reverse string is obtained through the above program.
EX.NO: 09 EXTRACT THE FUNCTIONALITY OF BOOK CLASS IN
LIBRARY CLASS
DATE:29.11.2021
AIM:
To write a Python program that demonstrates the functionality of a Book class within a
Library class.
ALGORITHM:
Step 1: Start
Step 2: Define a Book class with attributes like title, author, and isbn.
Step 3: Add methods in the Book class to display book details and set book
details..
Step 8: Display all the books in the library using the display_books method.
End
FLOWCHART:
PROGRAM:
# Book class
class Book:
def __init__(self, title, author, isbn):
self.title = title
self.author = author
self.isbn = isbn
def display_details(self):
print(f"Title: {self.title}, Author: {self.author}, ISBN: {self.isbn}")
# Library class
class Library:
def __init__(self):
self.books = []
def add_book(self, book):
self.books.append(book)
print(f"Book '{book.title}' added to the library.")
def display_books(self):
if not self.books:
print("No books in the library.")
else:
print("Books in the library:")
for book in self.books:
book.display_details()
def search_book(self, search_term):
found_books = [book for book in self.books if book.title == search_term or
book.isbn == search_term]
if found_books:
print("Search results:")
for book in found_books:
book.display_details()
else:
print("No books found with the given search term.")
# Main program
library = Library()
# Adding books to the library
book1 = Book("The Great Gatsby", "F. Scott Fitzgerald", "12345")
book2 = Book("1984", "George Orwell", "67890")
library.add_book(book1)
library.add_book(book2)
# Displaying all books in the library
library.display_books()
# Searching for a book
search_term = input("Enter the title or ISBN of the book to search: ")
library.search_book(search_term)
OUTPUT:
RESULT:
Hence, the functionality of the Book class is successfully extracted within the Library class,
demonstrating object-oriented programming concepts in Python.
EX.NO: 10
FILE HANDLING
DATE:
AIM:
To write a python code to implementation of file handling in python.
ALGORITHM:
Step 1: Start
Step 2: Using open function we have to open a new file called pp.txt ,x is
used to create the file.
Step 4: Again, we have to open the file pp.txt and we have to write the
python programming inside the function, w is used to write in the file
,f.write() is used after we have to close the function.
Step 5: Again, we have to open the file pp.txt and we have to read the file, r
is used to reading in the file, f.read() is used, after we have to close the
function.
Step 6: Again, we have to open the file pp.txt and we have to append the
file, a is used to appending the string into the file , f.write() is used, after
that we have to read the file using f.read().
Step 7: Using open function we have to create the new file called new.txt, x
is used to create a new file and we have to write the string using f.write()
function.
Step 8: Using for loop we can append the pp.txt file into the new.txt by
using f.write() function.
Step 9: We have to read the new.txt file using f.read() function and all the
output will be displayed on the screen.
START
F=OPEN("PP.TXT",'X')
F.CLOSE()
F=OPEN("PP.TXT","W")
F.WRITE("PYTHON PROGRAMMING")
F.CLOSE()
F=OPEN("PP.TXT","A")
F=OPEN("PP.TXT","R")
F.WRITE(“LAB”)
PRINT(F.READ())
PRINT(F.READ())
F.CLOSE()
F.CLOSE()
F=OPEN("NEW.TXT","W")
F1.WRITE("HELLO EVERYONE TO THE ") F1=OPEN("NEW.TXT",'X')
F1.CLOSE() F1.CLOSE()
F=OPEN("PP.TXT","R")
F1=OPEN("NEW.TXT","A")
false
FOR WORD F1.CLOSE()
IN F:
true
F1.WRITE(WORD)
F=OPEN("NEW.TXT","R")
PRINT(F2.READ())
STOP
PROGRAM:
f=open("pp.txt",'x')
f.close()
f=open("pp.txt","w")
f.write("python programming")
f.close()
print("Reading the file")
f=open("pp.txt","r")
print(f.read())
f.close()
print("\nAppending the text")
f=open("pp.txt","a")
f.write(" lab")
f=open("pp.txt","r")
print(f.read())
f.close()
f1=open("new.txt",'x')
f1.close()
f1=open("new.txt","w")
f1.write("Hello everyone to the ")
f1.close()
f=open("pp.txt","r")
f1=open("new.txt","a")
for word in f:
f1.write(word)
f1.close()
print("\nAdding new file into the old file ")
f2=open("new.txt","r")
print(f2.read())
OUTPUT:
RESULT:
Hence the python code to implementation of file handling in python is obtained through the
above program.
if(s.count("0")>s.count("1")):
print("Lose")
else:
print("Win")
OUTPUT:
RESULT:
Hence the python code to implementation and winner is obtained through the above program.
Problem Statement:
It is IPL season and the first league match of Dhilip’s favorite team, "Chennai Super Kings".
The CSK team is playing at the IPL
after 2 years and like all Dhoni lovers, Dhilip is also eagerly awaiting to see Dhoni back in
action.
After waiting in long queues, Dhilip succeeded in getting the tickets for the big match. On the
ticket, there is a letter code that can
be represented as a string of upper-case Latin letters.
Dhilip believes that the CSK Team will win the match in case exactly two different letters in
the code alternate. Otherwise, he
believes that the team might lose. Please see the note section for the formal definition of
alternating code.
You are given a ticket code. Please determine, whether CSK Team will win the match or not
based on Dhilip’sconviction. Print
"YES" or "NO" (without quotes) corresponding to the situation.
Note:
Two letters x, y where x != y are said to be alternating in a code if the code is of the form
"xyxyxy...".
PROGRAM:
a = input()
len_a = len(a)
x = a[0]
y = a[1]
s=0
if x == y:
s=1
else:
for i in range(len_a):
if i % 2 == 0:
if x != a[i]:
s=1
break
else:
if y != a[i]:
s=1
break
if s == 1:
print("No")
else:
print("Yes")
OUTPUT:
RESULT:
Hence the python code to implementation and win or lose status is obtained through the
above program.