Introduction to python
Introduction to python
For loops and conditional statements such as if, elif, and else keywords, are great
tools for building a custom workflow. Let's extend our toolkit by examining another
technique - while loops.
2. If statement
00:16 - 00:34
Before we discuss while loops, let's reiterate the flow of an if statement. It checks for
a condition, which, if met, triggers an action, and then ends. If the condition is not
met, the action is skipped.
3. If statement versus while loop
00:34 - 00:52
The syntax for while loops is similar to an if statement. We start with the while
keyword followed by a condition and end the line with a colon. The next line is
indented and contains the action to perform while the condition is met. They are
useful for continuous tasks. For example, think of a racing game where a player
should accelerate while a button is pressed. Or monitoring traffic to a website and
allowing access while there is less than a predefined threshold of unique IP
addresses.
1. 1
https://unsplash.com/@joaoscferrao
5. While loop
01:31 - 02:08
Let's show an example in action. We have a variable called stock, set to 10,
representing how much stock of an item we have available in an online store. We
have another called num-purchases, equal to zero. We want to evaluate whether the
number of purchases is below the stock count. Inside the while loop, for every new
purchase, we increment the num-purchases variable by one. We then print the
remaining stock by subtracting the number of purchases from the stock.
6. Output
02:08 - 02:19
The output prints the remaining capacity during each iteration of the loop. It runs
down to zero then the while loop exits.
7. A word of caution
02:19 - 02:33
Remember, we saw that when using a while loop the code will continually execute as
long as the condition is met. What if don't increment num_purchases by one inside
the while loop?
8. Running forever
02:33 - 02:44
The loop will continue to run forever because the stock is always larger than the
number of purchases, meaning the condition is always met!
9. Breaking a loop
02:44 - 03:19
To avoid this we have a couple of options. We can use the break keyword inside the
loop, which will terminate the code. This can be helpful if we just want to do
something once or add additional code to set the number of times an action is
performed. Break can also be used in for loops. If we're already running the code
and want to terminate the Python program we can press Control and C on Windows
or Command and C on a Mac to interrupt it.
10. Conditional statements within while loops
03:19 - 03:56
Just like how we can include conditional statements in for loops, we can also do this
in while loops! Rather than printing how much stock is left, we can customize the
print statement based on the remaining stock! If stock is more than seven we print
"Plenty of stock remaining". Using elif, we print "Some stock remaining" if there are
more than 3 items left. Another elif checks whether any stock is left, printing "Low
stock!". Otherwise, we print "No stock!"
11. Conditional statements output
03:56 - 04:06
The output is now customized depending on the state at that point in the while loop,
and it still exits when stock reaches zero.
12. Let's practice!
04:06 - 04:11
While you're enjoying the course, let's try out some exercises!
1. Courses
Course Outline
o
o
o
o
Daily XP1720
Exercise
Exercise
Converting to a while loop
You can often achieve the same tasks using either a for or while loop.
To demonstrate this, you're going to take the for loop you built earlier in the course and modify it
to use a while loop that achieves the same results!
max_capacity = 10
Today's date is the 22nd, and they have a pre-release version available from the 24th for all
users who purchase a subscription on or before the 24th!
You'll need to build a custom workflow that provides tailored messages depending on the date.
Create a while loop based on current_date being less than or equal to release_date.
Increment current_date by one for each day that passes.
Check if current_date is less than or equal to 24 and, if so, print "Purchase before the
25th for early access"
Otherwise, check if current_date is equal to 25, printing "Coming soon!".
release_date = 26
current_date = 22
# Promote purchases
if current_date <=24:
print("Purchase before the 25th for early access")
Let's round out our knowledge by examining some additional techniques to use
when building workflows.
2. Complex workflows
00:05 - 00:36
When developing software we might need to implement extensive custom logic. For
example, we may need to loop through multiple data structures, evaluate them
against multiple conditions, update variables based on these checks, and display
various outputs depending on the results. We have the majority of the knowledge
required for this complex workflow, given our ability to use for and while loops,
boolean operators, and if, elif, and else statements.
3. The "in" keyword
00:36 - 01:09
Let's examine some more conditional keywords and techniques to help us design
complex workflows. First, there's in, which is used to check if a value exists in a
variable or data structure. Using our dictionary, products_dict, containing product IDs
as keys and prices as values, we can check whether the ID "OS31" is included in the
dictionary keys, printing True if so, and False if not.
4. The "not" keyword
01:09 - 01:33
Next is the not keyword, which is used to confirm that a condition has not been met.
We can combine it with the in keyword to confirm that a value is not in a data
structure! Here, we check if "OS31" is not in the dictionary's keys. If not, we print
False, otherwise, we print True.
5. The "and" keyword
01:33 - 02:01
Another important keyword is and, which allows us to check if multiple conditions are
met. Here, we check if "HT91" is a key in the dictionary and that the minimum price
of all products is more than 5 dollars. If both of these conditions are met, we print
True. If either or both of these conditions are not met, we print False.
6. The "or" keyword
02:01 - 02:42
The last keyword for us to discuss is or. This allows us to provide multiple conditions
and evaluate if at least one of them is true. Modifying our previous example, here,
we again check if "HT91" is a key in the dictionary, but now we use or to look at
whether the minimum price is less than five dollars. If one or both of these conditions
is met then we print True, otherwise, we print False. As "HT91" is a key in the
dictionary, it returns True, despite the minimum price being more than five dollars.
7. Adding/subtracting from variables
02:42 - 03:16
We can combine keywords with other techniques to build custom workflows. One
example is updating variables based on loops or conditions. Recall that we can
increment the value of a variable using a for loop, like so. We can also swap the
plus-equals for minus-equals to subtract one from the value of a variable, such as
stock. However, we might need other ways to update variables, such as a data
structure like a list.
8. Appending
03:16 - 03:51
Say we want to use a list to store information that meets certain criteria, such as the
product IDs for all products that cost at least 20 dollars. To do this, we first create an
empty list, like so. We then loop through the keys and values in our dictionary. Inside
the loop, we check if the value, or price, is 20 or more. If so, we the list.append
method to append the key, or product ID, to the list.
9. Appending
03:51 - 03:59
Outside of the loop, we print the list, which returns the four product IDs that met our
price criterion!
10. Let's practice!
03:59 - 04:04
Appending to a list
You've been provided with a dictionary called authors, which has information about 20 of the
most popular fiction authors in the World. It contains the names of authors as keys and the
number of books they've created as values.
You're interested in finding out how many of these authors have made less than 25 books. To do
this, you will create a list called authors_below_twenty_five, filling it with author names
conditionally based on whether they have created less than 25 books.
Instructions
In this exercise, data about the same authors has been aggregated to create a new dictionary
called genre_sales, where the keys are the genre and the values are the average sales for that
genre among the 20 most popular authors.
Your job is to find the most and least popular genres among these authors, along with their
average sales revenue.
Instructions