Find us on Facebook

Thursday, December 13, 2018

CALENDAR



Aim: Implement a Python program to display calendar of given month of the year
Program:
# import module
import calendar
# ask of month and year
yy = int(input("Enter year: "))
mm = int(input("Enter month: "))
# display the calendar
print(calendar.month(yy,mm))
Output:
Enter year: 2014
Enter month: 11
  November 2014
Mo Tu We Th Fr Sa Su
               1  2
3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30.

Continue Reading…

DECIMAL TO BINARY,OCTAL & HEXA DECIMAL



Aim: Implement a Python program to convert decimal number into binary, octal and hexadecimal number system
Program:
# Python program to convert decimal number into binary, octal and hexadecimal number system
# Take decimal number from user
dec = int(input("Enter an integer: "))
print("The decimal value of",dec,"is:")
print(bin(dec),"in binary.")
print(oct(dec),"in octal.")
print(hex(dec),"in hexadecimal.")
Output:
Enter an integer: 344
The decimal value of 344 is:
0b101011000 in binary.
0o530 in octal.
0x158 in hexadecimal


Continue Reading…

REMOVE PUNCTUATION



Aim: Implement a Python program to remove to all punctuation from the string provided by the user

Program:
# Program
# define punctuation
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
# take input from the user
my_str = input("Enter a string: ")
# remove punctuation from the string
no_punct = ""
for char in my_str:
   if char not in punctuations:
       no_punct = no_punct + char
# display the unpunctuated string
print(no_punct)
Output:
Enter a string: "Hello!!!", he said ---and went.
Hello he said and went



Continue Reading…

HCF



Aim: Implement a Python program to find the H.C.F of two input numbers.
Program:
# define a function
def hcf(x, y):
   # choose the smaller number
   if x > y:
       smaller = y
   else:
       smaller = x
   for i in range(1,smaller + 1):
       if((x % i == 0) and (y % i == 0)):
           hcf = i
   return
# take input from the user
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("The H.C.F. of", num1,"and", num2,"is", hcf(num1, num2))
Output:
Enter first number: 54
Enter second number: 24
The H.C.F. of 54 and 24 is 6




Continue Reading…

File handling



Aim: Implement a Program to implement the concept of files
Algorithm:
1.Start
2.Open a file in write mode
3.Save some data.
4.Display the content in the file
6.Stop
Program:
 colors = ['red\n', 'yellow\n', 'blue\n']
f = open('colors.txt', 'w')
f.writelines(colors)
f.close()
Output:
Red
Yellow
blue







Continue Reading…

Pickling



Aim: Implement a Program to implement the concept of pickling.
Algorithm:
1.Start
2.Create a dictionary
3.Save a dictionary into a pickle file.
4. Load the dictionary back from the pickle file.
5.Dispaly the dictionary.
6.Stop
Program:
 # Save a dictionary into a pickle file.
    import pickle
    favorite_color = { "lion": "yellow", "kitty": "red" }
     pickle.dump( favorite_color, open( "save.p", "wb" ) )
# Load the dictionary back from the pickle file.
    import pickle
    favorite_color = pickle.load( open( "save.p", "rb" ) )
print favorite_color
    # favorite_color is now { "lion": "yellow", "kitty": "red" }
Output: 

{ "lion": "yellow", "kitty": "red" }



Continue Reading…

TUPLES


Aim: Implement a Program to implement the concept of tuples.
Algorithm:
1.Start
2.Create a Zero-element tuple tuple a=()
3. Create a One-element tuple b = ("one",)
4. Create a Two-element tuple c = ("one", "two")
5.Print tuples and length.
6.Stop

Program:
# Zero-element tuple.
a = ()
# One-element tuple.
b = ("one",)
# Two-element tuple.
c = ("one", "two")
 
print(a)
print(len(a))
 
print(b)
print(len(b))
 
print(c)
print(len(c))
 
Output: 
()
0
('one',)
1
('one', 'two')

2
Continue Reading…

Dictionary


                              
Aim: Implement a Program to implement a dictionary comprehension.
Algorithm:
1.Start
2. Create a dictionary
3.Update existing entry
4.Add a new entry.
5.Print the updates.
6.Stop

Program:
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
dict['Age'] = 8       # update existing entry
dict['School'] = "DPS School"            # Add new entry
print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']
Output:
dict['Age']:  8
dict['School']:  DPS School

Continue Reading…

LIST and SLICING


:                                  
Aim: Implement a Program to implement list and slicing operations.
Algorithm:
1.Start
2.Define a list
3. Slice from third index to index one from last
4.stop
Program:
values = [100, 200, 300, 400, 500]
# Slice from third index to index one from last.
slice = values[2:-1]
print(slice)
 
Output:
[200, 300]


Continue Reading…

Set operations



Aim: Implement a Program to implement set operations
Algorithm:
1.Start
2.Enter 2 sets E,N
3.Perform set union
4.Perform set intersection
5.Perform set difference and symmetric difference
6.Print results
7.Stop

