List (Python)
LIST
1. Write a program that reverses an array of integers (in place).lst = [10, 11, 12, 13, 14, 15]
print(lst)
lst.reverse()
print("Reversing the list")
print(lst)
2. Write a program that inputs two lists and creates a third, that contain all elements of the first followed by all elements of the second.ListOne=['H','e','l','l','o']
ListTwo=['W','o','r','l','d']
ListThree=[]
print(ListOne)
print(ListTwo)
ListOne.extend(ListTwo)
ListThree.extend(ListOne)
print(ListThree)
3. Ask the user to enter a list containing numbers between 1 and 12. Then replace all of the entries in the list that are greater than 10 with 10.mylist=[]
user_input=int(input('Please enter a number: '))
for i in range(user_input):
numbers = int(input('Please enter some values between 1 and 12: '))
mylist.append(numbers)
print(mylist)
for i in mylist:
if i>10:
mylist[mylist.index(i)]=10
print(mylist)
4. Ask the user to enter a list of strings. Create a new list that consists of those strings with their first characters removed.my_ListOfStrings=[]
newListOfStrings=[]
lengthoflist=int(input('Enter length of list: '))
for i in range(lengthoflist):
my_ListOfStrings.append(str(input('Please enter a string: ')))
print(my_ListOfStrings)
for i in range(len(my_ListOfStrings)):
newListOfStrings.append(my_ListOfStrings[i][1:])
print(newListOfStrings)
5. Create the following list using for loop:(a) A list consisting of the integers 0 through 49.
mylist=[i for i in range(50)]
print(mylist)
(b) A list containing the squares of the integers 1 through 50.mylist=[i*i for i in range(1,51)]
print(mylist)
(c) The list ['a', 'bb', 'ccc', 'dddd', ...] that ends with 26 copies of the letter z.import string
listABCD=string.ascii_lowercase
count=1
newList=[]
for i in listABCD:
newList.append(i*count)
count+=1
print(newList)
06. Write a program that takes any two lists L and M of the same size and adds their elements together to form a new list N whose elements are sums of the corresponding elements in L and M. For instance, if L=[3, 1, 4] and M=[1, 5, 9] then N should equal [4, 6, 13].L=[3,1,4]
M=[1,5,9]
N=[]
for x in range(len(L)):
N.append(L[x]+M[x])
print('Elements of L = ', L)
print('Elements of M = ', M)
print('Adding elements of L and M together = ', N)
7. Write a program that rotates the elements of a list so that the element at the first index moves to the second index, the element in the second index moves to the third index, etc., and the element in the last moves to the first index.myList = [1, 2, 3, 4, 5]
print('List: ', myList)
myList=(myList[-1:] + myList[:-1])
print('After rotating: ', myList)
8. Write a program that reads the n to display nth term of Fibonacci series.fib = [0]
a, b = 0,1
for i in range(21):
a, b = b, a+b
fib.append(a)
while True:
n=int(input("enter a number between 0 and 21 "))
if n<0:
print("Please enter a positive number: ")
elif n==0:
print('Fibonacci number at position 0 is 0')
elif n==1:
print('Fibonacci number at position 1 is 1')
elif n>21:
print('please enter a number less than 21')
else:
print('Fibonacci number at position ',n,' is ',fib[n])
9. Write a program as per following specifications(a) Return the length of the longest string in the list of strings str_list. Precondition: the list will contain at least one element.
string_lengths_list = []
str_list = ['abcdeg', 'abacate', 'pera', 'goiaba', 'udfdfsva', 'abacaxi', 'laranja', 'maca']
print('Given list of strings: ',str_list)
for x in str_list:
string_lengths_list.append(len(x))
maximum_length_of_string = max(string_lengths_list)
for y in str_list:
if len(y) == maximum_length_of_string:
print('\nThe longest string of length ',maximum_length_of_string, ' is ',y)
(b). L is a list of numbers. Return a new list where each element is the corresponding element of list L summed with number num.
L = []
newList=[]
length_Of_list = int(input('\nPlease enter a length for your list: '))
for y in range(length_Of_list):
L.append(int(input('Please enter list elements: ')))
print('\nEntered list: ', L)
num=int(input('please enter a number: '))
for x in L:
newList.append(x+num)
print('New list: ',newList)
For n in range (a,z,1):
ReplyDeleteFor m in range(1,27,1):
count+=n
count+=m
Z1=m*n
Print(Z1)
Is this solution true ..
Deletethere is error in line 1: For n in range (a,z,1): because range() function only works with integers
Deletebtw what is the question?