Lists python (JNNC Technologies)

                                                    http://jnnctechnologies.com/
                               https://clanguagetraininginvizagjnnc.blogspot.com/  
                               https://javatrainingjnnctechnologies.blogspot.com/
                                  https://pythontraininginjnnctechnologies.blogspot.com/
                                      https://dotnetjnnctechnologies.blogspot.com/
                                                   https://softwarecoursesselenium.blogspot.com/
                                     https://summerinternshipinjnnctechnologies.blogspot.com/
                                           


Lists are very similar to arrays. They can contain any type of variable, and they can contain as many variables as you wish. Lists can also be iterated over in a very simple manner. Here is an example of how to build a list.

1
2
3
4
5
6
7
8
9
10
11
mylist = []
mylist.append(1)
mylist.append(2)
mylist.append(3)
print(mylist[0]) # prints 1
print(mylist[1]) # prints 2
print(mylist[2]) # prints 3
# prints out 1,2,3
for x in mylist:
print(x)

Accessing an index which does not exist generates an exception (an error).

1
2
mylist = [1,2,3]
print(mylist[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.


1
2
3
4
5
6
7
8
9
10
11
12
numbers = []
strings = []
names = ["John", "Eric", "Jessica"]
# write your code here
second_name = None
# 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)

Post a Comment

0 Comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();