Python 101: Beginner friendly Python programming | Kalob Taulien | Skillshare

Python 101: Beginner friendly Python programming

Kalob Taulien, Web Development Teacher

Python 101: Beginner friendly Python programming

Kalob Taulien, Web Development Teacher

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
33 Lessons (2h 49m)
    • 1. Welcome to Python 101

      1:37
    • 2. Python2 vs. Python3

      2:12
    • 3. How to install Python

      2:27
    • 4. How to execute Python code

      4:58
    • 5. Basic arithmetic (simple math)

      6:11
    • 6. Welcome to Python variables!

      4:07
    • 7. Python is indentation sensitive. Let's talk about code formatting.

      6:31
    • 8. Commenting your code is a good practice

      3:45
    • 9. Introduction to Python data types

      4:35
    • 10. Numeric data types

      4:07
    • 11. String data type (sentences)

      3:23
    • 12. Lists data type

      5:54
    • 13. Dictionary data type

      6:12
    • 14. Tuple data type

      6:15
    • 15. Set data type

      4:51
    • 16. Boolean data types

      2:50
    • 17. None type

      2:36
    • 18. Indexing and slicing strings and lists

      7:12
    • 19. How to accept user input

      3:15
    • 20. Changing data types through type casting

      5:31
    • 21. Different ways to format strings

      4:04
    • 22. How to tell a program what to do

      15:06
    • 23. Shortcut code for telling a program what to do

      3:11
    • 24. Multiple comparison operators

      4:34
    • 25. Looping through items in an iterable (ie. Lists)

      7:08
    • 26. How to loop forever (careful!)

      2:41
    • 27. How to skip loop iterations and break out of them early

      6:30
    • 28. Re-usable code: Functions!

      12:35
    • 29. Variable scoping determines where the variable can live

      5:20
    • 30. How to create a localhost webserver

      3:38
    • 31. Introduction to Python modules with the Random module

      2:03
    • 32. 310 Final project

      11:34
    • 33. 320 Summary

      1:42
  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

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.

147

Students

--

Project

About This Class

Hi there, 

Welcome to Python 101 - your first step to learning how to program using Python. 

Python is in the top 3 most popular programming languages in the world. BIt's easy to learn, easy to read, and easy to write, plus Python is incredibly powerful. 

Some of the things you can create with Python are: 

  • Websites
  • Automation tools for your home 
  • APIs 
  • Artificial intelligence 
  • Machine learning 
  • Mathematical algorithms 
  • Scraping tools
  • And more...

Some of the most popular companies in the world use and support Python, too. Such as:

  • Google who owns YouTube (made with Python)
  • Facebook who owns Instagram (made with Python) 
  • Dropbox (made with Python)
  • You get the point.. there's a lot of them! 

And in 2021 you will definitely want to know Python as it becomes more powerful and more popular. As it grows, so will job demand for Python programmers. The nice thing is: Python is easy to learn and quick to get a hang of.

But why use Python instead of other languages like Java? 

Python is consistently the top choice for new programmers - not because it's beginner-friendly, but because it's easy to read and write. You don't have to learn all sorts of new rules or write crazy letters beside each other. Python is made in such a way you can literally just read through your code like it's a book. No other language has that kind of power. Plus it's fast and scales well when there's a lot of use, unlike Node.js. 

How much effort will this course take?

Barely any! I've taught thousands of people how to program using Python and I've designed a perfect beginner course. I expect this course to take you about 7 hours in total, that's including all the small tasks at the end of almost every lesson and the final project. 

Why Kalob as your instructor? 

Hello, I'm Kalob, your instructor through Python 101. I'm a seasoned Python programmer, senior web developer, and have taught tens of thousands of people how to code using Python. I have helped people learn to make websites with absolutely no prior experience and then helped them land their first job at companies like Google, AWS, Facebook, Netflix, and more. 

I write Python every day, even for my hobby projects. It's my #1 choice for a programming language and I'm passionate about it. And once you start learning Python, I'm confident you'll be passionate about it as well. 

Can I take this class if I'm on Windows, macOS, or Linux?

Yes, of course, you can! There are only a few small cases where you would do things differently on Windows than on macOS, and I show you all the commands for every operating system in this course. 

Heads up! macOS and Linux are very similar operating systems behind the scenes. So we only need to cover one of them to learn both. Windows is the oddball, but it's still supported in this course. 

Requirements:

You will need a few things to get started in this course. First, a text editor like VS Code. And then you'll need to download and install Python.

Thank you for reading through this course description. I hope you choose Python as your programming language — if you do, I promise you won't regret it. 

Happy coding!

Meet Your Teacher

Teacher Profile Image

Kalob Taulien

Web Development Teacher

Teacher

Hi everybody! I'm Kalob Taulien.

 

Here's the TL;DR (short) version about me:

I have been coding since 1999 and teaching people how to code since 2013 I have over 350,000 web development students world-wide I'm on the Wagtail CMS core development team (Wagtail is Python's #1 most popular website making system) I try my best to answer EVERY question my students have  I love teaching — it's definitely one of my natural talents  Also I love goats! (Great conversation starter with me if we ever get to meet in person)

Below you can find all my Skillshare courses. The categories go from easiest to hardest, except for the Misc. Coding Courses at the very end. 

If you're brand new to coding, start with BEGINNERS WEB DEV.&nb... See full profile

