0% found this document useful (0 votes)
8 views3 pages

Python Programs

The document contains a NumPy program that demonstrates creating arrays, performing element-wise operations, and generating random numbers. It also includes examples of calculating factorials, checking for Armstrong numbers, and determining if a string is a palindrome. The code is structured with comments explaining each section and its purpose.

Uploaded by

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

Python Programs

The document contains a NumPy program that demonstrates creating arrays, performing element-wise operations, and generating random numbers. It also includes examples of calculating factorials, checking for Armstrong numbers, and determining if a string is a palindrome. The code is structured with comments explaining each section and its purpose.

Uploaded by

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

NumPy program

import numpy as np

# Create a NumPy array from a Python list

arr1 = np.array([1, 2, 3, 4, 5])

print("Array 1:", arr1)

# Create a 2D array (matrix)

arr2 = np.array([[1, 2, 3], [4, 5, 6]])

print("Array 2:\n", arr2)

# Perform element-wise operations

arr_squared = arr1 * arr1

print("Array 1 squared:", arr_squared)

# Perform array addition

arr_sum = arr1 + 10

print("Array 1 + 10:", arr_sum)

# Create an array of zeros

zeros_array = np.zeros((2, 3))

print("Zeros array:\n", zeros_array)

# Create an array of random numbers

random_array = np.random.rand(3)

print("Random array:", random_array)

Array & Attribute of array

import numpy as np
# create a 2-D array

array1 = np.array([[2, 4, 6],

[1, 3, 5]])

# check the dimension of array1

print(array1.ndim)

Factorial of the number

num = 5

num = int(input("Enter a number: "))

factorial = 1

if num < 0:

print("Sorry, factorial does not exist for negative numbers")

elif num == 0:

print("The factorial of 0 is 1")

else:

for i in range(1,num + 1):

factorial = factorial*i

print("The factorial of",num,"is",factorial)

Armstrong number

num = int(input("Enter a number: "))

sum = 0

temp = num

while temp > 0:

digit = temp % 10

sum += digit ** 3

temp //= 10

if num == sum:
print(num,"is an Armstrong number")

else:

print(num,"is not an Armstrong number")

Palindrome program

my_str = 'aIbohPhoBiA'

my_str = my_str.casefold()

# reverse the string

rev_str = reversed(my_str)

# check if the string is equal to its reverse

if list(my_str) == list(rev_str):

print("The string is a palindrome.")

else:

print("The string is not a palindrome.")

You might also like