DAND – Python Introduction continued

I finished the control flow and functions lecture of the python section today. The lections are definitely not designed for complete programming newbies, but they are rather a real quick refresher on the most important concepts which are needed for the program. The quizzes are also a little bit trickier than you would expect for normal Python introductory course. For me, this fits quite well to my profile since I have already some knowledge of the key concepts like loops, if conditions, etc, but I definitely have a lot more to learn.

In the control flow lecture, if statements, for and while loops were topics which were already familiar to me. Hence, I will not go into more detail here. New to me were Break & Continue, Zip and enumerate and list comprehensions.

Break & Continue:

Break stops the loop when a condition is met.

y = [“a”,”b”,”c”,”d”]

for e in y:
if e==”c”:
break
print (e)

This will output:

a,b

c and  d will not be printed since the break statement is run when e = c

The continue statement works similarly. If a certain condition is met, the loop stops, but different from the break statement, it will continue with the next iteration of the loop.

for e in y:
if e==”c”:
continue
print (e)

The output is a,b,d. C is not printed but the loop is continued after the c iteration. Therefore, the last item of the list, d, is printed as well.

Enumerate and zip

Enumerate is a built-in function which returns the index of the current position in the loop.

names = [“Dennis, “Hans”, “Rick”]

for i, name in enumerate(names):

print (i, name)

output: 0 Dennis, 1 Hans, 2 Rick

The zip function is used to combine multiple lists.

names  = [“Dennis”, “Hans”, “Rick”]

age = [22,44,55]

personalinfo = list(zip(names,age))

print (personalinfo)

Output:

[(‘Dennis’, 22), (‘Hans’, 44), (‘Rick’, 55)]

It returns a list of tuples which combines the information of both lists, names and age.

To return this new element it has to be put in a list. Otherwise, the print statement will just return the zip object. Another option is to loop of the zip element.

names  = [“Dennis”, “Hans”, “Rick”]

age = [22,44,55]

personalinfo = zip(names,age) # zip element is not turned into a list!

for info in personalinfo:

print (info)

Output is the 3 different tuples:

(‘Dennis’, 22)
(‘Hans’, 44)
(‘Rick’, 55)

It is also possible to unzip a tuple with the asterisks symbol: *

name, age = zip(*personalinfo)

This will store the different elements of the zipped object as a list in the variables name and age and undo the prior executed zipping.

list comprehensions

This is a concise way of writing a for loop in one sentence. The structure is the following:

newlist = [x+2 for x in range(5)]

print (newlist)

Output: [2, 3, 4, 5, 6] # For every x in the range 0-4 (5 is non inclusive) 2 is added to x.

The general structure of the list comprehension is, that for ist specified what is done to the element over which is iterated: x+2

Then it is specified how many iterations should occur: for x in range(5)

As the last step, the arguments are embedded in [] brackets to create the list. This has the big advantage that no empty list has to be created first to which then elements have to be appended.

This basic structure can vary a bit when taking if statements into consideration.

newlistif = [x+2 for x in range(5) if x%2==0]
print (newlistif)

Output: [2,4,6]

Here, the if statement is added at the very end, behind the range argument. Now, +2 is only added to even numbers.

However, if and else statement is necessary. This order changes.

newlistifelse = [x+2 if x%2==0 else x+1 for x in range(5)]
print (newlistifelse)

Output: [2, 2, 4, 4, 6]

Now the if statement is located directly behind the argument which specifies the action (x+2). This makes sense since it is also the argument the if statement refers to. If the if statement is true, x+2 will be added to x. The if statement is then followed by the else statement. The else statement is then followed by the action which refers to the else statement (x+1).

Functions:

I won’t go into much detail regarding the general functioning of functions since this was already a familiar concept to me. Functions work the following:

Multiple inputs can be put into a function and inside the functions body, it is defined how these inputs are handled. In my exemplary function my_function, the 2 inputs a and b are given into the function and then multiplied with each other. The return statement returns the results of the function’s

def my_function(a,b):

return a*b
result = my_function(5,2)

print (result)

Output: 10

New to me was the lambda function. This is a quick way of defining short functions. The prior function can be rewritten as a lambda function in the following way:

resultlambda= lambda a,b: a*b
print (resultlambda(2,3))

Output: 6

As you can see. The function can be written and assigned to a variable in only one line instead of 3.

Iterators and generators were the last concepts introduced in the functions lecture. This, I have to admit, I did not quite get. Generators are functions which create iterators. They are an alternative to lists which also returns iterators. The yield statement makes this function a generator. It makes a return statement obsolete and yields the element which is iterated over.

def generate(x):

for x in range(x):

yield x
var1 = generate(5)

for var in var1: print (var)

output: 0,1,2,3,4

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s