Python Pii

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 12

1

# Integer data type

num1 = 42

# Float data type

num2 = 3.14

# Boolean data type

is_true = True

# String data type

message = "Hello, world!"

# List data type

fruits = ["apple", "banana", "orange"]

# Tuple data type

coordinates = (50.0, 100.0)

# Dictionary data type

person = {"name": "Alice", "age": 25}

# Set data type

numbers = {1, 2, 3, 4, 5}
# Print the values and their types

print("num1:", num1, type(num1))

print("num2:", num2, type(num2))

print("is_true:", is_true, type(is_true))

print("message:", message, type(message))

print("fruits:", fruits, type(fruits))

print("coordinates:", coordinates, type(coordinates))

print("person:", person, type(person))

print("numbers:", numbers, type(numbers))

#Demonstration of list

list1=[123,456,'abc','xyz']

list2=[12,67,45,23,90,11,2]

print("list1[3]:",list1[3])

print("List count of list2(67):",list2.count(67))

print("Length of list2:",len(list2))

list1.insert(1,100)

print("Insert in list1:",list1)
print("Min value in list2:",min(list2))

print("Min value in list2:",max(list2))

list1.pop(1)

print("Pop out list1:",list1)

list2.sort()

print("Sort list2:",list2)

list2.reverse()

print("Reverse list2:",list2)

list2.append(100)

print("Append to list2:",list2)

print("Slice of list2:",list2[1:3])

////////////////////////////////

#Demonstration of Tuple

vowels=('a','e','i','o','i','u')

index=vowels.index('e')

print('The index of e:',index)

index=vowels.index('i')

print('The index of i:',index)


testtuple1=(1,2,3)

print(testtuple1,'length is',len(testtuple1))

testtuple2=('abc','xyz','lmn')

print(testtuple2,'length is',len(testtuple2))

num=(1,3,2,8,5,10,6)

print('Maximum is:',max(num))

print('Minimum is:',min(num))

tuple=('aaa','bbb','ccc')

print('Maximum is:',max(tuple))

print('Minimum is:',min(tuple))

my_tuple=('hello',123,12.25)

print(type(my_tuple))

var_int=100

print(type(var_int))

var2=100.25

print(type(var2))

var3='hello world'

print(type(var3))

tuple=('first',123456,12.25,'##3%%')

print(tuple[0])

print(tuple[1])

print(tuple[2])

print(tuple[3])

//////////////////////

#Demonstration of Dictionary
dict={'name':'Akash','age':23}

print(dict)

print("Before updating age")

print(dict['age'])

dict['age']=22

print("After updating age")

print(dict)

result=dict.popitem()

print("After deleting age")

print(dict)

print("poped item=",result)

dict.clear()

print(dict)

////////////////////////////

#Demonstartion of math functions

import math

a=float(input("Enter a value for a:"))

b=float(input("Enter a value for b:"))

print("1--> Square Root")

print("2--> Power")

print("3--> Ceil")

print("4--> Floor")

print("5--> Absolute")

print("6--> Factorial")
print("7--> Mod")

print("8--> Hypotenuse")

print("9--> Log")

print("10--> Log1p")

print("11--> Sin")

print("12--> Cos")

print("13--> Tan")

print("14--> PI value")

print("15--> Exponential Value")

print("16--> Sinh")

print("17--> Cosh")

print("18--> Tanh")

n=int(input("Enter a value for n:"))

if n==1:

print("Square root of a=",math.sqrt(a))

print("Square root of b=",math.sqrt(b))

elif n==2:

print("Power =",math.pow(a,b))

elif n==3:

print("Ceil of a=",math.ceil(a))

print("Ceil of b=",math.ceil(b))

elif n==4:

print("Floor of a=",math.floor(a))

print("Floor of b=",math.floor(b))

elif n==5:
print("Absolute value of a=",math.fabs(a))

print("Absolute value of b=",math.fabs(b))

elif n==6:

print("Factorial of a=",math.factorial(a))

print("Factorial of b=",math.factorial(b))

elif n==7:

print("Modulus=",math.fmod(a,b))

elif n==8:

print("Hypoteneus=",math.hypot(a,b))

elif n==9:

print("Log of a=",math.log(a))

