Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Python Programs

Uploaded by

Chirag Saraf
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)
14 views

Python Programs

Uploaded by

Chirag Saraf
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/ 2

Python Programs:

1. Find and display the largest and least numbers of a list without using
built-in functions max() and min(). The list elements are user given
inputs.
l = []
size = int(input("Number of elements in the list:"))
print('Enter',str(size),' numbers one by one:')
for i in range(size):
d = int(input())
l.append(d)
print(l)
max = l [0]
min = l [0]
for d in l:
if d > max:
max=d
if d<min:
min=d
print('The maximum number in list is', max)
print('The minimum number in list is', min)

2. Write a program that input a string and ask user to delete a given word
from a string.
text = input('Enter a string: ')
words = text.split()
data = input('Enter a word to delete: ')
status = False
for word in words:
if word == data:
words.remove(word)
status = True
if status:
text = ' '.join(words)
print('String after deletion:',text)
else:
print('Word not present in string.')
3.Write a program to count the total word length in a user given string
text = input('Enter a sentence without punctuation: ')
words = text.split()
sum=0
for item in words:
sum = sum + len(item)
print(‘Total word length of the string:’,sum)

4. Write a program to add two 2-dimensional NumPy arrays where the


elements of the arrays are user given.
import numpy as np
rows = int(input("Enter the number of rows: "))
cols = int(input("Enter the number of columns: "))
print("Enter the elements of the first array (one row at a time):")
arr1ele = []
for i in range(rows):
row = [int(x) for x in input().split()]
arr1ele.append(row)
print("Enter the elements of the second array (one row at a time):")
arr2ele = []
for i in range(rows):
row = [int(x) for x in input().split()]
arr2ele.append(row)
array1 = np.array(arr1ele)
array2 = np.array(arr2ele)
resultant = array1 + array2
print("Resultant array after addition:")
print(resultant)

You might also like