



































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 43
This page cannot be seen from the preview
Don't miss anything!




































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
Enter the number n STOP Print (It is not a prime) k 2 to n-1; Rem n mod k If (Rem = = 0)
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
Fact Fact*K If K = n K1, 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
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
K ≤ n
Print ‘F[1] to F[n]’
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
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
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
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: term1; Step6: Sum1; 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
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.
n=
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:
e power x value is = 2.
e power x value is = 146.
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:
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]
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])
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