List:
A list is a collection of ordered and changeable objects variable. It stores more than one value in a single variable.
ex: fruits=[“apple”,”banana”,”mango”] ->list of strings
odd_numbers=[1,3,5,7] ->list of numbers
Why is it called collection?
Stores more than one and different values in a single variable
Why ordered?
Each element of the list is assigned an index number and can be iterated sequentially using this index.
ex: print(fruits[0]) will print “apple” print(fruits[-3]) will also print “apple”
Why changeable?
An element at an index can be changed/deleted.
fruits[0]=” orange” will replace apple with orange in the list. d
el fruits[1] will delete bananas from the list
Note: List values need not be of the same datatype [“apple”, i 2] is also a list
Note: Operations on the list are almost similar to that of strings.
So now, we are ready to write some to fun codes with lists. we'll create a bowl of fruit_salad.
Create an empty list of fruit_salad, a list of fruits, and a list of additives.
![]() |
Comments
Post a Comment