Welcome to AR-327 !!


Today's learning objectives:

  • Understand what a collection datatype is
  • Get familiar with different collection data types
  • Get familiar with loops and iterations in Python
  • Get confident in using functions in Python

Collection Data Types

Last week we covered variables in Python:

A variable is like a box.
In this box we can store data.
Variable box Data symbol
Data 1 boolean values Data 2 integer and float values Data 3 string values

But what if we have a lot of variables?


Imagine we have a piece of code that manipulates all the beams (50+) in a structure.

But what if we have a lot of variables?

Imagine we have a piece of code that manipulates all the beams (50+) in a structure.
Is it a good idea to explicitly create an individual variable for each beam?
	    
            beam_1 = 5.0
            beam_2 = 4.5
            beam_3 = 6.0
            beam_4 = 5.2
            beam_5 = 5.1
            beam_6 = 4.8
            beam_7 = 5.3
            beam_8 = 5.6
            beam_9 = 4.9
            beam_10 = 5.4
            beam_11 = 5.0
            beam_12 = 4.7
            beam_13 = 5.2
            beam_14 = 5.5
            beam_15 = 4.6
            beam_16 = 5.1
            beam_17 = 5.3
            beam_18 = 5.7
            beam_19 = 4.8
            beam_20 = 5.2
            beam_21 = 5.0
            beam_22 = 4.9
            beam_23 = 5.4
            beam_24 = 5.6
            beam_25 = 4.7
            beam_26 = 5.3
            beam_27 = 5.1
            beam_28 = 5.5
            beam_29 = 4.8
            beam_30 = 5.2
            beam_31 = 5.0
            beam_32 = 4.6
            beam_33 = 5.3
            beam_34 = 5.7
            beam_35 = 4.9
            beam_36 = 5.4
            beam_37 = 5.1
            beam_38 = 5.5
            beam_39 = 4.8
            beam_40 = 5.2
            beam_41 = 5.0
            beam_42 = 4.7
            beam_43 = 5.3
            beam_44 = 5.6
            beam_45 = 4.9
            beam_46 = 5.4
            beam_47 = 5.1
            beam_48 = 5.5
            beam_49 = 4.8
            beam_50 = 5.5
        
    

But what if we have a lot of variables?

Is it a good idea to explicitly create an individual variable for each beam?
	    
            beam_1 = 5.0
            beam_2 = 4.5
            beam_3 = 6.0
            beam_4 = 5.2
            beam_5 = 5.1
            beam_6 = 4.8
            beam_7 = 5.3
            beam_8 = 5.6
            beam_9 = 4.9
            beam_10 = 5.4
            beam_11 = 5.0
            beam_12 = 4.7
            beam_13 = 5.2
            beam_14 = 5.5
            beam_15 = 4.6
            beam_16 = 5.1
            beam_17 = 5.3
            beam_18 = 5.7
            beam_19 = 4.8
            beam_20 = 5.2
            beam_21 = 5.0
            beam_22 = 4.9
            beam_23 = 5.4
            beam_24 = 5.6
            beam_25 = 4.7
            beam_26 = 5.3
            beam_27 = 5.1
            beam_28 = 5.5
            beam_29 = 4.8
            beam_30 = 5.2
            beam_31 = 5.0
            beam_32 = 4.6
            beam_33 = 5.3
            beam_34 = 5.7
            beam_35 = 4.9
            beam_36 = 5.4
            beam_37 = 5.1
            beam_38 = 5.5
            beam_39 = 4.8
            beam_40 = 5.2
            beam_41 = 5.0
            beam_42 = 4.7
            beam_43 = 5.3
            beam_44 = 5.6
            beam_45 = 4.9
            beam_46 = 5.4
            beam_47 = 5.1
            beam_48 = 5.5
            beam_49 = 4.8
            beam_50 = 5.5
        
    

No! let's discuss why.

So clearly we need a better way to handle large ammounts of variables... Hence, we have Collection Data Types!

Collection data types:

A collection is like a box that can can contain multiple boxes.
Variable box
Data symbol Variable box

Collection data types:

A collection is like a box that can can contain multiple boxes.
Variable box set ([])
tuple: ()
list: []
dictionary: {}
Data symbol Variable box

