Python Programming Practicals for Statistics and Applied Statistics - Prof. university, Summaries of Law of Torts

A comprehensive set of python programming exercises and examples related to various statistical concepts and techniques. It covers a wide range of topics, including prime number identification, fibonacci sequence generation, pascal's triangle construction, matrix addition, vowel and consonant counting in strings, central and non-central moment calculations, uniform random number generation, frequency distribution analysis (mean, variance, standard deviation, median), binomial distribution fitting, poisson distribution fitting, negative binomial distribution fitting, and exponential distribution fitting. The document serves as a valuable resource for students and practitioners interested in applying python programming to statistical analysis and problem-solving. It offers a structured approach to learning and practicing python programming in the context of statistics and applied statistics, making it a useful tool for both classroom instruction and self-study.

Typology: Summaries

2022/2023

Uploaded on 02/14/2023

sai-srija-law
sai-srija-law 🇮🇳

1 document

1 / 43

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
I-Semester Statistics / Applied Statistics Guidelines for Python Programming Practicals
Page 1 of 43
Python Programming Practical’s
1. Program to examine the given number is a prime number or not.
2. Program to find the Factorial of positive integer.
3. Program to find the largest among the given three numbers.
4. Program to generate Fibonacci sequence up to given number n.
5. Program for finding the roots of a quadratic equation.
6. Program to construct a Pascal Triangle.
7. Program to find the value of ex , Sin x and Cos x using series expansion
8. Program to find the sum of two matrices Amxp and Bmxp
9. Program to find the product of two matrices Amxp and Bpxr.
10. Program to sort the given set of numbers using bubble sort and finding median.
11. Program with a function that accepts a string and counts the no. of vowels & consonants.
12. Program that opens specified text file and then displays list of all unique words found in the file.
13. Program to find the Median, Mode for the given of array of elements.
14. Program to find the first four Central & Non-central moments to the given array of elements.
15. Program to generate random numbers from Uniform, Binomial, Poisson, Normal, Exponential.
16. Program for preparation of frequency tables and computing mean, median, mode, variance and
standard deviation of the frequency distribution.
17. Program to Fitting of Binomial distribution for the given frequency distribution.
18. Program to Fitting of Poisson distribution for the given frequency distribution.
19. Program to Fitting of Negative Binomial distribution for the given frequency distribution.
20. Program to Fitting of Exponential Distribution for the given frequency distribution.
21. Program for finding the Correlation and regression lines for the given bi-variate data.
22. Solution to simultaneous equations by Gauss - Siedal method (minimum 3 variables)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b

Partial preview of the text

Download Python Programming Practicals for Statistics and Applied Statistics - Prof. university and more Summaries Law of Torts in PDF only on Docsity!

Python Programming Practical’s

  1. Program to examine the given number is a prime number or not.
  2. Program to find the Factorial of positive integer.
  3. Program to find the largest among the given three numbers.
  4. Program to generate Fibonacci sequence up to given number n.
  5. Program for finding the roots of a quadratic equation.
  6. Program to construct a Pascal Triangle.
  7. Program to find the value of ex^ , Sin x and Cos x using series expansion
  8. Program to find the sum of two matrices Amxp and Bmxp
  9. Program to find the product of two matrices Amxp and Bpxr.
  10. Program to sort the given set of numbers using bubble sort and finding median.
  11. Program with a function that accepts a string and counts the no. of vowels & consonants.
  12. Program that opens specified text file and then displays list of all unique words found in the file.
  13. Program to find the Median, Mode for the given of array of elements.
  14. Program to find the first four Central & Non-central moments to the given array of elements.
  15. Program to generate random numbers from Uniform, Binomial, Poisson, Normal, Exponential.
  16. Program for preparation of frequency tables and computing mean, median, mode, variance and standard deviation of the frequency distribution. 1 7. Program to Fitting of Binomial distribution for the given frequency distribution.
  17. Program to Fitting of Poisson distribution for the given frequency distribution.
  18. Program to Fitting of Negative Binomial distribution for the given frequency distribution.
  19. Program to Fitting of Exponential Distribution for the given frequency distribution.
  20. Program for finding the Correlation and regression lines for the given bi-variate data.
  21. Solution to simultaneous equations by Gauss - Siedal method (minimum 3 variables)

1. CHECKING GIVEN NUMBER IS PRIME OR NOT

