Modifying Lists in Python

Publicado el: 19 octubre 2020
en el canal de: Mechatronics 101
13
0

#1. Create a list called "components" and populate it with a number (minimum 5) of mechatronics components. Print each item (component) by accessing each #
#element in the list, one at a time. This would be a good application for experimenting with a FOR loop

components = ['resistor', 'capacitor', 'motor', 'wire', 'relay']#create a list
for component in components:#This loop will go through all the elements creating a new variable that gets printed in every iteration
print(component.title())

#2. Utilizing the same list as in 1. instead of printing the item in each element, print a message using each element in the list.
#The text of each message should be the same but each message should utilize a different element.

for component in components:#This time the new variable is added to a string before being printed.
print(f"{component.title()} is part of the mechatronics in this device.")


#3. Create a list of your own using a minimum of 6 elements. Print a unique message for each element, using the element in the text.

animals=['dogs','cats','fish','turtles','birds','snakes',]
for pets in animals:
print(f"{pets.title()} can be purchased at Petco.")


#4. Add an element to your list using the appropriate method()

animals.append('hamsters')#adds an item at the end of the list
print(animals)

#5. Remove an element using the appropriate method()

animals.remove('snakes')#permanently removes the string form the list
print(animals)

#6.Remove an element from your list and place it in a separate variable
new_pet = animals.pop()#"pops" an element from the list
print(new_pet)#new variable

print(animals)
#7.Put the "popped" out element back into your list after having printed it with a personal message

print(f" The element {new_pet.title()} was 'popped' from the list." )
animals.append(new_pet)
print(animals)
print(f" The element {new_pet.title()} was returned to the list." )


#8.Use del to remove all items from your list
del animals
animals=[]

#9.Print your empty list
print(animals)

#10. Create a new list with a minimum of 6 items

smart_phones= ['samsung','alcatel','motorolla','apple','nokia','sony','lg','htc']

#11. Print your list in its original order

print("This is original list:")
print(smart_phones)


#12.Use sorted() to print your list in alphabetical order without actually modifying the original list

print("\nThis is the sorted list:")
print(sorted(smart_phones))#temporarily sorts but returns the original variable

#13.Show that the list is still in its original order using print

print("\nBack to the original list again:")
print(smart_phones)


#14. Use sorted() to print your list in reverse alphabetical order without permanently altering your list

print(sorted(smart_phones, reverse=True))#temporary reversed list




#15. Print the original list to show that it was not permanently altered

print(smart_phones)


#16. Use reverse() to change the order of your list, print the list to show that it has changed

smart_phones.reverse()#reverses the unsorted original list
print(smart_phones)


#17.Use reverse() to change the order of your list again, print the list to show that its back to its original order


smart_phones.reverse()#reverses back to the original list
print(smart_phones)


#18. Use sort() to permanently store your list in alphabetical order.  Print the list to verify

smart_phones.sort()#permanently sorts alphabetically
print(smart_phones)


#19. Use sort() to change your list so its stored in reverse alphabetical order. Print the list to show the change' 

smart_phones.sort(reverse=True)#reverses the list permanentlycallsign.sort(reverse=True)
print(smart_phones)


En esta página del sitio puede ver el video en línea Modifying Lists in Python de Duración hora minuto segunda en buena calidad , que subió el usuario Mechatronics 101 19 octubre 2020, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 13 veces y le gustó 0 a los espectadores. Disfruta viendo!