Program:
# define two sets
E = {0, 2, 4, 6, 8};
N = {1, 2, 3, 4, 5};
# set union
print("Union of E and N is",E | N)
# set intersection
print("Intersection of E and N is",E & N)
# set difference
print("Difference of E and N is",E - N)
# set symmetric difference
print("Symmetric difference of E and N is",E ^ N)
Output:
Union of E and N is {0, 1, 2, 3, 4, 5, 6, 8}
Intersection of E and N is {2, 4}
Difference of E and N is {8, 0, 6}
Symmetric difference of E and N is {0, 1, 3, 5, 6, 8}

Continue Reading…

Number of each vowel in a string



Aim: Implement a Program to count the number of each vowel in a string
Algorithm:
1.Start
2.Enter the string s.
3.Take each character in the string and compare with ‘a,e,i,o,u’.
4.If found increment corresponding counter by one.
5.Print the counter values.
6.Stop
Program:
# string of vowels
vowels = 'aeiou'
# take input from the user
ip_str = input("Enter a string: ")
# make it suitable for caseless comparisions
ip_str = ip_str.casefold()
# make a dictionary with each vowel a key and value 0
count = {}.fromkeys(vowels,0)
# count the vowels
for char in ip_str:
   if char in count:
       count[char] += 1
print(count)
Output:
Enter a string: Hello, have you tried our turorial section yet?
{'e': 5, 'u': 3, 'o': 5, 'a': 2, 'i': 3}4)


Continue Reading…

Lexicographic sorting


  
Aim: Implement a program to illustrate how words can be sorted lexicographically (alphabetic order).
Algorithm:
1.      Start.
2.      Read the string .
3.      Breakdown the string into a list of words using split() function.
4.      Sort the list using sort() function.
5.      Display the sorted words.
6.      Stop.
Program:
# take input from the user
my_str = input("Enter a string: ")
# breakdown the string into a list of words
words = my_str.split()
# sort the list
words.sort()
# display the sorted words
for word in words:
   print(word)
Output:
Enter a string: Hello this Is an Example With cased letters
Example
Hello
Is
With
an
cased
letters
this

Continue Reading…

String Palindrome


Aim: Implement a program to check if a string is palindrome or not
Algorithm:
1.      Input a String
2.      Make it suitable for caseless comparison using casefold() function
3.      Reverse the string
4.      Check if the string is equal to its reverse then go to 5 else go to 6
5.      Print string Is a Palindrome go to step 7
6.      Print string Is Not a Palindrome
7.      Stop
Program:
# Program to check if a string  is palindrome or not
# take input from the user
my_str = input("Enter a string: ")
# make it suitable for caseless comparison
my_str = my_str.casefold()
# reverse the string
rev_str = reversed(my_str)
# check if the string is equal to its reverse
if list(my_str) == list(rev_str):
   print("It is palindrome")
else:
   print("It is not palindrome")
Output :
Enter a string: malayalam
It is palindrome
Continue Reading…

Factorial of a number



Aim: Implement a program to find the factorial of a number using recursion
Algorithm:
1.Start
2.Enter he number ‘n’
3.call function factorial(n)
4.print result
5.Stop
Function factorial()
1.Start
2.if n=1 return n
3.if not return n*factorial(n-1)
4.Stop
Program:
# Python program to find the factorial of a number using recursion
def recur_factorial(n):
   """Function to return the factorial
   of a number using recursion"""
   if n == 1:
       return n
   else:
       return n*recur_factorial(n-1)
# take input from the user
num = int(input("Enter a number: "))
# check is the number is negative
if num < 0:
   print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
   print("The factorial of 0 is 1")
else:
   print("The factorial of",num,"is",recur_factorial(num))
Output :
Enter a number: 7
The factorial of 7 is 5040

Continue Reading…

Python Program to Add Two arrays using function



Aim: Implement a program to add two matrix
Algorithm:
1.Start
2.Enter The first matrix
3.Enter the second matrix
3.call function Add(m1,m2)
4.print result
5.Stop

Function add(m1,m2)

1.Start
2.sum= append(m1[i][j]+m2[i][j])      
4.Stop
Program:
def add(m1,m2):        
            result=[]          
            for i in range(0,n):                  
                        sum1=[]                      
                        for j in range(0,m):                             
                                    result.append(m1[i][j]+m2[i][j])                     
                                    sum1.append(result)   
            return sum1


n=input("enter matrix limit")
m=input("enter matrix limit")
a=[]
print "first matrix"
for i in range(0,n):      
            b=[]    
            for i in range(0,m):
                        b.append(input())       
                        a.append(b)

print "second matrix"
c=[]
            for i in range(0,n):      
                        d=[]    
                        for i in range(0,m):                 
                                    d.append(input())       
                                    c.append(d)
print add(a,c)

            Output :
enter matrix limit:2
enter matrix limit:2
first matrix:2 3 4 5
second matrix:4 5 6 7
6 8
10 12

Continue Reading…

Popular Posts

Text Widget

Search This Blog

Powered by Blogger.

Blogger Pages

Like Canvas?

Sponsor

Footer Widgetized Areas

About Canvas

About Canvas

Subscribe Us

About

Text Widget

Tags

Total Pageviews

print pdf

cal

Most Popular

    TUPLES
 LIST and SLICING
      ASCII VALUE
Copyright © KTU Btech Cse Python Lab Manuals | Powered by Blogger
Design by Saeed Salam | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates