COMPUTER SCIENCE PYTHON PRACTICAL PROGRAMS

XI CBSE Computer Science Lab Programs

COMPUTER SCIENCE PYTHON PRACTICAL PROGRAMS

1. Program to obtain length and breadth of a rectangle and calculate its area. Solution. #to input length and breadth of a rectangle and calculate its area length = float( input("Enter length of the rectangle : ")) breadth= float( input (" Enter breadth of the rectangle: ")) area=length * breadth print ("Rectangle specifications ") print ("Length = ", length) print ("breadth ", breadth) print ("Area = ", area)

2. Write a program in python to display even and odd numbers from 1 to N. Solution. num=int(input("Enter the Range:")) for i in range(1,num+1): if i%2==0: print(i, "is even number") else: print(i,"is odd number")

3. Write a program in python to all print prime number from 1 to n. Solution. num=int(input("Enter the number:")) for val in range(1,num + 1): # If num is divisible by any number # between 2 and num, it is not prime

Prof. K. Adisesha

Page 1

XI CBSE Computer Science Lab Programs

if num > 1: for n in range(2, val): if (val % n) == 0: break else: print(val)

4. Write Python script to print the following for n numbers pattern: 1 1 3 1 3 5 1 3 5 7

Solution. num=int(input("Enter the number:")) for a in range (2, num, 2): for b in range (1, a, 2): print (b, end =(' ')) print ()

5. Write a program to find sum of the series: S=1+ X + X2 +X3+....Xn Solution. x = int ( input ( "Enter value of X :" ) ) n= int (input( "Enter value of n (for x ** y) :" )) s=0 for a in range(n): s += x ** a print ("Sum of first" , x,'^',a , "terms :", s)

Prof. K. Adisesha

Page 2

XI CBSE Computer Science Lab Programs

6. Write a program to check a number whether it is palindrome or not.

num=int(input("Enter a number : ")) n=num res=0 while num>0:

rem=num%10 res=rem+res*10 num=num//10 if res==n: print(n,": Number is Palindrome") else: print(n, ": Number is not Palindrome")

7. Write a program to test if a string is palindrome or not. SOURCE CODE: string = input("Please enter your own String : ") if(string == string[:: - 1]): print(string, ": is a Palindrome String") else: print(string, ": is Not a Palindrome String")

8. Write a program to calculate Simple and compound interest.

p=float(input("Enter the principal amount : ")) r=float(input("Enter the rate of interest : ")) t=float(input("Enter the time in years : ")) SI=(p*t*r)/100 x=(1+r/100)**t CI= p*x-p print("Simple interest is : ", round(SI,2)) print("Compound interest is : ", round(CI,2))

Prof. K. Adisesha

Page 3

XI CBSE Computer Science Lab Programs

9. Write a program to input a character and to print whether a given character is an alphabet, digit or any other character.

SOURCE CODE: ch=input("Enter a character: ") if ch.isalpha(): print(ch, "is an alphabet") elif ch.isdigit(): print(ch, "is a digit") elif ch.isalnum(): print(ch, "is alphabet and numeric") else: print(ch, "is a special symbol")

10.Program to count frequency of a given element in a list of numbers

SOURCE CODE: Lst=eval(input ( " Enter list :")) length=len(Lst) element=int(input( " Enter element :")) count=0 for i in range(0, length): if element==Lst [i]: count += 1 if count == 0: print (element, "not found in given list") else: print(element, "has frequency as", count, "in given list")

Prof. K. Adisesha

Page 4

XI CBSE Computer Science Lab Programs

11.Program to create a dictionary containing names of competition winner students as key number of their wins as values

SOURCE CODE: n=int(input("How many students? ")) winners ={ } for a in range(n): key=input("Name of the student : ") value=int(input ("Number of competitions won : ")) winners [key]=value print ("The dictionary now is : ") print (winners)

12.Write a program to calculate the factorial of an integer using recursion. SOURCE CODE:

def factorial(n): if n == 1: return n else: return n*factorial(n-1)

num=int(input("enter the number: ")) 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 ", factorial(num))

Prof. K. Adisesha

Page 5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download