Class Ratings

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

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Your creative journey starts here.

  • Unlimited access to every class
  • Supportive online creative community
  • Learn offline with Skillshare’s app

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. Welcome to Python 101: Welcome to Python one-to-one with your teacher and Caleb telling in this course we are going to be learning beginner level python. So even if you've never written Python before, you're going to be able to learn how to write Python and a lot of different things about Python. In this course, we're going to learn about different data types and data structures, how to accept user input, typecasting, print formatting for loops and while loops, how to break out of loops running a local Python server. Lots of different comparison operators. And at the end we're going to make a game of rock paper scissors, where you can play rock paper scissors against a computer. Now that's all fun and everything. But why should you even learn Python? Python is one of the world's most popular programming languages. It is significantly more popular than JavaScript is more popular than PHP. It is a very popular language. It's also very easy to learn. It is the best language to learn if you're learning coding of any degree, Python is where you should start. Everyone agrees that you should learn Python first before you learn even JavaScript. Python is also incredibly powerful from making websites to automating basic things in your life. To security and artificial intelligence and, and object detection in images and videos and all sorts of things. Python can literally do everything. It's easy to learn and it is incredibly powerful. It is the only language in the world that is easy to learn and incredibly powerful. Welcome to my Python 101 course. With me, your host Caleb Italian, and I look forward to seeing you inside. 2. Python2 vs. Python3: Python two versus Python three. Python two is old and outdated. And while there still are companies using Python two, it's now considered a security risk. It's not maintained whatsoever. And so we're not going to be learning Python two because that's old. It's deprecated. It's obsolete. We're going to be learning Python three, and in this course we're actually going to be using Python 3.9. So at the time of recording, this is the newest version of Python. But if there is a newer version of Python, let's say Python 3.13.11. Absolutely. Go ahead and download that in the next lesson. And you can use a newer version of Python as well. Everything you learn in this course is still very applicable to newer versions of Python. And that's because Python is reverse compatible. So I would highly recommend not learning Python 2x whatsoever. And honestly you're not missing much. It, you missing maybe a little bit here and there where like syntactical sugar is little bit different. So they get print statements are gonna be a little bit different. But honestly, if you learn Python three, you've basically learned Python two. Now there are a few differences which I just mentioned in Python three versus Python two, for example, the print statements are a little bit different. But for the most part, Python three is not only significantly faster, but it is just crammed full of new features that you're going to want to take advantage of it because it just makes your life a lot easier. And every company that you'll work for in the future is going to be using Python three. So that's why we're not going to be learning Python two. We're simply sticking with Python three. Python two was deprecated at the beginning of 2020, I believe. So the idea is that no one should be using Python two anymore whatsoever for any reason. And so moving forward, we should be using Python three, and we're already on Python 3.9. We're pretty far into the Python three lifecycle when it's Python for gonna come out, who knows, maybe never. We don't know that yet. But even if Python for work to be announced, you can still learn everything in Python three and apply it to Python for and again, that's because Python is largely reverse compatible. In the next lesson, let's go ahead and install Python on our computer is it doesn't matter if you're using Mac or Windows or Linux. We're going to get it up and running on our computer and run a little bit of code. 3. How to install Python: Hello and welcome to installing Python. In these videos, we are going to be using Python 3.9. Now if you don't have Python, you're going to need Python. Now if you have a Mac, like what I'm using here, it comes with Python, but it comes with Python two, and that's no good. We need Python three. And we're going to be using Python 3.9. Now if you're on Windows, you're going to need Python as well. So if you head on over to Python.org and click on Downloads, you can go in here and you can download the proper version that you're looking for. So I'm going to be using Python 3.9. And I'm just going to install that or download that. Where are you download, download, download. And if you scroll on down here, you can download different versions here. So I'm on a Mac 64-bit, so I would download this one if you're on Windows and Windows ten most likely. Or windows seven, you're going to want to download the Windows version. And that's probably going to be your executable installer, your Python 3.9 dot EXE. And then you're going to want to install that. So I already have Python installed on my computer. But once you have that downloaded and installed, what you can do is you can open up your command line. Now if you're on Windows, you're going to want to probably use Windows sub system for Linux Ws L. That's just the easiest way to work on a Unix based system. If you're on Mac, all you have to do is open up your app called terminal. And once you have Python installed, you should be able to do Python dash v. And it's going to show you your version. Now if it shows you the wrong version, like if you do Python dash v and it says like Python two, that's no good. You can do Python three, dash v. And it'll show you that I'm using Python 3.8 as my default. And if I want Python 3.9, I can do Python 3.9 dash capital V, and it's going to show me python 3.9. So in this course we're going to be using newer Python, Python 3.9. Now if you're watching this in the future after this was recorded and there's a new version of Python. Go ahead and get the newest version. You don't need to have Python 3.9 or 3.8 to 3.7, you can get the newest version of Python. So go ahead and make sure you in, you download and install Python. And once you have downloaded and installed, let's go ahead and look at how we can actually run some Python code in our command line. 4. How to execute Python code: Okay, let's take a look at how we can run some Python code. And so first of all, there's really one way to run Python code. The main way is through your terminal, but there are different types of terminals. So on Windows you can use Windows subsystem for Linux, or you can just use PowerShell or bash if you're on Linux or terminal, if you're on Mac OS or item too, if you're on Mac OS, you know, whatever command line tool works for you, all we really need to do is open up our command line tool here. And if I just clear this, I should be able to type Python 3.9 dash v. And we're going to see it's Python 3.9 that I'm using. Now to execute any Python file is really easy. So typically you just type Python and then your file name dot p, y, p y stands for Python PY. Now if Python is using Python two or a different version of Python you don't want to be using. You can always just type in Lake Python 3.8 if you have a newer version or just Python three or Python 3.9, and then your filename 2.txt. So let's go ahead and give this a shot. At this point in time, you should already have a text editor installed. If you don't have a Text Editor, I highly suggest VS Code. It's made by Microsoft, is well maintained. It's fast, it's intuitive, it's smart. It is a good editor. And if you're like me, you don't really like Microsoft products. Well, hey, guess what? This is one of the good ones. So let's go ahead and open up VS Code. And in here we can go to, I think it's just view and then yeah, terminal. And this will open up a little terminal for us in here. And in here we can write the exact same command, Python dash P, Python 3.9, dash v is going to give me 3.9. Now first things first, we need to create a new file here. So let's just call this one Hello World. And let's do a print statement. Let's print hello world. And what I'm going to do is File Save As. And I'm going to save this right to my desktop. Helloworld dot p y. And you can see down here that this automatically told me this is a Python file. If it doesn't do that, you can go down here and you can select Python, but it should automatically do that for you. Next, we want to do an ls dash l a, or if you're on Windows, you can type DIR and you'll be able to find where you are exactly. So I don't think I'm actually on my desktop right now, PWD. I'm not. So what I want to do here is CD over to my desktop. And if I do ls dash LA or if you're on windows again, you can just type DIR LS dash L a is going to show me all of these different files that I have in here. And I've got one in here called HelloWorld somewhere. How low World, where are you? Helloworld dot pi. So what I can do now is Python 3.9. And instead of dash v, where it's going to give the filename. So H-E-L-L-O. And then I hit Tab for autocompletion helloworld dot pi because I have two files. And it says hello world and I can do anything I want in here, hello world from Python 101. And let's go ahead and rerun this command, hello world from Python 101. And that's how we run our code in our terminal. And so in the future, what I'm going to be doing is I'm going to be writing some code in here. And I'm also going to switch back to my terminal where I can actually write some code in here, and it's the exact same thing. So I can cd over to my desktop. And if you need a command line refresher, I have a Command Line 101 course. Definitely go check that out. So all I did was change directory to my desktop. And now I can do the same thing here, Python 3.9, helloworld dot p y, and it says the same thing. Now what I'm going to do behind the scenes is I'm going to alias Python 3.9 to be just Python. So when you see me type python in the future, if this again turns out to be Python two for you, just make sure you type Python three or Python 3.9 if you've got multiple versions of Python on your computer. And what I would like you to do is very simply create a new file called hello world and print something. So write print parenthesis, and then in quotations, hello world. And then you need that end in quotation and that ending print statement. And then in your, either in your terminal or in your VS Code terminal or in your PyCharm terminal or wherever you are, just type Python 3.9 or Python just straight Python, helloworld dot pi, and make sure that it works once that's working, you know, you have the right version of Python installed and it is working with Python three. In the next lesson, let's go ahead and take a look at some basic arithmetic using Python. 5. Basic arithmetic (simple math): Okay, welcome to basic arithmetic. In this lesson, we're going to take a look at just some basic math. And this is really, really important to know because we can do some really cool things with simple, simple mathematics. So for example, we're going to be talking about addition, subtraction, division, multiplication, exponents and this thing called modulus, which is really just a remainder. It's a, it's a, it's a really fancy word for remainder. And so what you see in front of me here is I have just gone to file open and I opened a whole folder called Python 101. And you can do this on your VS Code as well. And it just opened up this folder for me. So all of my working files are going to be on the side in on the left here. And so I want to go over to view and open up my terminal again. And this way we've got our files on the left. And we have our terminal at the bottom. And we can write some code up top, up, top in here. And so let's take a look at some basic arithmetic. Now there's two ways we can execute Python. We can execute Python in a dot py file like we did in the last lesson where we had hello world or in your terminal, wherever you want to use that, you can simply type Python. And if you want to get out of this python shell, we can just type quit opening and closing parentheses, and that will quit it for us. So I could type Python 3.9. And you can see that the first one was using Python 3.8 because I haven't made that alias yet. I said I was going to, but I haven't got around to it yet. And this one is Python 3.9. And so now we can do some basic arithmetic in here. Let's go ahead and move this up. And let's do some basic arithmetic. So we could do like five plus five is going to be ten. And this is just executing simple Python for us, right in our Python shell. So that's addition. We just use a plus sign subtraction is very, very similar. Let's go ahead and make this just a wee bit bigger here. We could do like 99 minus 11. That's going to give us 884 times four is going to be 16, and we just use an asterisk. So on a standard North American English keyboard, you use Shift eight and that gives you this little character here. And so when I hit enter, that's going to be 16. Division is just a slash, just like that. So we could do 100 divided by 25 and that's going to give us four. And you can actually see that gave us 4.0. That's technically a float, that's not an integer. We're going to talk about these in the future. Now to clear your terminal, let's say I write a bunch of stuff and gives me errors. I can just hit Control L. Control L tends to clean out the terminal for me. So that's what I'm going to be doing in the future. Now let's talk about exponents. And we could do four to the power of four, that's two multiplication signs side by side, that is the power of. So in other languages it's sometimes looks like this. Four to the power of four. In Python, it's four star, star four, and that gives us an exponent. So this is going to be four times, four times four, which in my mind is going to be 64. Nope, I was super, super wrong. 256. This is why we let computers do mathematics for us. And then we have this weird one called modulus, modulus, MOD USUS modulus. And all this is going to do is give us a remainder. So if we did ten divided by three, we're going to see 3.33333335. It's a weird one. So let's say we wanted the remainder. So three goes into 103 times, so 369, and it has a remainder of one. So we can do modulus here. And to get that remainder, we use the modulus sign, which is really simply a percent sign. So we do modulus three and this is going to give us one. And again that goes 369. And then it basically takes that last number and it says ten minus nine. And that gives us our remainder. And so if you remember doing long division, well, maybe not remember how to do long division, but if you remember long division from school, it's basically that it's going to give us the remainder very easily. We don't have to do any sort of long division. And in fact, a lot of people think you need to know a lot of math to get into web development or into programming or into Python. Truth is you do not. And here is the case in point. I could do something like 88 modulus 6.4. Now, I don't remember how to do long division. It's a fairly simple thing of mathematics, but I just don't remember how to do it. That was a long time ago for me. And that was an unexpected indent. If you ever see that, you probably have an extra space in there. And this is going to give us a modulus of 4.7999 and, and, and, and, and, and, and, and, and, and, and N5. And so I don't need to know long division anymore. The computer knows how to do it. And that is the nice thing about pretty much all programming languages, not just Python, but all programming languages can do this. So what I would like you to do is go ahead and try some basic addition, subtraction, division, multiplication, dry modulus, and also try exponents. Now at truth be told, you're not actually going to be using modulus and exponents all that often you probably won't even really use multiplication all that often, or maybe even division all that often in web development. But in data science you're going to use these all the time. And these are very, very important, especially when you get into things like algorithms. If you ever wanted to get into algorithms. Now in web development, you don't need to get into algorithms. It's not really all that important unless you're working for Google and you're working with Google's search division, which is pretty unique. Most web development, you just don't need that. So go ahead and give addition, subtraction, division, multiplication, modulus and exponent's a try. Make sure you open up your Python terminal. So again, quit. And I just went into Python. And that goes straight into my Python terminal for me or my Python Shell as I call it to the Python shell. And to quit you just type quit parentheses. And that gets you out. When you're done that, let's head on over to the next lesson where we learn about variables. 6. Welcome to Python variables!: Welcome back. Let's take a look at variables now for this one, I'm not going to be using the terminal, not just yet anyways, I might open that up in a little bit. And I'm gonna create a new file here called variables, variables dot p y. So what a variable is, is it's really just a little piece of memory allocated in your computer to a named Association. Which again sounds like a very complicated thing, but it really isn't. So it looks something like this. We can say name or maybe let's not do nameless due course Is equal to Python 101 and this is a variable. So let's take a look at some of the syntax and some of the, how it actually works behind the scenes. So behind the scenes, this is our name and this is going to allocate some sort of memory on our computer automatically does that for us. Then we say is equal to just one equal sign. And then with quotations around it, we say python space 101. And this is called a string. We'll talk more about strings and numbers, and floats and all sorts of data types down the road. You don't need to know that right now. Now you don't need a semicolon if you come from a PHP or JavaScript background, often we use semi-colons. Python doesn't care. Python doesn't need semi-colons. Python works on new lines and it works on indentation. So what we could do is we can say print, hello world. And you see how this isn't quotations. And this is in quotations. Well, we can basically swap that out. We can say instead of hello world, we could say print course. And what this is going to do is say, find a variable called Course if it exists, and then find what that value is. And so in your computer somewhere, it's being stored as Python 101. So when we print this is actually going to say python 101, and that's a variable. And that's really all there is two variables now there's different variable types, different data types. We'll talk about each one down the road individually. But for now, you just need to know that a sentence has quotations around it. So if you have a space in there, or if you have any letters from a to Zed or any European or Russian or Asian characters, really any sort of character that's not a number or a decimal, you put quotations around it. And so let's go ahead and give this a shot. Let's save this and view our terminal. And what I can do here is because I have variables in this folder. I can also do ls dash l a, or if you're in Windows, you can type DIR, where if you're on Windows you can type DIR. So ls dash LA shows me variables up high. And I could type Python VAR hit Tab for autocomplete variables dot py, and it says Python 101. Now if this was a different course, if this was, let's say a JavaScript course, we could simply change that here. And it will change it here for us as well. So we can go ahead and rerun this and it says JavaScript 101. Now that's the nice thing about variables is we can use them as many times as we want. So we don't have to just declare it once. In fact, if you declare ones, it's probably not that useful. Actually, no, I, I explain that wrong. Declaring it once is useful and you only need to declare it once. Accessing it more than once makes it useful. So we could print this a number of times and I just printed that over and over and over again. Hit save. And let's rerun Python variables dot py. And it says JavaScript one-to-one over and over and over again down here. And that's all there is to a variable. Now what I would like you to do, as, you know, just a little bit of side practice is I just want you to create a variable and print it. So create a variable, call it something like Python one-to-one, or create a new variable, call it name, name yourself and then print out your name. That's all I want you to do. Nice and simple should take you 30 seconds to a minute when you're done that, let's head on over to that next lesson where we look at a little bit of formatting in Python because I mentioned that Python doesn't care about semi-colons. It doesn't really care about curly braces and things like that, and it works on indentation. So take a look at that in the next lesson. 7. Python is indentation sensitive. Let's talk about code formatting.: Python is a unique language because it doesn't care about things like curly braces. So in JavaScript you have something that looks like this, a function thing, parentheses and curly braces. And then we can have some sort of indenting in here, or we don't have to have any indenting and we can just write this. That works too. But we're not writing JavaScript, we are writing Python, and in Python we use indentation. So a thing like a variable, like Course is equal to Python 101. And then to create another variable, we just put it on another line and we don't have to worry about a semi-colon or declaring multiple variables at once, like we do in JavaScript or any other language, we'd simply do. Name is equal to caleb Tallinn. And if you wanted to, you could always follow me at Caleb Darlene on Twitter. Let's do that. In fact, let's do Twitter at Caleb telling. And then for another variable we simply do the variable name is equal to and then some stuff in here. Now, when it comes to things like functions, which we'll talk about near the end of this course. Or when we talk about things like conditional statements, which is in the middle of this course, we're going to see things like if something is equal to something else, colon. And then we have our code in here. And what's nice about this is we don't need all those curly braces. We don't have to get lost and where all the curly braces are, we simply use a colon. And that tells Python that the next line is supposed to be indented. Now you can indent any way you want as long as it's consistent. So if you have one space, that's fine too. Spaces, three spaces for spaces, 68 spaces. You could do anything you like. But typically, we stick with tabs or four spaces, 1234. And you see those tiny little dots in there, that's four spaces. I usually just hit tab and that works. Now in other programming languages. If you have any programming experience in another language, typically, we can get away with writing nothing in here, and that's fine. We can stub out some code that way, but that's not going to work in Python. In Python, if we have no code inside of here, we simply type pass. But all our code can go in here. And then what do we want to get out of this if statement? We simply delete that tab and we go back to the leftmost line here. And then we could say something like if something else is equal to something else, pass, and this should have a pass as well. So we're looking at something like that and that's indentation. So when you indent, you're saying, hey, there's a colon at the end here. Make sure you indent. And that's going to execute all this code in here. And if there is no code, we simply type Pass. And that just means do nothing. Otherwise, watch this if we do. Course Is equal to Python 101. And let's get rid of this pass and let's execute this file. We're going to see an indentation error. So we can do Python indentation dot pi. That's the file that I made up here before I started this video. And it says file indentation dot py line eight, syntax error, unexpected end of file while parsing. And so it expects more to be down here. What we could do is we could simply say, let's touches up. We can say pass. And when we do this, we don't see any errors. So if you see a syntax error, chances are it's just indentation. Indentation isn't working. Let's go ahead and do one more example here. Let's delete that pass and let's write another if statement, but this one has a pass. And so this time we're not going to see an unexpected end of file while parsing. Let's go ahead and run this. And this time we see an indentation error. And anytime you see an indentation error, it's definitely indentation error base. And it expected an indented block after line eight. So I guess this is actually before line eight. So it's saying there's an indentation error on line eight and it is expecting something up here. So we could type P a, S, S for pass. And we don't see any errors. And so the whole reason I'm making this video is because in other programming languages, we tend to use curly braces and curly braces when it opens and closes as everything inside of the curly braces is going to be part of our if statement or function or something like that. In Python, we simply use indentation. Now, what happens if we go ahead and indent course and Twitter? Well, this is expecting something else and so it gets us weird indentation here automatically. And when we run this, we should see an indentation error as well. And we see indentation error, unexpected indent. And again, all your code should stick to the left as far as possible. Unless you have a colon. Whenever you have a colon, you hit tab. If you have another if statement in here, if something, do something else, we can say if something else, colon, do another thing. And so every time you have a colon and you just want to indent and whenever you want this if block or a function. And again, we'll talk about those in the future. But whenever you want those to not work or to end, rather not to not work, but to end. You simply on indent and you go back to, well, in this example we would go back to our left-most area. In this example, we would come back out where it says, if so, we would write more stuff in here. And to get out of this if statement, all this stuff in here, we simply say do more stuff in here. And then what's nice about VS Code is we can simply collapse this with these little arrows. And VS code isn't until it's intelligent enough to know that these are if statements that are being, you know, they're trying to be executed. But the syntax itself is suggesting that, for example, only this should be collapsed like that. And all of this should be collapsed like that. Now there's no homework or assignment for this particular lesson. This is simply a conceptual lesson. I want you to know that indentation is very important when it comes to Python. 8. Commenting your code is a good practice: Code comments. In every programming language, we have some way to write a code comment and a code comment in Python. And let's just create a new file here called Comments.find. Is if we have something like name is equal to Caleb tall, lean, that's me. And we can have a code comment that starts with a number sign. And this is a code comments. And you can see that it's sort of grayed out because it's not actually going to execute when we tried to run this file using Python. And so what we can do here is print name. And if we go down here and type python Comments.find, we're gonna see that it prints my name out. Now, the reason comments are important is how well, first of all, they're not going to be executed. Even if we have a tab in here, it's not going to care because it is simply a code comment. It doesn't matter. Code comments or for humans there not for the computer. Now we can have another code comment at the end of a file or at not the end of a file, but the end of a line. And we simply hit space number sign. Another code comments. And we can go and execute this. And this is still not going to say anything about another code comment. So comments are great for sharing thoughts and ideas to come back to. If you're writing some piece of complex code down the line, you might want to comment your code just to let you know if you put your code down for a month or six months and you come back to it, you're going to know exactly what's going on. Now, typically in Python, we don't comment basic code. So everything you're learning in this course, we tend to not comment. But if you're working with something complex, maybe Data Science are large datasets or something a little bit unique for your website. Chances are you're going to want to write some comments. And not only is it good for you, but a code comment can be good for other developers. So if you're using a version control system like Git, you're going to want to share your code comments with other people. So when they download your source code, they can just simply read through it and go, oh, this conquest complex piece of code simply says, this is a code comment, or a better comment would be this is the teacher's name. And so when people read your code, or if you download this code, if you read this code and you say, who is this name, who is Caleb Aeolian. You can read this and say, Oh, it's the teacher's name. Okay, that's who Caleb tall lean is. And so it makes a little more sense that way. Another great thing you're going to see is todo TO DO. And you can see mine actually highlight to-do, do a thing in here. So if you're stubbing out some code for a complex feature and maybe you're halfway through. And tomorrow you want to pick it up. Tomorrow morning you want to pick it up and you're like, okay, well, I still need to do the thing. You can leave a little code comment for yourself to do. Do a thing in here. Now if your to-do doesn't highlight and VS Code, that's because I have an extension called todo tree. And all this does is it shows you to do is fix me is et cetera. And so if I go back here and you can see down here, once I have that extension installed, it gives me my to do's Python one-to-one comments at pi. And it brings me straight, straight to that line when I click to-do, do a thing in here. And so you're gonna see a lot of to-dos are a lot of fixed means in production code or while you're writing some production code with a team. Now what I want you to do as your task for this lesson, Reddit code comment, execute that file and make sure that it doesn't show up in your code. That super easy should take you about 30 seconds, maybe a minute out when you're done that, let's head on over to the next lesson where we talk about Python datatypes. 9. Introduction to Python data types: Let's talk about Python datatypes. We have different variable types, such as numbers, strings, lists, dictionaries, tuples, sets, booleans, and none. There's actually more. And in Python you can create your own datatype as well. So in this lesson, I'm simply going to show you a bunch of data types and what they could look like. And in the next several lessons, we're going to go through each 11 by one. So let's get started with one that we've already seen, one called a string. And this is simply a sentence, a sentence. Nope, a sentence like that. And so it has an opening and a closing quotation mark. You could also use apostrophes if you wanted to, but if it starts with an apostrophe must end with an apostrophe. If it starts with a quotation, it must end with a quotation mark. And this allows any form of sentence, any form of characters. Anytime you have basically anything that's not just a number or a decimal, you're going to want to use a string. Next we have integers, and an integer is just a whole number, like the number 99. Then we have floats. And a float is like 99 or one, or 0.2 or pi 3.104, that's going to be a float. And a float simply means it's a whole number, plus it has a decimal in there. Then we have these things called lists. Now in other languages these are called arrays, but in Python it's called a list. And it looks like this. Item one comma, item two, comma item three. We'll talk about these in depth as well. So don't don't feel that you have to remember all these. I just want to put these in your brain for the next few minutes. And then we're going to go over each one of these one by one. And so a list here opens with a Hard brackets, I guess you would call it an ends with a closing bracket. And then you have these different items and they're separated by a comma and a space, or just a comma technically, but it looks nicer if you use a comma and a space. And then it doesn't even just have to be full of strings because we know these are now strings from line one, integers. And we could put floats in here. And so this is going to be a string, this is going to be a float accessing the variable called floats, which is going to be a value of 3.14 and integers, integers, integers. And this is a variable. Its reference online too, and its value is 99. So this is like saying 99. And so that's a list. Then we have these things called tuples, tuple. And a tuple is a lot like a list, but we can't change it. So once it said it's set forever, we'll talk about these again in the future. So we can do something like item one, floats integers. We could have all sorts of things in here. Then we have sets. And sets are a lot like it list again, we'll talk about these in the future in its own particular video as well. But a set is like a list. But in a list, just like when you're going grocery shopping, it's in order. So you've got item one, floats integers. When you have a set, it uses curly braces and it just says item one, item two, item three, but these are not going to maintain their order. So whenever you access a set in the future, even though we wrote item one, item two, item three, it could end up being like item three, item one, item two, it's not going to maintain its original sort of listing. It's not going to maintain its order. That's a better way to put it. It's not going to maintain its order. And so that's a set. Then we have this thing called a dictionary. And a dictionary, if you come from JavaScript, looks like an object. And so we have some sort of key, some sort of value comma, a second key, and a second value comma, and that is a dictionary. Then we have Booleans and Booleans are either true. And I'm using a code comment here or false. And notice that it's a capital T for true, and it's a capital F for false. And the last one we might possibly use in this course is going to be the NoneType. So none is equal 20 n e, and this starts with a capital N as well. Notice this one is variable, but as soon as I capitalize that N a changes color for me in VS code. So that's syntax highlighting and that's telling me that it's trying to access the none datatype. And this simply means that there's nothing in it. So the next number of lessons led to go through each of these one by one, starting with integers. 10. Numeric data types: Number data types are really important for doing math. So for example, let's go ahead and write something like items is equal to four. And let's just pretend that we are writing some sort of e-commerce system. So the user has bought or has checked out four items and now wants to go and by these four items. And let's say there's going to be a price and each price is going to be 1997. So we could say print the total is going to be, well, we know how to do basic math now, so we do items times the price. And if we print this total and then execute this file, we could use Python integers, integers dot pi. We're going to get 79.88. Now this is really, really important because if these weren't number types, so this one is an integer and this one is a float. If this was, let's say a string and we execute this, this is going to not do what you think it's going to do. So let's go ahead and execute this once more. And we're going to see 1997199719971997. And all this is doing is saying 1997 times four. And so it's taking that string and it's doing it four times. Now a fun little thing you could do here if you wanted to, is you can make a fun little joke and you could say, word is equal to. Now. Print word times, I don't know, like ten. And let's just comment this out so that we only get one print statement here. And this is going to say none and, and, and, and, and, and, and, and, and, and, and, and, and, and a Batman or something like that. And so this is what happens when you multiply a string, which we've somewhat learned about with a, with a number. Let's, let's get rid of that. And Doo-doo-doo-doo, By the way, how do you like that for a DAD joke, Batman in Python. And so what we want to do is we want to make sure that these are simply number types. So that when we're doing mathematics, we're using either a number, a whole number, or a float. And once more, a float is simply a number with a decimal in it. So even if we did 4.0, that's a float as well. If we do just float or if we do just for rather, that is an integer. And both of these are considered number types. So this one is an integer. Let's do this integer. And we'll do a cold current here, and this is a float, and this is multiplication. Now what I would like you to do is recreate my terrible Batman joke. Take a number as any sort of variable. It doesn't matter what the variable name is as long as you use it consistently throughout the rest of your code. And then multiply it by a string so that you get none and, and, and, and, and, and, and n, n and n and n Anna. And actually there's one more thing I wanted to show you here. Python does this really cool thing. So if we did a really large number like this, if you look at this, can you tell me quickly what number that is? Probably not. But what we could do is we could do underscores, underscores. And Python doesn't care about underscores in Numbers, Aaron, in integers. It doesn't matter. It simply says, make it readable in Python is all about making your code readable and maintainable. And so now all of a sudden we can see that this is 1 trillion. If we don't have those, that's pretty hard to read, we have to then count the zeros manually and figure out what the number is. However, if we put those underscores back, we could do print total. That's not going to be right. And let's go ahead and select that cut and put that in here. And it gives us this large number without the underscores. And so this just makes it readable. So that's a fun little thing. I almost forgot to mention that that's really important when you're working with a massive, massive numbers. But your homework stays the same. Go ahead and try to recreate this batman joke that I did. When you're done that, let's go ahead and talk about strings. 11. String data type (sentences): Strings. Strings are basically just sentences. We use quotations around them like we've seen before. So we could do something like a sentence is equal to and then we can fill this was like lorem ipsum or really anything we want in here. We can put a number in here, 123 if we wanted to do. But this is not going to multiply the way we expect it to multiply or add or anything like that. Basically, your basic arithmetic is somewhat useless when it comes to a string. And so a string should really just be a sentence, a sentence in here. And all we're saying is, this is a string because it opens and closes with a quotation mark. Or instead of using a quotation mark, we can use simply an apostrophe. So either one of these works, it just has to start and end with the same thing. Now in Python, we can do multiline strings with three at the start and three at the end. So a sentence here and on a new line. So there's two ways of doing this. Now you're not going to see this too often, is going to be pretty rare that you see something like this. Chances are you're going to see it on one line using just one set of quotation marks. But let's go ahead and run this Python strings. And Oh, that's not going to do anything. Let's print the sentence. Now let's try it. And it says a sentence in here, and this is on a new line and even kept our blank line in there. Now let's go ahead and have some fun with this. And let's go into our shell. It's type Python 3.9 or just Python three or, or whatever version of Python you want to use. Hit Control L to clear that out. And let's do a sentence. Sentence is equal to thing in here. And let's see what we can do. We can do sentence dot and then hit tab. And we're going to see we can do all sorts of things with this. And there's a lot of different things in here. So let's go ahead and just use some basic ones in here. We could use sentence dot, upper with parenthesis. And it changes it all into uppercase or sentence dot lower. And that's going to change it all into lowercase. And if you ever want to reference what that sentence is, you can just type sentence and it basically printed out for you instead of having to use the print statement. Now as a fun little fact, you can always do type. Put your variable in there and it's going to tell you what it is. Str means it's a string. You'll see INT, you'll see float list, dictionary, all sorts of things in there a little bit down the road. But for now we're just working with a string and we're using dot lower and dot operator. So what I would like you to do as your task for this particular lesson is create a sentence and then do dot upper or lower on it and print it out and then execute that. And so what I mean here is you can have a sentence on multiple lines if you wanted to, and then you could do dot upper. And remember it needs those parentheses. That means it's going to execute some logic, some sort of function. It's not just another variable, it's something that's going to be executed. So it takes parenthesis. Now we can do Python, strings that pi, and it's all in uppercase. Go ahead and give that a shot. And when you're done with that, let's head on over to lists where we learned about how we can accept multiple variables in a single variable or multiple values in a single variable rather. 12. Lists data type : Lists. Lists are how we can store multiple items in a single variable. And so it list looks like this. List is actually keyword, so we tend not to use LES t. So we use usually LST. If you come from JavaScript, it's called an array. If you come from PHP, it's called an array. In Python, we call it a list. It's the exact same thing, just a different name, I guess. I'm not sure why, but we just call it a list. And so it looks like this. Instead of using quotations like a string or just a straight number, we use an opening bracket and a closing bracket. And then we have anything we want. So we can have a string and here we could have a number in here. And we can have a float in here. We could even if we wanted to have another list in here and you're going, and you're going to see list inside a list when you get into data science using Python or most programming languages really. And that's how we create vectors or matrices. We're not going to get into vectors or matrices. That's advanced math and we don't really need that for web development. But let's create an empty list in here, a new item, new item. And we close that off and let's do one more. My name Caleb. And so this is somewhat of an advanced list. Then in this list we have our first value, second value. Third value. Our fourth value is actually a list itself and its first item. And this is where it gets a little complicated, is a new item. And then in our original list, because this one is closed off right here, in our original list, our fifth item is Caleb. Now, at this point in time, if we just print this out, print lst, we're going to see that this isn't actually super useful. And really it just print out exactly what we wrote. What good is that? Now a little bit down the road, we're going to be learning about loops. And we can actually loop through all of these. And we can say something like four item in lst, print that item and what this is going to do. And again, we'll talk about this down the road more in depth is we're going to say for every item in here. So we've got 345. It's going to print that item. And notice that indentation. So we said for every item in our list, colon Tab, print each of those items. And so now we're getting into a lot of referencing this item references this, which references each one of these at an individual point in time. So if we type python list.pop, it's now going to actually print it out for us so we could do something a little more unique, Say the item is and now it's oh, I gotta save that. Let's go ahead and rerun this. And it says the item is string, the item is one, the item is 3.1 for the item is an array or a list with a new item in it. And then the item is Caleb. And so really what you need to know for this is is not this part. I'm going to comment this out, but you can access that code later on. But for this particular lesson, all you need to know is that this is a list and it stores multiple values in it. And those values can be variables as well. They don't have to be hard-coded like what I did here. The could also be variables. Now let's go ahead and open up our shell, python 3.9. And what we can do here is create a new list. So let's give it names is equal to a list called Caleb and Nathan. And we have a list. We can also do names dot append. We could add someone to this. We can say add John to it. Names dot append. And then we use an opening parentheses and a closing parentheses. And inside of it we put some sort of data type. Here. I just put a string called John, and that's going to append it. We can do names. And when we type out names here we see Caleb, Nathan, and John. So we've added to it. We can also remove a name. We could do names dot remove, and we just give it the item we want to remove. So let's remove Nathan and we type names again. And this names variable and this list is now just Caleb and John. Now a really cool thing we can do here, and you're going to see this quite a bit, especially in data science, is we could pop out that last value. So we can say that last value is going to be John, names dot pop. And it doesn't take any sort of parameter here, we just say dot pop. And now it looks like it does nothing. But if we type names, it's just a simple list with one item in it. But what it did here was it took that last item called John. And so what it did there was it took that last value out, like up here, would take Caleb oh, and throw it into a variable called John. Now what I'd like you to do is go ahead and give this a shot. Create a list of names, give it three or four names. And then I want you to do list.append and add a name to it. List.rev move, remove a name from this list. And then list.pop and put that into a variable. And just as a reference, there's a lot we can do with lists. We hit names, dot hit tab a couple of times here and we have a pen, clear copy, index, extend, count, insert, pop, removes, sort and reverse. These are all the different things we can do with a list. So if you wanted to go ahead and try to reverse the list, See what happens. Go ahead and run sort on that list and see what happens. And feel free to experiment with it. You're not going to break anything at this point. So go ahead and give that a shot and when you're done that, let's head on over to the next lesson where we learned about a very, very popular datatype. It's actually a data structure. Lists are data structures as well, but a very popular data structure called a dictionary. 13. Dictionary data type: Dictionaries. Dictionaries are a special data type. It's actually data structure. And so if you're familiar with a JavaScript object, it's pretty much the exact same. Or if you're familiar with JSON, it's very similar to them. And so it really just looks like this. We could do Person is equal to. And then we use curly braces here. And this is how it knows. It's either going to be a set or a dictionary. And we'll talk about sets and just a little bit. But let's start with a dictionary. So it has some sort of key, colon, some sort of value. Now your key is typically a string, and the value could be literally anything. It could be another dictionary. It could be a list that could be an integer, float, a string can be literally anything. And so let's go ahead and write name. Caleb. And Twitter is going to be at Caleb Tolkien. And so now what we can do is if I go down, let's actually just make this a little smaller. It's sort of in the way. What I can do here is print the person and to access any one of these, I simply add the key so it's hard brace or hard bracket rather. And then we put the key in there, and the key is going to be Twitter. And so what this is going to do is it's going to access this dictionary. It's then going to look up Twitter, which matches right here we can see this, matches this how you can see I'm using quotations and apostrophes interchangeably. It doesn't really matter as long as they start and end with the same thing. So it's going to look up Twitter and it's going to give me that value. And so let's go ahead and save this and run Python dictionaries. And we're going to see at Caleb, tell him that's my Twitter handle. Now we can add things too, which is really nice. And the whole reason we use dictionaries is because currently we would have to have a variable called name and a variable called Twitter. And maybe we wanna add a new one called Instagram. And we would have to add that as well as a variable that would be Instagram is equal to whatever the value is going to be. So instead of three variables, we can do one big variable. And so let's go ahead and add that we can do person Instagram. And that's the key, is equal to at coding dot for dot everybody. And when I print this whole thing, let's print the entire person object. We're going to see that Instagram is now in there. In fact, let's print before and after. So let's comment this out. Copy that line, paste that line. And so before it's going to print all of this stuff, Instagram is not going to be in there. We then said person1 variable at a Ki is equal to this value, and then print that again. And so we run this for a second time. We need to save that file first. If we run this for a second time or third time, I guess. The original value here is key value named Caleb. Twitter is Caleb telling. Over here we see that it's key value named Caleb Twitter at Caleb Italian Instagram at coding dot for dot everybody. And so we simply added something in there. Now we see that we have key value and we can delete that as well with a D, E, L keyword, Dell. And all we do is type person and we access the object or the key or the property that we want to delete. And let's go ahead and delete key. And so we're looking at the person KI And we're simply going to delete it from here. And so if we, for a third time print this. We can see due to, due to due in the third one down here, there is no key. So the first to have a key, the second one we added something and the third one we deleted something. And that's how we work with dictionaries. Dictionaries can become really, really complicated. And the nice thing about a dictionary is you can have dictionaries inside of dictionaries. And why is that nice? Oh, it's because you can have one variable storing multiple variable types. Now when would you even use a dictionary? Like why wouldn't you just use the variable name Twitter or the name Instagram as variables. While we do this, because these are all related somehow. We're talking about one individual. Hear me. The name is Caleb. That's my Twitter and it because it's all related around me, I throw it all into one particular datatype called a dictionary. So in the future when you're working with multiple variables and they're all somewhat related by some sort of subject. Like maybe a favorite food or I ingredients for like making your own pizza for example. You could do something like that. You can have pizza is equal to and then agree ingredients is a list of ingredients, the name of whatever you're making, maybe it's called Caleb's pizza or John's Pizza. There could be a phone number in here for them to call you at. There could be a website. So instead of Twitter, We can put website and then HTTP colon slash, slash my awesome pizza website.com. We're gonna have all sorts of stuff in there and it's all related. And so that's why we use dictionaries. We use dictionaries because it's all related information. Now you don't have to use related information. That's typically how we as humans use dictionaries. Now what I would like you to do as your task is create a new person or a favorite food, or maybe the people in your household, including your animals. And go ahead and create key-value pairs. Don't forget the syntax is a curly brace and you've got a key as a string, colon and then some sort of value. And it doesn't have to be a string. It could be anything. And then we use a comma. And that's how Python knows that there's going to be another one. And so we put another one in your name, Caleb, comma, Twitter ad. Caleb telling this one doesn't have a comma. Python also doesn't care if you put an extra comma in there, in JSON or in JavaScript and might care, but in Python, you put an extra comma. It's still going to work the exact same. It does not care. Go ahead and try that out. And when you're done that, let's talk about tuples, which is a somewhat unique thing in Python. 14. Tuple data type: All righty, welcome back. Let's talk about tuples. So tuples are like lists in the sense that they're an iterable. And so we use this thing called a for loop when we're learning about lists and we can do the same thing with tuples. And this just means it's an iterable. It means that we have multiple items that we can loop through and take an action on every single item. But the difference between a tuple and a list is, or maybe it's called tuple on some people who can say tuples, Some people will say tuple, IC tuple. But the difference between a tuple and a list is that a tuple is immutable. And I'm saying immutable verses mutable. And again, developers and programmers like their fancy words. This really just means it's not changeable versus it's changeable. That's it. That's all that means. And so let's do an example here. We have actually a better example is going to be right inner, inner shell. So let's open this up. And let's type python and then your version of Python. So I want to use Python 3.9. And so we have this thing called a list. So we can call it like animals is equal to a list of a cat, a dog, as zebra, and an aardvark. Forgive me if I spelled Aardvark wrong. I don't think I've ever type that word in my life. So we have animals here. And what we can do is animal's dot sort. And what this is going to do, it looks like it does nothing but if we type animals again, it put artwork at the very beginning and it's sorted alphabetically. Now, that's with a list. If we have a tuple, we can't do that. So what we can do is create a tuple. And we're going to call this one foods. Foods is equal to, and it has an opening brace in here. And I just made that a little bit bigger. So let's go ahead and do, let's close that for a quick sec. And so it basically looks like this pizza, orange, apple and passed out. And so here's a weird little caveat in older versions of Python. Python might think that this is somewhat of a function. So often you're going to actually see an ending comma in there. And that just tells Python, hey, by the way, for sure 100%, this is a tuple. And so if we do type foods, we're going to see this as a tuple. Let's clear this out. Type foods once more. Now if we do foods dot and hit tab a couple of times, we don't have all these different options. Whereas if we have animals dot and hit tab, we have all these different options like sort. So we can't sort foods because it's a tuple foods. It simply doesn't exist. We can do count or index. So let's run sort and we're going to see we get an attribution error, an attribute error, tuple object. This tuple that we just created has no attribute called sort. This is an attribute sort. We have that on our list. We don't have that on a tuple. We also, in the list lesson, said something like animals dot append. We could add a fish, animals, and we've just added that to the end of the list. Whereas a tuple, we can't do that. A tuple. We're using foods. We don't have append, we don't have AD, We don't have anything. Once a tuple is set, it is set for life. We cannot do anything with it. Now why is that important to know? Well, because when you use a tuple, the idea is that it's a little more efficient. So for example, animals.com with all these different things and this has logic behind it and this means it has to store the stuff in memory. Whereas foods dot, this is a tuple only has two things. Foods dot count, n dot index, and that's all we have. So it's a little more memory performance. It's just faster. So if you ever need to create a list that never needs to change, never needs to be sorted, never needs to do anything. You just maybe need to store it as somewhat of an immutable or unchangeable list down the road. A tuple is a way to go. So let's close this down and let's create a new tuple example. So let's do foods again is equal to a tuple. So remember list starts with this. A tuple starts with parentheses. We could do pizza, Fish TO meadows. And let's just go ahead and throw a comma in there just for, just for a healthy reason, just so that Python always knows now mod modern python is smart enough. It knows, hey, if you're using modern version of Python, that this is going to be a tuple. Older versions of Python will say, maybe this is a function. And so we just add that little comma in there to tell it that it's always going to be a tuple. And so what you do for food in foods, print, the food is whatever that food is. And so this food is now referencing this food which is going to loop through each one of these. And pizza becomes a food, fish becomes a food, tomatoes becomes a food. Let's go ahead and save that. And let's go to view our terminal. Let's quit. And I'll move it back down. And we can type Python tuples dot pi, that's the name of my file. And it's going to say the food is pizza, the food is fish, the food is tomatoes. And so this is called an iterable. The only difference here again really is we can't do anything once this is set it set for life, we can't do anything with it. We can add, we can't remove, we can't do anything except loop through those values and do something with those set values. So that's a tuple. It's a weird one. If you want to. This one is completely optional. Give this a shot and rights like foods is equal to and then some sort of tuple and tried to append to it. Notice that it's a, it'll give you an attribution error or an attribute error. But if you don't want to, you really don't need to do anything because it's very similar to a list. It just, it's a restricted list. In fact, that is a very good way of putting it. I should have said that at the very beginning, tuples are restricted lists. 15. Set data type: Let's talk about sets. So sets are interesting because they're a lot like a list, but the kind of look like a dictionary. So remember an, a list looks something like this. Varname, varname to varname three. And so this is a list with variables called variable, varname, varname into varname Three, and a dictionary. A dictionary looks like this, has the opening and closing curly braces. And then remember a dictionary has key-value pairs, key value pairs, and you can have multiple key value pairs. Now a set looks like this, and if I even just move that down a little bit more so we can see all of this in one view. A set of, let's just call it S is equal to. And it starts with opening and closing curly braces too, but it doesn't have a key value pair. It simply takes items or values like a list or a tuple does. And so we could say item one, item two, item three. Now, what makes a list really unique is actually two things, not a list and what makes a set unique is two things. A set first of all, is not going to maintain its order in a list and a tuple that maintains its order 100% of the time. So if this were a list, this would say item one, item two, item three. We're a Tupelo would say item one, item two, item three. A set does not do that. A set is going to say, okay, we just store this anywhere and it doesn't matter. It doesn't have to remember the ordering, which makes it also a little bit more performance as well. And so if we just comment these out and print s, And let's go down into our terminal here and type python setup.py. We've got to save that file first. Setup.py. Look at this. It does not maintain its ordering. It says item to item one, item three. It does not care about the ordering that is very important. That is unique attributes, number one, unique attribute number two about Asad is what items are unique. So if we type item two twice, let's make this I touched smaller. In a list we can have multiple item. So in the list this would say item one, item two item to item three. A tuple will say item one, item two item to item three. A set is going to say item one, item two, item three in any order. And it's going to say, oh, these are the exact same. So just merge them together. And so if we run this code once more and we're going to see item to item one, item three. Again, it does not maintain its ordering. And item two only shows up once, even though we wrote it twice, it only shows up one, so it keeps it completely unique. Now, let's go ahead and open up our Python shell here, and let's do Python 3.9 or whatever version of python you're using. And let's create a site. And what I'm going to do is I'm literally just going to copy this. That's Doo-doo-doo-doo. I copied all of that and I'm gonna paste it in here. And so I've got this set called S. And you notice that it's changed this ordering again, it's now item three item to item one. Not in any particular order. It just happens to be 3.2.1. So we can add to a set, whereas a tuple we couldn't a list, we could set, we can. So we do S dot and then we have all these different things we can do. Let's do S dot add, S dot Add Item four. And let's do this again. And we can see item for three-to-one. We can also remove an item with S dot remove. And there's a lot of different things we can do in here. We're not going to learn about all of these in Python one-to-one. But we can do S dot removed. So S dot remove. Let's remove item three. Item three. And let's see what S is. And it removed item three for us and it actually maintained its ordering that time, often it will not. So we can use S dot add an item to add an item to the set randomly and there wherever it is going to go, it doesn't matter. And S dot remove and then the item value, whatever that item value is going to be. We got rid of item three and it got rid of it in our set. Now I would like you to give this a shot because I feel like not enough people use sets, especially for uniqueness. And the nice thing about this is eventually you can turn a list into a set to make it completely unique. That's called typecasting. We'll talk about that a little bit down the road. But for now we'll just go ahead and create a set. Remember, its syntax is an opening and closing curly brace. There is no key value pair, it's just a list of values. There is no key in there, so there's no Colin's whatsoever. And then I want you to do set.seed and set dot remove and make sure it actually adds and removes those particular items that you're trying to add or remove. And when you're done that, let's head on over to the next lesson where we talk about Booleans, which is going to be a probably a super quick lesson because Booleans are super, super basic. 16. Boolean data types: Booleans are probably the easiest and arguably the most important datatype. And so when your code is executed, pretty much everything is going to boil down to some form of a boolean. And a Boolean or a bool for short, is a true or a false value. That's it. It can't be a number, can't be a string. It can't be anything. It's either true or false. It's either one or 01 being true, 0 being false. And so we can say something like, can you code is equal to true? Because what you're going through this course, I would say you can code. And then we can say a conditional which we haven't learned about yet, but we'll learn about these shortly. We can say, if can code is equal to true. And notice that it's got a capital T. False also has a capital F in Python. Print. You can code. And let's go ahead and execute this. Python Booleans, C.elegans as you can code. Now what if I change this to false? This is not going to work. This is simply going to print out nothing we expected. And that's because we're saying, first of all, can code. This is like saying true is equal to true. Actually that's not, that was a lie. Now it's like saying it's true. So can code is a lot like saying this. Because this value is true and we're comparing it to a true value. It's going to execute this code. And notice the indentation. The indentation is going to become more and more important in Python as we progress through this. Now when I say everything boils down to a boolean and we'll touch more on this when we're talking about comparison operators, which is the if keyword here. An if statement is always looking for something to be true. So if can code is equal to one, or if it's a string, a string of eggs by the rate of any kind. If can code, it's going to take this value and try to evaluate it. Is it true? Is it false? And that's basically boiling it down to a boolean. And because there is a value in here, Python is going to say that string is actually true. And we're going to say this is true. And the if condition here, the if statement is looking for a true value is always looking for something to be true. And if it's false, it skips it. Now there's not really much to do here, just because Booleans are so basic. It's either true or it's false. There's no other option. And this is a boolean. Once more I'm saying be OOO, AN, or sometimes you'll see the word bool for short. In the next lesson, let's talk about none, which is basically nothing. 17. None type: None is a data type. That means there's nothing in it yet, or we've purposely set it to be nothing. And it looks a lot like a Boolean where instead of using lowercase, we use uppercase. And you'll notice that n, o n e is actually highlighted here versus NO ONE IS NOT highlighted here. And that means one is a keyword, one is not a keyword. So we can say something like my wallet has nothing in it. And then down the road we can have some sort of code. And then we can say the wallet has $82.45 in it. Now all this is doing is saying, hey, there's going to be a variable down the road. And we need to be able to put something in it. But we need to be able to maybe write that variable a little bit early. Now this is really hard to explain without more complicated code. But all you need to know for this particular lesson is that none literally means nothing. There's nothing in it. It's just an empty variable. And then we tend to overwrite that a little bit later. So in the real world you're going to see something like wallet is equal to None. And then we could say, if wallet is none. And this is a comparison operator. But notice we're not using the same one we used before. We are saying is none. And that means while it literally is nothing, print. There's nothing in my wallet and my wallet. And we can execute this by doing Python, none dot py. And it says there's nothing in my wallet. We could also do the standard type of comparison. And it's going to do the exact same thing. Now, those comparisons are actually different. That's a subject for another day. But usually what you're going to see is if while it is none, then we could say, there's nothing in my wallet. While it is equal to 82.45. And then we could say print, wallet has wallet. And really this is just going to execute from the top to the bottom. So it says there's nothing in my wallet. If there is nothing in my wallet, reassign that. So there's $82.45 in there. And then say print my wallet has $82.25 in there. Now there's nothing to do with this particular lesson because none is also super, super easy. And we're getting really close to learning about conditional statements, which is basically lines three through five here. But before we move on, let's talk about indexing and slicing iterables. 18. Indexing and slicing strings and lists: Let's talk about indexing and slicing iterables. So an iterable is really anything that you can loop through, such as arrays, Sets, tuples, or even strings. And so what I want to show you is a non subscript double error. But first, before we do that, we need something that's non subscript double. And what non subscript means is if I have a list, 12345, I could print lst and then 0123 or four. And so when we are talking about indexing and index is this here, this is going to be index 01234. And computers always start counting at 0 y. I'm not sure it probably because a number is actually the number 0 is actually a number to a computer and it doesn't mean nothing that's more of a human concept. It's not really computer concept. So that's my explanation. Whether that's right or wrong, I have no idea. But if we wanted to get the number one in here, we would do 0. And let's go ahead and actually change this. So let's get rid of that and work with this properly. So we're not going to get confused with these 123. And let's just do three. And so if I run this l-s-t and I get the 0 index, that's going to be this first 1012. It just happens to be that the value is called 123. So let's go ahead and run this Python indexing and slicing dot py. And it says one. If I change this to two, which one do you think this is going to run? Remember it starts counting at 0012. So this is now going to say three. This is called an offset. Offsets are kind of difficult to wrap your head around in computer science and in programming. But once you get the hang of it, it becomes basically second nature. So this makes it subscript. A list is a subscript double. That means we can use this particular value in here. And as we were saying, list. And because it's sub scriptable, we use a hard bracket, the index we want to use, so the index number closing bracket. And this tells it to pick out a number two, so 012. And then we close the print statement. Now let's go ahead and give a boolean a value here. Let's say it's true. And we can say B, just the variable b. And let's give it the 0 index here. Now notice how it doesn't have a list of things. It doesn't have 12 or three in it. It's not a list of tuples set, or a string or really anything that subscript. But we're going to see some sort of error in here. And it actually says type error bool. This is a Boolean, it's an object. Everything in Python is called. An object, is not a sub scriptable and that means we simply cannot use an index in it. And so that is an example of a non subscript double error. Now what if we wanted to in our list here, let's go ahead and add some more. For five. What if we wanted to just get the first one? Well, that's easy. We know that already we could do LST 0. What if we wanted to get multiples? What if we wanted to get 123? We could do list 0 and then print list1 and list2. Or we could do 0. And then where does it stop at three? So let's go ahead and run this. And we're going to need to print this as well. Print our list, start at 0, that's one, and go to three. And this could, could, could do needs to be saved first, I keep making that error and says 1-2-3. And so all this is doing is saying started 001234 and go up to the third one. So 012 and it goes up to the third one, remember 0123. So this is technically the third one. And it's going to say, grab all of those. Now we could also say, start at a particular number and go all the way to somewhere else. So we can say two colon colon. And when we do this, let's just run that. It says 3-4-5, So it starts at 01234. So it's gonna start at 2012. And the colon, colon means go straight to the end. And we can also work our way backwards. We can say list minus one, so start at the very back. And what do you think this is going to do? If this one is 0 and that's our index. What is minus1? Is there going to be five or as minus 05? So that's a good question and this is why we need to experiment with Python as we're learning. It's always fun to experiment and learn how things work. And we're going to see that this is actually five because 0 minus one, minus two, minus three, minus four, minus five. Let's go ahead and try minus five and see what this runs. Boom. It worked its way backwards. So if we ever just wanted like the last two, we get a minus two colon, colon, and this is going to give us 45, just like this 45. Now, Strings are also iterables. And we can do the same thing here. So we can say course Is equal to Python, one-to-one print course. And then we can give it also an index. So 0123456789. And so let's say we wanted the n, we would go 012345. Let's print that and that's going to give us the letter n. Now what I would like you to do for your task is uncomment that and do a list. So LST is equal to something. It doesn't have to be called LSE can be called literally anything. And I want you to experiment with different index numbers with different slicing and indexing. Remember, this is called slicing. If we use Colin's, if we don't use a colon or a set of colon, this is called an index. So this is an index. But what's going to be returned here is a slice from this array or from this list. Go ahead and give this a shot. Have some fun with it, tinker around with it, and just get some muscle memory to remember that this is 0123 and bam four. Try that out and get some hands-on experience with this. In the next lesson, we're going to learn how to accept user input so that we can work with it. 19. How to accept user input: So we all know that Python is basically a command line program. It can be visual for sure. But currently, as we know it, it is just a command line program and so it has to run on your command line. And sometimes you want to ask someone for some information. We can do that with the input function. And so let's say you want to make a command-line game, or you want to accept some form of user inputs. In raw Python, you're going to need to use the input function. Now as a heads up every time you ask for input from a user, even if they type in a number, it's always going to come back as a string. So let's give this a shot and I'll show you why knowing different datatypes is really, really important. So we could say your age is going to be input. And we can say what is your age? And just as an example, this is Python user input. This is just simply going to ask me what my ages. So I'm gonna type 31 does nothing but it did ask me for my age before it xe to the script. So that's pretty cool. Is it paused it online one. Then we can say dog years is equal to aij times seven and print dog years. Let's go ahead and give this a shot. What is my age? 31. And you notice that it says 31 third, 1 third, 1 third, 1 third, 1 third, 1 third, 1 third, 1 over and over and over and over again. And that's because age is coming back as a string and we actually need that to come back as a number. So in the next lesson, we are going to be learning about casting. How do we turn this into a string? But for now, let's take the shortcut and let's just say this is going to be an integer. And so we're saying Whatever, this comes back as if I take 31 or my name, it doesn't matter. It's going to try to cast that as an, as a number or as an integer rather. And so let's go ahead and run this one more time, 31 and I'm going to be 217 years old in dog years. Now this is honestly not the greatest example because we're getting ahead of ourselves by talking about casting. We haven't learned about casting yet. Instead, what we could do is we get say, name is equal to input. What is your name? And then we can say print, hello and then the name. And when we rerun this, we're going to see Python user input. What is your name? My name is going to be Caleb. And it says hello, Caleb. And there's an extra space. Let's go ahead and ditch that space. What does my name? My name this time is going to be Nathan and says hello, Nathan, hello Caleb. And it's stored whatever I wrote in here into this name input. And so now we can create some sort of guessing game, or for our project, we can input some sort of value for, let's say rock-paper-scissors. Now the main thing to take away from this is you can ask for any sort of information, but it's always going to come back as a string and that's why it was important to no strings, integers, floats, things like that. In the next lesson, let's go ahead and cast some data so that we can start working with this a little more appropriately. And casting again is as simple as running int or bool or something like that, where we're going to look into that in the next lesson. 20. Changing data types through type casting: Locally, locally. Let's take a look at typecasting in the last lesson, we looked at it very, very briefly. And so typecasting is simply changing one data type to another data type. So if we wanted to change a string into a list, we could do that. We could change any of those into a Boolean or a dictionary, or a tuple. And you'll notice that these words are being highlighted. This is normal text and you can see is in Python is a keyword, so it's being syntax highlighted there. We've got a string list, bool, Dick, tuple, set, et cetera, et cetera, et cetera. Basically any data type that we've talked about so far, we can cast it. So let's go ahead and create an example here. Aij is equal to input. What is your age? And then we can print the type of age. And in fact, let's throw this into its own variable. So data type is equal to function type. And then we throw whatever that value is, it's going to be a string. We know that all input is going to be a string. We learned that in the last lesson. Then we can go ahead and cast that age by saying we know it's a string, turn it into an integer. And we're gonna throw age in here. And so all we're saying is whatever age is, turned it into an integer and put it back into that same age variable. Then we can do the same thing, datatype and I just copied and pasted there. And let's move this down a little bit here. And let's run this Python type casting. What is your age? I'm gonna put, oh, let's error this out. Caleb. I'm gonna say Caleb is my age. Caleb cannot be turned into a number. Let's see what error pops up. We get a value error, value literal for int with base ten. And so it's trying to turn Caleb into a number doesn't work that way. Let's try this again. Was my age, I'm currently 31. And we need to print this Doo-doo-doo-doo. So what I'm gonna do here is I'm gonna click here and then I'm going to command click down here. Or if you're on Windows, I believe it's just control-click. And I'm gonna type in two spots at the same time, print newline. And so this has all of our code. Let's go ahead and give this a shot. What is my age? 31. And originally it was a string and then it wasn't integer. Now we can work with that as an integer. We can say print your age in dog years is. And then we could say age times seven. Let's go ahead and give this a shot. What is my age? 31. And it says your age in dog years is 217. And just as a fun fact, you're probably looking at this wondering why I use multiple parentheses here. Do we need this? Do we not need this? Whenever you have a question like that, it's always good to experiment, experiment and then ask the question. So what does my age 31? And it still works. All I did was clean that up a little bit by using parentheses around age times seven. That's all that was. Now this is one example. We can go ahead and create another example. Let's convert a list into a set so that all the items are unique. So let's say we've got a grocery list. Grocery list is equal to and we need to pick up apples, oranges, nanopores. Ben enters, bananas, bananas and apples. And you can see here, and I just made that a touch smaller. You can see here in my grocery list I have apples twice. And so if I wanted this to be completely unique, what I could do is say this grocery list. Grocery list is equal to a set. Because we know sets are completely unique. It's not going to hold its order anymore. But honestly, if we're doing grocery shopping, we don't need it to be necessarily in order. And then let's print at this grocery list, printed this grocery list. And what this is going to do is return apples, oranges, bananas in any order, doesn't matter what the order is, but apples is only going to show up once. We can also type type of grocery list. And we're going to see that this is a set. So we know that up here, this is a list. We're casting it to be a set, so it's going to be completely unique. It's going to get rid of the two apples in here and turn it into just one apples. Then we're going to print our unique list and we're going to make sure that it is in fact a set. So let's go ahead and run this. And we've got oranges, apples, bananas. We don't have apples in there twice. And we can see that it is in fact a set. Now, I have a particular task for you that I would like you to try. I want you to make a grocery list and it could have any foods in there, but it has to be a list. And then I want you to cast it as a tuple. And the idea here is you make your grocery list at home. And when you're when you're at the grocery store, you're like, I don't want to be buying anything else. So I have to stick to this list. I can't add anything, I can't remove anything. I have to get everything on this list, cast it as a tuple. And then I want you to, just like we did here, print your list or it's going to be a tuple. So print your tuple. And then I want you to print the type of variable that this is. So this should now say a tuple instead of a list. Go ahead and give that a shot. It's really important that you learn about typecasting because it is very important when we're working with user input and what's the point of making a program? If it's not going to accept any form of user input, we need to be able to accept user input and we need to be able to cast things a certain way. Go ahead and try that out. And when you are done, let's head on over to that next lesson. 21. Different ways to format strings: Let's talk about print formatting. So there's like a 100 different ways, not actually a 100 different ways, but there's like a 100 different ways to format your print statements in Python or, or to format your sentences in Python. There's so many different ways, but we're going to learn the two most modern ways. One is going to use dot format and the other one is going to use this thing called an F string. So let's go ahead and set up a little example here. Welcome message is going to be hello, some sort of name. Welcome to Python 101. And then we want to print this message, print the welcome message. Now, we need to somehow replace this. So let's create a name. Because currently we don't know how to dynamically put something in here into a variable. Let's create a name. Welcome, Caleb. And so the first way we can do this is using the Format function. And I think this was introduced in Python 3.6. So we can do curly braces, dot format at the end name. And all this is going to do is look for this curly brace. And then it's going to format it with dot name. So it name is going to be caleb. And it's going to inject it right in there. So let's go ahead and run this code and we're going to see that it actually works the way we expect it to work. And it says, hello, Caleb, welcome to Python 101. Now that's one way. The more modern way, arguably the better way, the way I really like to do this is with an F string. And so an F string looks like a regular string. So we could say Hello, some sort of name. Welcome to Python 101. And instead of using dot format, what we can do is we can put a variable in here, so we use curly braces name. Now this itself is not going to work. I'm going to show you what this looks like. It says Hello, curly braces name. That's not working. But if we put a little f in front of it to format this, Python now knows that this variable is going to be basically from somewhere else. It's going to be from above it at some point. And so it's going to throw Caleb into here. And so I'd much rather prefer the F String method over the format method just because you can read it. You can say hello, name and you know exactly what that's going to be. Welcome to Python 101. And that's not going to work until I save that file and run it again. There we go. Hello Caleb, welcome to Python 101. Just like that, that's exactly what we're expecting. Now. There's the old way where we could put a percent sign in here, a string. And then we get say, percent sign name. And you're going to probably see this quite a bit, but this is going to work as well. This is the old way of doing it. And so not F, do, do, do, do, do There we go. Still outputs the same thing. But this is saying there's going to be some sort of string. And at the end we use the percent sign and then, and then the variable name. But honestly I don't like doing the swayed. It's the old way and it doesn't seem as intuitive as simply writing the name right in here. This way you can read it like a regular sentence which is very Pythonic. You can say, hello name. Welcome to Python one-to-one. And you know exactly what's going to go in here. With the other methods, you don't know exactly what's going to go in there. It could be a name, it could be a number, it could be anything you don't really know, but when you name your variables explicitly like this, you have a good idea of what's going to be injected. So what I would like you to do for this particular lesson is right to the dot format method and also write the F String method. Go ahead and inject a variable into a string and then print that string out, execute that file. And if it's all working the way you expect it to work, let's head on over to that next lesson, where we finally learn properly about comparison operators. 22. How to tell a program what to do: Alrighty, let's finally take a proper look at comparison operators. A comparison operator is how a computer can compare values to make some sort of decision. So everything in computer science really boils down to this. Without the ability to make a decision based on existing data, a computer doesn't know what to do. And in fact, a program is rendered completely useless if it doesn't know what to do. Now the idea is if something is true, do a thing. Otherwise, if it is false, do something else. And it splits the code into some, some sort of a path. And so let's take a look at the syntax. It looks something like this. If something is equal to something else, DO a thing. And so this is saying if something is the same as something else, do something. We can also say the opposite if something is a not something else and do it. And so we're looking for a false operator there. And by that I mean, like if we're saying if something, for example, if true is equal to true, do something. Then we can also write an else statement. If true is not equal to true for whatever reason, do something else. And so let's go ahead and switch this back. If we say can code is equal to true, do a thing, otherwise, do something else, and let's comment this out as well. And so now we can actually start to work with this. And notice we're using Colin's here and when we use a colon, we indent automatically print indent there and we have an indent here. So let's go ahead and set this up for success. We can say, can code is equal to true. Print. You can code. Otherwise print. You don't know how to code yet. And let's go ahead and run this. We can do Python, comparison operators, comparison operators dot py. And he says you can code. And the idea here is, again, we've got some sort of variable that we're comparing. We're saying Ken code is true. If that can code value is equal to true, then print this. Otherwise. If we say we can't code yet and we type false up here, this is a lot like saying false is equal to true and won't. That just makes no sense. False is not true, true is true, right? And so if we type, if can code which is false, is equal to true, this statement does not become true in the computer's mind, in the computer's CPU. It's just not true. So it's not going to execute this. What it's going to do is say, look for an else statement and then print that. And so we're splitting our code into two different ways to execute itself. And so we can do this again. And it says, you don't know how to code yet. And that's because we changed it from true to false. Again, if we change that back to true, it says you can code. Now this is just one example. This is a Boolean example. We can write another example here we can say teacher is equal to Caleb Pauline. If the teacher is equal to Caleb telling, print, show the teacher portal on a website or something like that. Otherwise, you're probably a student. And we say print. You are a student. Welcome to Python one-to-one. And boom, it says show that teacher portal and that's because teacher, this value is the same as this value. Now, what happens if we did this? Caleb, Pauline, but all lowercase, Caleb, Colleen with a capital T and a capital K is not the same as Caleb telling with lowercase letters. So it's going to say I'm a student, which is technically wrong. But a good way to compare this is because we never know if the users are gonna make a typo or capitalised things. People do weird things sometimes. And I've seen this a lot. Kal OB with a capital B. I don't know why. It just sometimes happens. So we can say teacher dot lower. And that's going to change this into a lower-case value, are going to look like that. Teacher dot lower is equal to the lowercase value. So this is a little less strict now. And it's going to check to see if, even if there is an uppercase letter somewhere else, it's still going to match the name perfectly. And when I re-run this, it's going to say show that teacher portal. Now let's go ahead and get a little fancier with this. And we can ask for user input. And so now we can say what is your name? Your name is going to be input. What is your name? And then we can say, if that name is equal to Bob. And we're looking for a capital B here. Print. Welcome Bob. Otherwise, print. Hey, you're not Bob. Get outta here. And when we run this, it says what is your name? And I could type in. Caleb says you're not Bob, Get out of here. I could type it again. What is your name? Bob. Welcome Bob. And so now we have a way of running different sets of code. Now this might not seem like a really good example, but we can get a little more complicated with it. Just a touch, touch more complicated. So now we can say bring food is going to be pizza, because apparently Bob likes pizza. And for people who aren't Bob, you get everything else. The food we're going to bring is going to be Nat fish, salmon, print. You are eating. And then bring food. And this is going to be an f statement. So we can put bring food in there. And what this is doing is it's splitting our code and then bringing it back together. And so we're going to ask for the name. If the name is Bob is gonna say welcome Bob. And it's going to bring pizza, is gonna say you are eating pizza. If the name is not Bob, it's gonna say, hey, you're not Bob, get outta here. And the food is going to be salmon. And then it's going to say you are eating bring food. And in both cases we're declaring a variable here called bring food. And so our code is essentially split. Fork in the road. And either way there's going to be a bring food at the end of it. It's just going to be a different type of food. And then at the end, that fork in a road, that split code is going to come back together. And we're going to say you are eating whatever that food is. Let's go ahead and give this a shot and see what happens. It says what is your name? My name is going to be Caleb. And it says, you know, Bob, get outta here. You are eating salmon. I could run that again. And if I type my name is Bob. It says, you're not Bob. I probably added a space in their BOB. There we go. Originally attempt to be OB space. I didn't mean to have that space. And remember, strings when we're comparing are very sensitive. And in fact, they are not only case-sensitive. You know, if there's an extra space in there. So if we said Bob with a space versus bob without a space, that's important to distinguish. So now it's saying, welcome Bob, you are eating pizza. And so now my program is getting a little more advanced. It's asking for some form of input. And we're doing an if statement. If that name is Bob, the food is going to be pizza. Otherwise, if the name is not Bob, who is going to be salmon. Now we can make this one step more complicated. Maybe not complicated, but advance and we can do an LF. So in other programming languages, we call it an else-if in Python we call it an LF, then we can say the name is equal to Caleb. Bring food is going to be tacos because I love tacos and it's going to print. Welcome to your teacher portal, I guess. Because, why not? And so now what this is doing is it's going to go from top to bottom, is gonna say is that name Bob? Maybe it is, maybe it isn't. If it is, then cool, execute this code and ignore all of this. Otherwise, if it's not, Bob is going to then check to see if that name is Caleb. If it is Caleb cools can execute this code and it's going to ignore all of this. Otherwise, if the name is not Bob or Caleb is going to execute this code. Let's go ahead and run this. I'm going to type K, L O B. And it says, welcome to your teacher portal. I am eating Tacos. And that's because it didn't recognize that the name is Bob. So it's skip this entirely. It did recognize that their name is Caleb. So it executed this. And because of that and notice the indenting here, we've got three paths we can go down if the name is Bob, if the name is Caleb, or everyone else. And so because it executed this path, it's not going to go down this path. And again, as a lot like a fork in the road when you're when you're driving somewhere or you're walking in the forest and you see a split in your path, you can only ever take one path. And that is exactly what if L if and else statement is now we can do unlimited else if statements we could do if the name is equal to Nathan, bring whey protein. And we're going to welcome him with Welcome to the gym. And let's go ahead and give this a shot and we're going to type the name is Nathan. And it says, Welcome to the gym. You are eating weight protein, whey protein. I have no idea how to actually spell that. Good enough though, to get enough of this example. Spelling is unimportant. But yeah, that, that's a comparison operator. So we have. If statements, if statements, we can have unlimited L if statements and an else statement. And that just basically splits our code into, consider it like a fork in the road. Now we have another way of doing this. If we wanted to ask for someone's name, we can say the name is equal to input. What? What is your name? Then we can say that name has to be named dot lower. And we can say, if the name is not Bob, print, you're not Bob. Get out of here. And then we could exit the code or we could do something with that. Otherwise, if the name is Bob and this is really just the inverse of Doo-doo-doo-doo. Where's our first exit? Nor does it gives a good example here. If we get rid of this stuff, it's the exact opposite of this. So first one is saying, if you are Bob, this one down here saying, If you're not Bob, print. Welcome Bobby boy. And what does my name I name is going to be Bob. Oh, nope, I spelled it with a space in their BOB knows baseless Time and it says, welcome Bobby boy. And the first one, did you notice, even though I typed Bob, there was actually a space in there. I typed it too fast. And he said, You're not Bob, get out here and we do the same thing. Caleb, and says You're not Bob, Get out of here. And so there's two ways to do this. We can say if something is equal to or if something is not equal to, we can do that kind of comparison operator. We can also do different types of comparison operators. And I'm gonna fly through these pretty quickly and I want you to, at the end of his video, give these a shot. I want you to try these out. So let's say aij is equal to 18. If you're, aij is equal to or greater than 18, print something like you can vote. So there's an, there's a greater than or equal to symbol. You can do just greater than. Or instead of doing just greater than, you can do less than if you're less than 18 year, probably not allowed to vote. If you're less than or equal to 18. So that's 1817161514, et cetera, et cetera, includes a number 18 or if the age is exactly 18, or if the age is not 18 n. So actually what I'm gonna do is print these out. So we've got greater than, greater than or equal to, less than, less than or equal to, is equal to, n is not equal to. So these are the core comparison operators and pretty much every programming language. So once you learn these in Python, you've learned them in pretty much every other language. And so you'd really just need to remember that this one is the same and this one is not the same. And so what we're doing in any comparison operator here is we're saying is the left equal to or not equal to the right? And what the computer is going to do is boil this down to a true or false statement. It's going to say, let's do this. If the age is equal to 18, well, 18, if we replace this, 18 is equal to 18. Is that true or is that false? In reality, that's true until the program sees it like this as o that is true. Execute this. Otherwise. Let's change that age to 21. And let's say if the age is not 18, the person has to be exactly 18 for whatever reason. Is the left the same as the right. And so we're saying is 18 not 21, or rather is 21 not 18. And so what we're doing here is we're saying 21 is in fact not 18. And this is where it gets little tricky in our heads, but this is the opposite. And so this is true because 1821 are not the same and we're comparing that they're not the same. That is in fact a true statement and this will execute. So I'm gonna get rid of this example because in the source code that's not gonna make any sense. Let's go ahead and comment that out. And you can access the source code at anytime and just uncomment what you like. What I would like you to do as sort of your task for this particular lesson, is create an example with a greater than, greater than or equal to, less than, less than equal to is the same and is not the same. Operators. Go ahead and give those a shot. Feel free to use user input or hard-coded value so you don't have to type it every time. But the point is, you're going to be using these probably hundreds of times a day. And it's really important that you understand which ones do what here. 23. Shortcut code for telling a program what to do: Let's talk about comparison shortcuts. In the Boolean lesson, I believe it was I said something along the lines of everything gets boiled down to a boolean. And in the last lesson I said, basically if statements or comparison operators are looking for a true statement. So notice that if something is true, we can do something else. And really, all this is saying is, is there a Boolean that tends to be true or false? And if it's true, if whatever that statement is, is true, execute the code inside of our indented code here. Now, in the last lesson, we did something like if aij is equal to 18. In this particular lesson, let's go ahead and do something a little bit shorter. So we're just going to say if something, do something else and let's make this a print statement, print. And so we're not doing or using any comparison operators here, we are simply saying if something is true and again, everything boils down to true or false. So we could say something is equal to None, for example. And let's go ahead and do an else statement. Print. And I need to fix that one. This is false. And we're going to say this is true. And so when we execute this comparison operators, let's execute comparison shortcuts and says this is false and none always returns false. What if something is a string, a string in here, and we execute this. This is going to be true. Anything, any string with a value in it is going to be true, but an empty string, that's going to turn out to be false. And it says this is false. And so an empty string is in a comparison operator. And in Python, a lot like saying if something is false. We can also do Booleans if something is true, and this is going to print this as true. Sure enough, this is true and false is going to execute the other path. So this is going to be false. And so this is a lot like saying if something is equal to true, let's execute this. And it's gonna say it's false because false is not true. As a shortcut, we can just get rid of that. And boom, it's still false. What I would like you to do is give the shortcut a little bit of a test. So try it out with a list and just an empty list, and then try it out with a list that has items in it. Try it out with a set, try it out with an empty set. Try it with a tuple and, And see if an empty tuple returns true or false. And then put items in there and see if a tuple that has items in it is true or false. Go ahead and give those a shot because you're going to see this kind of shortcut everywhere when you're writing production level code or professional developer level code. When you're done, you know, tinker around. Let's head on over to that next lesson where we learned about multiple comparison operators. So we can compare multiple things at the same time. 24. Multiple comparison operators: Let's talk about multiple comparison operators. Okay? Multiple comparison operators as when two things have to be true, or three things have to be true, or four things have to be true. Or one of two things has to be true. And so what I'm saying here is the end keyword. I'm also saying the or keyword. And so we can say aij is equal to 31, name is equal to Caleb. And then we can write an if statement like If your age is equal to or greater than 18 in Canada or in some parts of Canada anyways, you can print. I can drink alcohol. Now, what if I wanted to check if the age is 31 and the name is Caleb will currently the only way we know how to do this is by nesting these if statements. And we'd have to say if name is equal to Caleb. Now I can drink alcohol. So it's going to say yes, this is true because 31 is in fact greater than or equal to 18. And then it's going to say, is Caleb equal to Caleb? Well, yep, that's true. And then I can print I can drink alcohol. But there's a way to merge these together. So what we can do here is take this if statement is nested one and just delete this. And then we can say end. And so now we're saying if the age is 18 are greater, and get rid of that if statement and the name is Caleb, then I can drink alcohol. And so we just merge two if statements together. Now, the trick here is and is saying that both items on the left of this word and the right of this word have to be true. If either one of them is false, this is not going to print. So let's give this a shot. Let's say the name is going to be Python multiple comparison operators. And it doesn't print anything else. Else statement in here. Print. Don't do anything just so we know that it's actually working here. Don't do anything. And so one of these was false. Now if I change this back to Caleb, and so this is going to execute. And that's because my age 31 is over the age of 18 and my name is Caleb. I can drink alcohol. And so it's saying over here is true, and over here is true. Now we also have an end statement. So what I'm gonna do is I'm going to copy this, comment that out and then just paste it down here and change this end to an OR. And so what this is saying is the item to the left or the item to the right needs to be true. Only one of them needs to be true. And so if I go back up here and change my name back to Jacob, 31 is greater than or equal to 18, so that's going to be true. Name is equal to Caleb, that's going to be false. And so with an or statement, just one of these needs to be true. And we could say, I can drink alcohol if the name is Caleb or if the person is 18 or older. So go ahead and give this a shot and it's going to say I can drink alcohol. And effectively what this is doing is it's going to render it from the left to the right and is gonna say, this is true or something else. This happens to be false, but it doesn't matter if they're both true. Typically they both need to be true in irregular if statement or a comparison operator. But the keyword here is saying, only one of these needs to be true for this to execute. And so if I actually execute this code, you're gonna see that it works the exact same way. And it says I can drink alcohol comes because it's only looking for one of these answers to actually be true. But if we're looking for both answers to be true, we use the End keyword and that's gonna say both true. Or whatever's on the left. And whatever is on the right needs to be true. So both of them need to be true. And this in fact is going to say, don't do anything. And that's because one of these, while both of these need to be true, one of them is in fact false. And so it's gonna say don't do anything. And I was an undo that. So your task for this lesson is to give this a shot used the end keyword and the or keyword. Tinker around with that because you're going to see this a lot, a lot, a lot, a lot in production code. There's going to be a lot of comparison operators that use the End keyword as well as the or keyword. So please give this a shot and really let it sink into your brain how this is going to work for you. 25. Looping through items in an iterable (ie. Lists): For loops, we've actually dealt with these in a couple of lessons already. But a for-loop is just a way to iterate through some sort of iterable and is going to allow us to take an action based on every item in a list or a tuple, a set, or a string. So some sort of iterable object. And so we can say faith foods is equal to a list of pizza, tacos, and salmon. And so a for-loop is really easy. We're just going to do for every food in the iterable, the favor foods, iterable, print food. And so what this looks like is for item in your iterable, you can do something with said item. That's all that is. And so the syntax is really easy. But also notice indenting important, we're using a colon, so we indent at the very beginning of the next line. And so when I execute this code, we're going to see pizza, tacos, and salmon. Now we could also get a little fancier with this. And we can say print F String. My favorite food is, and then the food itself. And this is going to print three different statements. My fav food is pizza, tacos n salmon. And so literally that's all there is to this. Now we can mix this with a conditional or a conditional comparison operator. And we can say, if that food is equal to pizza, size is equal to input, what size pizza would you like? And then print an F Statement. You ordered a large pizza. And instead of saying large, we can change this to size. And so let's go ahead and try this now. And so what this is going to do is this is going to loop over every single one of these items, pizza, tacos, and salmon. No matter how many items are, it's going to loop over all of them. And then it's going to say, if that food happens to be pizza, which is the first one here, what size pizza would you like? That's going to ask me for this, the size and then it's going to say I ordered a whatever size pizza. Let's go ahead and give this a shot. And it says what size pizza, what I like, I want a large pizza. And it says you ordered large pizza or I could type small and it says you ordered a small pizza. Now what's cool about these iterables is we can swap this out for a set. So this is going to be a set or we could cast it. In fact, let's do that. Let's cast this. Fe, foods is going to be a set of favorite foods. Now this is going to work the exact same way, but let's, let's go ahead and comment this out. And let's just print the food. Print food. And let's execute. It. Says tacos, pizza, salmon. Tacos pizza, salmon. We know that that's a set primarily because it's not maintaining its order anymore. We could, instead of using a set, we could use a tuple. This is going to work the exact same way. We don't even notice the difference at this point. That's pretty cool. What else can we do here? Let's use a string, so let's loop through all the characters in the word pizza. So food is equal to pizza. With an exclamation mark. We can say four letter in food. Print that letter. And because we know that strings are sub scriptable, how we learnt that, we learned about subscripting in a lesson a little while ago. So it's going to get p i is f z a exclamation mark. And so let's go ahead and try this. It's also going to print out the foods from up above here in the FE foods. So let's just pay attention to the bottom part. So it says pizza tacos, salmon. That's from the top part up here. And then it says p i, z, z, a exclamation mark. And that's because it's printing every single letter in our string. We can also loop through dictionaries. So let's go ahead and comment this out. And comment that out and comment that out. We can loop through a dictionary. So we can say our dictionary of, let's say person is equal to name. Caleb. Twitter is going to be at Caleb Eataly and feel free to come follow me there. And Instagram is going to be at coding dot for dot everybody. And so if we tried to do this, we're gonna get something a little bit strange because there's a key-value pair. Which ones do we loop through? And so by default, we're going to see what it does for, let's just call it a value in person. Let's print that value. And we're gonna see that first of all, I need to save that file again. It just prints the keys name, Twitter and Instagram. What's cool about this as now, because this is a dictionary, we can say dot items. And this is going to be a key value pair. So we've got a key value pair. So now we're going to deconstruct this. So for every loop here, we're going to get the name and the value or the key. And the value, the key and the value. So it's going to be a key value pair. And we can do the key is key and the value is value due to, due to due. Let's throw an F at the front there for an F string. And let's try this out. And it says the key is name, the value is Caleb. The key is Twitter and the value is Caleb telling. The key is Instagram and the value is coding dot-dot-dot everybody. And we did that with items. And you can also try this with keys. And you can also try it with just values. But you're not going to be able to deconstruct. So you're gonna have to use a regular for loop. And I'm purposely making this a little bit more difficult to understand. Because sometimes you have to pick up code that we don't always understand and you're gonna have to sort of figure it out. Now what I would like you to do for your task, for this particular lesson is just this loop through some sort of Iterable. And if that's item that you're looping through is one of the items in your list. Print something. You can also do a very basic sort of example here and just loop through all the letters in a word if you wanted to. But the idea here is that you're using the for keyword, some sort of variable name in an iterable. And then you can print whatever that variable is. Go ahead and give that a shot. And when you were done, let's head on over to the next lesson. 26. How to loop forever (careful!): Hello and welcome back. Let's talk about while loops. While loops are not like for-loops whatsoever. So a for-loop, if we open up the for-loop example here, a for loop is going to loop through every item inside of some sort of iterable until it exhausts itself. And then it's going to just silently finish. And we don't have to worry about handling that sort of finish in a while loop we do though. And so a while loop looks like this. While something is true, do something. And so this can actually get a little bit dangerous here. So you're gonna wanna be careful with this. But what we can do is we can loop through something until we tell it not to. We don't explicitly need some sort of iterable. And so as an example here we can say, while number is less than or equal to 100, print that number. Now the number doesn't exist yet, so let's make it exist. Num is equal to 0. And so while num is equal to 0, or I said one, but it should be 0. While num is equal to 0, print the number. Then what will we have to manage this on her own now, so we could do num is equal to num plus one basic addition here. So for that first iteration, it's gonna say while 0 is less than or equal to 100, print that number and then add one to that number. Then it's gonna say, okay, well num is now number one. It's still less than a 100. Print the number one, make it number 22 is less than a 100, prints a number to make it the number three, so on and so on and so on. And so what we're saying here is eventually this number is going to be the number 100 or 101 and it's going to be a false statement. And so eventually, eventually this is going to be false. And that was actually incorrect. Eventually, this whole statement is going to be false. And if it's false, it's not going to execute. We want it to execute for as long as we want it to execute until that statement. This statement here becomes false. Let's go ahead and run this Python while loops dot py. And we can see if we scroll on up, it is just going and going and going. So it goes from 012345678910 all the way down to 99100. And so we can do something a 100 times, 1000 times, 10 thousand times, five times if we wanted to, doesn't really matter what the number is. The important thing is that eventually this statement becomes false. Now what I want you to do is give this a shot to try it out on your own. And in the next lesson, I'm gonna show you how we can break out of a loop or how we can skip certain numbers in a loop. 27. How to skip loop iterations and break out of them early : Alright, and the last two lessons we looked at for loops and while loops, there is a way to break out of a loop or to completely bypass a loop iteration entirely. And so let's do two examples here. Let's do one with a for-loop and one with a while loop, and we're going to use break and continue. So first we need to set up an example. So let's say items is a list of items and we've got 12345. And so what we can do here is we can say for item in items. And if we were to print each item, we would get print one, print to print three, print, print five. What we can say here is, if that item is equal to two, we can completely skip this. And so this continue keyword says anything below this is not going to get executed in the for loop. And at the bottom we're going to print the item. And so it's going to go through the first item. It's going to be one, it's going to print that item, is going to actually compare that one is equal to two, that's going to be false, so it's not going to continue. Then it's going to print that item. Then is going to go into that second iteration. And it's gonna say is to equal to two. That is in fact true. It's going to continue and this is not going to execute. And in fact, we could say if item is equal to two or item is equal to four. Let's go ahead and skip that iteration altogether. So it's only going to show 135. So if we do python break and continue dot py, it's gonna say 135. So that is one example. I'm gonna comment this out. And let's create another for loop example for item in items. And then we can say if that item is equal to three break. And what break is going to do is it's going to completely get out of this for loop altogether. So now we can print the item and it's going to print 12. And then it's gonna check to see if is on item number three. In fact, it, it will be eventually on item number three and we're going to break out of that loop so 345 don't show up at all. Let's save that and give this a shot and it's just as 12. And again, what that's doing is first iteration is one, one equal to three. Note, okay, so it's not going to break out. Two is two equal to three. Nope, so it's not gonna break out, it's gonna print that item instead. A3 is three equal to three, it is. And so it's going to break out of this for loop altogether. And it's going to stop working at three, and it's not going to execute four or five. Alright, let's move on to a while loop. We can do something like this. While num is less than or equal to 20. And let's make sure we have a number in here. That number is going to start off at 0. And we always need to increment our number. Number is equal to num plus one. Then we can say if that num modulus two, so it is equal to 0. So we're saying if, let's say the number four divided by two has a remainder of 0, then we're going to skip it. We're going to continue. And let's print that number, print that current number. And what this is going to do is it's only going to show every second number. And in fact, I actually have a logic bug in here. So before giving it a shot, let's, let's increment that number first. Because what we're saying here is if that number eventually hits, I continue with the incremental down here. It's never going to increment. So num is always, always, always going to be less than 20. We need to move this up. And so let's go ahead and give this a shot. And there we go. We're getting only odd numbers, 13579111315171921. Now if you're scratching your head wondering how we got 21 as because this number at 1 was the number 20, we immediately added one to it. So it's now 21. And then it's going to say if 21 modulus two has a remainder of 0, then continue otherwise print it. And that's how we got 21 in there. Interesting little thing. So now we're using a while loop. We're using modulus and reusing continue. Let's go ahead and break out of a loop if a loop is reaching the number 13. So we can say num is equal to 0, while num is less than or equal to. Let's give it some absurd number. Don't even know if that number is, let's give it. 10. Million. Num is num plus one. And then we can say if num is equal to 13, break out of this loop, otherwise, print this number. So when we see this, eventually this number is going to, let's actually start at the very beginning. We have a while loop. While 0 is less than a million. Then that 0 is then going to become one, then two, then three on every iteration. Eventually that number is going to reach the number 13. And when that number is 13, we're going to break out of this loop. And so let's go ahead and give this a shot. While num is less than 1 million incremented on every iteration, if that number is ever 13, break out of it. And let's see what happens. When we get up to 12. We get 123456789101112. It stops on the unlucky number of 13. Now what I'd like you to do is try out all four of these examples to try a brake and a continue keyword in a for-loop and in a wild loop. Also, if you ever get stuck in a while loop, you can always just cancel. So let's go ahead and type Pass, and let's execute this. And you're gonna see that my script looks like it's just loading forever. You can hit control C to cancel. And I'm going to undo that so that you have the source code later. Go ahead and give that a shot. When you're done, we're going to level up our coding skills with this thing called a function. 28. Re-usable code: Functions! : Alright coder, Welcome to the world of functions. Functions or how we can write code once and use it over and over and over again. So let's go over the basic syntax of a function in JavaScript, it looks like this function. Some name, parentheses, curly braces. In Python, we use D, F instead of the word function. Then some name, parentheses and a colon. And it's going to do something in there. Let's get rid of the JavaScript one because this is not a JavaScript course. And then to run the function, we do some name. And that's it. That's going to run the function for us. Now this is not a very good function. It does absolutely nothing. Let's go ahead and print something. Print hello world. And let's run this function. So let's do Python functions dot py and it says hello world. And every time we copy this is going to print it over and over and over again. And so this idea stems from the idea of derived d y. And it stands for don't repeat yourself. And so you can write one set of code over and over and over again. Now again, this function is still not very useful and we need to work with some sort of customization. So if we said hello and we wanted to put a name in here, and that has to be an F String. Well, this just simply isn't going to work. There is no name. It says name error, name. The function or the variable name is not defined on line two. And that is in fact that is true. It's not defined anywhere. And so we can set the name out here. Name is equal to Caleb, and let's see what this does. Ok, cool. So we inherited name from out here. But what if we wanted to throw some sort of name and here a different name. This is called an argument or a parameter, and we could do that. So let's get rid of this name and put name in here. And now if we run this function without a parameter in your notice that these sort of have to match. And there is no parameter in here. We didn't specify name, we're going to get some sort of error here. We get to some name is missing one positional argument called name. So let's go ahead and throw a name in here. G, U, L, L, Y. That's gully. Let's go ahead and run. It says Hello gully. We could do any other name, any other name, and it's going to work for us. And so basically what we're doing here is we're saying we defined a function and then we executed that function and that parameter in here. This name is now available inside of this function. And we pass that in through here. So we can do one parameter, two parameter three parameters would be 50 parameters though. Please, if you ever writing advanced code, don't use 50 parameters in your functions, that's very hard to read. I've seen some senior developers do that before. It's just a very bad practice. Do something basic. So let's say name and food. And let's say in here we're going to say hello name. Let's eat some and then whatever that food is going to be. And so again, if we run this without that second parameter on line four here, we get some name missing one required positional argument food. And so let's go ahead and change this back to Caleb. And let's see, tacos. And so all we're doing here is really matching how this looks. And so now we've got a function, first parameter, comma, second parameter. Now what's interesting about this, and this is a little more advanced, is if you just extract, this, looks like a tuple, doesn't it? And in more advanced Python, we're going to be talking about args, arguments and keyword arguments. And when you have arguments like this, it always comes in as a tuple. Now this is a standard variable. We can overwrite it and this is a standard variable. But what I'm talking about later is this thing called arg. So you're gonna see that down the road. Probably in Python 201. Or if you see it in the wild, you sort of have an idea of what people are looking at. And so when I write this now, Doo-doo-doo-doo, it's going to say Hello Kayla, but let's eat some tacos. And that's because name is matching this first parameter and the second parameter is matching food. Now what if you wanted to add some sort of default in here in case someone didn't add something, we can add a default value two, we can say food is going to be pizza. And so what this is saying is, this is now a required positional argument or a parameter. And so we have to have that. And food is not, food is going to default back to pizza if we don't give it any sort of value whatsoever, if we completely ignore that it's even there, it's going to default to say pizza. So when we run this, it's going to say hello Caleb, let's eat some pizza. But the nice thing about this is just because that's the default doesn't mean we can't overwrite it. We can totally override it. In the second parameter here. We can say oranges. And let's go ahead and run that. And it says, let's eat some oranges. Now, this is called a keyword. Argument is as a keyword and is looking for some sort of value. So you could also do food is equal two oranges. And this will work as expected. And that's really all there is to a function. And now we can add our if statements in there. We can add a wild loop, a for-loop. We can do anything we want inside of this function and it will execute every single time. So now we can say if that name dot lower is equal to caleb, all lowercase print. Welcome Caleb. And if this doesn't match anything, it's just going to print this one statement. If the name is in fact Caleb is going to print two statements. It's going to print welcome Caleb and hello Caleb, let's eat some, whatever that food is going to be oranges. And so let's go ahead and run this and it says hello Caleb, let's go ahead and change it. Change it to Jacob. And now we're using a name. Variable as a parameter or an argument inside of our function were lower casing it. We're using a conditional statement. We're checking to see if it's Caleb, and we're using a print statement in there. Let's do one more fun little example. Let's do name is equal to None. And let's get rid of this. So Do to Do, to Do, we have to default values here. Name is going to be none and food is going to be pizza. Let's, let's just see what this looks like. It says, Hello none, let's eat some pizza while none isn't a person. But what we can do now is we can say if name is none, we can overwrite that name is equal to Zafar. And then we're going to, and then it's going to basically use that name again. So just leave some space for us there. And it's going to say that name, if it is, none, is going to be Zafar. And so this is a lot like setting the default here to be Zafar. The only difference is really we're doing this manually. And so Python comes with default keywords, but we don't necessarily need to use them. We can fall back and use our own way as well. We can also do some internal workings as well. So we can say person type is going to be, let's say a human at first. And this is just an internal variable. This can not be set from outside of or when we execute the function person type, just happy, sad. And then we can say, if name is equal to Zafar, we're gonna change that person type to be a cat. And we can print that as well, print person type. And so now we're making a fairly advanced function here. We've got a lot going on. Let's go ahead and run this Python functions dot py and says, it's a cat. And it's no, we have some internal workings of a function. But what if we wanted to store the result of a function in a variable? So sum variable is equal to this and then print the variable is SUMVAR. Now this simply isn't going to do anything for us. Let's lowercase that. It says none. And so what we're looking at here is if we have a function def, some function, and it doesn't return anything, it's always going to return none. So every function by default is none. In Python, what we can say instead is return a value. And so let's comment out this example because it's getting a little unruly. And we can say thing is equal to some function print thing, and this is going to say a value. And so now we have a function using the return keyword. And it's simply going to store whatever it returns and it can only return one thing. It's going to return this one thing in here. And this is a lot like saying a value. Just like that. Let's go ahead and execute this and see that it works. And it says a value. So at this point in time, this is probably not super useful to you. Let's create a basic exponent calculator. So let's go ahead and comment that out. And let's do def x EXP for exponents num1, num2. And we can say the total is going to be num one exponent numb to return the total. And now we can say something like big number is equal to EXP. And then let's say 33 to the power of six. And let's print that big number. Print big number. And let's execute this. And it gives us a very big number, whatever that number actually is. Chances are that's right, it is using an exponent, so it's number two, the exponent of number two. So in summary, a function is defined with the DEF keyword, then we give it a name. We have unlimited parameters. We can have default parameters for default values for the parameters. If we wanted to do like we have up here, then we can work with these variables inside of the function. And when we want to return something so that we can store it in a variable outside of this function, we can just simply use the return keyword and return whatever we want. And then to execute it, we simply do a variable is equal to and then that function name the two parameters that it's looking for, num1 and num2, and we can print out that big number. Now what I would like you to do for your task is a very, very simple function. Functions are a weird thing to think about at first. So if you're brand new to functions, this is going to be a little bit interesting for your brain to wrap around. So just create something very basic like this and try to execute it like I have on line 28. And then go ahead and explore how to break it. So numb. Two, you could change that name to anything else. Or instead of doing exponent, you could do a modulus. You could even try not putting it in a variable. You could try returning something like that. Either way, I want you to get some hands-on experience with this because functions are a major part of python. There's just no way to avoid them. You have to write them in. The nice thing is, if we had another big number, big number two, we could do like 12 to the power of 12 if we wanted to. And now it's doing this math for us. Or if there's an if statement in there, it's going to execute that if statement. It's just going to execute the same code over and over again for us. And all we're doing is injecting small little variables in there to just, just tweak it and just a little bit. So go ahead and give that a shot. When you're done, we're gonna talk about a little more advanced subject called scope. 29. Variable scoping determines where the variable can live: Let's talk about scope. Variables that live inside of a function simply cannot be accessed from outside of a function. So let's create a function called Deaf myFunc. And it's going to create a new variable in here. Name is equal to Caleb. And so, so far we've been able to write variables anywhere and access them anywhere. But inside of a function. When a variable is declared, it can only live inside of that function, and that's called scope. So if we try to print name, this simply is going to give us some sort of error. So if we do python scope.dish, it says name is not defined. Even though it is. Even if we run this function, it's not going to work for us. We're going to run this function. It's going to, let's maybe even return something. Let's make it a proper function. And we still see that name is not defined on line six. And so it's trying to look for it. But really, you can think of it like this. When code is inside of a function, it's zipped up. It's like I just collapsed it here, but it's like it's being zipped up and you don't have access to that name. So you can pretend that it doesn't exist when it's inside of a function. When you have a variable inside of a function, you can use it any way you like. You can declare new variables. You can use variables from parameters or arguments or anything like that. But simply put, a variable that is declared inside of a function cannot be accessed from outside of a function. Now let's create a, another example here where let's get rid of this. And let's print out whatever myfunc returns. And let's create a name outside of the function. Name is equal to Caleb. Now we saw that originally we got a name error. What this is going to do now is it's going to look outside of its function. So this is called scoping. So it's gonna look inside of its function for the name. If it doesn't find that name, is going to then look outside of that function. And if it doesn't find it outside of that function is going to look outside of that function. And if you come from the world of JavaScript, this is called a closure. So let's go ahead and run this. And we're gonna see that it says Caleb, and what this is doing here is I've got a function called myfunc. I'm executing it. It's returning the name and that name is being defined out here. Now what if I change that name inside of the function name is equal to new name. What is this going to print out? Because I've declared it up here. It's accessible in here in this entire function, but I've overwritten it with a new value. Is this going to print out new name or is this going to print out Caleb, let's go ahead and give this a shot and remember when you don't know things like this, it's always good to try it out, try it out and then ask questions about how it works. And so you see, my function is returning new name. But what happens if I print name? Just name itself, because again, it's declared here. It's being scoped into its function and then it's being returned. So this one we know is going to return Caleb, not Caleb. Sorry, new name. I was looking at the wrong words there. This one's going to return a new name because we're overriding it in the function. But because we're overwriting it in the function is also going to overwrite this one. Let's go ahead and give this a shot. And it does not. And the reason for that again is because we are simply looking for a name outside of a function if it doesn't exist in the function. But hey, guess what it does. It exists inside of this function. And so it's going to use this scoping. And as one last example, let's go ahead and comment this out. And so this is going to return Caleb and name. It should also be caleb as well. And we see Caleb, Caleb. And so that is scoping. So whenever you see a name error that a particular variable is not defined, chances are it's just outside of the scope and you have to make it accessible somehow. And so if it's not accessible inside of a function, you can always make it accessible by passing in Name and maybe giving it a default. And yeah, that's pretty much what scope is. So there's no task for this lesson. But what I want you to take away from this lesson is the idea that if name doesn't exist or if any variable doesn't exist inside of a function, it's going to look on the outside of that function. So it's going to look everywhere else above it for that name. If it doesn't exist, then you're gonna get a name error. Let's do this, let's get rid of that. Try it again and we get a name error. Name is not defined. But it back. And it says Kayla, Kayla. So this is a tricky thing to wrap your head around, but whenever you see a name error, just assume that there is some form of scoping going on or your variable simply is not defined. And chances are it's probably not defined. Where do you think it's going to be defined? And thus, we get into the realm of scoping. This is really important when dealing with functions as well. Now, moving forward, it's always a best practice to put your content inside of a parameter, put your variable inside of a parameter, or I guess it's called an argument as well. Put it inside of a parameter and explicitly pass it in. It's very Pythonic to do this in JavaScript. We don't do this. In JavaScript. We leveraged closures a lot and it gets really messy in Python. We don't like that. In Python. We really, really, really like when we can just pass something in explicitly. 30. How to create a localhost webserver: Alrighty, let's talk about a local server. This really doesn't have anything to do with Python one-to-one, but it is a very fun, interesting thing that we can do. So in Python, we can set up a simple HTTP web server. If you've taken any of my previous classes and you've used ajax and you ran into some quarters error or you aren't allowed to load data from somewhere else on the web. Chances are. It's because your file was being loaded from your browser. And so you had something like users slash website dot HTML loaded up in your URL. And you know, for the most part that's going to work, but sometimes it doesn't, and sometimes you need a local server. So to run a local server, we'd just go over to our terminal here. And we can type Python or Python three if you're using Python three or Python 3.9, if you're using Python 3.9, dash M HTTP dot server. And it says serving HTTP on port 8 thousand. So if we open up our browser and we go to localhost port 8 thousand, we're going to see all of our files show up. Now that may or may not be what you want it let's open up the helloworld one or we can actually see the source code in there and the URL changes its local host port 8 thousand slash helloworld dot pi. Let's go ahead and create. Let's actually cancel out. Let's go ahead and create a new file in here, index dot HTML. And let's do HTML colon five, tab, tab Python 101. Welcome to Python 101. And let's put that in an H1. And so now we can serve static HTML through Python. Now there's not a lot you can do with this. This is a very basic you can't except get requests or post requests. You can't use forms and things like that. It is a very basic server and that's why it's called a simple server. So let's go ahead and rerun this server. And when we head back here, we're going to see that index.html loads for us. It says welcome to Python 101. We still have access to that old file. Hello, World dot pie shows us our source code. That's cool. But now that we have an index file, it's loading as if it's a regular web server. And so this can be really good for just simple web servers if you need to create like for example, a JavaScript project where you're making a random Star Wars character generator. You can use localhost port 8 thousand. And what's nice about that? If you know anything about proxying and if you don't, you can just ignore this part. But if you know about proxying, then you can use a service called end grok. And you can use this server to make your website completely accessible to the web using a URL that they give you, which is really, really cool. So now you have a simple web server running at any point in time. Using a simple web server. And people then can view your files or they can download your files. So this is pretty fun and this is actually not used in Python one-to-one whatsoever, but honestly it doesn't belong in Python two or Python 301. So I thought I'd throw it in here as a fun little thing to do. And if you want to make the site accessible to the web, definitely check out and grok.com. Oh, and lastly, if you want to cancel it, you just hit control-C to cancel. Once you've done having some fun with that, let's head on over the next lesson where we learned about random choices. 31. Introduction to Python modules with the Random module: Alright, let's take a look at creating random choices in Python. And this gets into using Python packages, which is really, really good to know about. It's a little beyond Python 101, but it's also, we're going to need it for our final project. And so this is a good introduction into Python packages. So what I have here is just my terminal. And if I type Python 3.9, this is going to give me my Python Shell. And to create random choices, first of all, we need to import a package called random. This doesn't come default with Python. You have to actually import it. And so now we can import this and we can say random dot choice, and then we can give it a list. And so it's giving it a list of rock, paper, scissors. And it's gonna give me rock. And it's just random everytime you just happen to be the rock showed up a few times in a row. Scissors showed up a few times in a row. It's just completely random. And so the syntax here is really easy. So if I scroll back on up, we have import random and then we used random dot choice. It's a function, so it has parentheses around it, and it only takes one argument. And that one argument has to be a list. It has to be some sort of Iterable. So I gave it a list and he just gave me rock, paper or scissors. And we're going to need this inner Final Project. So what I would like you to do is open up your Python Shell, import random, and then type random.choice and give it a list. That has to be a list of choices and they just keep executing it. And like I did. And it will show you all the different choices are rather it won't show you all the different choices. It will choose one and eventually you'll be able to get all the random choices. Go ahead and give that a shot. We're going to need this in our project, which is coming up in our next lesson. 32. 310 Final project: Alright, welcome to your project. We're going to make a rock paper scissors game where you play rock paper scissors against a computer. So you're going to need a few things for this. I'm going to tell you what kind of things you're going to need as a little bit of guidance. And then I'm going to let you write the code on your own. So first things first you're going to need random. We learned about that in the last lesson. I straight up told you we're going to be using it in the project. You're going to need a wild loop that runs forever until you tell it to stop. So you can just keep playing rock paper scissors until you, when you're going to need to use input to accept one answer from the user, being you. So it's gotta be rock, paper or scissors. We're going to be using print statements, print statements and we're going to be using print formatting. We're going to be using comparison operators, break and continue. And with all of these things equipped in your tool belt, you can now create a very easy project, a symbol rock-paper-scissors project. So what I'm going to do is I'm going to fade the screen out for a few seconds. I would like you to then pause the video and try this out on your own. Try problem-solving this out on your own. You need to break it down into smaller tasks. Everything in this project we have learned in this particular course in Python 101. And the idea here is you want to keep playing rock-paper-scissors until you win against the computer. So I'm going to fade out to black. You pause the video, give it a shot. If you get really stuck. Just feel free to resume the video and you can see exactly how I go about doing this. Okay, let's create a rock paper scissors game where you play rock paper scissors against the computer. So first I'm going to save this as project dot py. So first things first, what do we need? We need to constantly ask ourselves for input. So we're going to need some sort of while loops and do while true. And we know that it's going to run for ever, because remember, it's going to constantly run while this is true. And then we need to ask ourselves, do we want to pick rock, paper or scissors? So let's do. My answer is equal to input. Choose rock, paper, or scissors. Now what happens if I don't choose rock-paper-scissors? Well, we need to basically quit the loop and try again so we can either break or we can continue. So let's do. If my answer is not rock and my answer is not paper. And let's make that a little smaller. And my answer is not. Scissors. Then print. Please choose a correct answer. And let's continue. And so this just ensures that our answer is always going to be rock, paper or scissors. After that, if the code makes it past line seven, we know that the answer is either rock-paper-scissors and in fact, what we can do, just to make sure we can say My answer is equal to my answer dot lower. Now we need the computer to make some sort of a random choice. It needs to choose rock, paper or scissors. And so we need to import Random, import random and we need to give it a random choice. So computer answer is going to be random dot choice. And you can choose from rock, paper, or scissors. Now we need to compare. We need to see if rock beats paper, if rock beats scissors, all that kinda stuff. And we can do a pretty big if else, if else statement. So I'm going to just shut that down a little bit and then move that down rather. And we need to see if the computer's answer is the same as my answer. So we can say if my answer is the same as the computer answer, print. You tied. And let's continue. Because once you've tied, you've tied, you just have to start over again. Then we can say l. If my answer is equal to paper, and the computer answer is equal to, well, what is paper beat? Paper beats rock. We can say print. You win. And in fact, let's, let's add a computer answer in here. Let's print the computer answer. F. Computer. Chose the computer answer. And where were we? If my answer is equal to paper and the computer's answers equal to rock not rocks is gonna say I when l, if my answer is equal to rock and computer, answer is equal to scissors. Print you when l, if my answer is equal to scissors and the computer Answer Is equal to paper, print, you win. Now everytime we win, what do we want to do if we want to quit the game every time we when we break. So let's do break, break and break. And if we don't win, or rather if we haven't tied and we haven't won. The only other option is we've lost. So then you can say print. You lose. Try again. And because this while loop is going to keep going on forever, if you lose, it's going to hit the end of this file and then it's going to start back up at the top. Let's go ahead and give this a shot and see if there's any typos or anything. Guth Doo-doo-doo-doo. Clear. Python Project at pi, choose rock-paper-scissors. That's due rock. Oh, I won. Let's try it again. Paper. I tied. Let's try scissors. I lost. The computer, chose rock, rock beats, scissors. Okay, now what happens if I want to quit while I can hit control C? And that gives me this Grove's little keyboard input error. What I can do instead is if my answer is not rock or any of these, then please tracking, please choose a correct answer. I can also do one more if statement in here. I can say if my answer is equal to quit, simply break. And let's try this again. Now it's gonna ask me for rock paper, scissors. I can type quit and it didn't work for me. There we go. I have to actually type quit without a space at the end. So that space at the end, very, very important. And that is how we create a rock paper scissors game. And so really all I did was I executed it from the top to the bottom. So I asked myself, do I want to play rock-paper-scissors? Yes. So I started a while loop. Then I asked myself the actual question, do I want to choose rock, paper or scissors? And then whatever that answer is, I made it lower. And then I said, if my answer is equal to quit, breakout, get outta here. I don't want to play rock-paper-scissors anymore. Then I also check to see if my answer was rock, paper or scissors. If it's not rock, paper or scissors, is gonna tell me to please choose a correct answer and then it's going to continue. So it's going to skip all this stuff down here. And it's going to start at the top again, choose rock-paper-scissors. Then we're going to generate a random computer answer on every single iteration. So this is going to change it every single time. So it's not going to be rock or paper or scissors. Every time it's going to be completely random, it's going to be rock or paper or scissors, we don't know. And then we're gonna say, the computer chose rock, paper or scissors. We're not sure, but it's going to print it out for us. If my answer is the same as the computers answer, we tied, continue and basically don't execute anything else. No, technically we don't need that because this if statement was already called. And so because this if statement was executed, this, this if statement, the L if and the else statement not going to be executed. So it's gonna jump back down to the bottom to line 30. There's nothing there and so it's going to restart automatically for us. So that was actually optional. Then we said, well, if the answer was not a tie, then what did we do? We say we check to see if my answer was paper and if the computer's answer was the losing answer, paper beats rock. So i win, break. And that just gets out of the whole game entirely. Then I check to see the same thing. But this time I'm checking to see if I chose rock. And if the computer chose scissors, if so, rock beats scissors, I win and then break again. And then lastly, we do the same thing. If my answer is equal to scissors and the computer's answers equal to paper, I wouldn, and then break out of the game again. Otherwise an every other scenario if it's not a tie and if I don't win, I've lost. And that's saying you lose. Try again. And because it's not breaking out of this loop, it's going to boo, boo, go back up to the top and ask you again. And that is your rock-paper-scissors project. Now there are ways to make this significantly more friendly. We're doing a lot of N's in here. We don't necessarily need to do ends in here. We haven't learned about the in operator. So if you want to learn about the in operator, the in comparison operator, definitely go and check that out. So you can check to see if, for example, my answer was in a list of rock, paper or scissors. Instead of writing all of this out. We could also merge these three together. These three L if statements. We can merge them all together. And say if my answer is paper and the computer's answers rock, cool. Or my answer is rocking the computers answer is scissors or my answer, scissors and the computer's answers paper. Then I went, We can do one big L If statement here. So there's a little bit to gain here. I would definitely suggest try out my code and then just try making it more efficient. Try merging all of these else ifs together. Learn about the in operator. Learn how you can compare a string to an array, for instance. But yeah, that is the entire project. That's all there is to it. So feel free to download this code and use it whenever, whenever you want. 33. 320 Summary: Welcome to Python, one-to-one summary. You have made it all the way through Python one-to-one. First of all, it congratulations, I hope you learnt quite a bit. Remember this is just basic Python. There's a lot we didn't touch on. I have several other courses. I have other Python courses if you want to check them out. I have Python for Everybody.com. I'm going to type that out. Python for Everybody.com. It is a massive, massive course. It will teach you so much more than what you learned in this course. Don't forget if you ever want to follow Me, you can always follow me at UW, I made that way too big. Twitter.com slash. Caleb, telling, if you want coding tips and tricks, you can always go to Instagram.com slash coding dot for dot everybody. And if you want any sort of help with any sort of code, especially web development code, come on over to learning to code on Facebook. It's a Facebook group. There's about 62 thousand members in there right now with hundreds of people joining every single day. There's a lot of support. You can, you can have it completely free. You don't have to sign up for anything. You just need a Facebook account. I tend to answer a lot of questions and they're significantly faster than I do on other platforms. Last but not least, my name is Caleb tall, lean. Thank you for taking this course. I appreciate you taking the time. I know there's a lot of other Python courses out there and you could have taken any other course, but you chose to take this one. And I really appreciate that. And the fact that you made it all the way through this course, I am very proud of you. So keep up the good work, keep working hard on your Python skills. And I look forward to seeing you either in the Facebook group or maybe working with you in the future, or seeing you in another course of mine. Happy coding. By