Create a Python program that populates an array variable whose values(at least 5) are input by the user. It should then perform some modification to each element of array using a loop and then display the modified array in a second loop. Include Header comments in your program that describe what the program does.
1. Example with numbers:
Python Code for modifying the number to square of that number and displaying using loop is given below:
# define a empty list which is similar to an array
array = []
# Size of array is asked from user
# As input function returns string value so int() function is used for type conversion
array_size = int(input("Enter the size of array: "))
# User is pompt to enter the values
for i in range(array_size):
user_input = int(input ("Enter the number {} : ".format(i+1) ))
array.append(user_input)
# Modification part here provided numbers are squared and stored in same array list
print("\n***processing modification***")
for index, value in enumerate(array):
array[index] = value ** 2
# Print the squared values of the numbers
print("\nThe square of provided numbers :")
for index, value in enumerate(array):
print("Square of number" + str(index+1) + " : " + str(array[index]))
Output:
2. Example with strings:
Python Code for modifying the names to reverse of the name and displaying using loop is given below:
# define a empty list which is similar to an array
array = []
# Size of array is asked from user
# As input function returns string value so int() function is used for type conversion
array_size = int(input("Enter the size of array: "))
# User is pompt to enter the names
for i in range(array_size):
user_input = input ("Enter the Name {} : ".format(i+1) )
array.append(user_input)
# Modification part here provided names ae reversed and stored in same array list
print("\n***processing modification***")
for index, value in enumerate(array):
# array[index][start:end:step] --> element at each index are reversed using step -1
array[index] = array[index][::-1]
# Print the reversed names from input
print("\nThe reverse of provided names :")
for index, value in enumerate(array):
print("Reverse of the name " + str(index+1) + " : " + array[index])
Output:
Get Answers For Free
Most questions answered within 1 hours.