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

Python Assignment

python assignements

Uploaded by

Ravirajan P
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Assignment

python assignements

Uploaded by

Ravirajan P
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 5

MODEL 3 - ASSIGNMENT 1

Problem Statement:
You work in XYZ Corporation as a Data Analyst. Your corporation has told you to
work with statistics.

import pandas as pd
import numpy as np
import os

df = pd.read_csv("Data.csv")
print(df)

u = np.array(df['x'])
v = np.array(df['y'])
print("Mean of column X: ",np.mean(u))
print("Mean of column Y: ",np.mean(v))

print("Mode: ")
print(df.mode())

print("Standard Deviationof column Y: ",round(np.std(v),2))

print("Range of column X: ",np.min(u),"to",np.max(u))


print("Range of column Y: ",np.min(v),"to",np.max(v))
# Importing the NumPy library with an alias 'np'
import numpy as np

# Defining a Python list 'a' containing integers


a = [1, 2, 3, 4]

# Printing the original array 'a'


print("Original array")
print(a)

# Converting the array 'a' to a NumPy array of type float using asfarray()
x = np.asfarray(a)

# Printing the array 'x' after converting to a float type


print("Array converted to a float type:")
print(x)

model – 4 assignment 1

1. Create a list named ‘myList’ that has the following elements: 10, 20, 30,
‘apple’, True, 8.10:
a. Now in the ‘myList’, append these values: 30, 40
b. After that, reverse the elements of the ‘myList’ and store that in
‘reversedList’

myList=[10,20,30,'apple',True,8,10]

myList.append(30)
myList.append(40)

reversedList=myList.reverse()
print(reversedList)

Create another tuple named numeric_tuple consisting of only integer


values 10, 20, 30, 40, 50:
a. Find the minimum value from the numeric_tuple
b. Concatenate my_tuple with numeric_tuple and store the result in r1
c. Duplicate the tuple named my_tuple 2 times and store that in ‘newdupli’

from array import *


import numpy as np

#4
numeric_tuple = (10, 20, 30, 40, 50)
my_tuple = (30, 60, 50)
#4a
min = numeric_tuple[0]
for i in numeric_tuple:
if min>= i:
min = i
print('4a: ', min)
#4b
r1 = my_tuple + numeric_tuple
print('4b: ', r1)
#4c
newdupli = my_tuple * 2
print('4c: ', newdupli)
#6
a = array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print('6: ', a[-4::-3])
#5
print('5: ', len(a))
#7
print('7: ')
n = np.array((), dtype=int)
for i in range(4):
num = input(str(i+1)+'>')
n = np.append(n, int(num))
print(n)
print(n.astype(float))

Assignment 2- Numpy

1.Create a 3x3 matrix array with values ranging from 2 to 10

# Importing the NumPy library with an alias 'np'


import numpy as np

# Creating a NumPy array 'x' using arange() from 2 to 11 and reshaping it into a
3x3 matrix
x = np.arange(2, 11).reshape(3, 3)

# Printing the resulting 3x3 matrix 'x'


print(x)

Output

[ 2 3 4]
[ 5 6 7]
[ 8 9 10]]

2.Create a NumPy array having user input values and convert the integer
type to the float type of the elements of the array. For instance: Original
array [1, 2, 3, 4] Array converted to a float type: [ 1. 2. 3. 4.]

# Importing the NumPy library with an alias 'np'


import numpy as np

# Defining a Python list 'a' containing integers


a = [1, 2, 3, 4]
# Printing the original array 'a'
print("Original array")
print(a)

# Converting the array 'a' to a NumPy array of type float using asfarray()
x = np.asfarray(a)

# Printing the array 'x' after converting to a float type


print("Array converted to a float type:")
print(x)

Output:

Original array
[1, 2, 3, 4]
Array converted to a float type:
[ 1. 2. 3. 4.]

3. Write a NumPy program to append values to the end of an array. For


instance: Original array: [10, 20, 30] . After that, append values to the end
of the array: [10 20 30 40 50 60 70 80 90]

# Importing the NumPy library with an alias 'np'

import numpy as np

# Creating a Python list


x = [10, 20, 30]

# Printing a message indicating the original array


print("Original array:")

# Printing the original array


print(x)

# Appending values to the end of the array using np.append() and assigning the
result back to 'x'
x = np.append(x, [[40, 50, 60], [70, 80, 90]])

# Printing a message indicating the array after appending values


print("After append values to the end of the array:")

# Printing the array after appending values


print(x)

Output:

Original array:
[10, 20, 30]
After append values to the end of the array:
[10 20 30 40 50 60 70 80 90]
4. create two NumPy arrays and add the elements of both the arrays and
store the result in sumArray

import numpy as np

array1 = np.array([3, 14, 15, 92])


array2 = np.array([2, 18, 28, 18])

sumArray = array1 + array2

print('sumArray =', sumArray)

5. Create a 3x3 array having values from 10-90 (interval of 10) and store that
in array1

import numpy as np
x = [[30*y + 10*x for x in range(1, 4)] for y in range(3)]
x = np.squeeze(np.asarray(x))
print(x[:1])
print(x[2][2])

You might also like