Collections and Strings

Objectives ⟶

  • Discuss how a while loop is different from a for loop

  • Recap the list data type and for loops.

  • Introduce dict, tuple data types.

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.

Python Challenge

Loading

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.

Working with a Dictionary

Creating an empty dictionary

An empty dictionary is denoted by a set of curly braces {}. You can create an empty dictionary and store it to a new variable using an assignment opeartor =.

# create an empty dictionary
my_dict = {}
Dictionary with Initial Values

You can create a dictionary with initial values by supplying a set of key-value pairs.

# create a dictionry with initial values
my_dict = { "my_key1": "my_value1", "my_key2": "my_value2" }

# equivalent to
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

Advanced Exercises

Challenge yourself!

These questions are intended to challenge yourself. If you need help, click on the <> Solution button.

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading