Session Ready
Exercise

Create a List

Lists are probably the most versatile data structures in Python. A list can be defined by writing a list of comma separated values in square brackets. Lists might contain items of different types. Python lists are mutable - individual elements of a list can be changed while the identity does not change.

Country =['INDIA','USA','GERMANY','UK','AUSTRALIA']

Temperature =[44, 28, 20, 18, 25, 45, 67]

We just created two lists, one for Country names (strings) and another one for Temperature data (whole numbers).

Accessing individual elements of a list

  • Individual elements of a list can be accessed by writing an index number in square bracket. The first index of a list starts with 0 (zero) not 1. For example, Country[0] can be used to access the first element, 'INDIA'
  • A range of elements can be accessed by using start index and end index but it does not return the value of the end index. For example, Temperature[1:4] returns three elements, the second through fourth elements [28, 20, 18], but not the fifth element
Instructions
100 XP
  • Create a list of the first five odd numbers and store it in the variable odd_numbers
  • Print second to fourth element [1, 4, 9] from squares_list