Coding 101: Python for Beginners | Alvin Wan | Skillshare

Playback Speed


  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

Coding 101: Python for Beginners

teacher avatar Alvin Wan, Research Scientist

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

17 Lessons (2h 6m)
    • 1. Introduction

      1:57
    • 2. Project: Build a Nifty Tool

      3:30
    • 3. Variables, Data Types, Expressions

      9:20
    • 4. (Bonus) Guided Exercises for Variables etc.

      9:25
    • 5. Collections

      8:56
    • 6. (Bonus) Guided Exercises for Collections

      9:48
    • 7. Using Functions: Email List Reformatter

      9:46
    • 8. (Bonus) Guided Exercises for Using Functions

      10:32
    • 9. Defining Functions: Temperature Report

      7:58
    • 10. (Bonus) Guided Exercises for Defining Functions

      9:15
    • 11. If, While: Umbrella Assistant

      9:57
    • 12. (Bonus) Guided Exercises for If, While

      11:24
    • 13. For Loop: Password Strength Checker

      6:31
    • 14. (Bonus) Guided Exercises for For Loops

      7:07
    • 15. Using Objects: Birthday Countdown

      4:57
    • 16. Sample Project: Secret Messages

      4:02
    • 17. Conclusion

      1:44
  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

26,055

Students

56

Projects

About This Class

What You’ll Learn

You’ll learn the fundamentals of code using Python. However, unlike most coding courses, this class doesn’t give you an exhaustive list of minutiae; instead, you learn just enough to start using code. The goal is for you to finish the class with (a) a set of nifty tools, (b) the ability to write more nifty tools, and (c) the recognition of code’s value in automating day-to-day functions. You’ll cover the following concepts throughout the course:

  • Data types, expressions, variables
  • Functions
  • Objects
  • Control Logic

What You'll Build

Along the way, you’ll create a set of tools to demonstrate these concepts:

  • Email List Reformatter
  • Temperature Report
  • Umbrella Recommender
  • Password Strength Checker
  • Days Until your Birthday
  • Secret Messages

The class is designed for anyone with little to no knowledge of coding. No prerequisite knowledge is required. All you need is a laptop, internet, and an hour of time.

Exclusive Resources

The class will include links to exclusive resources including the following:

  • All 6 completed nifty tools
  • 70+ practice problems with solution walkthroughs (an hour of bonus content!)
  • Extra 27 practice problems with full working solutions
  • Downloadable slides, with access to all updated versions in the future
  • Links for further reading

Want to learn more?

Interested in creative coding? Check out my VR 101 (AFrame Nature Scenes) class.

Interested in data science or machine learning? Check out my SQL 101 (Database Design), Data 101 (Analytics), or Computer Vision 101 (Applied ML) classes.

Follow me on Skillshare to be the first to hear about more courses in these areas!

Acknowledgements: This course roughly follows UC Berkeley’s introductory computer science textbook: composingprograms.com. B-roll used in introductory video CC-0 licensed by Pexels.

Meet Your Teacher

Teacher Profile Image

Alvin Wan

Research Scientist

Top Teacher

Looking to learn coding? machine learning? Let me help! I was formerly a computer science lecturer at UC Berkeley, where I served on various course staffs for 5 years. I'm now a research scientist in industry, working on cutting edge computer vision. I've designed a few courses to get you started -- not just to teach the basics, but also to get you excited to learn more. Check out the courses on my profile! Or scroll down for a guide to getting started.

Website | Github | YouTube | Twitter | Research

 

Featured Reviews

"Alvin Wan is a fantastic teacher. The instruction format was just what I was looking for. This is fu... See full profile

Class Ratings

