0% found this document useful (0 votes)
67 views

Statistics and Analytics - Unit 4 - Python Experiments 1

This document provides Python programs and examples for various statistical and analytical tasks. It includes programs to add integers and strings, find the sum of natural numbers, determine if a number is odd or even, calculate variance and standard deviation of data, display student marks from a text file, create bar and pie charts using Matplotlib. The programs demonstrate how to perform basic statistical calculations and data visualization in Python.

Uploaded by

Siddu Uttur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Statistics and Analytics - Unit 4 - Python Experiments 1

This document provides Python programs and examples for various statistical and analytical tasks. It includes programs to add integers and strings, find the sum of natural numbers, determine if a number is odd or even, calculate variance and standard deviation of data, display student marks from a text file, create bar and pie charts using Matplotlib. The programs demonstrate how to perform basic statistical calculations and data visualization in Python.

Uploaded by

Siddu Uttur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Course Title: STATISTICS AND ANALYTICS UNIT-4 Experiments 20 to 26

20. Write a python program to add 2 integers and 2 strings and print the result.

a) Program to add 2 integers:

Example 1:

# Program to add two integer numbers


a=10
b=200

c=a+b

print("SUM=",c)

Example 2:
# Program to add two integer numbers with user input
x = input ("Type a number: ")
y = input ("Type another number: ")

sum = int(x) + int(y)

print ("The sum is: ", sum)

b) Program to add 2 strings:

Example 1:

# Program to add two strings numbers


s1 = "Elect"
s2 = "ronics"

s3 = s1 + s2

print(s3)

Example 2:
# Program to add two strings with user input
s1 = input ("Type first string: ")
s2 = input ("Type second string: ")

s3 = s1 + s2

print (s3)

harsha.snmp@gmail.com | harshasnmp.wordpress.com Page 1 of 3


Course Title: STATISTICS AND ANALYTICS UNIT-4 Experiments 20 to 26

21. Write a python program to find the sum of first 10 natural numbers.

# Program to find sum of first 10 natural numbers


n=10
sum = n * (n+1) / 2
print(sum)

22. Write a python program to find whether the number is odd or even.

# Program to find whether the number is odd or even


num = 13
if ( num % 2 ) == 0:
print(num, "is even number")
else:
print(num, "is odd number")

23. Write a python program to find the variance and standard deviation for the
given data..

# Program to find variance and standard deviation for the given data
# Importing the NumPy module
import numpy as np

data=[4, 8, 6, 5, 3, 2, 8, 9, 2, 5]

# Calculating variance using var()


print("Variance=",np.var(data))

#Calculating deviation using var()


print("Standard deviation=",np.std(data))

24. Write a python program to display student marks from the record (text file).
# Program to display student marks from the record (text file)
# import linecache module
import linecache

# define the name of the file to read from


filename = 'data.txt'

# define line_number
line_number = 3

harsha.snmp@gmail.com | harshasnmp.wordpress.com Page 2 of 3


Course Title: STATISTICS AND ANALYTICS UNIT-4 Experiments 20 to 26

# retrieve specific line


line = linecache.getline(filename, line_number)

print (line)

25. Write a python program to create a labeled bar graph using matplotlib. pyplot.
# Program to create a labeled bar graph using matplotlib.pyplot
import matplotlib.pyplot as plt

Student = ['RAMESH', 'GEETHA', 'SURESH', 'MANOJ', 'VENKAT']


Percentage = [45, 72, 82, 49, 47]

plt.bar(Student, Percentage)
plt.title('Student Vs Percentage')
plt.xlabel('Student Names')
plt.ylabel('Percentage')
plt.show()

26. Write a python program to create a labeled pie chart using matplotlib. pyplot.

# Program to create a labeled pie chart using matplotlib.pyplot


# Import libraries
from matplotlib import pyplot as plt
import numpy as np

# Creating dataset
cars = ['AUDI', 'BMW', 'FORD', 'TESLA', 'JAGUAR', 'MERCEDES']

data = [23, 17, 35, 29, 12, 41]

# Creating plot
plt.pie(data, labels=cars, autopct='%.2f%%')
plt.title('Sales of cars')

# show plot
plt.show()

harsha.snmp@gmail.com | harshasnmp.wordpress.com Page 3 of 3

You might also like