FirstThreeLessons
This commit is contained in:
44
LearnTheBasics/Lists.py
Normal file
44
LearnTheBasics/Lists.py
Normal file
@ -0,0 +1,44 @@
|
||||
mylist = []
|
||||
|
||||
#Appending elements to a list
|
||||
mylist.append(2)
|
||||
mylist.append(4)
|
||||
mylist.append(6)
|
||||
mylist.append(7)
|
||||
mylist.append(2)
|
||||
mylist.append(1)
|
||||
|
||||
print(mylist[0])
|
||||
print(mylist[1])
|
||||
print(mylist[2])
|
||||
|
||||
# Iterating through a list
|
||||
for x in mylist:
|
||||
print("Listvalues:",x)
|
||||
|
||||
# Accessing an index which does not exist generates an exception (an error).
|
||||
# mylists = [1,2,3]
|
||||
# print(mylists[10])
|
||||
|
||||
# Exercise
|
||||
# In this exercise, you will need to add numbers and strings to the correct lists using the "append" list method. You must add the numbers 1,2, and 3 to the "numbers" list, and the words 'hello' and 'world' to the strings variable.
|
||||
|
||||
# You will also have to fill in the variable second_name with the second name in the names list, using the brackets operator []. Note that the index is zero-based, so if you want to access the second item in the list, its index will be 1.
|
||||
|
||||
numbers = []
|
||||
strings = []
|
||||
names = ["John", "Eric", "Jessica"]
|
||||
|
||||
# write your code here
|
||||
second_name = names[1]
|
||||
for i in range(1,4,1):
|
||||
numbers.append(i)
|
||||
|
||||
strings.append("Hello")
|
||||
strings.append("world")
|
||||
|
||||
|
||||
# this code should write out the filled arrays and the second name in the names list (Eric).
|
||||
print(numbers)
|
||||
print(strings)
|
||||
print("The second name on the names list is %s" % second_name)
|
Reference in New Issue
Block a user