Collection data types:

A collection is like a box that can can contain multiple boxes.
Variable box set ([])
tuple: ()
list: []
dictionary: {}
Data symbol Variable box

In this course, we will focus on lists and dictionaries.

Lists

Lists

Variable box list: []
Data symbol Variable box

Lists

A list is a collection that is ordered and changeable. It allows to duplicate members.
Variable box list: []
Data symbol Variable box

Lists

Here is how we can create a list in Python:
        
            # Create a list of beam lengths
            beams = [5.0, 4.5, 6.0, 5.2, 5.1, 4.8, 5.3, 5.6, 4.9, 5.4,
                     5.0, 4.7, 5.3, 5.6, 4.9, 5.4, 5.1, 5.5, 4.8, 5.5]

            # or variant:
            beams = list((5.0, 4.5, 6.0, 5.2, 5.1, 4.8, 5.3, 5.6, 4.9, 5.4,
                          5.0, 4.7, 5.3, 5.6, 4.9, 5.4, 5.1, 5.5, 4.8, 5.5))

            # Note: you can create an empty list like so:
            beams = []
        
    

Lists

Lists can contain different data types:
        
            # Create a list of different data types
            beams = [5.0, "Beam A", True, 4.5, "Beam B", False, 1]

            # Create a list that contains other lists
            my_beam_sublist = [4.5, 6.0]
            beams = ["Beam C", [4.5, 6.0], True, my_beam_sublist]
        
    

Useful operations with lists

Lists can contain different data types:
        
            beams = [5.0, 4.5, 6.0, 5.2, 5.1, 4.8, 5.3]

            # Accessing elements
            first_beam = beams[0]        # First element
            last_beam = beams[-1]        # Last element

            # Removing elements
            beams.remove(5.1)            # Remove by value
            removed_beam = beams.pop(2)    # Remove and return element at index 2

            # Adding elements
            beams.append(5.6)            # Add to the end
            beams.insert(2, 5.4)         # Insert at index 2

            # Checking length
            num_beams = len(beams)       # Get number of elements
        
    

Exercice on Lists (10 minutes)

Go to the AR-327 website > python-II-list > On the bottom page download and solve exercises 01

Tuples

Tuples

Variable box tuple: ()
Data symbol Variable box

Tuples

A tuple is a collection which is ordered and unchangeable. It allows to duplicate members.
Variable box tuple: ()
Data symbol Variable box

Tuples

A tuple is a collection which is ordered and unchangeable. It allows to duplicate members.
Variable box tuple: ()
Data symbol Variable box
Compared to lists, why would we use tuples?

Tuples

Here is how we can create a tuple in Python:
        
            # Create a tuple of beam lengths
            beams = (5.0, 4.5, 6.0, 5.2, 5.1, 4.8, 5.3, 5.6, 4.9, 5.4,
                     5.0, 4.7, 5.3, 5.6, 4.9, 5.4, 5.1, 5.5, 4.8, 5.5)
            # or variant:
            beams = tuple([5.0, 4.5, 6.0, 5.2, 5.1, 4.8, 5.3, 5.6, 4.9, 5.4,
                          5.0, 4.7, 5.3, 5.6, 4.9, 5.4, 5.1, 5.5, 4.8, 5.5])
        
    

Useful operations with tuples

        
            beams = (5.0, 4, 6.0, "5.2")

            # Accessing elements
            first_beam = beams[0]        # First element
            last_beam = beams[-1]        # Last element

            # Removing elements -> Not possible, tuples are immutable

            # Adding elements -> Not possible, tuples are immutable

            # Checking length
            num_beams = len(beams)       # Get number of elements
        
    

Sets

Sets

A set is a collection which is unordered and unindexed. It does not allow duplicate members.
Variable box set: ([])
Data symbol Variable box

Sets

A set is a collection which is unordered and unindexed. It does not allow duplicate members.
Variable box set: ([])
Data symbol Variable box
Compared to lists and tuples, why would we use sets?

Sets

