Welcome back!

In the last workshop we have covered basics of Python. In this workshop, we will continue introducing more about Python and data structure.

If you didn't attend the last workshop, don't worry. This notebook will include Recaps from the first workshop to refresh your memory about related concept.

In computer science, a data structure is a data organization, management, and storage format that enables efficient access and modification. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data.

Getting Started

Workshop Best Practices

Python Collections

There are four collection data types in the Python programming language:

When choosing a collection type, it is useful to understand the properties of that type. Choosing the right type for a particular data set could mean retention of meaning, and, it could mean an increase in efficiency or security.

Recap: About Data Types

Task 1: Lists

Lists are created using square brackets:

Properties of lists

List items are ordered, changeable, and allow duplicate values.

Recap: About Indexing

We can access characters in a string by indexing. Square brackets can be used to access elements of the string. The first character has the position 0.

List items are indexed in a similar way to string. The first item has index [0], the second item has index [1] etc.

List Length

To determine how many items a list has, use the len() function:

List Items - Data Types

List items can be of any data type:

A list can contain different data types:

Assigning Values to Multiple Variables using Lists

If you have a collection of values in a list, tuple etc. Python allows you extract the values into variables. This is called unpacking.

Task 2: Tuples

Tuples are written with round brackets.

Properties of Tuples

Like lists, tuples are:

Unlike lists, tuples are:

Tuple Length

To determine how many items a tuple has, use the len() function:

Tuple Items - Data Types

Tuple items can be of any data type:

Create Tuple With One Item

To create a tuple with only one item, you have to add a comma after the item, otherwise Python will not recognize it as a tuple.

Task 3: Sets

Sets are written with curly brackets.

Properties of Sets

Set items are unordered, unchangeable, and do not allow duplicate values.

Get the Length of a Set

To determine how many items a set has, use the len() method.

Set Items - Data Types

Set items can be of any data type:

Task 4: Dictionaries

Different from lists, tuples, and sets, dictionaries are used to store data values in key: value pairs.

Dictionaries are written with curly brackets, and have keys and values:

Properties of Dictionaries

Dictionary Length

To determine how many items a dictionary has, use the len() function.

Dictionary Items - Data Types

The values in dictionary items can be of any data type.

Summary: Choosing a Collection

When to use a dictionary:

When to use the other types:

Loops and Iterations

Python has two primitive loop commands:

These loops are frequently used with If - Elif - Else conditional statements

Recap: If - Elif - Else Statements

Task 5: Creating While Loops

With the while loop we can execute a set of statements as long as a condition is true.

    Note: remember to increment i by adding 1, or else the loop will continue forever.

The break Statement

With the break statement we can stop the loop even if the while condition is true:

The continue Statement

With the continue statement we can stop the current iteration, and continue with the next:

The else Statement

With the else statement we can run a block of code once when the condition no longer is true:

Task 6: Creating For Loops

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.

With the for loop we can execute a set of statements, once for each item in that sequence (e.g. a list, tuple, set etc.)

Recap: Looping Through a String

Even strings are iterable objects, they contain a sequence of characters, i.e. arrays.

Since strings are arrays, we can loop through the characters in a string, with a for loop.

    The for loop does not require an indexing variable to set beforehand.

The break Statement

With the break statement we can stop the loop before it has looped through all the items:

The continue Statement

With the continue statement we can stop the current iteration of the loop, and continue with the next:

The pass Statement in For Loops

for loops cannot be empty, but if you for some reason have a for loop with no content, put in the pass or continue statement to avoid getting an error.

The difference between Pass and Continue is shown below:

The range() Function

To loop through a set of code a specified number of times, we can use the range() function,

The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.

    Note that range(6) is not the values of 0 to 6, but the values 0 to 5.

The range() function defaults to 0 as a starting value, however it is possible to specify the starting value by adding a parameter: range(2, 6), which means values from 2 to 6 (but not including 6)

Recap: Slicing strings with indices

You can return a range of characters by using the slice syntax.

Specify the start index and the end index, separated by a colon, to return a part of the string.

Indexing a range is very similar to slicing a string

The range() function defaults to increment the sequence by 1, however it is possible to specify the increment value by adding a third parameter: range(2, 30, 3)

Recap: Stepping strings with indices

Incrementing a range is very similar to stepping strings with indices

Else in For Loop

The else keyword in a for loop specifies a block of code to be executed when the loop is finished:

    Note: The else block will NOT be executed if the loop is stopped by a break statement.

Nested Loops

A nested loop is a loop inside a loop.

The "inner loop" will be executed one time for each iteration of the "outer loop":

Functions

A function is a block of code which only runs when it is called.

You can pass data, known as parameters, into a function.

A function can return data as a result.

Task 7: Creating a Function

In Python a function is defined using the def keyword:

Task 8: Calling a Function

To call a function, use the function name followed by parenthesis:

Task 9: Input Arguments

Information can be passed into functions as arguments.

Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

The following example has a function with one argument (fname). When the function is called, we pass along a first name, which is used inside the function to print the full name:

    Arguments are often shortened to args in Python documentations.

Task 9: Arguments VS Parameters

The terms parameter and argument can be used for the same thing: information that are passed into a function.

From a function's perspective:

Number of Arguments

By default, a function must be called with the correct number of arguments. Meaning that if your function expects 2 arguments, you have to call the function with 2 arguments, not more, and not less.

Keyword Arguments

You can also send arguments with the key = value syntax.

This way the order of the arguments does not matter.

Default Parameter Value

The following example shows how to use a default parameter value.

If we call the function without argument, it uses the default value:

Return Values

To let a function return a value, use the return statement:

Task 11: Creating Lambda Functions

A lambda function is a small anonymous function.

A lambda function can take any number of arguments, but can only have one expression.

Syntax

    lambda arguments : expression

The expression is executed and the result is returned:

Lambda functions can take any number of arguments:

Why Use Lambda Functions?

They are useful when you want a function that you will use just one time.

They contain simple logical operations that are easy to read and understand.

The power of lambda is better shown when you use them as an anonymous function inside another function.

Say you have a function definition that takes one argument, and that argument will be multiplied with an unknown number:

Use that function definition to make a function that always doubles the number you send in:

Exercises - You made it!

Solutions are in the Workshop Drive.

If you want more assistance/ explanations, kindly contact shanghai.library@nyu.edu

Ending credits

Tutorial framework: https://www.w3schools.com/python/default.asp

Modified and organized by: Pamela Pan, Jie Chen