Definition : The given number ‘n’ is said to be a prime number if it is divisible with only one and itself. Example: Let n = 11. It is not divisible with any number from 2 to 10 so it is a prime number. Algorithm: Step 1: Start Step 2: Enter the number to find the factorial ‘n’ Step 3: for k 2 to n-1; Step 4: do { Step 5: Rem  n mod k Step 6: if (Rem ==0) then print(‘It is not a prime number’)and goto step Step 7: } Step 8: Print (It is a prime number’) Step 9: Stop. Flow Chart if k=n No Print( ‘N is prime number’) yes

START

Enter the number n STOP Print (It is not a prime) k  2 to n-1; Rem  n mod k If (Rem = = 0)

2. FACTORIAL OF A GIVEN NUMBER N

Definition : The factorial of a given number ‘n’ is defined as the product of the first ‘n’ consecutive numbers. It is recursively defined as n! = n. (n-1)! if n > 1 = 1 if n = 0 Example: Let n = 5 then the factorial of n can be calculated using the above recursive equation as 5! = 5 .4! =5. 4.3! =5.4. 3. 2! = 5.4.3. 2.1! = 5. 4. 3. 2. 1 = Algorithm: Step 1: Start Step 2: Enter the number to find the factorial ‘n’ Step 3: Fact 1; Step 4: for k 1 to n; Step 5: do { Step 6: Fact  Fact * k; Step 7: } Step 8: Print Fact; Step 9: Stop. Flow Chart for Factorial of given number: START Enter ‘n’ value STOP

K  K+

Fact  Fact*K If K = n K1, Fact 1 Print ‘Fact’

# 2. Python Program to find the factorial of given number def factorial(num): if num == 1: return num else: return num * factorial(num - 1) num = int(input("Enter the Number to find the Factorial: ")) if num < 0: print("Factorial cannot be found for negative numbers") elif num == 0: print("Factorial of 0 is 1") else: print("Factorial of", num, "is: ", factorial(num)) OUTPUT: Enter the Number to find the Factorial: 5 Factorial of 5 is: 120

4. GENERATION OF FIBONACCI SERIES

Definition: The Fibonacci sequence of numbers satisfying the property that the sum of the two consecutive numbers is equal to the next number and the first two numbers are 0 and 1. Mathematically the relation can be expressed as Fj = Fj- 1 + F (^) j- 2 Example: The Fibonacci sequence is J : 0 1 2 3 4 5 6 7 8 9 10 11 12 … … … F[J] : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … … … Algorithm: Step 1: Start; Step 2: Enter the number up to how many Fibonacci numbers to be generated ‘n’ ; Step 3: F[0]  0; Step 4: F[1]  1; Step 5: for j 2 to n Step 6: do { Step 7: F[j]= F[j-1] + F[j-2]; Step 8: Print F[j]; Step 9: } Step10: Stop Flow Chart for Fibonacci Series Generation START Enter ‘n’ value STOP

F[K]  F[K-1] + F[K-2]

K  K+

K ≤ n

F[0]  0

F[1]  1

K 2

Print ‘F[1] to F[n]’

# 4. Python Program to Generate Fibonacci Sequence of n numbers

n=int(input('Enter How many Fibonacci numbers to be generated excluding first two')) a= b= print(a) print(b) for j in range(n): c=a+b print(c) a=b b=c j=j+ else: print('Those are the Generated Fibonacci Sequence Numbers.') OUTPUT: Enter How many Fibonacci numbers to be generated excluding first two 0 1 1 2 3 5 8 13 21 34 55 89 Those are the Generated Fibonacci Sequence Numbers

6. GENERATION OF PASCALS TRIANGLE

Definition: The Pascal's triangle can be constructed by arranging the binomial coefficients nCr in a triangular fashion that each value of n with all possible values of r so that the sum of above two coefficients is equal to the below coefficient. Example: The Pascal's triangle for n=7 is presented below Algorithm: Step1: Start Step2: Enter the height of the Pascal’s triangle n=; Step3: for k  1 to n; Step4: do { Step5: for j  1 to k; Step6: do{ Step Step8 C[k,j]  fact(n) / (fact(k).fact(n-k)); Step9: Print blank space C [k,j] Step10: } Step11: n  n+ Step10: Enter a new line Step12: } Step13: Stop

# 6. Python Program for the construction of Pascal Triangle

n=int(input('Enter number of rows:')) a=[] for i in range(n): a.append([]) a[i].append(1) for j in range(1,i): a[i].append(a[i-1][j-1]+a[i-1][j]) if(n!=0): a[i].append(1) for i in range(n): print(" "*(n-i),end="",sep="") for j in range(0,i+1): print("{0:6}".format(a[i][j]),end="",sep="") print() OUTPUT: Enter number of rows: 4 1 1 1 1 2 1 1 3 3 1

