Introductory Lesson 2
- Roberto Polli - [email protected]
# before starting this lesson # import the python3 print capabilities # using the following statement # NB since now you *must* always use # parenthesis with print! from __future__ import print_function, unicode_literals, division import sys # Import further python3 features. if sys.version_info.major < 3: from future_builtins import * import __builtin__ as builtins
# it's easy to create a list list_a = ['this', 'is', 'a', 'list']
# you can append items to a list # with the append method list_a.append("mutable")
# Print its content print(list_a)
# See its length len(list_a)
In python you can create lists of subsequent numbers.
In python 2, the range function returns a list,
while in python 3 it returns an iterable object.
This is to avoid memory issues when dealing with large lists.
a = 11 #range in python 2 returns a list # of consecutive ints from_0_to_10 = range(a) l = len(from_0_to_10) l == a print(l)
# in python 3 things are slightly different # so the above code won't work. import sys if sys.version_info.major < 3: range = xrange # Override the range function with the xrange function. from_0_to_10 = range(a)
# Exercise: can you print the content of from_0_to_10 print(from_0_to_10)
# In python 3, you need to create a list from the range object. # The above code # should be replaced with the following from_0_to_10 = list(range(a)) print(from_0_to_10)
# you can get list items by index from_0_to_10[0]
# Exercise: what happens if I get the 11th element? from_0_to_10[11]
# python is calling under the hood from_0_to_10.__getitem__(0)
# python lists are doubly linked ;) from_0_to_10[-1]
# Exercise: please check the manual! help(list)
straight = [1, 2, 3, 'star'] print(straight)
# I can take the middle of the list... straight[1:3]
k = 2 # ... or using a separator straight[0:k], straight[k:4]
straight[:k] # I can omit the first...
straight[k:] # ...and last index
# Strings behaves like lists s_a = "Counting: 123"
# Have length.. l_a = len(s_a) print(l_a)
# ..indexes print(s_a[0], " ", s_a[-1])
# and a last element s_a[l_a] # ...what's happening there?
# ...we can even slice them f = "prova.txt" f[:-4], f[-4], f[-3:]
# Note: use the os.path module for path manipulation. # The above code will break with 4-letter extensions! from os.path import splitext splitext("prova.foo.yaml")
a_list = ['is', 'iterable', 'with'] for x in a_list: print(x)
for x in a_list: # You need `from __future__ import print_function` # for python2 to support the `end` argument print((x), end=' ') y = x + str(2) break # stop now
# what's the expected output of the # following instruction? print(("x,y: ", (x, y))) # Differently from C, `for` does not create # a scope
a_list = ['is', 'iterable', 'with'] while a_list: # pop() modifies a list removing # and returning its last element x = a_list.pop() print(("pop out %s" % x)) break # what happens if I remove this break? print(a_list)
# What's the expected behavior of the # following instructions? for x in a_list: print((x + a_list.pop())) # for + pop() is not always a good idea ;)