Lists. Lists are created by placing all elements inside square brackets. The items are separated by commas. The lists can have different types of items like strings, floats, and integers.
List of integers.
List==[1,2,3,4,5,]
List with mixed data.
List=[4.1,"eggs", 2,[6,7,8,, 9]
List indexing.
In python,indexes start at zero(0). A list containing ten elements will have indexes from 0 to 9.
List=['s','a,'h','k','j']
Print(list[0])
Print(list[3])
List slices.
List slices provide a more advanced way of retrieving values from a list. We index a list with two colons separating the integers which return a new list containing all the values in the old list between the indexes.
Values=[0,1,4,9,16,25,36,49]
Print(values[1:4])
Print(values[0:6])
If the first number in a list is omitted, it is taken to be the start of the list while the omission of the last integer is taken to be the end of the indexing.
Print(values[:5]
Print(values[3:]
Dictionaries.
These are data structures used to map arbitrary keys to a value. Printing a key that is not part of the dictionary returns a key error.
Price={"oranges": 25, "graves": 40, " mangoes": 50}.
Print(price["graves"])
Print(price["mangoes"])
Tuples.
Tiples are very similar to lists, except that they are immutable(they cannot be changed).
They are created using parentheses instead of square brackets.
Examples.
Word=("mangoes","tomatoes","potatoes")
Print(word[0])
Print(word[2])
Print(word[1])
Tuples can also be created without parentheses.
No comments:
Post a Comment