Tuesday, 16 July 2019

12th Prog-6 Write a Python function sin(x, n) to calculate the value of sin(x) using its Taylor series expansion up to n terms. Compare the values of sin(x) for different values of n with the correct value

Algorithm:
Input:
1. x   2. n(number of terms
output: 
sin(x,n)
Method
1. Take in the value of x in degrees and the number of terms and store it in separate variables.
2. Pass these values to the sine function as arguments.
3. Define a sine function and using a for loop, first convert degrees to radians.
4. Then use the sine formula expansion and add each term to the sum variable.
5. Then print the final sum of the expansion.
6. Exit.

#Program
import math
def sin(x,n):
    sine = 0
    for i in range(n):
        sign = (-1)**i
        pi=22/7
        y=x*(pi/180)
        sine = sine + ((y**(2.0*i+1))/math.factorial(2*i+1))*sign
    return sine
x=int(input("Enter the value of x in degrees:"))
n=int(input("Enter the number of terms:"))
print(round(sin(x,n),2))

Explanation
1. User must enter the value of x in degrees and the number of terms and store it in separate variables.
2. These values are passed to the sine functions as arguments.
3. A sine function is defined and a for loop is used convert degrees to radians and find the value of each term using the sine expansion formula.
4. Each term is added the sum variable.
5. This continues till the number of terms is equal to the number given by the user.
6. The total sum is printed.

Output
1. Enter the value of x in degrees:30
Enter the number of terms:10
0.5
2. Enter the value of x in degrees:45
Enter the number of terms:15
0.71

C

Monday, 15 July 2019

12th CS Prog-5 Read a file line by line and print it.


File Handling

The write() Method
It writes the contents to the file in the form of
string. It does not return value. Due to buffering,
the string may not actually show up in the file
until the flush() or close() method is called.

The read() Method
It reads the entire file and returns it contents in
the form of a string. Reads at most size bytes or
less if end of file occurs.if size not mentioned
then read the entire file contents.

# write() ,read() Method based program

f = open("cs.txt", 'w')
line1 = 'Welcome to 12th Class Python."
f.write(line1)

line2="\nPython is a general purpose and high level programming language"
f.write(line2)

line3 = "\nReadable and Maintainable Code"
f.write(line3)

line4="\nMultiple Paradigms"
f.write(line4)

line5 = "\nRobust Standard Library"
f.write(line5)

line6="\nSimplify Complex Software Development"
f.write(line6)
f.close()


f = open("cs.txt", 'r')
text = f.read()
print(text)
f.close()

OUTPUT
Welcome to 12th Class Python.
Python is a general purpose and high level programming language
Readable and Maintainable Code
Multiple Paradigms
Robust Standard Library
Simplify Complex Software Development

Thursday, 11 July 2019

12th Prog-4 Write a recursive Python program to test if a string is a palindrome or not.

def is_palindrome(s):
    if len(s) < 1:
        return True
    else:
        if s[0] == s[-1]:
            return is_palindrome(s[1:-1])
        else:
            return False
a=str(input("Enter string:"))
if(is_palindrome(a)==True):
    print("String is a palindrome!")
else:
    print("String isn't a palindrome!")



Output

Enter string:121
String is a palindrome!

Enter string:45
String isn't a palindrome!

12th Prog-3 Write a recursive code to find the sum of all elements of a list in a python.

def sum_arr(arr,size):
   if (size == 0):
     return 0
   else:
     return arr[size-1] + sum_arr(arr,size-1)

#main program
n=int(input("Enter the number of elements for list:"))
a=[]
for i in range(0,n):
    element=int(input("Enter element:"))
    a.append(element)
print("The list is:")
print(a)
print("Sum of items in list:")
b=sum_arr(a,n)
print(b)


Output
Enter the number of elements for list:6
Enter element:2
Enter element:3
Enter element:5
Enter element:7
Enter element:9
Enter element:10
The list is:
[2, 3, 5, 7, 9, 10]
Sum of items in list:
36

Monday, 8 July 2019

12th-Prog-02 Write a Python recursive code to compute the nth Fibonacci number.

#Write a Python recursive code to compute the nth Fibonacci number.:  video Click here

def fib(n1,n2,n):
    if n==0:
        return
    res=n1+n2
    print(res)
    fib(n2,res,n-1)
a=0
b=1
n=int(input("How many numbers to be printed: "))
print(a)
print(b)
fib(a,b,n-2)

Output
How many numbers to be printed: 5
0
1
1
2
3

12th class Python Program list 2019-20

  1. Recursively find the factorial of a natural number. Click Here
  2. Read a file line by line and print it. Click Here
  3. Remove all the lines that contain the character `a’ in a file and write it to another file. Click Here
  4. Write a Python function sin(x, n) to calculate the value of sin(x) using its Taylor series expansion up to n terms. Compare the values of sin(x) for different values of n with the correct value: Click Here
  5. Write a random number generator that generates random numbers between 1 and 6 (simulates a dice).Click Here
  6. Write a recursive code to find the sum of all elements of a list. Click Here
  7. Write a recursive code to compute the nth Fibonacci number. Click Here
  8. Write a Python program to implement a stack using a list data-structure. Click Here
  9. Write a recursive Python program to test if a string is a palindrome or not. Click Here
  10. Write a Python program to plot the function y = x2 using the pyplot or matplotlib libraries. Click Here
  11. Create a graphical application that accepts user inputs, performs some operation on them, and then writes the output on the screen. For example, write a small calculator. Use the tkinter library. Click Here
  12. Open a webpage using the urllib library Click Here
  13. Compute EMIs for a loan using the numpy or scipy libraries. click Here
  14. Take a sample of 10 phishing e-mails and find the most common words. Click Here
  15. Write a Python program to implement a queue using a list data-structure.                  Click Here

5.2. Data Management: SQL and web-server
  1. Integrate SQL with Python by importing the MySQL module : Click Here
  2. Find the min, max, sum, and average of the marks in a student marks table. Click Here
  3. Find the total number of customers from each country in the table (customer ID, customer name, country) using group by. Click Here
  4. Write a SQL query to order the (student ID, marks) table in descending order of the marks.Click Here
  5. Write a Django based web server to parse a user request (POST), and write it to a CSV file: Click Here
5.3. Additional Programs

    1. Write a Python program with function to count the number of lines in a text file ‘ 'STORY.TXT’ which is starting with an alphabet ‘A’. Click here

     2. Write a Python program with method/function DISPLAYWORDS() to read lines from a text file STORY.TXT, and display those words, which are less than 4 characters.Click Here

    3. Write a Recursive Python program with function BinarySearch(Arr,l,R,X) to search the given   element X to be searched from the List Arr having R elements where L represents lower bound and R represents upper bound. Click Here

   4. Mysql and data base programs: Click Here
         Click Here



Friday, 5 July 2019

12th python Chapter-05 Recursion

# Program for Factorial of natural number using recursion

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


#main programme
n=int(input("enter n"))
if n==0:
    print("factorial is 1")
elif n<0:
    print("Sorry factorial does not exist ")
else:
    print("factorial is:",r_fact(n))

Output
enter n 5
('factorial is:', 120)

enter n -3
Sorry factorial does not exist

enter n 0
factorial is 1



#Program for Fibonacci series using recursion:  video Click here

def fib(n1,n2,n):
    if n==0:
        return
    res=n1+n2
    print(res)
    fib(n2,res,n-1)
a=0
b=1
n=int(input("How many numbers to be printed: "))
print(a)
print(b)
fib(a,b,n-2)

Output
How many numbers to be printed: 5
0
1
1
2
3



# Python Program for recursive binary search. 
 
# Returns index of x in arr if present, else -1
def binarySearch (arr, l, r, x):
 
    # Check base case
    if r >= l:
 
        mid = l + (r - l)/2
 
        # If element is present at the middle itself
        if arr[mid] == x:
            return mid
         
        # If element is smaller than mid, then it can only
        # be present in left subarray
        elif arr[mid] > x:
            return binarySearch(arr, l, mid-1, x)
 
        # Else the element can only be present in right subarray
        else:
            return binarySearch(arr, mid+1, r, x)
  else:
        #Element is not present in the array
        return -1
 
# Test array
arr = [ 2, 3, 4, 10, 40 ]
x = 10
 
# Function call
result = binarySearch(arr, 0, len(arr)-1, x)
 
if result != -1:
    print "Element is present at index %d" % result
else:
    print "Element is not present in array"