print("Log of b=",math.log(b))

elif n==10:

print("Log1p of a=",math.log1p(a))

print("Log1p of b=",math.log1p(b))

elif n==11:

print("sin(a)=",math.sin(a))

print("sin(b)=",math.sin(b))

elif n==12:

print("cos(a)=",math.cos(a))

print("cos(b)=",math.cos(b))

elif n==13:

print("tan(a)=",math.tan(a))

print("tan(b)=",math.tan(b))

elif n==14:
print("Value of pi=",math.pi)

elif n==15:

print("Value of e=",math.e)

elif n==16:

print("sinh(a)=",math.sinh(a))

print("sinh(b)=",math.sinh(b))

elif n==17:

print("cosh(a)=",math.cosh(a))

print("cosh(b)=",math.coh(b))

elif n==18:

print("tanh(a)=",math.tanh(a))

print("tanh(b)=",math.tanh(b))

else:

print("Invalid entry")

////////////////////////

def get_numbers_from_user():

numbers_input = input("Enter a list of numbers separated by spaces: ")

numbers = [float(num) for num in numbers_input.split()]

return numbers

def calculate_mean(numbers):

return sum(numbers) / len(numbers)

def calculate_mean_deviation(numbers):
mean = calculate_mean(numbers)

deviations = [abs(num - mean) for num in numbers]

return sum(deviations) / len(deviations)

def main():

numbers = get_numbers_from_user()

mean_deviation = calculate_mean_deviation(numbers)

print("The mean deviation of the numbers is:", mean_deviation)

if __name__ == "__main__":

main()

////////////////////

import math

def get_numbers_from_user():

n = int(input("Enter the number of data points: "))

numbers = []

for i in range(n):

num = float(input("Enter data point {}: ".format(i+1)))

numbers.append(num)

return numbers

def calculate_mean(numbers):

return sum(numbers) / len(numbers)


def calculate_variance(numbers, mean):

return sum([(num - mean) ** 2 for num in numbers]) / len(numbers)

def calculate_standard_deviation(variance):

return math.sqrt(variance)

def main():

numbers = get_numbers_from_user()

mean = calculate_mean(numbers)

variance = calculate_variance(numbers, mean)

std_deviation = calculate_standard_deviation(variance)

print("The standard deviation of the numbers is:", std_deviation)

if __name__ == "__main__":

main()

//////////////////////////

import numpy as np

def rank_data(values):

"""Assign ranks to data, handling ties appropriately."""

sorted_values = sorted(list(set(values)), reverse=True)

ranks = [sorted_values.index(x) + 1 for x in values]

return ranks

def calculate_spearman_rank_correlation(x, y):


"""Calculate Spearman's rank correlation coefficient for two lists of ranks."""

n = len(x)

if len(x) != len(y):

raise ValueError("Lists x and y must have the same length.")

rank_x = rank_data(x)

rank_y = rank_data(y)

d_squared = [(rx - ry) ** 2 for rx, ry in zip(rank_x, rank_y)]

return 1 - (6 * sum(d_squared)) / (n * (n**2 - 1))

# Read n values from the keyboard

n = int(input("Enter the number of value pairs: "))

x_values = []

y_values = []

for i in range(n):

pair = input(f"Enter pair {i+1} (separated by a space): ").split()

x_values.append(float(pair[0]))

y_values.append(float(pair[1]))

# Calculate and print the Spearman's rank correlation coefficient

spearman_rank_correlation = calculate_spearman_rank_correlation(x_values, y_values)

print(f"Spearman's rank correlation coefficient: {spearman_rank_correlation:.2f}")


def interpret_spearman_rho(rho):

"""Interpret the Spearman's rank correlation coefficient."""

if rho == 1:

return "There is a perfect positive rank correlation."

elif rho == -1:

return "There is a perfect negative rank correlation."

elif rho == 0:

return "There is no rank correlation."

elif rho > 0:

return "There is a positive rank correlation."

else:

return "There is a negative rank correlation."

# Example usage:

rho_value = calculate_spearman_rank_correlation(x_values, y_values)

interpretation = interpret_spearman_rho(rho_value)

print(f"Spearman's rank correlation coefficient (rho): {rho_value:.2f}")

print(interpretation)

You might also like