OUTPUT:

  • Enter the value of x in degrees:
  • Enter the number of terms :
  • Enter the value of x in degrees:
  • Enter the number of terms :
  • Enter the value of x in degrees:
  • Enter the number of terms :

7 b. EVALUATION OF COS (X) USING SERIES EXPANSION

Definition: The value of Cosine of X can be evaluated from the series expansion as Cos (X) = 1 - ( X^2 /2!) + (X^4 /4!) - (X^6 /6!) + … Example: The value of Cos 30 is Cos (X) = 1 - ( X^2 /2!) + (X^4 /4!) - (X^6 /6!) + … Algorithm: Step1: Start Step2: Enter the value of ‘X’ Step3: Enter the number of terms sum n= ; Step4: x  (x22)/(7180); Step5: term1; Step6: Sum1; Step7: for j  1 to n Step8: do{ Step9: term  (- term)( xx) /(2k*(2k-1)) Step10: Sum  Sum + term Step11: } Step12: Print Sum Step13: Stop

# 7 b. Python Program to find the Value of Cosine for given angle in degrees.

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

# 7 c. Python program to calculate e power X Using Taylor Series

n=

Enter the value of x in degrees:

sum=1. term=1. for i in range(1,n+1): term=(term*x)/float(i) sum+=term print ("e power x value is =", sum) OUTPUT:

Enter the number of terms :

e power x value is = 2.

Enter the value of x in degrees:

e power x value is = 146.

# 8. Python Program to add the given two Matrices A and B

m=int(input('ENTER THE NO. OF ROWS:')) n=int(input('ENTER THE NO.OF COLUMNS:')) a=[[int(input('ENTER THE ELEMENTS OF A : ')) for j in range(m)] for i in range(n)] b=[[int(input('ENTER THE ELEMENTS OF B : ')) for j in range(m)] for i in range(n)] c=[[0 for j in range(m)] for i in range(n)] for i in range(0,m): for j in range(0,n): c[i][j]= int(a[i][j] + b[i][j]) print('The elements of First matrix A are:') for i in range(m): for j in range(n): print(a[i][j]) print('The elements of Second Matrix B are: ') for i in range(m): for j in range(n): print(b[i][j]), print('The sum of the Two Matrices A and B is') for i in range(0,m): for j in range(0,n): c[i][j]= int(a[i][j] + b[i][j]) print(c[i][j]) OUTPUT:

Enter the number of terms :

ENTER THE NO.OF COLUMNS:

ENTER THE ELEMENTS OF A : 1

ENTER THE ELEMENTS OF A : 2

ENTER THE ELEMENTS OF A : 3

ENTER THE ELEMENTS OF A : 4

ENTER THE ELEMENTS OF B : 5

ENTER THE ELEMENTS OF B : 6

ENTER THE ELEMENTS OF B : 7

ENTER THE ELEMENTS OF B : 8

The elements of First matrix A are: 1 2

Enter number of rows for first matrix: Enter number of columns for first matrix: Enter number of columns for second matrix: Enter elements of First matrix A : 1 2 3 4 Enter elements of Second matrix B : 5 6 7 8 The First matrix A is : 1 2 3 4 [1, 2] [3, 4] The Second matrix B is : 5 6 7 8 [5, 6] [7, 8] The Product of two matrices A and B is : 19 22 43 50 [19, 22] [43, 50]

# 10. Sort the given vector of n elements using Bubble sort and Finding Median

def bubblesort(arr): n=len(arr) for i in range(n): for j in range(0,n-1): if(arr[j]>arr[j+1]): arr[j],arr[j+1]=arr[j+1],arr[j] n=int(input("number of elements")) arr=[int(input("enter elements to be sorted"))for i in range(n)] bubblesort(arr) a=[0 for i in range(n)] print("Sorted array is:") for i in range(n): a[i]=arr[i] print("%d"%a[i])

Finding Median

if(n%2!=0): median=a[((n-1)//2)] elif(n%2==0): median=(a[(n-1)//2]+a[((n-1)//2)+1])/ print('Median : ',median) OUTPUT: number of elements enter elements to be sorted enter elements to be sorted enter elements to be sorted enter elements to be sorted enter elements to be sorted Sorted array is: 8 15 29 32 56 Median 29