Collections and Strings

Objectives ⟶

  • Recap the list data type and for loops.

  • Introduce dict, tuple data types.

  • Prepare for the upcoming quiz.

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

While Loops

Indefinite Iteration

Last time, we covered the first type of loop in Python - the for loop. A for loop takes a list-like data and iterates over each element.

Generally speaking, you use a for loop when you know how many times you'd like to iterate before entering the loop. for i in range(6) will iterate 6 times unless you explicitly exit the loop (which we haven't discussed yet).

The while loop serves a different purpose. You can use the while loop if you want to keep on iterating until a certain condition is satisfied.

At each iteration, the while loop checks whether a condition is satisfied. It will run indefinitely until the condition is NOT satisfied.

Skip Discussion For Now

Although while loops can be useful at times, we won't find many uses for the purpose of this course. For now, you only need to remember that while loops can be used to keep iterating when you don't know how many times to loop in advance.

Now comes the data type that is critical in programming - the dictionary type.

Dictionary

Key-Value Pairs

Do you know what terpsichorean means? How about appoggiatura? If you know... you must read dictionaries for fun. 🤡 If you don't, you're on the same boat as me. Let's look these up in an English dictionary ( https://www.dictionary.com/ ).

Terpsichorean is defined in the dictionary as "pertaining to dancing". Appoggiatura is defined as "a note of embellishment preceding another note and taking a portion of its time" .

Why am I talking about these obscure vocabularies in a Data Analytics course? You're about to find out.

Python Challenge

Loading

A Python dictionary is... just like a real dictionary.

Creating an empty dictionary

You can create an empty dictionary using the following syntax: my_dict = {}.

Dictionary with initial values

You can create a dictionary with initial values using the following syntax: my_dict = { "my_key1": "my_value1", "my_key2": "my_value2" } .

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

I’m so good at finance...

Even my bank says my balance is outstanding.

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Tuples

Immutable

The final collection data type we'll discuss is the tuple type. A tuple is an immutable collection. You can only set the values when you create a tuple.

TL;DR It's EXACTLY like a list, but you can't update an element once you create it.

Python Challenge

Loading

Quiz Preparation

In-class

These questions are intended to prepare you for the upcoming in-class quiz. The questions on the quiz will be highly similar to these exercise questions.

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading