Skip to content

Commit

Permalink
numpy basics
Browse files Browse the repository at this point in the history
  • Loading branch information
amantiwari8861 committed Jan 17, 2024
1 parent 8de311e commit 5178ac6
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 78 deletions.
134 changes: 73 additions & 61 deletions numpy/1.numpy.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,87 @@
# Importing NumPy
import numpy as np

# Creating Arrays:
# # Creating Arrays:

# 1D array
# # 1D array
arr_1d = np.array([1, 2, 3])
print("1D Array:")
print(arr_1d)

# 2D array
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
print("\n2D Array:")
print(arr_2d)

# Array with zeros
zeros_arr = np.zeros((3, 3))
print("\nArray with Zeros:")
print(zeros_arr)

# Array with ones
ones_arr = np.ones((2, 2))
print("\nArray with Ones:")
print(ones_arr)

# Array with a range of values
range_arr = np.arange(0, 10, 2)
print("\nArray with a Range:")
print(range_arr)

# Basic Operations:

# Addition
result_add = arr_1d + arr_1d
print("\nAddition Result:")
print(result_add)

# Multiplication
result_mul = arr_1d * 2
print("\nMultiplication Result:")
print(result_mul)

# Element-wise square root
result_sqrt = np.sqrt(arr_1d)
print("\nSquare Root Result:")
print(result_sqrt)

# Indexing and Slicing:

# Accessing element
element = arr_1d[0]
print("\nAccessing Element:")
print(element)

# Slicing
subarray = arr_1d[1:3]
print("\nSliced Subarray:")
print(subarray)

# Mathematical Functions:

# Sum of all elements
# arr_1d = np.array([1, 2, 3],dtype="int16") #2 bytes
# print("1D Array:")
# print(arr_1d)
# print(type(arr_1d[0]))
# arr_1d = arr_1d.astype(np.int32)
# print(arr_1d)
# print(type(arr_1d[0]))
# data_type = arr_1d.dtype
# print("Data type:", data_type)

# # 2D array
# arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
# print("\n2D Array:")
# print(arr_2d)
# print(arr_2d[0][2])

# # Array with zeros
# zeros_arr = np.zeros((3, 4),dtype="int32")
# print("\nArray with Zeros:")
# print(zeros_arr)

# # # Array with ones
# ones_arr = np.ones((2, 3),dtype="int16")
# print("\nArray with Ones:")
# print(ones_arr)

# # Array with a range of values
# range_arr = np.arange(0, 10, 2)
# print("\nArray with a Range:")
# print(range_arr)

# # Basic Operations:

# # Addition

# result_add = arr_1d + arr_1d
# print("\nAddition Result:")
# print(result_add)

# # Multiplication
# result_mul = arr_1d * 2
# print("\nMultiplication Result:")
# print(result_mul)

# # Element-wise square root
# result_sqrt = np.sqrt(arr_1d)
# print("\nSquare Root Result:")
# print(result_sqrt)

# # Indexing and Slicing:

# # Accessing element
# element = arr_1d[0]
# print("\nAccessing Element:")
# print(element)

# # Slicing
# subarray = arr_1d[1:3]
# print("\nSliced Subarray:")
# print(subarray)

# # Mathematical Functions:

# # Sum of all elements
total_sum = np.sum(arr_1d)
print("\nSum of Elements:")
print(total_sum)

# Mean of elements
# # Mean of elements
mean_value = np.mean(arr_1d)
print("\nMean of Elements:")
print(mean_value)

# Element-wise exponentiation
# # Element-wise exponentiation
exp_arr = np.exp(arr_1d)
print("\nExponential of Elements:")
print(exp_arr)
print(exp_arr)


# """ The NumPy library contains multidimensional array and matrix data structures (you’ll find more information about this in later sections). It provides ndarray, a homogeneous n-dimensional array object, with methods to efficiently operate on it. NumPy can be used to perform a wide variety of mathematical operations on arrays. It adds powerful data structures to Python that guarantee efficient calculations with arrays and matrices and it supplies an enormous library of high-level mathematical functions that operate on these arrays and matrices.
# """
File renamed without changes.
Binary file added o level/__pycache__/calcmodule.cpython-310.pyc
Binary file not shown.
21 changes: 21 additions & 0 deletions o level/calcmodule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def add(n1,n2):
return n1+n2

def sub(n1,n2):
return n1-n2

def multi(n1,n2):
return n1*n2

def divide(n1,n2):
try:
if n2==0:
raise RuntimeError("pls do not divide by zero")
except:
print("pls do not divide by zero")
return

return n1/n2

def volOfSphere(radius,PI=3.14):
return 4/3*PI*radius**3
22 changes: 22 additions & 0 deletions o level/calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import calcmodule as cm

num1=int(input("enter num1:"))
num2=int(input("enter num2:"))
result=0
choice=input("enter ur choice:")

if choice=="+":
result=cm.add(num1,num2)
elif choice=="-":
result=cm.sub(num1,num2)
elif choice=="*":
result=cm.multi(num1,num2)
elif choice=="/":
result=cm.divide(num1,num2)
elif choice=="vos":
r=float(input("enter radius"))
result=cm.volOfSphere(r)
else:
print("invalid choice!")

print("the result is ",result)
17 changes: 0 additions & 17 deletions tempCodeRunnerFile.python

This file was deleted.

0 comments on commit 5178ac6

Please sign in to comment.