CSC220 Data Structures Fall 2014: Python II
CSC220 Data Structures Fall 2014: Python II
Python II
Last time we covered the very basics of Python, leveraging knowledge you already know about programming in general –
variables, conditionals, loops, functions, etc. Now we'll continue our Python introduction and start using more Python
specific concepts.
Dictionaries
One more built-in type that is incredibly useful is the dictionary known in Python as the dict. I think of them as arrays
where the index is a string instead of an integer. These indices are keys and a dict is made up of key, value pairs.
More on functions
Just like most languages with which you might already be familiar, Python functions create a local scope. Variables created
inside of function are not visible outside of it.
def line(m,x,b):
y = m * x + b
return y
ordinate = line(5, 3, 7)
print(ordinate)
For lists, Python passes by reference instead of by value. That means you can alter a mutable sequence "in place."
run = [1, 2, 3, 4, 5, 6, 7]
def double_in_place(l):
# len(run) returns 7
# range(len(run)) returns 0,1,2,3,4,5,6
for i in range(len(l)):
l[i] *= 2
# as opposed to making a copy
def double_copy(l):
output = []
for val in l:
output += [val*2]
return output
Similarly to other languages, by default parameters are positional, that is, order matters.
# this
line(5, 3, 2)
# is different than
line(2, 3, 5)
Keyword arguments are when you give the parameter values by name. In that case, order doesn't matter.
# this
line(m = 5, x = 3, b = 2)
# is the same as
line(x = 3, b = 2, m = 5)
Notice you don't have to change anything about the function definition for this to work. However, you can use a
similar syntax to set default values for a function.
The last thing about Python functions that is really interesting is you can simulate returning multiple values.
def square_and_cube(x):
return x**2, x**3
List Comprehensions
Another common Python-ism you might run into is list comprehensions. You can use these to generate a list on the fly if
you need it.
# boring way
squares = [1, 4, 9, 16, 25, 36, 49]
# with a comprehension
squares = [x**2 for x in range(1,8)] # range(start,stop)
You can also add logic to a comprehension to make lists exactly the way you want them.
It's considered 'Pythonic' to use list comprehensions in many places where in other languages you'd write short loops, so
you'll see them often.
Modules
Instead of header files and linking to libraries, Python imports modules.
We'll be writing our own modules as we go on. By default, when a module is imported, it will evaluate all the statements.
That's fine if it only contains function definitions, but if there's other code, say testing code or example code, having that
execute on importation is probably not what you want. We get around this by checking what name the interpreter thinks the
module has when it's imported.
def normalize(x):
# ...
This is very common. Use this for everything where in C or C++ you'd normally write a header and separate source file.
Be sure to check the Python documentation for what modules are availble besides math and what their contents are.
Taking user input
converted by W eb2PDFConvert.com