Expectations Met?
    Exceeded!
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. Introduction: I had a hard time getting started with programming. The courses took seven, eight,12 hours and started with laundry lists of Python utilities. But I wanted to build an awesome website, a stunning game. If you feel the same way, I built this course just for you. Hey, I'm Mao. I'm a data scientist in a small startup and a computer science PhD student at UC Berkeley. I've taught hundreds of students how to code through Airbnb. I'm excited to bring all of these in-person teaching experiences online for you right now. Python is my language of choice for software engineering, data science, cutting edge AI research. In just 60 minutes, you'll cover core coding concepts, concrete skills, not just theoretical superpowers after a whole day of classes. I've also included over an hour of additional bonus content, more exercises, walkthroughs, and tips. You won't need to install anything on your computer. We'll instead use a free online tool called repl.it. I want to take you on the most direct path to building nifty tools that you can benefit from today. Once you finish this class, you'll have the power to begin automating parts of your day to day life with Python. There are two goals for this class. One, I want to get you excited for code. Two, I want to give you just what you need to start growing and learning. We won't work through an exhaustive list of Googleable Python utilities. Instead, we'll work on what you can't Google. This class is for any beginner to take. You could be a painter, an architect, a designer, a completely non-technical person. I'm excited to share with you the power of code, the excitement, and thrill. It's not some distant hard-to-reach enigma. All you need is a computer, internet, and an hour of time. Let's give coding a try. 2. Project: Build a Nifty Tool: Your project is to write a Nifty tool for yourself. Pick a super simple task. In this class, we'll cover examples related to manipulating text, calculating dates, and using weather information. I suggest starting from one of these categories. Your Nifty tool should automate a function that you often perform. Don't worry if you can finagle Excel to do the same thing or if you know that websites a, b, and c together can do something similar. If you can cut down the amount of time that it takes to complete a task from 60 seconds down to five, then that's a winning project idea. Building a tool or, in particular, building anything with code is a great way to see how much you've [inaudible]. It's how I started coding. In the introduction, I mentioned two goals for this class. Goal number one is to pique your interest in coding. It's okay to not master code in one hour. The goal is to show you how much you can do with code by just spending one hour. Now imagine what you could do with one more hour with the Data Science 101, SQL 101, or Computer Vision 101 classes. If I can get you interested in code, you'll be unstoppable. Goal number two is to give you just enough so that you can start learning and growing. There are two takeaways that you'll need for me to accomplish that. The first important takeaway is knowing what to search for to build what you want. When I first started coding, I googled "how to do x in Python" all the time. It's completely okay. You have to know what x is though, what to search for on Google. My goal is to give you this sense, this intuition in the next few lessons. The second most important takeaway is being able to read what you find. Even just a vague understanding is okay. As long as you keep coming back and building Nifty tools for yourself, over time you begin to develop familiarity and coding skills. In this class, we'll cover the bare bones of coding: basic terms, usages, and concepts. You'll be coding every step of the way as we build Nifty tools for you to use. Each lesson introducing new concepts will have a concept in the title, like variables. These are must-take lessons to build coding fundamentals. They also include coding demos for you to try. These lessons are followed by bonus content, which includes a number of different guided exercises for you to practice with. The bonus content in this class covers a total of over 70 short exercises. I suggest learning in layers. Start off by taking all the concept lessons and skipping all the practice. Then once you finish the class one time, I suggest doing it again, but this time including the practice so that you can layer on knowledge as you're ready to take it in. Here are a few tips that I have. Tip number one: to err on the side of caution, always copy the exact code that I have. All completed code will be available at this URL. Tip number two: pause the video when needed. I'll explain each line of code I write. But if you need time to type and try code to yourself, don't hesitate to pause the video. Tip number three: you learn best by doing. I suggest setting up yourself for success by placing your Skillshare and coding windows side-by-side, side shown here. One final note, this is a live class. Sure, I've recorded it already. But as you send feedback, leave reviews, and discuss, I'm watching, tweaking the classes, and adding or removing content based on common confusion. Please ask me questions in the discussion tab. Leave me a review. This class is built for you. I hope you're ready to code. I hope you're excited. I know I am. Let's start automating parts of your day-to-day life. 3. Variables, Data Types, Expressions: In this lesson, you'll write your very first lines of code. You'll touch on and use a few different concepts. However, rather than define them upfront, we'll define these terms as we use them. Here's what we'll be learning. Don't worry. These terms should all look foreign to you. This is just to say we'll be learning a lot. At a high level, we'll discuss how to hold data in these next two lessons. Start by navigating to repl.it. In particular, repl.it/languages/python3. I suggest pausing the video here and create an account if you haven't already, whereas you can code without an account, I suggest creating an account to save your code. After creating an account, make sure to navigate back to this new page repl.it/languages/python3. You should then see a screen like this one. In this lesson, I recommend following along. Just copy the code I have so you can see firsthand what your code does. First, on the right-hand side, this orange carrot denotes an interpreter that is waiting for our input. This interpreter will read your input, evaluate that code, and return the resulting value. Let's try this now. Now, let's begin coding on the right-hand side of your screen in the Python interpreter. Go ahead and type in five. This is a number, hit "Enter", Python evaluate it, and then return five. Let's now type in a decimal, 5.2. Hit "Enter", Python evaluate it, and then return the 5.2. Now let's try an expression. Here we'll type in 5 plus 2. The space between the plus and the numbers is optional. This is just convention to keep code. Like before, Python will evaluate this and return the result. I'm being deliberate about the use of the word "return". We input 5 plus 2, and Python returns the resulting value 7. We'll see later why returning is such an important concept. We can also try other expressions, such as 5 minus 2 and 4 divided by 2. Now, how do we define a variable? We can define a variable by assigning a value such as five to the variable name x. Now, can we customize these variable names? The answer is yes. You can use a, you can use z, you can use nyancat. You can use whatever you'd like with a few different restrictions. Your variable name can only contain letters and underscores. No periods, no spaces, no dashes; only letters and underscores. The next question is, can we customize the value? The answer is again, yes. You can add two, you can add an expression such as 3 plus 2. In some, we can change the variable name and we can change the value. However, the symbol in black, the equal sign, is always the same. Anything colored in black in these visualizations is fixed, like the equal sign here. Now let's try this in code. Still in our interpreter on the right-hand side, go ahead and start by defining a variable x and assigning it the value five. Go ahead and hit "Enter". Notice that no value is returned. There's no return value when you assign a variable. You can use whatever variable name you'd like. Remember, variable names can only include letters and underscores. Let's go ahead and now examine what the contents of the variable x contains. Type in x and hit "Enter". Just like before, this x is an expression. We input x, Python recognizes this as a variable name, and returns the value that x contains, which is five. Now here's a bonus tip. If you get a name error, this means you have a typo in the variable name. For example, we have not yet defined the variable u. So if I type in u and hit "Enter", the interpreter evaluates u, can't find the variable, and raises an error. Again, I'm using the word "raises" and "error" deliberately. So far we have learned that the interpreter can either return a value or raise an error. We also mentioned assigning a variable to an expression. Here we can define x is equal to 3 plus 2. Now, x is actually assigned to the value five. Go ahead and type in x, hit "Enter", and we can see that the variable x contains the value five. Now, how do I use variables? Remember from before, we wrote 5 plus 2. We know Python will return seven. This time, since x is equal to five, we can replace five with x. Just like before, this expression will return seven. Let's try it. Double-check that x is equal to the value five. Type in x and hit "Enter". Now, go ahead and type in x plus 2. This returns seven as you expect. Now here's an extra challenge. I'm going to now type in x equals to x plus 2. What do you think this line of code does? Here we have x is equal to x plus 2. We have the variable name and the expression. Now the variable x at this point is equal to five. We can plug in five for x, and now we know that the right-hand side evaluates to seven. We expect x to equal to seven. Here, let's go ahead and hit "Enter". Again, there is no return value, type in x, and we can see now that x is equal to seven. I hit "Clear" so that the code isn't clustered at the bottom of the screen. However, you don't need to hit "Clear" yourself. Now, let's talk about two other data types. Next up is text. We call text strings. Here is an example string: "Hello". Notice that the string is denoted by two double quotes. Make sure to use both start and end quotes. Between these quotes, you can add whatever characters you'd like besides quotes. Let's try some more text. Here's another example: "I love watermelon". Tip, always make sure to add a closing quote. Here's what it looks like if you forget. This EOL or end of line error means that we forgot a quote. You can also use single quotes for text. For example, here's a single quote and the text that we typed in earlier. For the purposes of this class, there's no difference between a single and a double quote. Now, what do you think this does? What does this plus do? As you may have guessed, it simply combines a strings together. Go ahead and hit "Enter", and you can see that the two strings are now combined. Notice there's a space after the double quote in the second string and before the W in World. This is to ensure that we have one space between the two words, Hello World, in our final string. Our last datatype is true or false. These are called Booleans. Just to show you how they're spelled, this is Boolean. Let's try it now. Go ahead and type in True with a capital T, and that's simply evaluates to true. Go ahead and now type in False with a capital F, and this also just evaluates to false. Now it's not very interesting, so let's see what other expressions can return Booleans. Like before, Python can read the input, evaluate the expression, and this time return a Boolean. Let's try one of those expressions. We can type in five greater than two, and that returns true. We can also use variables in our expressions. Recall that x has the value seven from before. Now, let's type x greater than 10. Here, Python will read x greater than seven, evaluate it, and return false. We can also check equality. To check equality, use two equal signs. Here we'll type in x equal equal to seven. Remember that one equal sign would assign x to the value seven. Two equal signs checks if x is equal to seven. Go ahead and hit "Enter", and this gives us true as expected. Now what if you want more complex conditions? We want to see if x is equal to seven and if x is greater than seven. Simply use the word "and". Here we can type in x is equal to seven and x is greater than 10. We know from before that x is not greater than 10. As a result, this will return false. We can also use "or". Let's go ahead and try that, x is equal to seven or x is greater than 10. We know that x is equal to seven, so this will return true. That's it. That was a lot of concepts and it's okay to feel overwhelmed at this point. You don't need to memorize everything. We're going to repeatedly use these concepts and by coding repeatedly in the next few lessons, you'll slowly get an intuition for what's going on. You've learned a lot in this lesson. This is a quick summary of the concepts you've touched on: data types, operations, expressions, and variables. Good work. That's your very first piece of code. We looked at storing one piece of text, one number, one Boolean. What if we have many pieces of texts, many numbers? In the next lesson, we'll look at collections of data. 4. (Bonus) Guided Exercises for Variables etc.: Welcome to bonus content for lesson 3, guided exercises. In this video, we'll walk through exercises covering concepts from the last lesson. There are three steps. Number 1, I will present an exercise. Number 2, you should pause the video and try the exercise on your own. Number 3, after you've made an attempt, play the video, and I will walk you through the solution. The goal is to make you comfortable with using these coding concepts and hearing the terminology. Some exercises will start with practice, which means we're covering a concept that previous exercises have not yet covered. All concepts will come from the previous video. The next few exercises after practice will then be prefixed with quiz. This means the previous exercise already covered the same concepts. You should try these quiz exercises as a test of your knowledge. Let's hop right in. Just like before, start by navigating to repl.it/languages/python3. Let's start with question number 1, this is a practice question. Output 9 in the interpreter using only the number 3 and any math operations you want. Give it a minute to try. Here's the solution or here's one solution, I'm going to type in 3 times 3. Question number 2, this is now a quiz question. Output number 5 in the interpreter using only the number 3 and any math operations that you want. Give it a try. Now, here is a possible solution, I'm going to type in 3 plus 3 minus 3 divided by 3. This will give us one, this will give us six and we have 6 minus 1 is equal to 5. Question number 3, we now have a practice question. Define a variable called x with the value eight. Give it a try. Here we have the solution x is equal to eight. Question number 4, define a variable called y with the value of three. Give it a try. The solution here is y is equal to three. Again, these spaces are optional and these are just the convention that I've been following. Question number 5. Practice: Define a variable called z with the value five using the variables x and y instead of the number 5. Remember here x is eight, y is three. Give it a try. Here's the solution and we're going to now type in z is equal to x minus y. We are going to double-check that z has the value five, and indeed it does. Question number 6, this is now a quiz question. Define a variable called a with the value 11 using the variables x and y instead of number 11. Again, x is eight, y is three, give it a shot. Now we have the solution a is equal to x plus y, double-check that a has the value 11. Question number 7. Practice: Define a variable sound containing the string cluck. Give it a try. Now we have sound is equal to "cluck". Here I'm using double quotes, you can also use single quotes for your string. In this particular question, it doesn't really matter which one you use. Let's go ahead and move on to question number 8. This is now a quiz question, define the variable sound containing the string moo. I'm going to type in sound is equal to "moo", and hit "Enter". It's going to move on to the next question, question number 9. This is now a practice question, define a variable containing the exact string denoted here in red. Remember, you can use double quotes or single quotes to denote a string, and this case the string contains a double quote. Give it a try. Here's the solution, because this string contains a double quote, you'll have to denote the string using single quotes. Let's go ahead and define the string using single quotes. Again, this doesn't say what the variable names should be, so I'm going to call the variable name quote is equal to, 'and she said, "Yum!"', add a single quote at the very, very end. Go ahead and hit "Enter", and there is our quote string. Now, here I'm going to click on ''Clear'' in the top right, you don't have to do the same, but if you do, make sure you click back into the window and hit "Enter" so that you see the orange color to once more. Question number 10, this is now a quiz question. Define a variable containing the exact text denoted here in red. Give it a try. Here's the solution, I'm going to type in now, a variable quote is equal to, and because this string contains double quotes, I will use single quotes to denote my string, 'and I said, "Ew!"'. Perfect, hit "Enter" and there is our string. Question number 11. Let's go ahead and define a variable containing the exact text denoted here in red. Here's a curveball though, the string contains a single quote now. Give it a shot. Here's the solution, because the string contains a single quote, we actually need to denote the string using a double quote now, the reverse of what we did before, type in quote is equal to, "Euh, and then 'merrpppp' ", and there is our variable. Question number 12, here's another practice question. Define a variable b that is true if x is eight. Give it a shot. Here's now the solution, b is equal to x is equal, equal to eight. Recall that to check equality, we use two equal signs. Go ahead and type in or hit "Enter". To double-check that this worked, we're going to check that b has the value true because x is eight and is indeed true. Question number 13, define a variable c, that is true if x is greater than five. Give it a shot. Now here's a solution, c is equal to x is greater than five, hit "Enter". Because x is eight, x is indeed greater than five, double-check that c is actually equal to true, and indeed it is. Question number 14. Practice: Define a variable d that is true if the variable sound is "moo". Give it a shot. Here's the solution, d is equal to sound is equal, equal to "moo", and hit "Enter". We're going to double-check that d has the value true because sound is indeed equal to moo, there we go. Question number 15, here is now a quiz question. Define a variable e that is true if the variable sound is "yuck". Give it a shot. Now we have e is equal to sound, is equal, equal to "yuck". Because we know that sound is equal to "moo", e should be false. Type in e and hit "Enter", and that is now false. Question number 16, this is now a practice question, define a variable f that is true if x is eight and if sound is "yuck". Give it a shot. Here's now the solution, we have f is equal to x is eight and sound is "yuck". Hit ''Enter''. Because we know that x is eight, this first part of the statement it's true. However, sound is "moo", so the second part of the statement is not true, therefore, this entire statement should be false. Go ahead and type in f, and it is indeed false. Question number 17, this is now a quiz question. Define a variable g that is true if x is five and the sound is "moo". Give it a try. Here's now the solution. You don't have to do the same, but I've clicked ''Clear'' in the top right. If you do the same, make sure to click into the window, hit "Enter", and there we go. Let's go in and type out, now the solution g is equal to x is equal, equal to five and sound is equal, equal to "moo". Here we know that x is in fact eight and not five, so this first part will be false, making the entire statement false, go in and type in g "Enter", and it is indeed false. Now for a quick temperature check, how are you doing? If this was doable, that's great. If this was barely doable, as in you got half the questions correct or more than a third, that's also great. As we progress, you'll hear more of these terms and concepts repeated again and again and again. That's how you'll build familiarity. We're looking for familiarity, not an A-plus. Number 3, it wasn't doable as in you only got one or two correct for the 17, no worries. Believe me, I know what's that's like. I suggest during this walkthrough again or rewatching the previous video, you can do it. Regardless of where you are, mastery is when you can come up with your own questions. If you feel you've mastered this lesson, try to come up with a few extra questions and solutions. Leave them in the discussion section of the class, and your fellow students will thank you for the extra practice. That's it for lesson 3's guided exercises. 5. Collections: You'll create your first collections of data. Like last time, you'll touch on a number of different concepts. Like before, instead of defining terms upfront, we'll define these terms as we use them. Here's what we'll be learning. Don't worry, just like before, these terms should all look foreign to you. At a high level, we'll discuss how to hold data in these next two lessons. Start by navigating to repl.it/languages/python3, you'll then be greeted with a screen like this one. We start with a collection called a list. A list is what the name implies, a list of items. How do I define a list? We can make this concrete by looking at some code. We always use square brackets, one to start the list and one to end the list. We also always use commas to separate each item in the list. In this case, our items are numbers. You can also have lists of strings. Recall, a string is a piece of text denoted by double quotes or single quotes. Let's see this in action. This is a list of numbers. This is a list of strings. There are three possible mistakes or errors. One possibility is a syntax error, end of line while scanning string literal. This means that the double quote I highlighted here is actually the beginning of the final string that never ends. This just means in practice that we've forgot a double quote before alex. So if you receive a syntax error, double-check your double quotes. The second possibility is an ellipses like this one. This simply means that you didn't add the square bracket for the end of the list. If you see these ellipses, go ahead and type in the end square bracket and hit Enter. The third possibility is a fused set of two names. In this case, john and alex have been mashed together because we forgot a comma between those two strings. So if you see names fused together, go ahead and double-check your commas. Now, let's assign a variable to this list. Here is a variable names assigned to the list that we just defined. To make sure we defined this variable correctly, type in names, hitting Enter returns the list of names. However, what if we only want the first name? How do we get an item from a list? Let's look at the list of names we have. We want the first one, the first name. Step number 1 is to know which position this item is in. This item is in the first position. Step number 2, find the index for the item you want. In most coding languages, the first item is actually at index 0, the second item is at index 1, the third item is at index 2, and so on and so forth. In step number 3, use square brackets and index to get the item. This code will return the first name in the list, which is jane. Let's try this now. Let's go ahead and type in names, square bracket of zero. Hit Enter and now we see jane. We can also access the other names in this list. Now, what happens if we access names of 1,000? No such item exists, so we'll get an index error. An index error means that the index we provided is invalid. The list only has three items in it, nothing exists at index 1,000, so Python raises an index error. Just like we can assign variables, you can also set items in a list to a value. For example, we can change the first item in the list to Jen. Go ahead and type in names of zero is equal to Jen. That looks like a lot to unpack, so let's work through it from left to right. Here, names of zero refers to the first item in list. The equals operator assigns the first item to the list to the value. Our value is a string with Jen. To check that our assignment was successful, type in names and hit Enter. This gives us the modified list of names. You can also check which names are in the list by using the in operator. For example, to check if Jen is in the list, go ahead and type in Jen in names. Notice that when we updated the list, we spell Jen with an uppercase J. This means that when we check for containment using in, we have to use Jen with an uppercase J as well. The strings must be identical, and that gives us true as expected. We can also check other names. Let's check if jack is in the list. Jack is in fact not in the list. So as expected, we get false. Now, let's move on to our second data collection, a dictionary. What is a dictionary? A dictionary maps keys to values. Think of it like your dictionary at home, which maps words to definitions. Let's make this more concrete by seeing some code. How do I define a dictionary? We always use curly braces, one to start the dictionary and one to end the dictionary. We use a colon to separate the key from the value. Here, the key is a string denoted using purple. Here, the value is a number denoted using pink. The dictionary and maps the key jane to the value of three. We use commas to separate entries in the dictionary. Let's try this encode now. Go ahead and type in jane colon three and john colon two. This is a dictionary mapping from names to numbers. In the top right, I'm going to go ahead and click on Clear. Again, you don't have to do this. This is just so that code isn't clustered at the bottom of your screen. If you also decide to clear your screen by clicking on the button in the top right, you'll have to hit Enter after hitting Clear so that you can see the orange carrot interpreter once more waiting for your input. Now, let's assign a variable to our dictionary. Let's say that this dictionary maps names to numbers of cookies. So we'll go ahead and call the variable name to cookies. Notice here that names to cookies is variable name, which means we're restricted to only letters and underscores like we talked about in a previous lesson. These are underscores here, not dashes. Let's go ahead and now define name to cookies is equal to the dictionary that we just typed in earlier. So jane has three cookies and john has two. To make sure that we defined this variable correctly, type in name to cookies. Hitting Enter returns the dictionary mapping names to number of cookies. However, what if you only want jane's number of cookies? How do I get an item from a dictionary? All we need is the key we're interested in. In this case, we want jane's number of cookies, so we need the key jane. Use square brackets and the key to get that item. This code will return the number that jane corresponds to, which is three. Let's try this encode now. Go ahead and type in name to cookies and pass in the key jane. Notice that we spelled jane identically both times. The first time was all lowercase and so is the second time. These keys, these strings must match exactly when you're typing them both times. We can also do the same thing with john. Now, what happens if I try looking up the number of cookies for bob? Note that bob is not in our dictionary. So here, we would instead get a key error. A key error means that the key does not exist in the dictionary. If you see a key error, you have a typo in your key. Just like you can modify lists, you can also modify dictionaries. Let's go ahead and type in name to cookies of jane is equal to 10. Here, for the key jane, we changed the value from three to 10. To check that our assignment was successful, go ahead and type in name to cookies. Hitting Enter returns the modified dictionary of cookie counts. You've learned a lot in this lesson. This is a quick summary of the concepts you've touched on: lists and dictionaries, how to both get and set elements, and finally how to check containment. If you'd like to access and download these slides, visit this URL. You can now represent any data of your choosing with Python data collections. However, how can we modify these collections of data in more complex ways? Maybe we'd like to reformat some text or perform some computation. In the next lesson, we'll discuss a coding concept called functions. 6. (Bonus) Guided Exercises for Collections: Welcome to Bonus Content. Lesson 4 is guided exercises. In this video, we'll walk through exercises covering concepts from the last lesson. Again, there are three steps for each exercise; I will present an exercise, pause the video and try the exercise, then I will walk you through the solution. Some exercises are practiced when the exercise is the first to cover a concept, and the other exercises are quizzes, you should try testing your knowledge with these. Let's hop right in. Navigate to repl.it/languages/python3. You'll see a page similar to the one on the right. I've minimized a few of the tabs so that you can only see the interpreter on the right hand half the screen. Now, let's go ahead and start off with question number 1. This is a practice question. Define a list with three numbers. Assign the list to a variable called numbers. Give it a minute to try. Now we can define numbers is equal to, and a list with three numbers, I'm going to write 4, 5, and 2. Hit "Enter". We're going to type in numbers to check that our list was defined successfully. Hit "Enter" and there we go. Question number 2. This is now a quiz question. Define a list with the numbers 4, 2, and 7 in any order. Assign your list to a variable called numbers2. Give it a minute to try. Now, here's a solution: numbers2 is equal to [4, 2, 7]. Again, don't forget your opening and closing square brackets. Now, question number 3. This is a quiz question. Define a list with the strings "a", "b", and "c." Assign the list to a variable called strings. Give it a minute to try. Here's the solution. Typing strings is equal to a list of strings ["a", "b", "c"]. Go ahead and hit "Enter". We're going to double check that this list was defined successfully, and indeed it was. Question number 4: define a list with one boolean. Assign the list to a variable called booleans. As a hint, remember booleans are either true or false with capital Ts and capital Fs. Give it a minute to try. Here's the solution. We're going to define a list called booleans is equal to, and this list will only contain one boolean, in this case, we'll say True. Go ahead and hit "Enter". Once again, we'll check that this list was defined according to way that we wanted, and there we go. Question number 5. We now want to get the first number in the list "numbers." Give it a minute to try. Here's the solution. Go ahead and type in numbers, square bracket of zero. Again, remember that zero is the index of the first item, so this will fetch the first item in the list of numbers, which, in this case, is the number 4. Go ahead and hit "Enter", and there we have four. Question number 6, this is now a quiz question. Get the third item in the list "strings." Give it a minute to try. Now we have strings, and the third item in the list is at index two. Remember, the first item is at index zero, the second item is at index one, and the third item is at index two, so go ahead and type in square bracket two, "Enter", and there we get the third item in the list. Question number 7. This is now a practice question. Define a dictionary mapping strings to Booleans. Assign "Jack" to False, "Jane" to True, and "Alex" to True. Assign the final dictionary that you get to a variable called "name_to_hungry." Give it a minute to try. Now we start off by defining the variable, so name_to_hungry is equal to, and now we're going to define the dictionary. Dictionaries are denoted by a curly brace, and we'll now type in the key which is "Jack" and the value which is False. Then the key is "Jane" and the value is False. The final key here is "Alex" and the value is True. Go ahead and add that curly brace. You'll notice that in this interpreter, the line is wrapped around. If your interpreter is wider than this or if your screen is wider than this, then you won't see this line wrap around. I didn't hit "Enter" or create a line break, this interpreter wrapped around on its own. Go ahead and hit "Enter". We want to double-check that this dictionary has what we want, so type in name_to_hungry, and that is the dictionary that we indeed want. Go ahead, and I'm going to hit "Clear" in the top right. You don't have to do this, but if you do, click into your window and make sure to hit "Enter" so that you see this orange carrot once more. Question number 8. Define a dictionary mapping names to numbers. Assign these keys to these numbers and assign the final dictionary to a variable called "name_to_burgers." Take a minute to try this question. Now, here is the solution. We're going to define the variable, name_to_burgers, is equal to, and here we have a dictionary denoted with a curly brace. We're going to assign the string "Emily" to one, the string "Jenny" to the number two, the string "Neel" to the value three. Go ahead and end this with another curly brace, hit "Enter", and we're going to double check this dictionary. There we go, we have what we want. Question number 9. Here's another practice question. Define a dictionary mapping strings to strings. Assign the dictionary to a variable called "names_to_ names." Go ahead and give it a minute. Here's the solution. Go ahead and type out names_to_names is equal to, and we're going to say "Jack" to "Emily", and then "Jenny" to "Neel", and "Alex" to "Bob". Go ahead and hit "Enter", and there is our dictionary mapping names_to_names. Question number 10. This is now a quiz question. Define a dictionary mapping numbers to strings. Assign the dictionary to a variable called "age_to_name". Give it a minute to try. Here's the solution. We're going to type out age_to_name is equal to, and dictionary with a curly brace, and we're going to come up with arbitrary numbers to strings. Here we're going to say, 14 to "Bob", we're going to say 15 to "Alex", and let's say 20 to "Jenny." Go ahead and hit "Enter", and there is our dictionary. Question number 10. This is now another practice question. Fetch the boolean corresponding to "Jack" in the dictionary "name_to_hungry." Give it a minute to try. Here's the solution. Name_to_hungry, and we want the value corresponding to the key "Jack", so we're going to add a square bracket and type in the key. As we'd expect from the previous set of questions, the value is False. Now, question number 12, this is a quiz question. Fetch number of burgers corresponding to "Neel" in the dictionary "name_to_burgers." Give it a minute to try. Now let's type out the solution, name_to_burgers, and the key is "Neel." We're going to type in "Neel" and hit "Enter", and there we get three. Question number 13, this is now practice question. Define a list of dictionaries. Go ahead and give it a shot. Now let's go and type out the solution. Here we're going to have the variable name, list_of_dict. Here we're going to define dictionary, we'll say "a" to one. Because I'm lazy, it's only going to have one key and one value, and we're going to have just a second dictionary in this list. Go and hit "Enter". Again, you can create whatever dictionaries you want with whatever keys and values you want. Go ahead and now move on to next question, question 14. Here's a quiz question. Define a dictionary mapping strings to lists. Give it a minute to try. Here is a possible solution; we're going to define a dictionary, let's say str_to_lst is equal to a dictionary mapping a string, let's say, "Jack" to a list of numbers. I'm going to be lazy here and just add one key and one value. Go ahead and hit "Enter, and there we go, str_to_lst. Once again, let's start off with temperature check, how are you doing? If this was doable, that's great. If this was barely doable, don't worry, keep going and you'll build familiarity as we go. Finally, if this wasn't doable, that's also okay. Watch this video and the previous one again. Each time you rewatch, the concept will sink in a little bit more. If you have any questions, definitely don't hesitate to leave a question in the discussion section in the class. If you feel you've mastered this lesson, try to come up with a few extra questions and solutions. Again, leave them in the discussion section of the class and your fellow students will thank you for the extra practice. That's it for Lesson 4's guided exercises. 7. Using Functions: Email List Reformatter: In this lesson, you'll work with a coding concept called functions and a related concept called methods. You'll touch on a number concepts listed here. Here's what we'll be learning. Go ahead and navigate to repl.it/languages/python3. You should then be greeted with a screen like this one. Let's go back to our slides. First, what is a function? Think of functions you learned in math class from grade school. Functions accept some input value and return some value. For example, consider the absolute value function. It takes in a number and returns the positive version of that number. One example input is negative five. We take the absolute value and the function returns positive five. Let's see what these functions look like in action. In this lesson, we will use several of these functions. How do I use a function? Consider the absolute value function again. In Python, the function's name is just abs. Use parentheses to call the function. Calling the function means we execute the function. In between the parentheses, add any inputs that the function needs. The absolute value function takes in just one input. We also refer to the input as an input argument or just the argument. Other functions may take different numbers of input arguments. For example, consider the function max. This function accepts two input arguments and returns the larger of the two. Again, add parentheses and the inputs. This time use a comma to separate the two arguments. Let's try this now. Go ahead and type in abs and pass in the argument negative five. This calls the function abs with the argument negative five. The function abs will then return positive five. Go ahead and now try this again with max. This time we're going to pass in two arguments, five and seven. The function then returns the larger of the two values, seven. You can also open a file with a built-in function. Go ahead and type in open. The first argument is going to be the name of the file. In this case, test.txt. The second argument of w means that we're opening a file in write mode. Go ahead and hit "Enter". What is that gibberish? That return value is a data type we haven't seen before. To interact with this new data type, we will need to learn what a method is. Back to our slides. What is a method? Each of the data types we've talked about so far. Numbers, strings, functions, these are all types of objects, and a method is a function that belongs to an object. That's three new words in a sentence, but this will be simpler to understand by example. How do I use a method? In this example, we're going to split a string into a number of smaller strings. First, you need an object. Here, we have a string object. Next, add a dot. This dot means we're about to access a method for the string object. Add the method name. In this case, the name is split. This method split will separate the string into many pieces. To call this method, just like you call functions, add parentheses. In between your parentheses, add your input argument. Here are all the parts annotated. From left to right, we need the object, a period, the method name, and the input arguments. Let's try this now. Go ahead and type in the object. This is a string object. Now we can call the method split with the argument of a comma. This will separate the string into three pieces by splitting the string at each comma. Hit "Enter" and you can now see the resulting list of three strings. There are other methods as well. For example,.upper. This method will uppercase all letters. Go ahead and hit "Enter". Now back to our file object. We can, after loading the file, use the read method to read the contents of that file. Here we get an error because the file test.txt does not exist yet, but that's okay. Later on, we'll reuse the read function. For now, we've covered built-in functions; however, some functions are not immediately available. For example, say we want to read a webpage, we would then need to import a function that reads a webpage. Let's do this now. From urllib.request, import urlopen. Breaking this down, urllib.request is the name of a library. A library is a collection of Python code someone else has written that we can use. Urlopen is the name of the function we want. We can use this function to open a webpage. Go ahead and hit "Enter". Now, we can use this function. We're now going to write page is equal to urlopen and then a mysterious URL. Here, I'm going to actually drag this right-hand side so that the window is a little bit wider. Go ahead and hit "Enter". Once that's read the webpage, go ahead and type in page.read. That's a mess, but that's okay. We don't need to understand the contents of this webpage just yet. Later on, we'll see how to properly read webpages so that we can actually understand the content. For now, you've covered a number of different important fundamentals in coding. We're going to use what we've learned so far to write a simple nifty tool that reformats a list of e-mails into a column of e-mails, one e-mail per line. Up to this point, we've been working in the Python interpreter. We type one line of code and the interpreter immediately evaluates that line of code. Now, we're going to instead write code in a file. After writing all code and saving the file, we will then run the file. A quick note. Note that the caret on the right is orange. This caret is now at the bottom of the screen where I've highlighted it. This orange caret means that the interpreter is waiting for our input. Later, when your nifty tool is asking for input, you can replace this orange caret with whatever texts you'd like. To the left, it may not be not be y. Start by collecting the input text from the user. To do so, use the input function. Go ahead and type in input and pass in one argument. This argument can be whatever text you would like. I'm going to type in text colon. My string will ask for text and we'll end with a colon. Hit "Run". You'll now see the string that you inputted as an argument. Notice the interpreter's orange caret is missing. This means your program is asking for input and not the interpreter. Type whatever text you would like and hit "Enter". Nothing much interesting happens, it seems. However, your program has finished and the interpreter's orange caret has returned. Well, where did the text go? Quite simply, the input function returned the text and our program didn't do anything with a return value. Let's fix that. Go ahead and assign the user inputted text to a variable called text. Now, we need to tell Python when to output and what to output. To do so, use the print function. Any argument that we provide to the print function will be outputted. Go ahead and type in print of text. This will output whatever the variable texts contains. At the top of the webpage, click on "Run". Once again, you'll see the text prompt. Go ahead and type in whatever text you would like and hit "Enter". You'll then see that your inputted text is outputted by the program. Next, let's actually process this piece of text instead of just printing it out. Delete the last line in the file and add a new line of code. In particular, split the text by each comma by using the split function. Here we'll call text.split and we'll split at every single comma. Assign the result to a variable called e-mails. Again, our goal is to output a list of e-mails in a column where every e-mail is on its own line. First, we'll need the character corresponding to a line break, which is a \n. Next, we'll use the join method for this string. The join method accepts a list of e-mails and inserts the line break between every pair of e-mails. Finally, assign the resulting string to a variable named output. For our last line of the program, output the newly formatted string with one e-mail per line. We're now done with the program, go ahead and hit "Run". Type in the listed e-mails, hit "Enter", and we get a reformatted list of e-mails where we have one e-mail per line. You've learned a lot in this lesson, as usual. This is a quick summary of the concepts you've touched on: calling functions, calling methods, and importing functions. If you'd like to access and download these slides or view completed code for the example in this lesson, visit this URL. You can now perform advanced data modifications, but what if you want a custom function? In the next lesson, you'll define your first functions. 8. (Bonus) Guided Exercises for Using Functions: The Bonus Content, lesson 5's guided exercises. In this video, we'll walk through exercises covering concepts from the last lesson. Again, there are three steps for each exercises. One, I will present an exercise. Two, pause the video and try the exercise. Three, I will walk you through the solution. Some exercises are practice. On the exercise is the first to cover concept and the other exercises are quizzes. You should try testing your knowledge with these. Let's hop right in. Navigate to repl.it/languages/python3. You should then see a page like the one on the right. I have split my screens that you can see the question on the left hand side and the interpreter on the right hand side. Question number 1. This is practice. A function "min" returns the smaller of two numbers. Call "min" with two numbers as input arguments. Give it a try. Here's another solution. Min, and I will put in two arbitrary numbers two and six. Question number 2, this is a quiz. A function "max" returns the larger of two numbers. Call "max" with two numbers as input arguments. Give it a try. Here's now the solution. We have max and I'm going to type in the two same numbers, and there we go. Question number 3. This is again practice. A function "sum" returns the sum of a list of numbers. Call "sum" with a list of numbers as input. Give it a try. Here is now a solution. Sum, add a parentheses to call the function, and then add a square bracket to denote your list. I'm going to pass in a bunch of random numbers here. I'm going to type in 4, 8, and 6. I'm going to close the list with the square bracket and then end with a END parentheses. Go ahead and hit "Enter" and we get our sum. Question number 4. This is now a quiz question. A function "all" returns whether all booleans in the list are true. Call "all" with a list of booleans as input. Recall booleans are true or false with capital T and capital F. Give it a try. Here's now the solution. We're going to type in all parentheses to call the function, and we're going to create a list of booleans with square bracket for a list and then we're going to type in True, False, False. End the list and the function call and hit "Enter". Question 5, here's now a quiz. A function "any" returns whether any boolean in a list is true. Sorry, this is a typo. Here, let me go in and fix that right now. Call any with the list of booleans as input. Give it a try. Here's the solution. We type in any and then give it a list of true, false, false, end the list, end the function call, hit "Enter", and there we go. Question number 6. This is now a practice question. A string method "lower" takes zero arguments and returns a lowercase version in the string. Lowercase this string. Give it a try. Now let's see the solution. We're going to type in "HAHAHA". To call a method, remember you need dot and then the method name. In this case the method name is lower and there are zero arguments, so we just add parentheses with nothing in between. Go ahead and hit "Enter" and there we go. Question number 7. This is now a quiz. A string method "lstrip" takes zero arguments and removes leading spaces. Leading spaces are any spaces that the string starts with. Go ahead and remove all leading spaces from "hehe." Give it a try. Here's now the answer. We're going to just type in the string that we had from before here. We have double-quotes space "hehe" and we're going to write.lstrip with no arguments, hit "Enter" and we've removed all leading spaces. Question number 8. This is now a practice. A string method "endswith" takes one string argument and returns true if the string ends with that argument. We want to check if he@d.com ends with.com. Give it a try. Here's now the answer. We're going to type in "he@d.com".endswith, and we're going to have another string, that is,.com. This should give us true as expected. Question number 9. This is now a quiz. A string method "startswith" takes one string argument and returns true if the string ends with that argument. Check if "Mr. Bean" starts with "Mr.". Give it a try. Here's now the solution. We're going to create the string "Mr. Bean", and then we're going to type.startswith. Here we'll have another string that is "Mr." and that is also true. Question number 10, this is a quiz question. A string method "find" takes one string argument and looks for that argument in the original string. Find the position of cali in supercalifragilistic. Go ahead and give it a shot. Here is now the answer. We're going to type in "supercalifragilistic.find" and then a string "cali". It's at position 5. Question number 11. A string method "count" takes one string argument and counts the number of times that argument appears. Find the number of "i"s in Mississippi. Give it a try. Here is now the solution. We're going to type in "Mississippi".count "i". It shows up four times. Question number 12. A string method "replace" takes two arguments, replacing all instances of the first string with the second string. Replace all "a"s in "hahahaha" with "e"s. Give it a try. Here's now the solution. We're going to type in "hahahaha".replace all "a"s with "e"s. Hit "Enter" and there we go "hehehehe." Question number 13. Here is now a quiz question. Replace all "Bob"s with "Jon" in "Bob poked Bob's dog." Give it a try. Here's the solution. We're going to type in "Bob poked Bob's dog,".replace all instances of Bob with "Jon". Hit "Enter" and there we go Jon poked Jon's dog. Oh, I forgot to clear so that you don't get stuck in the bottom part of the screen. I'm going to click on "Clear" in the top right, and if you do the same, make sure to click into the window, hit "Enter" and ensure there is an orange carrot. Question number 14. A dictionary method "values" returns all values in the dictionary. Return all values in the following dictionary. Give it a try. Here's now the solution. Go ahead and type out that dictionary, and then colon 2. Again, these spaces are optional. I just follow that convention of adding a space after these colons. Go ahead and type in now.values. Before we had a bunch of string objects where those string objects had some methods. Now we have a dictionary with a method. Go ahead and hit "Enter" and we get the values for this dictionary. Question number 15. Here's now a quiz. A dictionary method "keys" returns all keys in the dictionary. Return all keys in the following dictionary. Give it a try. Here's now the solution. We're going to have "a": 1, "b": 2.keys. Hit "Enter" and there we go. Question number 16. Say you have a string, and this string is just a comma. The string method "join" accepts a list of strings as input. For example, this comma.join of this list produces a, b, c. Use join to convert a list of URL pieces into a full URL. For example, from a bunch of these chunks, "Google.com", "Mail", and "Hi", to "google.com/mail/hi." Now, you may think that this is cheating because I gave you the solution right here with an example. But in reality, this is what happens when you Google a function or a method online. You'll see a bunch of examples that follow the definition of the method or function. In fact, this is a fairly reasonable expectation to have when you're trying to use a function. Go ahead and give this a shot. Here's another solution. We're going to write out the /string.join, and then we're going to pass in the list of strings: Google.com, mail, and then hi. Make sure to end your list and then end your function call. Hit "Enter" and there we go. Now, this was a particularly challenging question just because this is a really weird Python syntax, but this is just how Python does it. This is how you combine a bunch of pieces of strings with some separator. In this case, the separator is the slash. Once again, check how are you doing. This was a difficult section. Note that in reality, you can always Google how a function or method is being used by others. You can also try the function or the method in the interpreter to see how it works. As a result, there's no need to memorize these functions or these function's usages. You just need to know what calling a function or method means, which you've now done dozens of times in this video. If this is doable, great. If this is barely doable, don't worry, keep going and you'll build familiarity as we progress in the subsequent lessons. If this wasn't doable, that's also okay. Just watch this video and the previous one again. Each time you rewatch, the concepts will sink in a little bit more. If you feel that you've mastered this lesson, try to come up with a few extra questions and the solutions. Leave them in the discussion section of the class and your fellow students will thank you for the extra practice. That's it for lesson 5's guided exercises. 9. Defining Functions: Temperature Report: In the last lesson, you learned how to use functions. In this lesson, you'll learn how to define functions. Here's a list of concepts that we'll touch on. You'll learn how to define functions, you'll learn several concepts related to that, arguments and return statement. Start by navigating to repl.it/languages/python3. You should then see a screen like this one. To start, how do we define a function? As we mentioned before, think of functions from your math class. As a recap, functions accept some input value and return some value. Consider the square function. Taking a number x, multiply x with itself, and return the squared number. Here, start with def, D-E-F. Then follow it with the function name. Then a set of parentheses and a colon. In between the parentheses, add your input argument. In this case, our function square only takes in one argument, which will denote x. Then add two spaces. These two spaces are extremely important. These spaces are how Python knows that you're now adding code to the function. Since this function is simple, the first and only line of our function is a return statement. The return statement stops the function and returns whatever expression comes next. For us, the expression is x times itself. Here are all the parts annotated once more. Note that all parts in black are needed to define any functions. You always need a def, parentheses for the input arguments, and a colon to denote the function is starting. You also need the return statement to return values to the programmer calling your function. The function name, inputs, and expressions could all change. How do I use my own function? Just like you would call any other function, use parentheses and pass your input argument in. In this case, we pass in the argument value 5. Your function then accepts the argument 5 and assigns the variable x to the value 5. Effectively, the square function replaces all x's with fives and finally returns 5 times 5 or 25. Now, let's try coding this up. Like at the end of the last session, you will now write code in a file instead of in the interpreter to the left in main.py to find your first function. Typing def square x and don't forget the colon at the end of the first line. Go ahead and then return x times x. That's a lot at once. But fortunately, this is exactly what we just covered in the slides. Make sure there are two spaces before your return statement. Also make sure there's a colon at the end of the first line. As a good sanity check, check that the colors of each part of your code the same as mine. My def and return are blue. My square is purple. Go ahead and hit "Run". Now you can call your brand new function, just like you would call any other function. To the right, in your interpreter called the function, type in square of 5. As expected, this returns the square of 5 or 25. Let's try a few more. Square of negative 1 and square of 3. We will now write our second nifty tool: a temperature checker for any city in the world. Start by importing the URL opening function that we used in the last lesson. We tend to add import statements at the top of our file. So go ahead and at the very top of your file, type in from urllib.request import urlopen. Then define your new function. This new function will give in a city as an input argument, return the temperature for that city by reading a webpage. First, write the boilerplate for a function: def and then the function name get_temperature, and then the argument followed by a colon. Don't forget your colon at the end of that line. As a sanity check, again, check your colors. For the next line, don't forget to add two spaces in front. If you hit "Enter" when your cursor is at the end of that colon, Repl.it will automatically add two spaces for you. Next, construct a URL from the city. Go ahead and type in URL is equal to the address plus the city plus a little more. I'm going to go ahead and drag this right here so that you can see more of my code on the left-hand side. Go ahead and type in, double quote, and then format equals the following. The details of the wttr.in website are not important. All you need to know is that this website offers a weather data if you call a URL like wttr.in/paris. Next, go ahead and load that webpage. Page is equal to urlopen URL. Next, go ahead and read the data on that page. Raw is equal to page.read. We saw before that reading from a webpage can result in a bunch of nonsense output. This is because the return data is in an unreadable format. We're going to call decode so that we can read the return data. Go ahead and type in temp is equal to raw.decode, and utf-8 is just a form of data that we can read easily. Finally, return the temperature. We're now done with the function. Go ahead and hit "Enter", and once you're on a new line, make sure to hit "Backspace" so that you are now no longer in the function. Like before, we will use the input function to ask the user for input. Here, you will ask the user for a city name. Go ahead and type in city is equal to input, double quote, city. This city colon will be the prompt and the input function will return the user inputted text. Now, let's go ahead and call the brand new function that you just created by passing in the city that the user inputted. Type in temp is equal to get_temperature of city. Finally, print the return temperature. Click "Run" at the very, very top. You can see here now there is a prompt asking us for a city. Go ahead and type in whatever city you would like. I'm going to type in Paris, and there we go. Your nifty tool then returns a temperature. Nice work. You'll notice that there is now an orange caret indicating the Python interpreter and not your program is listening for your input. In order to try the program again, go ahead and hit "Run" at the very top of your screen. If you receive none instead of the temperature, this means that on line 15, the print function was printing a variable with value none. This means that temp has value none. In line 14, temp was assigned to the value none. This means that the function get_temperature must have returned the value none. This means that you have maybe forgotten line 11 in your function get_temperature. A function, by default, without a return statement returns the value none. As a result, if you see the value none in your program output, go ahead and make sure you did not forget line 11. Repl.it automatically saves your work. If you'd like to change the name of your Repl.it for easier access later on, at the very top of your screen, right next to your name, go ahead and click on the title and you can then rename your project. If you run into any errors, make sure to double-check your code against mine. A quick check is, do the colors match? If the colors do not match, you may have forgotten end quote, an end parenthesis, or the colon. Also, don't hesitate to post in the discussion section if you're having any problems and me and your peers will be more than happy to help. You learned a lot in this lesson, as usual. This is a quick summary of the concepts you've touched on: defining functions, arguments, and return statements. If you'd like to access and download these slides or view completed code for the example in this lesson, visit this URL. You can now define custom functions as needed. This completes the section on functions. In the next lesson, you'll learn about control logic. Sounds fancier than it actually is, but you'll find those tools extra helpful in building nifty tools. 10. (Bonus) Guided Exercises for Defining Functions: Welcome to some more bonus content, lesson 6 's guided exercises. I'm sure you're familiar with the format by now. There are three steps for each exercise. One, I present an exercise. Two, you pause the video and try the exercise. Three, I walk you through the solution. Some exercises are practice and other exercises are quizzes. You should try testing your knowledge with quizzes. Let's hop right in. Go ahead and navigate to aaalv.in/coding101/exercises6. You'll then be greeted with a page like this one. In the top left, you can click on the name of the "Repl.8" and click on "Fork." You'll then see an editable version like this one. Notice that I've changed the layout. You will now be using the file editor on the left half of the screen instead of the interpreter on the right. Instead of an orange carrot, you should be looking at the section of the page with a line number 1, 2, 3, so on and so forth. On this screen, I'm going to minimize the left-hand tab here by clicking on "Files" and then I'm going to drag on this center cursor so that my file's a little bit larger. All right, let's go ahead and start off with question number 1. For this first question, we have practice problem, we want to define a function "mul3" that takes a number argument and returns that number multiplied by 3. Go ahead and give it a shot. Here's the solution. Let's define the function def mul3 that accepts a single number argument. Here we'll add a parenthesis, we'll call that number argument x, and then add a colon, don't forget your colon. Then we return x times 3. Go ahead and run to make sure that your function and that your code is valid then we can try our function by typing in mul3 and 5 and that should give us 15. Question number 2, this is now a quiz. Define a function "sub3" that accepts a number argument and returns that number subtracted by 3. Go ahead and try. For the solution, define sub3. It accepts a single number argument and return x minus 3. We can hit "Run" and then try sub3, 10 should give us seven. Question number 3, this is now a practice problem. Define a function prod that accepts two number arguments and returns the product of both numbers. Give it a shot, and here's the solution. Define "prod" that accepts two number arguments. We'll call these x and y, returns the product of both numbers. On the right side we're going to start off by running and then prod 3, 5 for 15. Question number 4, this is now a quiz. Define a function "add" that accepts two number arguments and returns the sum of both numbers. Give it a shot, and here's a solution. Define add x, y, and return the sum of both numbers. Go ahead and run and then we can check, add 3, 5 gives us eight. Question 5, this is now a quiz. Define find function prod that accepts three number arguments and returns the product of all three numbers. I'll go ahead and scroll up a little bit more so you can see the previous solution as a reference. Go ahead and give it a shot. Here's a solution. We'll define prod and we'll give it three arguments and return the product of all three. Now go ahead and run. Here we now have prod 3, 5, 7, and that will give us a big number. Question 6. Define a function "gt3" that accepts a number argument and returns whether or not that number is greater than 3, give it a shot. Now, define the function gt3 that accepts a single number argument, we'll call this x and returns whether or not x is greater than 3. Go ahead and hit "Play" or "Run" and then we'll have gt3 of 5 and that should give us true. Question number 7, this is now a quiz. Define a function "lt10" that accepts a number argument and returns whether or not the number is less than 10, give it a try. Now, here's a solution. Define "lt10" that takes in a number argument and returns whether or not the number is less than 10. Go ahead and run, and then we can check "lt10," we'll type in five and that'll give us true. Question number 8, this is now a quiz. Define a function both gt3 that accepts two number arguments and returns whether or not both numbers are greater than 3. Here's a little hint, recall that x and y for some arbitrary code x and y, all return whether or not both of these are true. Give it a try. Here's now the solution, we'll type in the function name both gt3. This will take in two numbers, x, and y and we'll return x is greater than 3 and y is greater than 3. Go ahead and run the code. In the top right we can now try this code, both gt3 and we'll type in 5 and 7 and that will return true. Question number 9, this is now practice. Define a function sum3 that accepts a list of numbers and return the sum of that list multiplied by 3, give it a try. Now, define sum3 that accepts a list of numbers, we'll call this list "lst" and then we'll return the sum of the list multiplied by 3. Run their code and we can try the code now, sum3 and this will be 1, 2, 3. This sum should give us 6 times 3 should give us 18, hit "Enter" and we got 18. Question number 10, this now a quiz. A dictionary method "values" returns all values in the dictionary. Define a function "sumv" that accepts a dictionary and returns the sum of all values in that dictionary. Now, use your function "sumv" to compute the sum of all values in this particular dictionary. Let's go and define, oops, go, and give a shot first. Now, let's go and define this function def sumv. This will take in a dictionary and it'll return the sum of all the values, so here we'll do sum of all the values. Let's go ahead and run this code and try the code. Here we'll have {"a": 1, "b": 2}, and we'll get 3 as expected. Question number 11. This is a practice question. Define a function "is_jack_one" that accepts a dictionary as input and checks if the number corresponding to "jack" in the input dictionary, is one. Give it a shot. Now, define "is_jack_one," pass in a dictionary, and then we will check if the dictionary's value corresponding to "jack" is equal to 1. Go ahead and run your code. Here we'll write "is_jack_one" and we'll type in { "jack": 1} and yes it is. We'll also try a case where this should give us false so we'll pass in two, and that gives us false as expected. Question number 12, this is now a quiz. Define a function "add3_jack" that accepts a dictionary as input and returns three more than the number corresponding to "jack" in the input dictionary. Go ahead and give it a try. All right, and here's the solution. Let's go ahead and type in define, add3_jack that takes in a dictionary. First, you fetch the value corresponding to "jack," so dictionary of "jack," and then you add three to it. Let's go ahead and run this code and try this code, add3_jack, "jack": 1, and that should give us 4. Then we'll also try a different value "jack": 2 and that should give us five. Perfect. Once again, we want to check how are you doing, if this was doable that's great, if this was barely doable don't worry, keep going and you'll build familiarity. This was a challenging lesson. We continued to not only build off previous concepts but also integrate a number of new concepts. Number 3, if this wasn't doable that's okay, watch this video and previous one again. Each time you re-watch the concepts will sink in a little bit more. If you're feeling you've mastered this lesson just like before, try to come up with a few extra questions and solutions, lead them in the discussion section of the class and your fellow students will thank you for the extra practice. That's it for lesson 5 's guided exercises or lesson 6 's guided exercises. See you in the next lesson. 11. If, While: Umbrella Assistant: In this lesson, we'll cover what is called control logic. In particular, we'll talk about if statements and while loops. Start by navigating to repl.it/languages/python3, you should then see a screen just like this one. What is an if statement? An if statement allows you to run code if some condition is true. Here's how you can use an if statement. Start by writing if, then add your condition. In this case, the condition is checking that x is less than five. Add a colon at the end of the line. Don't forget this colon. Then add two spaces at the start of the next line. These spaces are also extremely important. Don't forget these. Finally, add the code that will run only if the condition is true. Here are all the parts annotated for an if statement. We need the if, a colon at the end of the first line, and two spaces before every line that we want to conditionally run. We can change the condition in blue to anything we want. We can also change the expression in orange however we want. In some, if x is less than five, assign x to the value 10. The if statement has one more optional part. You can also add an else statement. The upcoming code runs if the condition is not true. Like before, add two spaces, then add the expression to conditionally run. Here is the full if-else statement. Say x was previously three. After running this code, what would x be? Pause the video here and take a moment to think. After running this code, x would be 10. This is because the condition x is less than five is true. The first part of this if else statement runs. Let's write your very first if-else statements. This time, we'll be working with the interpreter on the right-hand side. Go ahead and define the variable x, x is equal to three, then write your first if-else just like we showed in the slides. Go ahead and type in if x is less than five. Don't forget the colon at the end. Notice if we go ahead and hit "Enter", the orange color is now gone and instead there are three dots. This means the interpreter is waiting for more input before it evaluates our code. In particular, the interpreter is waiting for us to finish the function. Don't forget to type in two spaces at the start of the line, one, two, and then go ahead and type in x is equal to 10. Now, go ahead and hit "Enter" and add the else statement. Again, don't forget the colon. Hit "Enter" once more, add in two spaces, and type in x equal to two. Hit "Enter" and then one more time. That completes our if-else statement. Now, as we discussed previously, the if condition is true since x is equal to three and three is less than five. Since the condition is true, x is now assigned to 10. As a result, we now expect x to be 10. Let's check that. Go ahead and type in x and hit "Enter". We can see that now the value is 10. Now, let's write one more if-else statement using strings instead of numbers, define the variable name is equal to bob. Now, let's go ahead and add an if statement using this string. If name and recall from lesson 3 to check for equality, use two equal signs. Here, we check if the name is equal to john. Hit "Enter", add two spaces, type in print name verified. If the condition is true, print out the string name verified. Go ahead and hit "Enter", type in else with a colon, and otherwise if the above condition is false, then enter two spaces and type in print unrecognized name. Hit "Enter" twice and we can see unrecognized name. This was because this time the condition was not true. The name is equal to bob, not equal to john. As a result, the else statement runs and the program prints unrecognized name. Let's now see what a while loop is. What is a while loop? A while loop iterates over a piece of code multiple times. It continues to iterate indefinitely while the condition is true. In other words, while some condition is true, keep running code over and over again. Start by writing while, then add your condition. In this case, the condition is checking that x is less than five. Add a colon at the end of the line. Don't forget this colon. Then add two spaces at the start of the next line. Finally, add the code that will keep running while the condition is true. Here, each time the orange expression is run, x will be incremented by one. Here are all the parts annotated. For a while loop, we need the while, a colon at the end of the first line, and two spaces before every line that we want to run repeatedly. We can change the condition in blue to anything we want and we can also change the expression in orange however we want. In some, while x is less than five, increment x by one. Say x is previously zero, after running this code, what would the value of x be? Pause the video here and ponder it for a moment. After running this code, x will be five and the first pass x is equal to zero. Zero is less than five, so we increment x by one. X is now one. One is still less than five, so we increment x by one again. Now, x is two. We keep doing this until x is five. Then five is not less than five and the while loop stops. The final value of x is then five. Let's try this. Go ahead and type in first x is equal to zero. Now, let's go ahead and add our while statement. We'll type in while x is less than five, colon, enter two spaces, and we're going to add an extra print statement so that we can see how the value of x evolves. Go ahead and type in print of x so that we can see the value of x, hit "Enter", two spaces, and then x is equal to x plus one. Hit "Enter" and one more time. We can now see the value of x evolve. It goes from 0, 1, 2, 3, 4, and then finally, go ahead and type in x and we'll see that it's now equal to five just like we said earlier. Let's build another nifty tool. This time, our tool recommend whether or not to bring an umbrella depending on the current weather in a city of your choice. Like at the end of the last lesson, you will now write code in a file instead of in the interpreter. To the left, it may not be wide. Start by importing the URL opening function that we used in the last lesson. Go ahead and type in from urllib.request import urlopen, then define your new function. This new function will, given a city as an input argument, return the weather condition for that city by reading a webpage. Go ahead and type in def get_condition and city. Don't forget your colon at the end of that line as usual. For the next line, [inaudible] will add this two spaces for you automatically. Go ahead and now define URL as equal to http://wttr.in/ and then plus city. Notice that there's now a change at the very end of this line. The format is now equal to percentage uppercase C. Again, the details of this particular website are not important. Read the webpage at this URL, so urlopen and the URL. We'll also read the data on that web page, so page.read, and then we're going to reformat the data into a readable format. Go ahead and type in condition is equal to raw.decode utf-8. Finally, return the condition. We're now done with the function. Go ahead and hit "Enter", and once you're on this new line, go ahead and hit "Backspace" to delete any leading spaces. Like before, we'll use the input function to ask the user for input. Here, you'll ask the user for a city name once more. City is equal to input, and the prompt is city, then we'll call our brand new function. Condition is equal to get_condition. Up to this point, this code has been very, very similar to the previous lessons. However, we'll now take a look at condition and add it to an if-else statement. Go ahead and write if condition is equal to clear. Make sure there are two equals signs and make sure to add a colon at the end of this line. Go ahead and hit "Enter" and we can go ahead and print no umbrella needed if the conditions are clear, and otherwise add an else statement with a colon, then we print bring an umbrella. That's it. Double-check the colors match and that you have both colons and spaces. Click "Run" in the very, very top, then type in a city of your choice. Your nifty tool then returns whether or not to bring an umbrella. Nice work. As usual, if you run into errors, make sure you double-check your code against mine. Quick check is whether or not the colors match. If the colors do not match, you may have forgotten end quote or the colon. In this lesson, we covered both if-else statements and while loops. If you'd like to access and download these slides or view completed code for the example in this lesson, visit this URL. This concludes the control logic part 1 session. In the next lesson, you'll learn about a different way to loop called a for loop. 12. (Bonus) Guided Exercises for If, While: Welcome to Bonus Content, Lesson 7's Guided Exercises. In this video, we'll walk through exercises covering concepts from the last lesson. Again, there are three steps for each exercise. I will present an exercise. Two, you pause the video and try the exercise. Three, I will walk you through the solution. Some exercises are practice and the others are quizzes. You should try these quizzes to test your knowledge. Let's hop right in. Navigate to aaalv.in/coding101/exercises7. You'll then be greeted with a page like this one. Go ahead and click on the top left, and click on "Fork." You'll then have an editable Repl.it, just like this one. Start with question 1. Practice: Define a function gt5 that accepts one number argument; if that argument is greater than five, return "yay!." Give it a shot. Here's the solution. Define in gtf, it takes in one number argument, which we'll call x. If x is greater than five, return "yay!." That's it. We can run this function, and then gt5 and let's pass in 10 and returns "yay!." Otherwise, if we pass in two, nothing happens. Question number 2, this is now a quiz. Define a function reaction that accepts one string argument; if that argument is among us, return "yay!." Give it a shot. Here's the solution. Define reaction that takes in one string argument. We'll call this string argument game. You can call it whatever you'd like. If game is equal to among us, then return "yay!." Now, we'll try this code and type in reaction some other game, tic tac toe, nothing happens, but if we type in among us, then we get "yay!. Question number 3, this is practice. Define a function gt5o, that accepts one number argument. If the argument is greater than five, return "yay!," and otherwise, return "nu!." Give it a shot. Here's a solution. Define gt5o that accepts one number argument, which we'll call x. If x is greater than five, return "yay!," and else return "nu!." Run this code, and we can now try gt5o. If we pass in 10, then we'll get "yay!," and otherwise, if we pass in two, we'll get "nu!." Question 4, this is a quiz. Define a function reaction that accepts one string argument; if that argument is among us, return "yay!," otherwise, return "nu!." Very similar to the previous questions that we've seen. Give it a shot. Now, here's a solution. Define reaction that takes in game; if the game is among us, return "yay!," and otherwise, return else return "nu!." We can now try this function, hit "Run." Type in reaction "among us," and that gives us "yay!." Reaction of tic tac toe gives us "nu!." Question 5, practice. Define a function blackjack that accepts a list of numbers. If the sum of the numbers is less than 21, return the sum, otherwise, return zero. Give it a shot. Here's a solution. Define blackjack that accepts a list of numbers, we'll call this lst. If the sum of the numbers is less than 21, so if sum of the list is less than 21, return the sum, otherwise, return zero. Hit "Run." We'll now try blackjack 20, 20. This should return zero, because it's greater than 21, and now, we try for 3, 4, 5, and this is less than 21, so we expect 12. Question number 6. This is now a quiz. Define a function can_cook that accepts a list of strings. If the list of strings contains lemon, return the list, otherwise, return an empty list. Here's a hint. If you type in the following code, "hello" in list, this will return True, if the variable list contains the string hello. Give it a shot. Here's the solution. Define a function can_cook that accepts a list of strings. If the list of strings contains lemon return the list. So if lemon, in list, return the list, otherwise, return an empty list, which is just two square brackets with no contents. Go ahead and hit "Run," and now, type in can_cook, and we'll pass in a list with lemon in it, and that will give us the list back. Otherwise, if they pass in list without lemon in it, then this will give us an empty list. Question number 7, this is a quiz. Define a function laugh that accepts a list of Booleans; if any of the Booleans are true, return "haha," otherwise, return "uh." Here's a hint. The any function will return True if any Boolean in the input list is True. Give it a shot. Here's a solution. Define laugh that accepts a list of Booleans. If any of the Booleans are true, return "haha," otherwise, return "uh." Note that this line right here, if any list, could also be if True in list, very similar to our previous question. Either one of these would work. To test our function, go ahead and run your code. On the right-hand side, type in laugh, and we'll pass in a list of Booleans where they're all false, that gives us "uh" as expected, and we'll pass in one true, that gives us "haha." Question number 8, Practice: Write a while loop that prints every number from 5-10. Here, we'll type in i is equal to five, and then I will write while i is less than 11, we'll print i, and then we'll increment i. Now, we can try running this code in the right-hand side. Go ahead and hit "Run." There we go. We've got all numbers from 5-10. Note that you could have also put this. Actually, here's an alternative. So I'm going to comment out this code, and here's an alternative solution you could have written. You could have written i is equal to four, while i is less than 10, i plus equal to one, print i.We will increment i by one before printing, so the first number will be five and the last number will be 10. Just like before, we get the same result. Question number 9, Quiz: Write a while loop that prints every odd number from 5-15. Give it a shot. Here's the solution. We can type in i is equal to five. While i is less than 16. Here, we can actually do something simpler. We can just say print i and then i plus equals to two. That'll give us every other element. The very first one would be five, and then seven, and so and so forth. Go ahead and run your code. Here, we can see five up to 15, all of the odd numbers, as we'd expect. Now, question number 10, Quiz: Write a function print_from_to, that accepts two number arguments and prints every number from the first argument to the second, for example, print_from_to three, six would print all numbers from three to six. Give it a shot. Here's the solution. Define print_from_to x, y. These are two variables, not the two numbers. Let's go ahead and now use these numbers. Let's define an increment i is equal to x, and while i is less than y plus one, print i and then increment i. Let's now try this code in the very top. Hit Play to run the code and type in print_from_to three, six, and this will print every number from three up to six. Question 11, Practice: Write a while loop that prints every number from 5-10 that is not a multiple of three. Now, we can use a while loop. Here, we'll start it actually by defining i is equal to five, and then we'll write while i is less than 11, actually. We will check if i modulo three is not a multiple of three. We want to check if this is not equal to zero, then we print the value i, and then we increment i by one. Here, we'll check, is five a multiple of three? If five modulo three is not equal to zero, then it is in fact not a multiple of three, and reprint the number, and then we do this all the way up until we get to 10. Go ahead and run your code. Here, we'll see all the numbers that are not a multiple of three printed. Question number 12, Quiz: Write a while loop that prints every number from 5-15 that is not a multiple of six. You'll notice, this is very similar to the previous question. Try it now. Here's a solution. Go ahead and type in i is equal to five, while i is less than 16, if i modulo six is not equal to zero, not equal to zero. If this number is not a multiple of six, then we print the number, and we continue to increment. Go and run this code. At the very, very bottom, you can actually see that we've now got every number from five up to 15 that is not a multiple of six, so you don't see 12 or six here. Once again, we want to check how are you doing. If this is doable, that's great. This was a very difficult lesson. We combined many of the concepts from before. If this was barely doable, don't worry, keep going, and you'll build familiarity as we go. Number 3, if this wasn't doable, that's completely okay. Watch this video and the previous one again. Each time you re-watch, the concepts will sink in a little bit more. If you have any questions, please don't hesitate to leave them in the discussion section of the class, and I'll be more than happy to answer them. If you feel you've mastered this lesson, try to come up with a few extra questions and solutions, leave them in the discussion section of the class, and your fellow students will thank you for the extra practice. That's it for Lesson 7's Guided Exercises. 13. For Loop: Password Strength Checker: In this lesson, we'll cover what is called a for loop. This will involve two different concepts. First is the range function, and the second is the for loop itself. Start by navigating to repl.it/languages/python3, you should then see a page like this one. In this lesson, we'll start by using a new function called range. Call range with the number like 10, go ahead and hit Enter. Range returns a special data type. This specific data type is not important. All we need to know is that, one, the return value is iterable. Two, an iterable is anything that you can iterate over. Three, we can convert any iterable into a list by using the list function. Let's call list on the returned iterable. First, assign the return value to a variable called iterable and then let's go ahead and call list on that variable. Now, you can see the contents. Range effectively returns a list of numbers from zero up to the number you inputted minus one. We inputted 10, so the list of numbers goes from zero to nine. Now, we're going to use the range function. Let's go ahead and go back to our slides. First, consider the iterable that we want to loop over. We want to loop over all the numbers in this range from zero to nine. To iterate over this range, we will use a for loop. The characters in black: for, in, and the colon are all required parts of a for loop. The green item variable i is assigned to the next value in the range on each iteration. On the first iteration, i is assigned to the first value in the range, a zero. On the second iteration, i is assigned to the second value in the range, a one, and so on and so forth. Then add two spaces at the start of the next line. These spaces are extremely important, don't forget them. Finally, add the expression that will run once for each value in the range. There are 10 values in the range object, so this expression will run 10 times once for each value in the range. Here are all the parts annotated. For a for loop, we need the for, in, a colon at the end of the first line, and two spaces before every line that we want to run repeatedly. We can change the item variable in green, the iterable in blue, and the expression in orange to whatever we want. Let's go ahead and try this in code. Let's go ahead and now type in for i in range 10. Don't forget the colon at the end. Hit Enter, two spaces, and print i. Go ahead and hit Enter twice and we can see all the values from zero up to nine. Now, let's try a for loop on strings. First, a string is also an iterable. Let's see what happens when we call list on a string. As you can see, we get a list of letters, one letter at a time. So if we iterate over a string, we'll also get one letter at a time. Let's try this. For i in Hello World, two spaces, and print i. As you can see, the program prints one letter at a time. Now, let's build another nifty tool. This time, our tool will check that your password includes a number. Like at the end of the last lesson, you'll now write code in a file instead of in the interpreter. To the left in main.py, start by defining a function called check that takes one argument the password to check. Let's go ahead and write def check, it takes in one argument, the password. Don't forget your colon, and now let's go ahead and type in has number is equal to the Boolean value false. At a high level, we will iterate over each character in the password and check if it's a number. If the character is a number, set this variable to true. Now, let's write your for loop. Go ahead and write for i in password. Iterate over every character in password. Check if the character is a number by writing an if statement. Go ahead and write if i.isdigit. Isdigit is a string method that is built in for us. It returns true if the string i is actually a number. Go ahead and enter, and now type in has number is equal to true. If this character i is indeed a number, this will assign has number to true. Let's go ahead and hit Enter, hit Backspace exactly twice, one, two, and type in return has number. This finally returns whether or not the password contains a number. Now, let's go ahead and hit Enter twice and Backspace once. Go ahead and type in password is equal to input a password, and this will ask the user for a password. Now, let's go ahead and use our check function, has number is equal to check of password. This will check if the password contains a number, and it will return a Boolean true or false. Now, let's go ahead and print that Boolean, and this will print whether or not the password contains a number. That's it. Double-check that the colors match and that you have your colons and spaces. Click Run at the very, very top, then type in some random piece of text with or without a number. I'm going to type in a random piece of text with a number, and we get true. Looks like our program indeed works. You've learned quite a few different topics in this lesson. In this lesson, you've learned both about the range function and for loops. If you'd like to access and download these slides or review completed code for the example in this lesson, visit this URL. This concludes the control logic series of lessons. In the next lesson, we'll briefly touch on what objects in Python are. You'll also work with potentially helpful utilities for your tools in the future. 14. (Bonus) Guided Exercises for For Loops: Welcome to Bonus Content, Lesson 8's Guided Exercises. In this video, we'll walk through exercises covering concepts from the last video. Again, there are three steps for each exercise. One, I will present an exercise. Two, you'll pause the video and try the exercise. Three, I'll walk you through the solution. Some exercises are practice and others are quizzes. You should try testing your knowledge with these quiz questions. Let's hop right in. Navigate to aaalv.in/coding101/exercises8. You'll then see a page like this one. I'm going to hit, oh. Once you're on this page, click on Fork. Click on the title on the top left and then click on Fork. Then you'll get an editable version of this template. On the left-hand side, I'm going to click on Files - to minimize this tab - and I'm also going to drag this bar over to the right, so there's more space for the code in our file. On the left-hand side, we're going to go ahead and start with question number one, practice. Define a function "print list" that accepts a list of strings and, for all strings, prints the string on a new line, go ahead and try. Here's the solution. Define print_list. We're going to pass in a list of strings and, for each string in the list, print the string. "Print." Each time print is called, the string will be printed on new line. Go ahead and run your code. On the right-hand side call print_list and we'll pass in a list of strings. Add a close square bracket, close parenthesis, and there we go. Question number two, a quiz. Define a function "print_gt3" that accepts a list of numbers and, for all numbers, prints whether it is greater than three or not. Give it a shot. Here's the solution. Define the function "print_gt3" that takes a list of numbers and, for numbers in the list, print whether the number is greater than three or not. Oops, there's no "the". Go ahead and run the code. In here, we print_gt3 and pass in a list of numbers. There we go. The first two numbers, one and three, are not greater than three, but five is indeed greater than three. Question number three, this is a quiz. Define a function "print_add3" that accepts a list of numbers and, for all numbers, prints three more than the number. Give it a shot. Here is the solution. Define "print_add3". This takes in a list of numbers and, for each number in the list, print three more than the number. Run the code. We can double-check that this function works by calling it with a list of numbers. There we go. We have three more than each number in the list. Question four, practice. Define a function, "print_a_names" that accepts a list of names and, for all names, prints the name only if the name starts with an "a". Give it a shot. Here's the solution. Define, "print_a_names" and takes in a list of names and, for each name in the list, we can print name, but only if the name starts with an "a". Recall from a previous set of exercises that we can check if name starts with "a". Run your code, and now let's try this function. We will have a bunch of names with "a" and a few names without "a". Question five, quiz. Define a function "print_nums_gt3" that accepts a list of numbers and only prints numbers greater than three. Give it a shot. Now, here's the solution. Define "print__nums_gt3" that accepts a list of numbers and, for each number, if the number is greater than three, print the number. Run your code. Now "print__nums_gt3" and we'll pass in a bunch of numbers, some greater than three. So here five, seven, and nine are all greater than three, so they are printed. Question number six, this is practice. Define a function "get_name" that accepts a list of names and returns the first name that starts with an "a". Give it a shot. Now, let's talk about the solution. Notice that here I said returns the first name, not print. So go ahead and define "get_name". This accepts a list of names and for name in names, just return if the name starts with "a" return that name. One thing that we haven't quite talked about here is that the return statement actually terminates the function. As soon as this return statement is executed, nothing else in the function will run. So this ensures that the first name that starts with a capital "a" is returned. Let's run. Let's now use this function. So here we'll have David, Alex, and let's say Emily. Here, we get Alex, the first name that starts with an "a". Question number seven, quiz. Define a function "get_odd" that accepts a list of numbers and returns the first number that is odd. Give it a shot. Here's the solution. Define "get_odd" that accepts a list that numbers and we'll once again loop through all the numbers and if the number is odd, then we return the number. We can now run this code and check that the code is correct Two, three, and five. Here, we expect three for our return value. Perfect. Once again, let's check how are you doing. If this was doable, great. If this was barely doable, don't worry. Once again, let's check how are you doing. If this was doable, great. If this was barely doable, don't worry. This was a challenging set of exercises. What's most important is that you can read the code and understand how it works. Third, if this wasn't doable, that's okay. Watch this video and the previous one again. Each time you rewatch, the concepts will sink in a little bit more. If you're feeling you've mastered this lesson, try to come up with a few extra questions and solutions. Leave them in the discussion section of the class and your fellow students will thank you for the extra practice. That's it for Lesson 8's Guided Exercises. 15. Using Objects: Birthday Countdown: In this lesson, we'll cover how to use objects. We won't go into too much detail about objects as they weren't a class of their own. We'll cover object properties and two specific objects, the datetime and the timedelta objects. Start by navigating to repl.it/languages/python3, you'll then see a screen like this one. First, import datetime. Now, we can construct any date and time that we want. For example, this is 2030, January 1st and this is 2050, May 1st at noon. We can also do something interesting. Previously, we saw that we could subtract numbers. Here, you can also subtract datetime objects. You can try this now by subtracting two datetimes. This is 2030, April 1st minus 2030, January 1st. This returns a timedelta object. This timedelta object represents the distance in time between these two datetimes, which is 90 days. Another nifty method is called now. This computes the current datetime. Go ahead and type in datetime.now. As you can see, I'm filming this class just a few days. As you can see, I'm filming this part of the class on Thanksgiving day. Let's see how long we have until January 1st, 2030. We can do this by doing another difference between datetime objects. We can do datetime 2030, January 1st minus the current datetime. That's quite a number of days into the future. Let's dive a little deeper into what this timedelta object offers. Assign a difference in the last line of code to a variable, 2030, January 1st minus the current datetime. Up to this point, we've seen a few interesting things about objects. First, we can call object methods. Second, we can subtract datetime objects from one another. One more concept. Objects can have properties that are not methods. You can access a property by also using the dot notation. For example, the timedelta has a property called days. Go ahead and type in diff.days. This tells us how many days until January 1st, 2030. Another example. The datetime object has properties like day and year. So let's go ahead and type in now equals the datetime.now and you can now type in.year and.day. Let's now build another nifty tool. We will write a program that reports the number of days until your birthday. Like at the end of the last lesson, you will now write code in a file instead of in the interpreter. To the left in main.py, start by importing the datetime utilities. Go ahead and type in from datetime import datetime. We're going to assign a variable to hold the current datetime. We're also going to assign another variable to hold the datetime for this year's birthday, bday equals datetime now.year, and let's pretend that our birthday is February 1st. However, say it's just two days past your birthday and it's currently February 3rd. This year's birthday has already passed, so February 1st is not the next birthday. Luckily for us, you can also compare dates as if they were numbers. We will now use an if condition to check if this year's birthday has already passed. If bday is less than now, go ahead and reassign this so that it is a new datetime with one year in the future. In other words, if this year's birthday has passed, calculate the datetime for next year's birthday. Go ahead and type in now diff is equal to birthday minus now. Finally, print the difference in days. At the very top of the file, go ahead and hit Run and you should see the number of days until your very next birthday. That concludes this lesson. This is a quick summary of the concepts you've touched on: object properties, datetime objects, and timedelta objects. If you'd like to access and download these slides or view completed code for the example in this lesson, visit this URL. This concludes all the concepts we wanted to cover in this class. In the next lesson, you'll synthesize what you've learned into one final example project. 16. Sample Project: Secret Messages: You'll build a simple short program that allows you to send secret messages to your colleagues or friends. Although this isn't the most practical example, it combines several of the concepts we've learned in this class. It's also hopefully a fun application. Start by navigating to repl.It/languages/python3. You should then see a screen just like this one. In this lesson, we'll encrypt our messages using an algorithm called a Caesar cipher. Back to the slides. What is a Caesar cipher? The Caesar cipher works by mapping each letter to another letter. In particular, each letter is mapped to another letter that comes 13 letters later. A is mapped to N, B is mapped to O, C is to P, and so on and so forth, but here's the interesting part. Since there are 26 letters in the alphabet, this also means that if we apply the Caesar cipher twice to a piece of text, you get back the original text. An A is mapped to an N, and then that N is mapped straight back to an A. The Python dictionary for this mapping can be tedious to write out, so I've already generated the dictionary at the URL above. Go ahead and navigate to the URL and copy all that code on that page. Once you've copied that code, go ahead and navigate back to your code. On this page, go ahead and start by pasting the mapping that you had just copied at the very, very top of the file. This defines a dictionary, like we covered in one of the earlier lessons, with the variable named mapping. Then we're going to create a function called cipher, which will compute the secret message. This function takes in one argument, which is the original text. So here we have function named cipher and one argument original. Start with empty text for the secret message, we'll then add one character at a time to this piece of text. Iterate over each letter in the original piece of text. For letter in original, don't forget your colon as usual. We're going to go ahead and uppercase the current letter, as our mapping only supports uppercase letters. So let's type in letter equals letter.upper. Now, let's look up the new letter that this old letter is supposed to correspond to. New letter is equal to mapping of the old letter. Finally, add the new letter to the text. Text is equal to text plus the new letter. Finally, return the cipher of text, hit "Enter", hit "Backspace" once, and return text. Then hit "Enter" and hit "Backspace" so that you have no more leading space. Let's go ahead and ask the user for text to encrypt. Type in text equals input of text or secret. Once we've got the user inputted text, go ahead and run the cipher function. So result is equal to cipher of text. This will run the cipher on the user inputted text. Finally, print the result, and that completes the program. Go ahead and hit "Run", type in whatever text that you would like to encode. Let's go ahead and type in hello. Then copy this, hit "Run" again, and paste. Hit "Enter", and your message will be sent back to you. You'll see that your secret message has been successfully decoded. Now, feel free to encrypt any secret message that you want. If you'd like to access and download these slides or view completed code for the example in this lesson, visit this URL. This concludes our final example. Have fun with your program, encode a secret message and send that cipher to your friends or your colleagues. Don't give away the message, though. They'll have to take this class to figure out the secret. Congratulations on learning so much and building so many tools. In the next and final lesson, we'll discuss next steps beyond this course. 17. Conclusion: Congratulations, one, you've covered a number of different coding fundamentals; two, you've had hands on experience with code; and three, you now have a slew of nifty tools to use. You've picked up enough code to begin automating parts of your day-to-day life. I suggest starting small. Start with the categories that we covered with the examples in this class, then you could begin to branch outwards. There may be an inkling of doubt though. How do I build categories of tools beyond what the examples have covered in this course? That's completely okay. Remember, the discussion is there as a resource for you to use. Go ahead and leave a question and I'll be more than happy to leave a response. Remember, this class is live. As you leave reviews and post feedback in the discussion section, I'll continue to update the course and possibly include more examples that students ask for. Remember the goal is to get you, number one, interested, and number two, able to read and look for code. What's more important than having the right answer is having the interest to continue to build nifty tools. I hope you've had fun, but maybe more importantly, I hope you can see how much value code can provide today. It doesn't have to be a months-long, years-long journey before you start to use code. Got a super nifty tool? I and your peers would love to try it out. Go ahead and post a link in the projects and resources tab for the rest of us to try. If this has piqued your interest and you're looking to do a little bit more, try out the Applications and Computer vision 101, Data Science 101, or SQL 101 on my Skillshare profile. I've also designed the course to be very short and self-contained. There's more content coming to help you ace coding interviews and become a more advanced Python coder. If that all sounds interesting to you, make sure to follow me on Skillshare to be notified as soon as a class is launch. Congratulations once more on making it to the very end of the course, and until next time.