Here is how you can create and use sets in python:
        
            my_set = {"beam_a", "beam_b", 125.0}

            # Or alternatively:
            my_set = set(("beam_a", "beam_b", 125.0]))

            # Accessing elements -> Not possible (unordered)
            # Only workaround: cast a list:
            set_as_list = list(my_set)
            a = set_as_list[0]

            # Remove elements
            my_set.remove("beam_a")

            # Add elements
            my_set.add("beam_c")

            # Get length of set:
            set_len = len(my_set)
        
    

Dictionaries

Dictionaries

A dictionary is a collection that is ordered and changeable.
It does not allows to duplicate keys.
Each key of the dictionary is assigned a value.
Variable box dictionnary: {:}
Data symbol Variable box

Dictionaries

Here is how you can create and use dictionaries in python:
        
            # Create a dictionary
            my_dic = {"color": (255, 45, 27), 
                      "shape": "circle", 
                      "number": 42}

            # Retrieve an element using its key:
            my_color = my_dic["color"] # (255, 45, 27)

            # Remove element and its key:
            my_dic.pop("shape")

            # Add element and key
            my_dic["is_active"] = True

        
    

Exercice on dictionaries (10 min)

Go to the website's page on dictionaries and solve the the first exercie

Loops

Loops


What is a loop ?

Loops


A loop in programming is a control flow structure that allows code to be executed repeatedly based on a given condition. This can be used to run the same code block over and over again, potentially with different values each time.

Loops


Let's see a first loop together:
        
            for i in range(10):
                print(i)
        
    

Loops


        
            # range(10) will return 0, 1, 2, ..., 9
            for i in range(10): 
                # ->
                # please notice the tabulation
                print(i)
        
    

Loops


        
            for i in range(10):
                print(i)
                if i > 5:
                    break
        
    

Loops


        
            for i in range(10):
                print(i)
                if i > 5:
                    # break will litteraly break the loop: interupt it
                    break
        
    

Loops


        
            for i in range(10):
                if i == 4:
                    continue
                print(i)
                if i > 5:
                    break
        
    

Loops


        
            for i in range(10):
                if i == 4:
                    # continue will interupt the current iteration in the loop 
                    # and start immediately the next one.
                    continue
                print(i)
                if i > 5:
                    break
        
    

Loops


        
            i = 10
            while i > 0:
                print(i)
                i = i-1
        
    

Loops


        
            i = 10
            # As long as the condition is met, we repeate the loop
            while i > 0:
                print(i)
                i = i-1
        
    

Loops


        
            i = 10
            # it this a good idea ?
            while True:
                print(i)
                i = i-1
        
    

Loops


There is much more to loops than what we have just seen but it gives a good basis, and other variants will be seen during the exercices.

Functions

Functions


What is a function ?

Functions


What is a function ?
A function is a block of code that only runs when it is called. You can pass data (parameters) and it can return data as a result.

Functions


Let's see an example:
        
            def calculate_beam_volume(length, width, height):
                volume = length*width*height
                return volume
        
    

Functions


Let's see an example:
        
            def calculate_beam_volume(length, width, height):
                volume = length*width*height
                return volume
        
    
  • def keyword. It signals the beginning of the function definion
  • calculate_beam_volume: (snake_case) name of the function
  • length, width, height: the function parameters
  • return keyword: signals the value that must be returned
  • volume (optional) value to return

Functions


        
            def calculate_beam_volume(length, width, height):
                volume = length*width*height
                return volume

            beam_width = 0.14
            beam_height = 0.2
            beam_length = 4.2
            volume = calculate_beam_volume(beam_length, beam_width, beam_height)
        
    

Functions


        
            # here you define the function
            def calculate_beam_volume(length, width, height):
                volume = length*width*height
                return volume

            beam_width = 0.14
            beam_height = 0.2
            beam_length = 4.2
            volume = calculate_beam_volume(beam_length, beam_width, beam_height)
        
    

Functions


        
            def calculate_beam_volume(length, width, height):
                volume = length*width*height
                return volume

            beam_width = 0.14
            beam_height = 0.2
            beam_length = 4.2
            # here you call the function
            volume = calculate_beam_volume(beam_length, beam_width, beam_height)
        
    

Exercice on functions (10 min)

Go to the website's page on functions and solve the the first exercie and the the third exercie
That is it for today! but...

Assignment for next week: