Lists and Tuples

What we needed to do:

Show your ability and knowledge of the use of Lists and Tuples in Python. When should you use one and when should you use the other?

 

What are lists?

In Python 3 there are many built-in data structures. The simplest one of them are lists. It is used to store a list of values. In other words, lists are a collection of items like strings, integers or lists.

How are lists used?

In a list every item has a index value.

When we use them, we need to enclose them with []. Unlike tuples, lists can be changed.

There are many functions that are already built in allowing to work with lists.

For example with the function length we can get the length of a list:

list = ["1", "hello", "Miri, "4"]
len(list)
>>4

 

What are tuples?

Tuples are a sequence of Python objects that are unable to be changed. So they can not be modified.

How are tuples used?

Unlike lists which use square brackets, tuples use parentheses.

house = (‚wall‘, ‚door‘, ‚window‘, ‚roof‘, ‚floor‘)

The output would look like this:

print(house)

(‚wall‘, ‚door‘, ‚window‘, ‚roof‘, ‚floor‘)

For loops

What we needed to do:

Show your ability and knowledge of how to create and when to use for loops in Python.

When do we use for loops?

For loops are used when you have a block code which you want to repeat over a sequence, so over a fixed number of times. That could be for example a list, a set or a string.

for i in range(0,3):
   print(i)

When we run this program, the output looks like this:

Output

0

1 

2

Basic Output

What to do

Show your ability to use Basic Output with the print statement in Python. You should show different ways of using print for single items as well as multiple items. How do you handle formatting of printing of some types (for example controlling the size of float output)?

In the course we have been writing small programs that work with user input. The program will do some magic with the input and will give something back. That means: Basic Output is what the program gives back to the user. In Python this can be done with the built-in-function „print“. The function can be used various times inside the same program.

There are different options to print parts of your program:

>>> print("Hello World!")
Hello World!
>>> name = "Miriam"
>>> print(name)
Miriam
>>> number = 42
>>> print(number)
42
>>> print "Miriam" + "Yaman"
Miriam Yaman
>>> print "Miriam","Yaman"
Miriam Yaman

Controlling float output: float numbers are numbers with a decimal point (e.g. 0.1).

Creating Functions

What to do

Show your ability to create your own functions in Python3. Explain how this works with your examples. Can you have more than one function with the same name?

How do I create a function?

Creating a function is „quickly“ done. All you need is the prefix „def“, a unique name (yes – you cannot use a name twice nor an existing name of a function) and parameters to work with (e.g. n or x, y…). In the end you like the function to return something, so don’t skip that. This is the frame for every function:

def function(x):
what do you want your function to do?
return x