Python 101: The Complete Beginner’s Guide | Alexander O. | Skillshare

Playback Speed


1.0x


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

Python 101: The Complete Beginner’s Guide

teacher avatar Alexander O., Web Developer & Cyber Security Expert

Watch this class and thousands more

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

Watch this class and thousands more

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

Lessons in This Class

    • 1.

      Course Promo

      1:45

    • 2.

      Introduction to python

      4:53

    • 3.

      Important note about replit

      4:31

    • 4.

      Introduction to replit

      5:20

    • 5.

      Hello World

      5:47

    • 6.

      Data types section intro

      1:21

    • 7.

      Working with Strings

      8:53

    • 8.

      Other types of Variables

      4:49

    • 9.

      Boolean Operators Updated

      4:13

    • 10.

      Arithmetic Operators Updated

      5:33

    • 11.

      Working with strings section intro

      1:29

    • 12.

      Basic string manipulation

      5:35

    • 13.

      String Indexing and Slicing

      8:50

    • 14.

      Common string methods

      7:19

    • 15.

      More string manipulation

      10:58

    • 16.

      Control flow and loops section intro

      1:41

    • 17.

      Intro to algorithms

      5:40

    • 18.

      If else statements

      11:46

    • 19.

      Simple password checker

      4:38

    • 20.

      Introduction to loops

      4:16

    • 21.

      For and while loops

      10:01

    • 22.

      The BREAK statement

      13:16

    • 23.

      Try except statements

      7:11

    • 24.

      Continue statement

      6:42

    • 25.

      Programming challenge

      17:37

    • 26.

      Functions section intro

      1:01

    • 27.

      Defining & calling functions

      3:16

    • 28.

      Parameters & arguements

      7:15

    • 29.

      Keyword arguments

      4:27

    • 30.

      Local & global variables

      5:19

    • 31.

      Data structures section intro

      1:04

    • 32.

      Lists

      7:34

    • 33.

      Using loops in a list

      2:57

    • 34.

      Dictionaries

      7:57

    • 35.

      Nested dictionaries

      4:53

    • 36.

      Sets

      6:57

    • 37.

      Programming challenge sets and loops

      5:52

    • 38.

      Programming challenge dictionaries and loops

      12:31

    • 39.

      Authentication system project

      4:22

    • 40.

      Step 1 build the registration function

      6:18

    • 41.

      Step 2 Build the login function

      7:04

    • 42.

      Step 3 creating the authentication system

      8:42

    • 43.

      File handling section intro

      1:17

    • 44.

      Reading and writing to files

      8:04

    • 45.

      Reading and writing inputs to text files

      7:54

    • 46.

      Read and write methods

      9:25

    • 47.

      Handling file errors

      7:44

    • 48.

      Multiple except blocks

      3:33

    • 49.

      The finally statment

      5:03

    • 50.

      Libraries and modules section intro

      0:54

    • 51.

      Introduction to libraries and modules

      4:32

    • 52.

      Creating a custom module

      6:14

    • 53.

      How to wok with a standard module

      3:36

    • 54.

      Password generation with random and string modules

      7:12

    • 55.

      The datetime module

      6:54

    • 56.

      Working with an external library

      8:48

    • 57.

      Encryption & decryption tool project intro

      3:34

    • 58.

      Task 1 create the encryption key

      6:11

    • 59.

      Task 2 creating the encryption and decryption functions

      8:35

    • 60.

      Task 3 adding the user interface

      15:26

    • 61.

      Oop section intro

      1:08

    • 62.

      Introduction to OOP

      4:52

    • 63.

      Createing a basic class

      12:56

    • 64.

      Creating a basic class part 2

      4:09

    • 65.

      Encapsulation (1080p)

      6:02

    • 66.

      Inheritance

      5:52

    • 67.

      Polymorphism

      3:26

    • 68.

      Final project preview

      4:48

    • 69.

      Part 1 creating the user interface

      4:39

    • 70.

      Part 2 adding the user registration function

      4:28

    • 71.

      Part 3 adding the login function

      6:24

    • 72.

      Part 4 adding the password strength checker tool

      12:20

    • 73.

      Part 5 hashing the user passwords

      8:01

    • 74.

      Part 6 adding the audit tool

      10:12

    • 75.

      Part 7 adding the view logs function

      5:28

    • 76.

      Part 8 adding the post login menu

      7:05

    • 77.

      Final project conclusion

      1:44

    • 78.

      Course Conclusion

      1:10

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

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

1

Student

--

Projects

About This Class

Python 101: The Complete Beginner’s Guide is designed to equip you with foundational Python programming skills essential for today’s security landscape. Tailored specifically for cybersecurity enthusiasts and professionals, this course will take you from Python basics to building scripts that automate security tasks, handle sensitive data securely, and interact with system resources efficiently.

Who Is This Course For?

  • Students looking to build Python skills for entry-level roles.

  • Current security analysts who want to enhance their toolkits with Python for automating tasks.

  • IT and security enthusiasts with little or no programming background who want to start using Python in a security context.

No previous programming experience is required—just a willingness to learn and explore!

What You’ll Learn

Through hands-on exercises, real-world examples, and project-based learning, you’ll gain:

  • Python Fundamentals: Start from scratch with Python syntax, variables, data types, control structures, and functions.

  • Data Handling for Security: Manage and process data with Python’s lists, dictionaries, and file handling capabilities, essential for working with logs, IP addresses, and credentials.

  • File Management and Automation: Open, read, and write files to automate repetitive tasks and streamline data processing.

  • Secure Programming Practices: Learn to safely handle passwords, hash data, and manage sensitive information following best security practices.

  • Intro to Object-Oriented Programming: Discover how classes and objects can help you build modular, reusable code for security applications.

  • Practical Cybersecurity Applications: Build scripts to solve security-related tasks, such as checking login attempts, filtering suspicious IPs, and working with basic encryption.

Course Highlights

  • Cybersecurity-Focused Projects: Apply what you learn in projects that simulate real-world security scenarios.

  • Step-by-Step Guidance: Each module builds on the previous one, making it easy to follow along, even if you’re new to programming.

  • Hands-On Learning: Gain practical experience with labs, exercises, and quizzes to reinforce each skill.

  • Code Along with Examples: Follow cybersecurity-themed examples to apply Python in ways that make sense for security tasks.

Why Python for Cybersecurity?

Python is one of the most versatile languages in cybersecurity, offering flexibility, simplicity, and an extensive library ecosystem perfect for data analysis, automation, and threat detection. Whether you’re scanning logs, analyzing network traffic, or creating custom tools, Python can empower you to work more effectively and make an impact in your security role.

Join the Course Today!

Take your first step into Python programming and start building skills that will support your cybersecurity career. By the end of this course, you’ll be able to confidently use Python to automate tasks, analyze data, and create security-focused scripts.

Enroll now and get ready to harness the power of Python for cybersecurity!

Meet Your Teacher

Teacher Profile Image

Alexander O.

Web Developer & Cyber Security Expert

Teacher


My passion is teaching people through online courses in a fun and entertaining manner.  I have been teaching online for about 3 years now and during this period, I have created over 25 different courses on different platforms including my own personal platform - The Web Monkey Academy.

What would you like to learn?

Would you like to learn how to build and manage your WordPress website? Would you like to learn advanced skills that will make you a true WordPress developer? Would you like to learn how you can establish a successful career as a web developer? Would you like to learn the basics of information and cyber security?

 If you want to do any of these things, just enroll in the course. I'm always improving my courses so that they stay up to dat... See full profile

Level: Beginner

Class Ratings

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

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

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

Transcripts

1. Course Promo: Are you interested in both cybersecurity and Python programming? If the answer is yes, you've come to the right place. Hi, my name is Alex. I'd like to welcome you to my very special Python programming for cybersecurity course. Now, this is a course I've designed specifically for both cybersecurity professionals and enthusiasts. Who would like to learn about Python programming, but in a way that is relevant to cybersecurity. So what exactly does a Python for cybersecurity course mean, I simply means that all the code and programs and tools we're going to build together in this course will be related to cybersecurity. Now, if you've never written the land of code in your life before, if you're completely new to the world of programming, don't worry. I'm going to teach you all the basics of programming. And of course, I will teach you the essentials of Python, such as variables and data types, control flow and loops, functions, data structures, object oriented programming, file handling, and so much more. Along the way, we're also going to build together different types of cybersecurity tools, such as a password strength checker tool, auditing tool, and encryption tool. And finally, for the class project, we're going to build together a user registration system. I should also mention that this course will come with tons of quizes coding exercises to test you to challenge you and ensure that you are on the right track. So if you're looking to add the Python skill to your resume, maybe you're trying to gain a promotion or forge a new career path, this course, Python programming for cybersecurity is going to be perfect for you. So I hope you register for the course today, and I hope to see you on the inside chairs. 2. Introduction to python: Welcome officially to the Python basics for cybersecurity professionals. My name is Alex, and I'm going to be your instructor. Now the purpose of this video is to welcome you officially to the course and also give you a few pointers on how best to take it. So first things first, what exactly is this course about? What do I mean by Python for cybersecurity? Well, all the examples, the tests, the challenges, the coding exercises, all of them, well, most of them will be tailored towards relevant cybersecurity projects. So as an example, we will be writing code on how to encrypt passwords, hash passwords, create user accounts, scan ports, things like that. So the kinds of projects or tools or tasks that you would normally perform in a real cybersecurity environment, we're going to make use of them in this course. And that's why it is a Python for cybersecurity course. So if you're a cybersecurity professional and you want to learn a little bit about Python that would allow you to create some relevant cybersecurity projects or tools, this course is going to be perfect for you. I should also mention that if you've never written a line of code in your entire life before, you're coming in here completely blind. Don't worry, I've got you covered. I'm going to start from the very basics, and gradually we're going to work our way up towards writing some more sophisticated programs. So the course is divided roughly into about ten different sections. This might change as I modify the course and maybe add or remove content. It all depends. But we're going to start from the very beginning, okay? What is programming? We're going to write very simple lines of code. And then by the end of the course, we're going to create ourselves a very powerful authentication system and also a very, very powerful password checker. That's going to be the project for this course. And I can guarantee you that if you focus on the course, you do all the exercises, the tests, I can guarantee that by the end of this course, you will have the knowledge required to be able to write some pretty good cybersecurity related programs. Now, I want to mention one thing about programming in general, and this is especially if you've never written a line of code in your entire life before. Going to take time, okay? It's going to take a while before you become truly proficient and fluent in working with a programming language. Now, Python, in particular, it's relatively easy when compared to the other programming languages out there. But even Python takes time, okay? So don't get frustrated if in the beginning, the first few weeks, you're still struggling to write a few lines of code, you're making mistakes. It is only natural. Think of it as you're trying to learn an actual verbal language. Maybe you're trying to learn how to speak, I don't know, Greek or Finnish or Swedish or whatever. You're not just going to pick it up automatically and very quickly, right? It's going to take time. It might take several months, maybe it might even take up to a year. In the beginning, you will be able to say simple things like Good morning. My name is Alex. But then to construct an actual full sentence, it will start taking time, right? You'll have to figure out, Okay, in this language, does the adjective come before the pronoun or does the noun come before the verb, you know, all that kind of stuff. It takes time. So in the very beginning, you will make plenty of mistakes. It could get frustrating, but do not give up, okay? I guarantee you that if you focus, you practice as much as you can. You do the tests and the challenges I've provided for you in this course. I guarantee that one day, you're going to wake up and suddenly realize that hold on a second, I'm actually getting pretty good at programming with Python. So hanging there, it's only part of the process. All the frustration in the very beginning, it's only part of the process. Hanging there, and I promise you you will be rewarded. Now, a few more things to mention real quick, we're going to be working with a particular platform here called replet don't worry, you don't have to pay for it. It's completely free of charge. I will take about weblte a bit later in subsequent videos. But just keep that in mind. If you're working with a Mac or Linux or Windows, you will be able to take this course. And also, if you do run across any challenges or something isn't clear, you can always reach out to me. I'll be more than happy to assist you in any way that I can. So once again, welcome to the Python Basics for cybersecurt professionals. I hope you will find this course very informative and very useful. And it is my sincere hope and believe that by the end of this course, you'll have learned how to program with Python. So thank you once again for enrolling. And now let's begin. 3. Important note about replit: Order for you to be able to write your Python code and then test to see how the code works, you're going to need to make use of a platform. Now, there's a wide variety of them out there. We do have replet and we also have Pi ham. These two, in my humble opinion, are the best. However, for this course, I have decided to go with plt for two main reasons. The first reason is because I personally believe that it's a little bit easier to use. Trust me, it's a very, very simple to use interface. And then, second, even though both are free. Replet is more free than Pi ham. Now, what am I talking about? See, with Pi ham, yes, you can download it. However, you will only be given a 30 day trial. It's only going to last for 30 days, and that's it. So unless you decide to go with the premium version, which is about $100, you will no longer be able to use Pi Cham. Now, with replet, it's not completely free. The way it works is this, okay? When you sign up for the free account, you will be given a certain package of number of ripples and then development time. What exactly am I talking about? See, I'm going to drag. This is my account right here. Don't worry. In the next lesson, I'm going to show you how you can create your account on replate and install replet and so on. But don't worry. I want to draw your attention to this, okay? So this is my replet account right now, and you will see down here, I have the startup plan, which is the free plan, and I have free pols, three out of three, and then development time. These two are the most important. What exactly are these? So, whenever you create a Python program, that counts as one ripple, okay? And you're given basically three opportunities to create three different types of Python programs. In all honesty, for the purposes of this course, you're only going to need one or two at the very most. So three is more than enough. Now, the development time is the one I really want to draw your attention to because right now you can see it says 90% used already, and I have been using this particular account for the past three weeks already. So what exactly is this development time? Whenever you open applet, as it is right now, my plate program is open. It's already counting as a development time. So as long as your plt browser is open, you're writing your code, you're running your program. All of these will count towards the development minutes. Now, how many minutes do you have? You have 1,200 minutes. You can see right here, my development time in minutes, I have used 1080 out of 1,200. So it's 90% used. Now, the good thing about this is that you can manage it properly. So what you want to do is whenever you're not working with plt, make sure that you close the browser, close the window, do not leave it open as it is right now, okay? Do not forget to do this. People very often make their mistake. I've made this mistake so many times already, and it's going to eat into your development time. So once you open up repplt and you're taking the class, you're writing your code, you finish your class for the day, you want to go out, make sure you close your replet window or tab or browser, close it, and then you can open it when you're ready to resume the lessons, okay? One other thing I want to mention to you is that with replet even if you've used up your development time completely at 100%, by the end of the month, it resets every 30 days. So you can come back after 30 days, and then you will have access to your account all over again. Also, obviously, you may not want to wait for 30 whole days. One thing you can simply do is that you can create another account. So hopefully you have more than one email account. So you can create several free accounts with Trapt as long as you have the email accounts to actually use to create those accounts. So it's a very, very flexible and a very easy to use platform, and that's why I have decided to go with plate. So if you have any questions about using Real plate or Pi harm, do let me know. I'm going to be here to assist you and help you in any way that I can. So Jemmy in the next video where I'm going to show you how to install earplt and, of course, create your account. I'll see you then. 4. Introduction to replit: So to get started with Weblet pretty easy, go to replet.com, and of course, you're going to click on Sign Up for free, and all you'll have to do is to provide your email and your password, and then you log in. And when you do that, let me drag my O browser in here. On your very first page, you're likely going to see something like this. You'll have to fill out your first name, your last name and then say it's for personal use. And then you can just simply say none or begina and then you click Continue. So just as an example, very quickly, let me just do this right, Alexander One, personal use, beginner, continue, and you want to go with the free package. Click on Continue with Starter, and you should be good to go. By the way, you will get an email asking you to verify your email address. So go to your email account and make sure that you verify your email address. So now that you have access to plt, by the way, I might call plt apple many times in this course, so please just be aware of that. This is going to be the homepage. And in order to create a apple for Python, you can simplcreate Python Button White here. Now, if you don't see create Python, simply go to Create Rapple all right, and then in here, you will see Python and then simply click on Python and then create the apple, and then you will have access to this. So I'm going to go ahead right now and click on Create Python, and let's just add a title. I'm going to call this one Lesson one, just as an example, okay? And then I'm going to click on Create apple. So right now, this has taken us to the main page where we can begin to add code for our particular program. So this is the main work area right here. And then on the right, you'll see we have access to the AI window, the console, as well as the shell. Now, AI, of course, this is something new that's been added. We will be working mostly with the console where we can run our code and also the shell terminal. And then over here on the left, notice that our file is actually called main dot PY. This is typically the default title that web will give your main program. If you wanted to create a new file, we'll simply come over here, click in there. And for example, I can call this one test dot PY. Okay. And just like that, we have another window for another program. But I'm going to go back to my main dot PY, and then down in here, we have access to different kinds of tools, such as the console, the debugga, the output, and so on. We're going to take a look at a few of them later on in this particular course. But now I want us to write our very first program. So over here in the main window, I'm going to type the words print, which is, of course, a command. And as you might have guessed, print simply means we want Python to display something, okay? So notice also that replet automatically will begin to provide different kinds of ways of using that particular command. So kind of like helpful hints. But I'm just going to add my open closed brackets and then double codes, and then I'm going to say hello world. Okay. And now over here, I'm going to go to the console window, and then I'm going to click on the Run button. And now just like that, we have our very first program printing out the words hello world. All right? So, don't worry. If all of this is a little bit overwhelming, we're going to be working with p throughout this course so you will get accustomed to all the functionality, the windows, and so on. However, there's two main shortcuts I want you to be aware of. If you are on the Mac, it's going to be a command K. If you're in Windows, it's going to be Control K. Now, when you do that, this will bring you to the search bar where you can very quickly type in whatever it is that you want to search or you want to run. And then also, if you wanted to run the command, instead of having to click on the Run button in here, you can simply use Command Enter on your Mac or Control Enter on your Windows, and Rap will automatically run the program for you. Do have additional buttons in here, you can split the plane if you wanted to, if you write in more than one particular program at a time, you can maximize your panel as well if you wanted to, or you can, of course, restore the panel, things like that. Just go ahead and close this tab and go back to the default setting. So that's pretty much it for the introduction to plt. Thank you for watching. I will see you in the next class. 5. Hello World: Well, welcome to Lesson two. And previously, we successfully created a very first program that printed the words Hello world. Now, I wanted to use this opportunity to really break this program down and help you understand what exactly we did because I'm going to assume it's your first ever program, and you're new to the world of programming. So, right here, we initiated the command called print. Now, print is a function. With Python, you have inbuilt functions that will do something for you. Print is one of such functions that will, of course, print out whatever it is that we tell it to print. Now, notice that we do have the parenthesis, okay, opened up. And typically, whenever you're working with functions with Python, you will have the name of the function, and then you're going to have parentheses, which are the brackets. Inside of the parenthesis, you're going to have something that we call a variable, okay? Variables are kind of like inputs or data that we give to the function to work with. Okay? In this case, right now, the variable is the text hello world. Now, we're letting Python know that hello world is text. It says string format. We're going to talk a bit more about data types a bit later. But in order for Python to understand that hello world is actually a piece of text, we wrapped it in double quotation marks, as you can see. That's why Python knows that, Oh, these aren't numbers. This is not some sort of a calculation. These are just letters or words that need to be printed out. That's why we wrap them in quotation max. Now, if I was to come over here right now and typed in something, just, you know, something random, notice it's, of course, outside of the quotation marks, okay? If I was to go ahead right now and run my program, you will see right now that it displays an error because the syntax is incorrect. If you're going to display anything using Python and the print function, it needs to be in quotation marks. So this isn't going to work. Let me go back. Add my double quotation again. Run, and we're back. Okay. What if I wanted to print Hello World, let's say, five times, okay? Now, I could do this, okay? I could simply just copy and then come over here, paste, paste, paste, paste. And then if I was to run you can see right now, it will print out hello world five times. Pretty straightforward. Okay, let me just go ahead and remove the four additional lines. I want to show you a different way, okay? We could do something like this, okay? I am going to create something that I've mentioned before, and that's a very variable. Okay. Variables, again, they're like inputs that we can use to store data. I'm going to call this variable, let's say text. Okay, let's call it text one, okay? That's the name of my variable, and I'm going to say it's equal to now in double quotation marks, hello world. Okay? So what I've done right now is I have simply said, I'm going to add the text hello world to the variable text one. So text one, the variable text one is now holding. It has the value of hello world. Now, check this out, okay? I'm going to go over here right now to my print function. Remove the text, the string. And I'm simply going to say text one. Because, again, text one is equal to Hello world. And now I'm going to run my command, and there it is, we have Hello world. And of course, I could simply copy this once again, and then I could paste, paste, paste, paste, run the command, and take a look at that. All of a sudden, we have an issue. Why? Well, that's because Python reads print text one. And then, again, immediately it sees another print text one, print text one, print text one, that confuses Python. So what you want to do right now is we want to add these in separate lines. It's typically very good programming syntax. If you're going to run more than one function, you want to run them in separate lines. You should never run multiple functions within the same line. So I'm going to go back in here, click on Run, and there it is. Everything is back to normal. So that's just a very small snippet of what we're going to be dealing with. Obviously, we're going to be dealing with far more advanced functions and programs, but, you know, step by step, we're going to get this. So hopefully, you now understand a bit better what variables are functions and how the syntax for Python works. Thank you for watching. I will see you in the next class. 6. Data types section intro: Welcome officially to the very first main section in the actual Python course itself. And in here, we're going to be talking about variables and data types. Listen, I guarantee you that regardless of what programming language you're trying to learn, this typically is going to be the very first section because at the heart of every program, no matter how basic or advanced it is, it's going to revolve around some piece of data, whether it's strings or integers, you're going to have data involved in every program. So in this section, we're going to talk about the different types of data out there. We're also going to talk about the bullying operations. So say, for example, you wanted to say something isn't true or something is false, stuff like that. And then, of course, the arithmetic operators, okay? How do you add, subtract, multiply, vide in Python, basic math, we're going to learn all of that in this section. Then, of course, at the end of the section, there's going to be a coding exercise which I want you to attempt. Give it to all. Don't worry. If you're unable to solve it, I will provide you with the solution as well. Congratulations. We're about to begin our journey into the world of Python. Let's begin. 7. Working with Strings: Well, welcome back. Now, let's spend a bit more time to talk about strings. So far, we've worked with the hello world string, and I did say that whenever you're wrapping your text, you typically want to use double quotation marks. However, that's not always true. The default is to wrap your text with double quotation marks. However, you can actually get away by using single quotation marks. So let me just go over here right now and edit that. I'm using single quotation marks. I press Run, and you can see it works perfectly fine. So you don't always have to use double quotation marks. The natural question you might have here is, Okay, so when do we use single? When do we use double? Does it really matter? In most cases, it really comes down to your personal preference, whether you are a single quotation user or a double quotation user. The default, most programmers tend to go with the double quotations. And there are instances where you could actually even use both of them in the same string. As an example, what if I wanted to say something like he said, hello, right? So let me just go over here right now and say he said Kremer. And I'm going to say hello. How would we be able to print this out successfully? Because if we clicked Run right now, it's going to give us an error message, okay? We need to wrap these texts, these letters in quotation marks. But how do we go about it? Well, we could do this. I could go over here right now, add a single quotation arc. Oh, sorry, it has to be inside. So the single quotation arc first, okay? And now for the actual words that he said hello, I'm going to use the double quotation marks in here to wrap the exact word hello itself. And then whenever actually, this is pretty common in programming languages, okay? Whenever you have an opening quotation mark, you want to have a closing quotation mark as well. So I'm going to add the single quotation mark right there. And now if I click Run, you can see right now it actually works. We could do the exact opposite. I could start off with a double quotation mark, okay? And then I'm going to come over here right now, rap hello in the single quotation marks and then at the closing double quotation marks, run, and you can see it works perfectly fine. However, what if I wanted very long strings, right? So I do have my Ipsum generto in here. So let me just grab this first paragraph over here, okay? Copy, come over here. Now, I could actually wrap everything as big as it is inside of the double quotation max, click Run, and it's going to work perfectly fine. But what if I actually wanted to create some space, and so, for example, I wanted to have some space in here. I come over here right now and add some space in, right? Now it's all kind of disorganized. I click Run, and now we're going to have an arrow message. It's not working anymore because Python cannot understand the space in between the lines of text. So this is where the triple quotation marks will come in. So watch this, okay. I'm going to add one, two, three. Okay. Oops, sorry about that. That is three. Okay. And of course, I'm going to come over here right now and add the closing three quotation marks. And now if I click Run, and there you go, you can see it actually works. So there are certain instances where you may want to use triple quotation marks if you have very specific kind of spacing. It's also typically used for, like, documentation, writing lines of code, things like that. That's where you would use the triple quotation. Now, we could also combine strings. All right. So as an example, let me quickly show you, let me create a new variable here and say X, okay? Equals, and then in double quotation mark, I'm going to say hello. Okay? And then I could create a second variable Y equals world. Okay. And now, if I wanted to combine both of them, I could say print in parenthesis, very simply, X plus Y. Now, if I click Run, there you go. Hello world. But if we wanted to add the space between hello and world, all I can do is I can either add the space at Hello right here. So I've added the space after the O, click on Run. You have Hello World, or I could do the opposite and simply add a space at the beginning of W, click on Run, and there you go, you have the exact same result. Now, let's make this just a little bit more sophisticated. This program just a little bit more sophisticated. I'm going to introduce to you a new function called input. So so far, we've been working with print. Now let me introduce to you input. So input is basically a function that will ask the user to provide something, okay? So I'm going to say input. And then in parenthesis, I'm going to say say something, please. Okay. So if I run oh, sorry about that. If I run the command, you can say right now you say say something, please, and now if I say, Hi, my name is Alex and I press Enter, nothing happens, okay? Because our program, all it does is to ask the user to say something, and that's it. It's not going to do anything with what the user has actually said. So to make this a little bit more advanced, I'm going to go back in here. Okay. Let's remove this let's create a new variable. I'll say user response. This is going to be the name of my variable. And by the way, let me just say something real quick. It's good etiquette, good programming etiquette. Whenever you're creating names for your variables or your functions and you're combining two words together, in this case, right now, I'm combining user and response. The second word, the first letter typically should be in capital letters. That's why the R for response here is in capital. I'm just saying, okay? So just bear that in mind. So use a response equals, okay? And I'm going to say input. And now in brackets, I'm going to say say something Please. Okay? However, I'm going to come down here. I'm going to now use the print function in brackets, I'm going to say user response. That's it. Pretty straightforward. I'm going to go ahead right now, run the command. It says, say something please, so I'm going to say, my name is Alex. Press Enter, and there you go. Now it's saying, my name is Alex. So successfully introduced to you a new function, which is inputs which will ask the user to provide something. We've talked about how to use the single quotation max, the double quotation max, as well as the triple quotation marks. I've also shown you how we can actually combine strings by creating multiple variables and simply using the plus operator to combine them. So gradually, gradually, we're getting better and better at walk in with Python and strings. So thank you for watching the video, and of course, I will see you in the next class. 8. Other types of Variables: Let's now take a look at a different form of data type. So far, we've been working with strings, right? But now let's take a look at floating points and integers, otherwise simply known as numbers. All right. So let's go ahead and remove this program, and let me first of all, create a variable X equals to four. Okay? And then Y equals, let's say, 6.0. Now, the obvious difference between these two is that X doesn't have any decimal point. Why does Y has the 6.0? All right? In fact, let me make this a bit more reasonable. Let's make it 6.3, just as an example, right? So y, in this case, would be a floating point because it does have a decimal point, while four will be the integer. So if I went ahead right now and I printed X plus Y and I run my program. You can see we're going to get the number 10.3. Pretty straightforward, right. However, we could do certain things like say, for example, if you wanted to convert 10.3 being a floating number, if you wanted to convert it to an integer, we could simply use a new function called the integer function. So what I'm going to do right here is inside of this first bracket, I'm going to open up another bracket and add INT. Alright, so INT sorry. Sorry about that. I NT. Okay, so INT here is a function that stands for integer, right? And of course, since we've opened the integer function, we're going to have to close it as well. So let me add the closing bracket. And now, if I was to run my program, you can see it has converted 10.3 to ten because of the function INT integer. This was the first time that we've added a function inside of another function. First, we have the print function, and then inside of the print function, we have the integer function. So this is one thing you should be aware of when it comes to Python programming. You can have a function inside of another function, and in fact, you can have three, four functions instead of one another, just as long as the hierarchy is correct. In this case, right now, I cannot have the integer function come in first and then print. In fact, you know what? Let's try it, okay? Why not? I'm going to go ahead right now. Print, okay? And then INT, okay, what's gonna happen? What's going to happen? I'm gonna click on Run. And there you go. So even though it kind of well, it didn't really work. I mean, we did get the X plus Y. We did get the correct answer to be 10.3. But unfortunately, it didn't convert the results to an actual integer. So Python basically ignored the very first part, which is the integer function and then just went ahead and said, Okay, print X plus Y. And then, of course, he told us that, Hey, there's some sort of an issue with the syntax. So have to be careful whenever you're trying to add functions inside of another function, there needs to be a hierarchy and it needs to be correct. So let's reverse that run again, and there you go. So likewise, if I wanted to convert the results to a floating number, I could do so as well. So for example, right, if I change the Y here to let's say five, right? But instead of nine, which would be the obvious answer here, I wanted it to be 9.0. I will change the integer function in here to float. So this is the opposite, right? So check this out. I'm going to click on Run, and there you go. It is now 9.0. You know, there's a good chance you may not need to use the integer or float function in your programs. It all depends on the kind of programs you will create. But nevertheless, it's important to understand these concepts that you can add. You can convert one data type to another, and that you can also have one function inside of another. So thank you for watching. I'll see you in the next class. 9. Boolean Operators Updated: One more type of data type you should be aware of will be the Bian operations or the Boolean values. Typically, it's going to be two of them. You either have true or false, and it will depend largely on the outcome of certain kinds of operations. Now, as an example, over here, I have the variable X equals five, the variable Y equals ten. Now, I have my very first print function in here that says X is equal to Y. The second is X is less than Y. So with bollans, you either have true or false. Now, what do you think the answer for the very first print would be? Well, the answer obviously would be false because five is not equal to ten, while the second should be true because X is, in fact, less than Y. So now if I go ahead and run this program, now you can see we have false and true. These are all we refer to as the Bollan values or the Bollan operators, and we typically use them whenever we're trying to create loops or conditional statements. Don't worry. We're going to talk about those in the next section. But typically, we may want to write a program that will check to see if something has happened, and then if it has happened, meaning, if it's true, then do something. Or we can also say, if this thing hasn't happened, therefore, if it is false, then do something else. So we're going to be making use of bullan operators and values a bit later, but the thing about Boolean values and operations is that they work with the comparison operators like your equals to less than is equal to, and so on. But we also have what we call the logical operators. Logical operators would be things like N or not. So as an example, let me give you another program in here. Let me first of all, say A equals true. Okay? And then B equals false. By the way, please note that the true and false here are not strings. And they're not strings because you can see that with true, it starts with a capital T. And with the false it starts with the capital F. So Python automatically knows that, Okay, you're dealing with bullions in here. So if I wanted to write a print function in here that says something like, let's say, A and B, let me write another one, print. Let's say A or B, and then last one print I'm going to say let's say nota A. What do you think the answers will be for the three? Typically, whenever you're dealing with the logical operators and you're comparing two or more values, all of them will need to be true whenever you're using the and statement. If one of them is false, then the answer will be false, okay? The opposite is with. So if you're saying true or false, it's going to be true. It's true and false, the answer will be false. And then obviously for the last one in here, not A will be false because not A means the opposite of true opposite of true is false. So if I run the program right now, you will see once again that A and B true and false will always be false, while true or false will be true while not A will be false. So once again, Bollin operators and values, they work with logical operators as well, such as your or not. And, of course, your comparison operators such as your equals to is not equal to less than, greater than and so on. So we're going to be working with boolean operators as we progress in the course. Thank you for watching. I'll see you in the next class. 10. Arithmetic Operators Updated: Let's talk about some more operators and starting off with the arithmetic operations. And this is just, you know, the mathematics or basic mathematics that we learned back when we were kids, right? So you have, you know, one times one, two, times two, three, plus one, ten, minus seven, that kind of thing, right? So what I've done here is I've assigned the value of ten to variable A and the value of three to variable B. And I've written out several arithmetic operations in here. We have A plus B, A minus B, A times B, A divided by B, A, modulus B, which will simply return the remainder when we divide A by B, and then the last one here is going to be a exponential B, which will raise the value of A by the power of B. So what are the results here going to be? Let me run the program, and of course, ten plus three is 13, ten minus three is seven, ten times three is 30, 10/3 will be 3.33 33, and then ten model loss three will be one. And of course, ten times ten times ten will be equal to 8,000, right? Okay, so I expect that you obviously knew the answers to all of these operations. But let's go further. I'm going to remove this, okay? And I do have some more operations in here. And the first one in here is what we call an assignment operator. You can see it right there, it says, plus equals, right? Let me add this new lines, and there you go. Okay, so we have plus equals. What this simply means is that we are going to say X equals X plus five. It's basically the same thing. So X plus equals five is exact same thing that say X equals X plus five. This is just a short form of writing this. So let me just go ahead and remove this. So what do you think the answer here would be? The answer obviously here is going to be 15 because ten plus five equals 15. Now, you can do the opposite, okay? You can say minus and then equals. The answer here obviously would be five because ten minus five will be five. And you have other ones, of course, like the a multiplication, which will give us a 50. And then we also have the division, which will give us two, two point oh. So these are what we call the assignment operators. Now, I want to draw your attention to one more thing, and this is what we call the operator precedence. Let me first of all, remove the code and let me drag this to the top and print on a new line. Let me copy this command and come down here and then print. Alright, so take a look at this. Okay, we have two different expressions, alright? The first one here says, two plus three times four. Well, the second one here is we have a bracket two plus three and then times four. What do you think the results of both expressions are going to be? Again, this might remind you of the math that you did back in your kindergarten or primary school days. But I always find this thing rather fascinating because here, we're going to get two different results. So you can pause the video and try to guess what the results of both expressions would be. If you have your results, let's go ahead right now and run. And the first one here will be 14 while the second will be 20 why? Because on the first expression in here, we have two operators. We have the plus arithmetic operator, and then we have the multiplication. Python will do the multiplication first before the addition. So three times four is going to be 12, 12 plus two, is going to be equal to 14. However, over here in the second expression, it's actually going to do the addition first because they're inside of brackets. That's why. So unless I'm mistaken, typically, it is you will have the parenthesis will be at the very top of the precedents. If you don't have the parenthesis, it will check for exponentiation. If there isn't any exponentiation then it will go to multiplication, it will go to division. I will go to modulos, addition, and then finally subtraction. And then in a very complex expression, if you do have comparison operators, those will come next, and then finally logical operators. But no need to go any further than that. Over here, we simply have two main operators. We have three. We have the parenthesis, which of course will take precedence over the multiplication. So two plus three is five, five multiplied by four equals 20. So once again, this is what we call the operator precedence. Again, to be honest, we're not really going to be working with these in this course, but nevertheless, this is something that you should be aware of. Thank you for watching. I'll see you in the next class. 11. Working with strings section intro: Welcome to the very next section where we're now going to focus specifically on the strings data type. I know previously we talked about strings already. However, we need to delve even deeper into this particular topic and why? Well, that's because the vast majority of time the data type you will be working with are going to be strings. Yes, you will work with integers and other data types, but strings typically will be about 80 to 90% of the types of data you work with. So in this section, we're going to delve a lot deeper. I'm going to show you how you can manipulate strings. So say, for example, you have two pieces of string, want to extract certain letters from the string. You want to extract letters from that other string, join them together, things like that. You will learn how to do such things. You're going to learn how you can convert entire strings to upper case or lower case, how you can slice them and do so many other things. So I believe you're going to enjoy this section. And also, just like with the first section, by the end of this section, there's going to be a coding exercise as well. So please do attempt the coding exercise. Hopefully, you will be able to successfully do it. But if you can't, I'm going to provide for you the solution as well so you can compare your code with my code. So without wasting any more time, let's begin. 12. Basic string manipulation: Already familiar with the different ways of printing out your string variables. What I have on my screen right here, I created the variable user name and assigned the value of cyber as a string to it, and then I simply said print user name. That of course resulted in printing out cyber very, very simple, very basic. And now from line four, I assigned the exact same value cyber to the variable user name. But notice that on line five and six, I demonstrate two different ways of printing out the variable. I could have said, welcome to the world of programming and then comma and then the name of the variable, which is, of course, user name, cyber, and the output is welcome to the world of programming, cyber. And then on line six, instead of using the comma sign, I use the plus symbol, and this basically is what we call concatenation whenever you're using the plus symbol, and then you have the exact same output. However, Notice that there is a slight difference in the spacing. With the comma symbol, you tend to have a bit more spacing between your string and then the actual name of your variable. Nevertheless, both methods work pretty well. It's all a matter of choice. However, let me show you a few other basic ways of manipulating your string. So say, for example, I wanted to print cyber ten times, right? Instead of me creating using name cyber, using name cyber, using name cyber ten times, what I could simply do right here is I could just add the asteric symbol and then just say times ten. And then all I have to do right now is just to simply print the user name. Now if I run the program, you can see we have cyber cyber cyber cyber cyber ten times. And if I want a space in between my words, I can just add a space between the R letter and the closing code. And if I run the program again, then you go, we have cyber cyber cyber. So this is what we call repetition, right? One other function I wanted to let you know regarding strings is the length of a string. We can determine how long a string is. Basically, in other words, we can determine the number of characters in a string. So for example, over here, if I wanted to figure out how many characters were in cyber, all I have to do is to come over here. After the print function, I can introduce another function called the length function, which is, of course, LN, and then I'll have to add my closing, break it by there. And now, if I run the program, can you guess what the value is going to be? The value is going to be five because cyber is made up of five characters, five letters. Now, you might be wondering, Okay, this is all good, but how is this relevant? How do we apply this in the world of cybersecurity specifically? I'm pretty sure you must have seen on websites before where they ask you to login. You begin to type in your password, but then you don't see the actual characters. Instead, you might see the dash symbol or most commonly the asteric symbol in their place. We can use the knowledge we've gained so far to actually create a similar scenario. So check this out, okay? I'm going to create a new variable right now and call it password. And now let's add the actual password. I'm going to say swordfish, which isn't exactly the best password, but never mind. We're going to go with swordfish. So now I want to print out sordfsh, so I can just say print a password, right? So if I run the program, it's going to say it's sordfish, but we want to replace each letter in sordfish with the asterics symbol. So basically, we should have nine asteric symbols in place of the actual letters themselves. So what do we do? I can create another variable right now and call it the masked underscore password. And now this will be equal to what? I'm going to add my codes and now add the asteric symbol. Okay? The asteric symbol, and now multiply it again by the length of the characters of our password string. And there it is. So now, if I run the program, and that did not work. Oh, do forgive me. For the print password, we need to change line three here to mask password because we're no longer printing out the password. Oops. That was a bit embarrassing. Okay, so we're going to print out the masked password now, and if I run the program, there you go. Okay, so now we have nine astatic symbols because setFish itself is made up of nine letters. And just to prove to you that this actually works, if I added three more characters, digits, in this case right now 123, and I run it again, now you can see the number of astatic symbols has increased by three. Instead of nine, it is now 12. So this is how you could recreate something very similar in your real world program. If you wanted to create a masked password system, you can use this kind of concepts. So thank you for watching the video. I will see you in the next class. 13. String Indexing and Slicing: Well, welcome back. Now, let's talk about some very important concepts when it comes to dealing with strings. We do have the concept of indexing. Now, don't worry, we're going to talk about indexing a lot more later on. But now let me give you sort of a brief introduction, right? Say, for example, over here, I do have my text, which is equal to cybersecurity. Now, what if we specifically wanted to, for one reason or the other, extract the very first letter in the word cybersecurity? The first letter here would be C, right? So I could do something like say first and then underscore character equals to text, and now I'm going to use my brackets. And now I need to indicate the index position of the letter I want to target. Now, in cybersecurity, C is the first letter. So what do you think my index number here is going to be? Nope, it's not one, it's going to be zero. Please keep in mind that in programming in general, not just Python, but general programming, your indexing starts from zero and not one. So if I now wanted to print out the contents of my variable called first character, the answer here is going to be C. Likewise, if I change this one to one, now it's going to be Y. So this right here is indexing, and we're going to be working with it throughout this course. Now, the second concept here is going to be the concept of sl sin. Icing allows us to take a portion of the string. Now, cybersecurity is a pretty long string. So what if we only wanted to target, like, the very first five characters, which would be cyber? How would we do this? Well, in here, I can add my index position for the very first character that's going to be zero. And then where do I want to stop? Five. This right here is what we refer to as slicing. The very first number in here represents where we want to start the slicing from. In this case, right now, I've added zero because I wanted to start from the very first letter. And then how many characters do we want? We want the five characters. So now, if I run the program, it's going to be Chi Burr. Now, what if we wanted to extract secure T only, right? We don't want to tag it cyber. We want to tag it security only. One thing I could do here is that I could decide to simply go with the index number of S, which will be zero, one, two, three, four, five. Okay? It could be five. And now I add my colon. I could indicate how many letters I want the slicing to contain. However, if I don't indicate a value in here, Python will automatically default to the end of the string. So now if I run the program, it's going to say security. Did you see how that worked? I indicated, Okay, I want to start from S and the index number of S here is going to be five. And because I did not indicate where I want the slicing to stop, Python will just go until the very end. That's why we have security. Likewise, I could do almost the opposite. I could leave the start empty, and now I could go over here. And if I indicated, let's say, five as an example, what do you think the value here is going to be? When you don't indicate your start position, Python will automatically start. The default position will be at the very beginning. So what do you think the answer here is going to be? The answer here is going to be cyber? Because even though we didn't indicate the beginning, Python will default to the very beginning, which is C. And then five characters is going to be C Y, BR, and that's going to be Psi bur. However, that's not all. We could also use the concept of negative indices. So for example, check this out, okay? I'm going to go over here, right? And if I wanted to target security, one thing I could do is I can start from the end, and I know that security is what it's eight characters, so I'm going to say minus eight. And now I'm going to add my colon and default to the very end. And now if I run the program, there you go. It is security because Python will start from the end, which is Y, and then it's going to go eight letters to the left. It stops at S because S is the eighth letter. It's the eighth character. When you're starting from the end, that's why we have security as the answer. You might be wondering, Okay, how will this be applicable to cybersecurity? Have you ever wondered how antimalware and antiviruses are able to scan and detect malware? Typically, the file extension will have dot EXE. So, for example, right? For example, if a file name equal to CMD IOP, you know, something weird, right? And then dot EXE. This would be an example of the typical kind of malware filename that you will have. So how can we write a program that will scan and then detect as soon as it detects the dot EXE extension, it knows that, okay, this could potentially be a malware. We haven't talked about the If statement yet. We're going to talk about it in the next section, but just assume right now that okay, if the file name Okay. And now, if I added in brackets, if I wanted to target the dot EXE from the end, because we never know how long the file name is going to be before the dot EX right. It's always better to target from the end. I know that minus four, starting from the end, if it's equal, to what dot EXE? If I know that the last four characters in the file name is equal to dot EXE, then I know that most likely this is going to be a Milware. So now I can just simply say prints, and then in brackets, I can say ML Ware found, you know, something like this. So this is how the concept of slicing can be applied in the world of cybersecurity. Now, before I round up this video, let me talk to you about one more function. That is useful when it comes to working with strings. And that's going to be the split function. It is a function that will break a string into a list of soft strings based on a specified delimiter. What am I talking about? Say, for example, I go back to my text, and it equals to cyber security. Now you can say it's two words, right? It's no longer just one single word cybersecurity. Now I've got two words cybersecurity. So if I wanted to split this, I can say words. Alright? Equals text, and now dot split. Okay? I'm attaching the split function to the variable text because I want to split cybersecurity. So now, if I simply printed the words, what do you think the output is going to be? It is going to be cyber and then security. And if you're wondering, Okay, how are we going to apply this in cybersecurity? Jerman Vix Video web will begin to take a look at cybersecurity applications of slicing, splitting, and so much more. 14. Common string methods: Welcome back. So let's continue talking about the different types of methods or functions that we typically apply on strings and one very common one. In fact, two very common ones rather would be the functions for converting text either to uppercase or lowercase. So in my example over here, I have my variable text equal to cybersecurity. If I wanted to convert everything to uppercase, I would simply say text and not upper open up new brackets, and there it is. And if I wanted to convert everything to lowercase, I'll do the exact opposite. This will be print text dot LWA. Dot ova, open up new brackets, and there it is. So if I run the program right now, you can see we have cybersecurity in capital letters, and then cybersecurity in lowercase letter. So these are two new functions you should be aware of dot opa dot LA. But there is also the function for trimming white space. So say, for example, I have my password variable in here. I've got space, and my actual password is swordfish, and then let me add some more space. If I wanted to trim all the unnecessary white space in here, all I would do is I would say prints, brackets and then password, and now strip. Once again, the strip function, we can also use it to remove any unnecessary white space, run the program, and there you go. Swordfish without the empty space. But we can also replace characters in our strings. Say, for example, let me just give you an example in here. Let's say I've got my message being equal to, and let's say, okay, use a password. Let's say the password right now is password one, two, three. Just as an example, right? But then we wanted to print something that will say, okay, use a password and then redacted. Okay? We don't want to display the actual password itself. It's similar to what we did in the previous lesson where we used the Asteris symbol to replace the characters. It's related but slightly different. So let me show what I'm talking about, okay? So I'm going to come over here right now. And I'm going to say, let's create a new variable. Let's say secure underscore message, okay, will be equal to now message, dot, and now a new function called replace. Okay? I want to replace the actual password with something else. So inside of my brackets, I am now going to put the actual password I want to replace which is password one, two, three, comma, and now the actual letters or characters or string, I want to replace it with. In this case, right now, I'm going to open up my brackets, and in capital letters, I can say redacted Just as an example, o? So now, all I need to do is to simply print in brackets secure underscore message. And now if I run a program, there you go, use a password reduct head. So you can use the replace function to simply replace any particular part of your string with something else, and this is how you will do it. To round up, let me show you one more thing, and this is finding substrings. Say, for example, we have a variable called email, and it's equal to, let's say, admin at labsyb.com. I'm pretty sure, of course, you must have encountered this so many times before where maybe you're trying to create a new account on a website, and they will ask you to provide an email address. If you typed in something bogus, for example, the system will be able to detect that, Hey, this isn't a valid email address, and that's because it could check to see if the at symbol was included in your input. So if the ad symbol isn't there, then obviously it knows, Okay, whatever this person typed in cannot be an email address. So let me show you how we can write a very simple program to check to see if the at symbol is in the string. So from here, I have my email equals admin at labsar.com. Let's check to see if the ad symbol is in it, okay? I'm going to say I. Okay? If I know we haven't talked about this particular if statement, yet, don't worry, we'll cover it in a lot more detail in the next section. But for now, just note that if is a statement that Python uses to check if something is true. So I'm going to say if and now email and then dot, fine. This is the function that we can use to check to see if something exists in our string. So that's the find function. And now we have to specify what exactly are we looking for in brackets and now codes, the At symbol. So we're trying to check to see if the at symbol exists, and one way how we can verify that indeed it does exist. It is in a string is by saying it is not equal to minus one. And then we add our colon. So this not equals to minus one is a way of saying, Okay, something does exist. Okay? So right here, we're saying if in the email you found the ad symbol, and it's not equal to minus one. So this confirms that, yes, indeed, the ad symbol is in our string. So now we can do something. We can say print, and now in brackets, we can say valid email format. So now if I run my program, there you go. Valid email format because the ad symbol was in fact found. If I remove the ad symbol, and now I run the program again, you can see nothing is printed out because the ad symbol is no longer there. It is not a valid email for Matt. I go back. I add the ad symbol again, I run, and now you can see it's working again. So, to give you a quick recap, first of all, we want to look for the ad symbol in an email address provided to us. So we're going to say if and then we're looking specifically for the add symbol. So if the ad symbol is present and we check to see if it's present with the not equals to minus one, so if the add symbol is in fact present, simply print valid email format. So take note of the find function. So that's it for the video, thank you for watching. I'll see you in the next class. 15. More string manipulation: Well, come back, so let me walk you through a few more operations by making use of the split function, as well as some good old indexing. So I have on my screen two notes. I have an email address and then also AURL. Now, looking at the email address, what if we wanted to extract the domain specifically? So we wanted to extract labsyba.com and just get rid of the Admin ad. How are we going to do this? Well, first of all, let's assign a variable email to B admin at labsyba.com. Okay. So what I'll do right now is I'm going to create another variable that will now hold what we're looking for. So I'm going to call that one domain. And now this is where the magic happens, okay? We want to split our string, we want to separate admin from labsyb.com, right? So in between admin and labsyb.com is what is the At symbol? So I'm going to say email and now split, and now in brackets, where are we going to split from? We're going to split from the At symbol, okay? And now because we're looking for the second element, when you split admin at labsab.com from the ad, one part will be admin, the other part will be labsab.com, right? So we want to go with the second element, and that's going to be one. One will stand for labsbdt com. And now all we have to do is simply print a domain. And if I run a program, then it is labsbadt com. If I changed one to zero instead, this will now represent the first element, which is going to be admin. So if I run a program again right now you can see it has changed to admin. So whenever you're trying to split a string into two, using the split function, first of all, you want to indicate, Okay, where do you want to begin the split from. In this case, right now, we set from the ad symbol. So right down at the middle, on the left will be the index value of zero. On the right will be the index value of one. And since we are looking for the domain specifically, we change zero to one. We run again, and there you go we have labcyba.com. Now, what of the second example in here the URL? What exactly are we looking for here? Let's say we wanted to extract the actual file itself. So we're not interested in the whole gctpslabs.com, furslashPAT, furslashT. All we're looking for is simply the file name. How can we extract file from this? So what I'm going to do very simply is to say, URL equals, and let me just grab this. Let me just copy that. And I'm going to paste it. And there you go. Okay, so Again, I'm going to create a new variable that will now hold the value of what we're looking for. Let's call this one the file underscore name will now be equal to URL, right? And now dot split, and now check this out. Looking at the URL, okay? This full URL. What do you think What symbol do you think we could use to split the string into multiple parts? Yep, it should be obvious it's going to be the forward slash, right? Because you can see right there it's everywhere. So we're going to split our string. We're going to spit our URL using the forward slash. And now how can we target the file? Do you remember negative indexing? Well, we start from the end of our string. So in this case, right now, what are we looking for, we're looking for the very first element from the end, and now that will be negative what? Negative one. So it's going to start from file. It's going to start from right to the left, right? So now, all we have to do is to simply print the file underscore name. And they should do the trick. Let's go ahead and run the program. And there you go. We have the file name. So by making use of the split function and also a negative indexing, we were able to specifically target the file name. Let me give you one more example. And here, we're going to extract some strings. Without using the split function, we're just going to use the index value. So let me provide the email address again, and I'm going to say admin at labsba.com. Okay, so what if we wanted to extract both the username and the domain? So we want to extract the user name which is admin, and then the domain which is labsyb.com. How do you think we can do this? Well, first of all, I can say my username is going to be equal to email and now remember how the indexes work when you have your start and then the end. So we want to start from the very beginning of our string. So we're going to say colon. We're going to leave the left side blank because we're going to start from the very beginning. And now let us see. A is going to be index number zero, right? So A is zero, D is one, M is two I is three, N is four, and of course, the add symbol is five. So I'm going to say over here right now, five, okay? And then I can say domain equals email and now inside, we want to start from what the sixth index, which is going to be L, colon and then leave that blank because we're going all the way to the end of the string, and now this should work. So all we got to do is to say print. Now in here, let's say, use a name. Okay, and then, use a name, and then print brackets colon domain. Okay. Com domain. And there you go. Let's run the program, and there it is. Username is Admin. Domain is lobsaba.com. Ya. But hold up just a minute. Before you pop the champagne, let's run another test, okay? I'm gonna change Admin here to I don't know. Let's say, Alex, for example. Never run the program. Uh oh, ta ta, da ta, da, da, da, da, da. We do have a problem. You can see right now that the program has malfunctioned. It's no longer working properly. Instead of using the name Alex, we have Alex at. And instead of labsyb.com, we have absyba.com. What is going on here. See, by trying to extract our strings by being very specific with the index values that we want to target, the program is going to rely very heavily on the actual email address, having the right number of characters both before and after the at symbol. So it's heavily dependent on that, but we don't want that we want the program to be flexible enough to always be able to accurately extract both the user name and domain regardless of how short or long the actual email address is. So what can we do? Well, Remember that the ad symbol is where all the action is. To the left of the add symbol is our username to its right is the domain. So we can target specifically the index value of the At symbol. And how do we do that? For the username, email, right now, Again, we're going to leave the first part of the column blank because we're starting from the very beginning, right? And now to target the actual at symbol, it's index value. I'm going to say email again and now dot index, and now in brackets code the at symbol. So now I'm saying the username always begin always start from the very beginning of the string and then end once you have reached the index value of the at symbol. Likewise, we can do something very similar with the domain. We can say domain. Okay, let's start from the beginning in here, email dot index. And now, what are we targeting again? We targeting the ad symbol. However, we specifically want to target the very first character, the very first index after the add symbol. So what do you think we're going to do? We the good people, we're going to say plus one. Plus one. So now I'm saying, Okay, start from the index value of At, but then add one to it. So it's going to go to AD and then add one. Okay, now, labs.com. So now if I run the program, there you go. It works axlabs.com. If I change Alex back to Admin, and I run the program, you can see it works in fact, let's try something completely different, okay? Let's say Jack Daniels at superyahoo.com, something weird, right? Let's run the program, and there you go. Jack Daniels and then domainsoperyahoo.com. So this is how to successfully and efficiently extract your strings without making use of the split function be simply used the index values, and we tag on it specifically the index value of the add symbol because remember, again, the add symbol is where all the action is. To the left of the ad symbol is a username to its right, is the domain. So I hope you enjoyed the video. Thank you for watching. I will see you in the next class. 16. Control flow and loops section intro: Officially to the third section, and now the phone really begins. Now, we're starting to take a sort of a deeper step or a bigger step into the world of Python programming because here we're going to be talking about control flow and loops. Now, so far in the previous two sections, we've talked about strings and integers, and the programs have been pretty for the most part, basic, right? You can either maybe have an input or we provide ourselves with some string and integers, and then we say, Okay, I'm going to do this and do this and do that and then simply print, right? But now with control flow and loops, what if we wanted to say, Okay, if something is this, then let us do that. But if something isn't this thing, let us do something else. This is control flow and loops. Also, what if, for example, we had a list? Okay? It could be a list of names, a list of books, a list of passwords, right? And then we wanted to, like, go through each password in that list and do something, right? Maybe we wanted to take each password and encrypt them, or maybe we want to take each password and then check to see if the password is actually strong or weak depending on several kinds of conditions, we're going to cover that in this section. This is going to be one of the major sections in this particular course, and I'm really excited to teach you about control flow and loops. Let's begin. 17. Intro to algorithms: Going to take a break from Raplt and talk a little bit about algorithms because believe it or not, we're actually going to start working with slightly more advanced concepts in Python programming. And it's very, very important for you to understand what algorithms are because they are vital, not just for Python, but any kind of programming language for that matter. Now, if I was to give you kind of like the standard definition of what an algorithm is, it's basically just like a set of instructions that need to be followed in order to arrive at a particular result, okay? In this case, right now, you can see this text. I have written some stuff there. The very first statement says, if the computer is running fast, Finish. Now, what do I mean here? I'm simply saying that, look, if our computer is performing very, very well, it's running very, very quickly. There is no need to check for anything. It's fine, right? That's why I said finish. But I notice on the third line, I said, else, you could also say something like butt, right? But in programming language, we don't use but, we use se. Alright? So se basically is signifying that if the previous statement is not correct, so if the computer isn't running fast, Okay, then what do we now need to do? We need to first of all, check if there is a virus. And then after that, we might need to uninstall unnecessary programs, and then we might eventually need to increase the ramp. So if we follow these three steps, a computer will be guaranteed to run faster. This is an algorithm, right? And two keywords here are the IF and s. We're going to be working with this quite a lot in this course. And if you are ever going to work with a different kind of programming language, you will always come across IL statements. These are fundamental building blocks of any program. I hope you understand what we did here. Pretty straightforward. I'm going to make this just a little bit more advanced, okay? Just a little bit, right? Let me open up the second tab. So imagine you're getting dressed based on the weather. Okay? So the very first statement, we've established a scenario, right? So our program is checking to see what we should wear based on the weather. So we have the very first result. If the weather is sunny, Okay, well, we can wear our sunglasses. However, notice the third line, it goes else if it's no longer just else. Why? Well, because the weather can come in various forms. It could be sunny, it could be rainy, it could be snowing, it could be cloudy. There's a lot more options, right? In the very first document here, there are only two conditions. The first condition is, Okay, is the computer running fast or is it not running fast? That's it. Nothing like, Oh, is the computer running at 25% capacity? Is it running at 50% capacity? We don't have any other conditions. It's just two. Either the compter is running fast or it isn't either A or B. That's why I would work perfectly here. However, in this example, we have more than two options because, again, we're dealing with the weather. So, if it's sunny, we can wear sunglasses. Else, if it's raining. Okay, we can take an umbrella. And then else if it's snowing, we can put on a heavy coat, right? But now look at the very last line. It simply says else. Not else if else. Because basically we are concluding the program here. We're saying, Okay, we've checked if it's sunny, we've checked if it's raining, we've checked if it's snowing. So if it's none of these three is going to be just a normal day. So else, just dress normally. Okay? So, the very first one here again is simply ELs because there are two conditions. The second one here, we do have the s if statement because there's more than two conditions. In fact, there are four different results based on the weather. That's why we can no longer just work with the I Ls. We have to introduce the s if statement. So hopefully, you understand why we used the Is statement in here and why we used else if in here. If you don't, maybe hopefully once we begin to create actual programs, using the algorithms, you'll understand it much better then. But this has been an introduction to the world of algorithms as well as the Is else if statements. Thank you for watching. I will see you in the next class. 18. If else statements: So in the previous video, we briefly discussed the concepts of algorithms as well as the Is if statements. Now, we're going to put it into action. We're actually going to write a program that would request a number 1-9 from the user. Now, if the number is greater than four, we are going to say, why so much? Else. Meaning that if it is four or less, we're going to say why so little. Now, the very last line, I have blurred it for a particular reason because once we write this first program, we're going to add an extra condition. I don't want to show you what that extra condition is just yet. So let's focus on writing this program, right? So let me just drag this away. Alright. And let me just remove this. Okay. So the first part of the program is we want to request a number between 129. So what can we do here? Well, first of all, we can create a variable called N. All right, for the number. And we're going to pass. Remember, we worked with the input function. Okay? So I'm going to say input because that's the function that I would request input from the user. So I'm going to say input, and I'm going to say enter a number between 129. Okay? Let's just first of all, make sure that this is working. Okay, enter a number 1-9, let me just create some space 1-9. Run again. Okay. Alright. So the first line works. Now, we want to check if the number is between or is less than four, right? So I'm going to say if All right. And then the integer because we want to convert whatever number we provided, we want to convert it to an integer because the user could be very naughty. They could decide to provide, say, 1.1, 4.5, 7.6, something like that. So we want to keep everything as an integer. So I'm going to wrap our variable right now in the integer function, right? So I'm going to say I integer N, now space and then the symbol for greater than greater than four, Okay. And now, pay very close attention. I'm going to add the double colon, okay? You need to add this whenever you're working with your ELs statements, okay? So I'm going to say that. And now, by default, actually, Python has written out this line for me, but I'm going to say why so much. Okay? Now, enter, and now I'm going to say else if, however, Okay. And now I'm going to say se. And then new line indent, print. And then I'm going to say, y so little. Okay. So I'm going to go ahead right now. Oh, by the way, make sure in your s you also have the double colon as well, okay? Let's go ahead right now and run the program, and let's see. So enter number 1-9, I'm going to say six. Press Enter. You can see right there, it actually works. Why so much. Let's run the program again. I'm going to say three, three right there, and then says why so little. So it actually works as you can see. Now, I'm going to go over this once again, okay? So first of all, half N, which is going to be the variable that will accept the input from the user, and the input into question, enter a number between 129. Okay? So now we're going to create a condition that will check to see the value of that number. So we have decided to convert the input to an integer. That's why we have the INT. A question you might have here is, wait a minute, Alex. Previously, when we had a function inside of another function, we have to use the brackets, right? The Is, this is not exactly a function, okay? It's more of an algorithm, more of kind of like a loop just checking to see the value of something. It's not an actual function itself. That's why we don't have the integer wrapped in brackets, okay? So it's going to check if the integer, if it's greater than four, well, print, why so much? Se, meaning that if the number is four or less than four print, why so little. You can say it works right. And the only thing you need to pay attention to would be the double colon after the four and also after the s. And then it's also very good practice that whenever you're walking with IL statements or s if you have good indentation, I didn't do this, okay? I didn't have my print statements directly underneath the E and L statements. This is not good at all. This is very, very poor programming syntax. So you always want to have your indentation correctly, okay? Have that whenever you're working with the EL statements. Okay. Now I'm going to bring back our program. And look at that. I've added an extra condition saying that what if the number is a specific number, which is five. Well, we want to say Bingo. Okay? So how exactly are we going to do this? Okay? Let me drag this away. Okay. Now, check this out. I'm going to come over here where it says, print why so much, and I'm going to press Enter because we want to add the s I in between the opening If statement and the closing s statement, okay? So now I'm going to say L, this is the short form of writing s if. So instead of saying sf, we use LF in Python. Okay? So L if the integer N, is equal or I'm sorry, the integer N. If it's equal to five, the equal is going to be double equal sine, not just one, okay? So else if the integer provided by the user is five, and now I'm going to add the double colon and then I'm going to say, sorry, I'm going to say print. And then in brackets, Bingo. Okay? So I'm going to go at it right now. We're on the program. And now let's check to see if this actually works. First of all, I'm going to type in three, okay? Okay, so it's still working. Why so little? Okay. Let's try eight. Why so much. Okay. And now, if I say five and I press Enter, guess what? It's not saying bingo, it's still saying why so much. You want to know why? Because the Python program executes line by line. Now, look at the hierarchy in here. The very first condition that Python is checking for is to see if the number is greater than four. And because five is greater than four, it immediately says, Okay, why so much? It doesn't even go further down to check if the number is equal to five because the very first statement is basically saying, Hey, if the number is greater than four, then print why so much. So to prove this to you, what I'm going to do here is this, okay? I'm going to say that let's actually change the hierarchy in here. So we will check, first of all, if the integer is actually equal to five. Okay, let that be the very first thing, it checks. And now over here, we will check to see if the integer is greater than four. So I've basically just changed the hierarchy. So the very first thing is going to check right now is if the integer is actually equal to five, it's going to say bingo, else if it's not equal to five and it's greater than four, we're going to say y so much. And then else if it's not equal to five, and if it's not greater than four, then print y so little. Okay? Let's run the program. So I'm going to come over here right now, run let me first of all, add five now and see, Enter, and there you go. Bingo. I actually worked. Let's run the program again. What if I typed in two? Enter, why so little? One, once again, let's go with seven. Enter, why so much? You can see right now, it works. So, the hierarchy of your commands is extremely important whenever you're working with Python or Pgamming in general. When you're checking for conditions, always keep in mind that on Python has gotten a condition that's satisfied, it will immediately print out the results. It's not going to go any further down the remaining lines in your program. So the hierarchy of your conditions, the conditions that are check, your commands is so, so important. And then, of course, also this little you know, knowing where to add the the colons, things like that. This is something you will eventually get used to. It might be a bit overwhelming in the very beginning, but as you begin to write more programs, you will eventually grow accustomed to them. So that's it for working with the E L L if statements. Thank you for watching. I will see you in the next class. 19. Simple password checker: All right let's write our very first piece of code related to cybersecurity and this one will check to see if a user ended password is actually valid or not. You're more than welcome to try writing the code on your own. If not, let me show you how you will do this. The first thing we need to do is we need to indicate what the password is. I'm going to call the variable in here is stored on the score password. Let's provide a value for the password. I'm going to go with something very, very generic. Let's say, password one, two, three. This is going to be the actual password. By the way, I'm not sure if I've mentioned this before, but in Python, if you wanted to add a comment or a hint in your code, you can do so by using the hashtag symbol and then typing your comment. Maybe I've mentioned this before. If not, well, there you go. Okay, so start password here is going to be password one, two, three. So now we need to ask the user to provide the password. And then once they provide the password, we can then run an EL statement that will check to see if the password they've typed in is actually correct or not, if it's correct, we can say access granted. If not, we'll say access denied. So the first thing I'm going to do right now is I'm going to get the user input. Let me just add my comment and say get user input as the first step. What we'll do right now is we can call the user input. We can call it the Entered password. This will be the variable that will store the input. So right there, I like the suggestion that Python has given us. It simply says entered password equals imputes and then we're going to ask the user to provide their password. The next thing we need to do right now is to see if whether or not the passwords actually match. We want to make sure that whatever they type in here is going to match the password that we have stored. By the way, it's amazing how I don't know if you see the exact same thing on your own screen, but Python is literally just writing the entire code for me. I promise you, this is the very first time I'm actually writing this code on replet. I promise you I haven't written this code before. It's amazing how it's so smart, it's so intelligent. It knows exactly what I want to type, and that's exactly what I want to do. I want to say, if the ended password is equal to the stored password. What are we going to do? We are going to print. Then in brackets, string access granted, sorry about that. S, what are we going to print? We're going to print Brackets, coats. Access denied. And there it is. Let me remove the extra coat right there. There you go. That is our code right there. Very simple, very straightforward. Let us try it. I'm going to run and now I'm going to ask for the password. Let's try James 123. Did that work? No, access denied. Well, it did not work. Let's run. The program one more time. Let's try password. Password, one, two, three, press Enter, and then you go, access has been granted, our code actually works properly. Of course, this is just a very simple block of code. It's not the entire program itself, but this is the very first step towards creating a more comprehensive password checker to. Thank you for watching the video. I will see you in the next class. 20. Introduction to loops: Let's now talk about loops, which are easily one of the most important aspects of any kind of program or programming language. But what exactly are they? Well, they are used typically to repeat a block of code multiple times until a specified condition is met or for a set number of iterations. In other words, a loop will run until all items in a sequence or list have been processed, or the loop will continue to run while a condition continues to be true or is satisfied. So basically, it's a loop. I will keep going on and on and on and on until something eventually happens. Now, they are very important because they help us to automate repetitive tasks. So instead of us having to repeat the same task over and over again, we can just write a loop that will run that task for us automatically, and of course, we use loops for building efficient programs that will adapt based on dynamic conditions. And when it comes to loops, there are two main types. The first one is going to be the four loop. Look at this, okay? We have a list of 129, okay? Numbers. And we can write a very simple program that will say, Hey, print each number out. So basically print one, two, three, four, five, six, seven, eight, nine. But instead of us having to write each individual line for each individual number, a, you know, print one, second line, print two, third line, print three, we can just write a loop that will go through all the numbers in this list and then print out those numbers until, of course, the last number has been printed, which is nine. This will be the definition or an example rather of a four loop. The second one is the W loop. So say, for example, the time is between 12:01 P.M. And 4:59 P.M. So as long as the time of the day is between this range, say, good afternoon. Notice the difference between the two structures. Okay? The first one here, which is the fol loop already has, like, a predefined list or sequence. And as long as we have items in that particular list, then do something. The wil loop is a little bit different because it says, Okay, as long as this condition continues to be satisfied or this condition continues to be true, what is the condition here? As long as the time is between 12 1:00 P.M. And 4:59 P.M. S good afternoon. Alright. In closing, loops must have an endpoint or will run for ever. Basically, a loop can crush the entire program. Let's look at the example in here. What if the numbers in this list was number one to infinity, right? That means the program will continue to print the numbers over and over and over again, and it will never stop. This will easily crash the program. And what if in this example right now, there is no specified time period. What if the program just checks to see if it's during the day or anytime during the night, say good afternoon. Well, it's always going to simply print out good afternoon, good afternoon, good afternoon, good afternoon. And it will eventually crash the program, as well. So it is very, very important that whenever we're working with loops, there must always be a certain kind of condition that will eventually be satisfied for that loop to end. Otherwise, it will crash the program. Thank you for watching the video, I will see you in the next class. 21. For and while loops: Now go back to our Trustee plt. And I'm going to give you two examples. One for the four loop and another for the Wil loop. So in order to create our four loop, let's first of all, create the list. In this case, right now, I'm going to provide the name of the list as names, okay? Equals, and then I'm going to have my brackets in here. And let's add some names. So I've got Alex. I've got Nancy. And I've got James. And let's say, last but not least, I have got Violetta. Honestly, I just came up with that name. Violetta. Okay. So we have a list right here of four different names, right? Alex, Nancy, James, and then Violeta. One thing I can now do right now is we can create a variable that would represent each item in the list. Kind of like how we've been assigning values to variables like, you know, X equals four, Y equals seven. I can now come in here and then simply say four. Okay? And now I will say four X. In names. Okay? What is those right here, let me just add the colon right there. So what we've done with this statement here is that we're saying, Okay, we are going to use X to represent each name in this list. So X will be equal to Alex that will be the first loop that will run. The second loop X will now be equal to Nancy, it will run. The third loop will now be X equals to James. And then finally, the very last loop will be X is equal to Violet. That's basically what this does in here. So we're saying for X in names. And now we have to tell the program, Okay, what should we do? Well, we should print out the name X. Okay. I am going to run, and there you go. Alex, Nancy James and Violetta. But what if I wanted to spice this program up just a little bit and let's say something like, you know, good morning, and then the name of that person. So we will have good morning and Alex. Good morning, Nancy. Good morning James. And, of course, good morning Violetta. How are we going to do this? Well, inside of the print function, I am going to add a string that will first of all, say good morning. I Okay. And then we're going to have the comma, which, by the way, is just part of the actual printout itself. It's not part of the actual program, if you know what I mean. It's part of the string. Basically what I'm trying to say. We can do so without the coma as well, in fact, you know what? Let me remove the comma, just so I don't confuse you. So I'm going to say good morning, okay? And then I am going to add a space because we want it to be space between the actual string, good morning, and then the name of the person. That's why I've added the space right there, okay? And then right here, I am going to add the plus sign and then X. So basically attaching the strain good morning to the name of the person. Let's run the program, and there you go. Good morning, Alex. Good morning, Ansy Good morning, James. Good morning Violetta. Now, if I remove this space because I want you to understand this very well. If I remove this space right now and I run the program again, you can see right now that it's all drumbled up, okay? There's no space between morning and then the name of the person. That's why, once again, I added the space right here between the G and closing quotation mark, okay? So, I mean, one more time. And there you go. This is an example of a very simple four loop. First of all, we created a list of names. We assigned this entire list right here to the variable names. And then we now need to create another variable that will represent each item, each name in this list. So we set four X in the list called names and now the function print Good morning, plus the name of the person in that list. This is an example of your four Loop. Now, what I'm going to do is we're going to create a wild loop. Now, to do this, let's come down here. And yes, we can run multiple programs and functions within the exact same file, right? So what I'm going to do right now is I'm going to create something called a counter. Okay, so counter equals zero. Now, what exactly am I doing here? What is this counter? Remember that with Wil loops, they will continue to run as long as a condition continues to be satisfied. That's the difference between a fall loop and a while loop. With the fall loop, we have a list of items, right? So the loop will simply run through all the items in that list. But a while loop, there has to be some sort of a condition that will be presented. And then we're going to tell Python that, Hey, as long as this particular condition continues to be satisfied, execute the loop. So the condition here right now is that while our counter is less than let's say three, do you now see we're saying, Hey, as long as our counter, as long as the number in here is less than three, continue to do something. And you can see Python kind of already giving us a hint of where to go next. Well, I'm going to say print print, and then I'm going to add a text right there, the string. Hello. Okay? And there you go. Now, what we're saying here is start from zero, okay? Start from zero and then check to see if zero is less than three. If it's less than three, then print Hello. However, we're not actually done. Why? Well, because we haven't specifically told Python to add a new number to the counter. So instead of zero, it will now become one. Check to see if one is less than three and then print out hello. We haven't told Python that yet because a loop can go either way. We can either continue to add or one, two, three to zero, or we could go the opposite direction, and then instead of zero going to one, it could be zero going to minus one, minus two, minus three, minus four. And in that kind of scenario, those numbers will always be less than three, and the condition will never be satisfied or it will always be satisfied, rather, and then that will crash the program. So we need to tell Python explicitly that, Hey, once you've run the very first loop where zero is less than three, add one to the counter. So I'm going to come in right now and I'm going to say counter and then plus equals one. This is our way of saying, Okay, Python, the very beginning of the loop will be zero. Check to see if zero is less than three. If it's less than three, print hello and then now add one to the counter. So it will now be one at less than three. Python says, yes, one is indeed less than three. It's going to put down hello. It's going to add one again, so one will now become two. Python is going to check, Okay, it's two less than three, yes, it's less than three, print hello. And then it's going to add one again. So now becomes three. And then finally, Python will say that, Okay, three isn't less than three because three equals three, and then it will end the loop. Right. Let's go ahead right now and run. And there you go. Hello, hello, hello. You can see right there that it stopped at three loops because the very first loop will be for zero. Second loop will be for one. Third loop will be for two. If I came here and I said, Okay, less than seven run, now you can see we have, again, 40, one, two, three, four, five, six, hellos. So that's the difference between the four loop and the wild loop. You can see right now with the four loop, we have our list or our way and then the loop will go through each particular item in that array and it will do something. But for the Wile loop, we need to first of all, specify, Okay, what's the beginning point? And then we'll now provide the condition, Okay, as long as this equals this or this is less than this, or this is this, do something and then either add one or remove one or just do something to make sure that the loop will now actually begin to run. That's the difference between the four loop and the while loop. Of course, we're going to be working a lot more with four and wile loops in this course. But thank you for watching. I will see you in the next class. 22. The BREAK statement: Let's now talk about the break statement because it's a very, very powerful statement, and we're going to be working with it in this course. But before I do that, I just want to show you a different way of actually printing out the names in this particular example. Right now, we're saying good morning and then plus and then X. X, of course, being the variable representing each name in our list. Another way of writing this would be to do something like this. First of all, I'm going to remove the plus X. Okay. But now I'm going to come right here at the very beginning. I'm going to type in F, Okay. And now, right here, I'm going to add my curly braces, not the usual braces, but the curly ones. And now here, I'll simply type in X. Okay? I can run the program, and you can see right now we have the exact same results. This right here is a different and also a more efficient way of actually printing out the values of the variables in a list or an array in a four or while look. Right? And we're going to be walking like this moving forward. Now, the break statement, what exactly is the break statement? It's used when we want to terminate a loop early. For example, in this flop that we're seeing right now, what it does is that it is simply going to go through all the names in our list and then print out good morning and then the name of the person, right? But what if for one reason or the other, we wanted to terminate the loop as soon as James has been reached, we don't want to print out good morning Violeta because we don't like violeta, right? This is where we could use the break statement to terminate the loop early. To demonstrate this, I'm going to write a completely different program, okay? So let's do this right? I'm going to type in let's say numbers. I'm going to create a new list right here numbers equals, and I'm going to put in my brackets in here. And let's go with a series of numbers. I'm going to go with one, three. Let's go with seven, eight, ten, and let's go with 11, and let's add one more, even number 14. Okay? So we have a list of numbers in here, and what we want to do right now is we want to create a loop that will run through all the numbers. However, once the first even number has been reached, we want to terminate the loop, meaning that once the number which is eight, we're going to end the loop, so it should run four times. The first loop for 1 second p for three, third, look for seven, fourth loop for eight. So think about this, right? We're going to create the fall loop first, and then we're going to introduce the I statement. So the I sell me will check, Okay, is this number actually even or is it odd? If it's even, then break the loop because we found the first even number else. If it's not an even number, if it's an odd number, continue with the loop. So, how are we going to do this? Gatap in enter the fall loop four let's say Num. Okay, Num will represent the numbers. And by the way, I'm not sure if I've mentioned this before, but I think it's time I do so. See, I've only typed in for number. You can see right now on my screen, Python is already suggesting in numbers because Python is actually quite smart, right? So in this kind of situation right now, if you're happy with the suggestion that the program has given you, you can simply press the tab key on your keyboard, and you will get the exact same result, right? So I've do forgive that I can't talk. I've taped the tab number. Or the tap key on my keyboard, and now I have four num in numbers. I'm going to add my colon. Okay? Now, I'm going to add the If statement, and would you look at that? I promise you, I've actually not typed this program before, but I think Python is so intelligent. I already has an idea of what I might be looking for. Yes, I want if num, and then percentage two equals zero. So what exactly is this? What is going on here? What this means is that we're going to check if the number that's been pulled out, if it's divisible by two and the remainder is equal to zero, then we know for sure that it is an even number. If you divide seven by two, you will get 3.5, 3.5. There's always going to be that remainder remain have been half. But when you divide eight by two, the answer is straight up four. It's not 4.1, it's not 4.2. It's not 4.5, it is four. So this is how we can check if the number is actually even. So if the number is, in fact, even, what do we do? Let's say print. Okay. And now I'm going to add the F string. Okay, and now the actual string itself found the first even number. Okay, let's add my comma. And now I want to display the actual number, curly braces, and what is it going to be? It is going to be numb. Okay. Awesome. So we've added what the program should do if the number is, in fact, an even number. Prince says, Okay, we found the first number, and now break the loop. We don't need the loop to run anymore. Break it it's over. We found the first evil number. We're getting out of here. El Make sure your tabs. Remember the tabulation is extremely important, okay? E and L s must be on the same tab, right, the same indentation, right? So s simply print out, let's just say, print. Me add my brackets and then my coats still looking for that even number. And there it is. Okay. Let's go ahead right now. Run the program, and there it is. Still looking for that even number. Still looking for that even number. Still looking for that even number. Oh, look, we found the first even number, and the number is eight. This is the use of the break statement. Now, let's use it in a wild L. Now to do this, I'm going to come all the way down here. And let's create a very simple program that would ask the user to type in something as or type in a particular letter, and as long as the letter isn't equal to Q, then just keep asking the user for a new letter, okay? So that's basically the condition that needs to be satisfied. So while the user is typing ABCDEF, as long as it's not Q, keep asking the user to input a new letter. So I'm going to say while true, what this does right now, this is a very simple function or loop in Python that automatically has created a condition that is always true unless something else happens. Remember that with the wild loops, there needs to be a particular condition that must be continually satisfied in order for the loop to actually run, and then it must be terminated once a very specific condition has been met. So we're going to assume right now that the condition is always true, and now I'm going to type in user input. This is going to be the variable that will represent the letter that the user will actually type in. So I'm going to say user input equals, and now input, remember the function input, which will now ask the user to type in something. So I'm going to say, enter a letter and then in brackets, I'm going to say, Oh, type Q. Okay. Type Q to exit the program. Okay? So we're giving the users here a choice, okay? You can either just type in a letter or if you want to exit the program, just type in Q, okay? So I'm going to press Enter. So now let's type in the condition that must we need to check whether or not the input was actually Q. So I'm going to come in right now, add the If statement. So we're going to check if the user input is equal to Q. What should we do? Well, I'm going to say print. In brackets. And I'm going to say you have chosen to exit the program. Choose to exit the program. Press Enter, and there it is. And now since they've chosen to oh, let me add the closing quotes here. Since they've chosen to exit the program, we are now going to break the loop. Otherwise, I'm going to come in here right now and then say print. And now F codes, typed. And now, remember, in the curly brackets, I'm going to say user on the score inputs. And there it is. Alright? Go to go ahead right now. Run the program. Okay, so now it's saying Enter a letter, I'm going to say Y. Press Enter. Okay? Now it says, Okay, you typed Y. Enter another letter, I'm going to say A. Once again, it's running until I now typing Q, and now it has terminated the program. And this right here is us combining the our Wil statement, the Wile Loop rather with the break statement. So let me run through this over again. I know you might have a question, why didn't we type in the s Because we have the I statement. So where is the s? Well, we could actually add the s. It's going to be right here, and I'll just say se. And then the tab byte here print, you typed this. So I can go ahead right now. Run the exact same program again. Okay? Coming here right now, typing G. It works well, typing, it works well, typing Q, enter, and now it works the exact same way. So one thing you need to understand here is that the reason why the else statement is actually optional here is because we're working with the wild true loop, the condition. So it's going to automatically assume that the condition will always be true. And now we're using the break statement here to intentionally control when the loop would actually end. So there's really no need for the Ls statement here. The loop will always run because we have said Wild true and we'll only stop via the break statement once the user has typed in the letter Q. So really, the Ls statement here is not necessary. You can still type it in. The program will still work perfectly fine. But whenever you're walking with the wild true and then the break statements, you don't need to add the Ls condition any more. So we've done quite a lot in this particular video. Hopefully, you've been able to follow and hopefully you now understand how the break statement is used. Thank you so much for watching the video. I will see you in the next class. 23. Try except statements: Well, come back. In the previous lesson, we successfully created this particular program that will accept input, positive numbers from the user. It will study numbers in a list, and it will continue to do so until the user types in the word done. And at the end of it, it will print out if there was an even number, it will print out what the first even number was and will also eventually print out all the numbers in the list. However, one thing we discovered was that when we ran the program and we added, let's say, for example, instead of an actual integer like eight or nine, we added, let's say, 8.6, right? When we press Enter, we ended up having this particular arrow message value arrow. Now, if we run the program again, and instead of 8.5, I said, you know, IOF, I typed in letters instead, I press Enter. You can see once again that we also have the exact same error. And that's because our program has been created in such a way that it is expected to receive positive numbers only. If we run the program one more time and I typed in, let's say, minus six, which is a negative number and I pressed Enter, you can see that it actually kind of works in a way. It does accept the negative six because it is an actual number. It just happens to be negative. But that still defeats the purpose because we only want the user to type in positive numbers. So it shouldn't even have accepted this negative six. So we do have quite some issues with our particular program, and we need to now introduce something known as the try and then the accept block. So the tri block in Python is basically part of what we call an exception handling mechanism, which would basically allow us to catch and handle errors that might occur during the execution of our code. So if an error occurs inside of the tri Block, the program will not crash. Instead, the error will be caught and the code inside the associated except block will then be executed. Keep in mind that it's not just about users making a mistake whenever they type in their inputs. It's also possible that a hacker will deliberately try to insert values that the program will not accept in an effort to actually crash the program. This is why we need to always make sure that our programs, whenever they're accepting inputs from the user, we create and design them in such a way that they're able to handle our wrong inputs from the user. So how are we going to now catch any particular errors? Well, I'm going to come over here. We're breaking, okay? So the reason why we're starting here is because this is where we're going to check whether or not the user has added either the done word or an actual number. So after break, right? After break because break will check, Okay, is it done? If it's not done, then it's now going to execute. This is where we now going to type in the words or rather the word try because this is where we want the program to catch an arrow if there is. So now it's going to be try and then colon. And now this right here, we can simply tab that Okay, let me just present it right here. So we're saying right here that if the user types in done, simply break the program. I'm sorry, break the loop. However, if it's an actual number, let us check to see if that number is positive. So right now we have credit variable NUM we've added the user input to NUM. So now, let us check to see if it is positive. The easiest way to check would be to simply say if the number is less than zero. This is the easiest way to check. If the number is, in fact, less than zero, we're going to say print, please enter a positive number. Now s because after I comes. Now se if it was in fact a positive number, we are now simply going to append. We're going to append the num variable two numbers, and that's it right here. Okay? And finally, because we have try, we're going to close it out by using the accept statement. So basically, what we're doing right here we're saying, Hey, if the value provided by the user was, in fact, less than zero, then print, please enter a positive number. But then what if the user didn't type a number? What if they typed in letters or strings, basically, right? So this is where the except will now come in. So we're going to say except value error, okay? So if there's an actual error within the value itself because it's not a number, now going to say, print, please enter a valid number. And that's it right there. So let us try and see if this will work. So I'm going to go ahead right now, run the program. Okay? So what I'm going to do right now is I'm going to negative six, press Enter. You can see right here, it says, Please enter a positive number. But what if I typed a string like this, for example, now it says, please enter a valid number. See how this works, right? So let me walk you through it all over again. Basically, right here where we're accepting the input from the user. We want to check to make sure that it's actually a positive number. The best way is if that number is less than zero, then we know it's negative, we can print out. Please enter a positive number. If on the other hand, it is greater than zero, we know it's a positive number. We can go ahead right now and append the numb to the numbers list. And now we will have to say, Okay, but what if there was an actual error within the type of input that user inserted? Maybe it wasn't a positive number. A, maybe it wasn't even a negative number. Maybe it was like letters or words or something else entirely. We can then say, Please enter a valid number. That's where the except comes into play. So that's it for the try and except statements. Thank you for watching the video. I will see you in the next class. 24. Continue statement: Before we round up the section on loops and flow control, I want to take a look at one more statement, and that's going to be the continue statement. So far, we've been working with the break statement, and this is of course used to end the loop once a particular condition has been met. But what if for one reason or the other, we simply wanted to skip a particular iteration or part of a loop. We don't want to end the loop exactly. We just want to skip a particular step. This is where continue comes into play. Let me give an example. Let me just remove this code. Let's say for number in no range 16. Now, you're probably wondering what exactly is range. Range here is simply an inbuilt function within Python, that will generate numbers between a certain range. In this case, right now, we're saying numbers 1-6. However, please do keep in mind that when you're working with the range function, the first number here, which is one will be inclusive while the last number here, which is six will not be inclusive to be exclusive. If I was to simply say, okay, just simply print and then in brackets, just print the number. You will see right now that we're going to get one, two, three, four, five. Please do keep this in mind. Now, the function by default will print one, two, three, four, five. But what if we wanted to skip three in this case? We want if we're going to print one, two, four, and then five, how are we going to do this? This is where the continuous statement is going to come into play. But first of all, I need to create the If statement to check if the number is equal to three, right? So now I'm saying, if it's equal to three, just continue. I don't need to add the statement in here because when you're working with the continuous statement, it is understood. Python will simply say, once I get to three, I'm going to skip it out. I'm going to skip three and then simply continue with the rest of the loop. Now, if I was to run the program, now you can see it is one, two, four, and then five. That's basically how the continue statement would work. Let me give you another very quick example. What if I extended the range here from one to let's say 11. But now we wanted to check for numbers that are even and then simply keep those even numbers, we only want to print out the odd numbers. Again, how are we going to do this? Let's first of all, check to see if it's divisible by two so I'm going to say percentage and then two right here equals to what equals to zero at my column now I'm simply going to say just continue and then print number. Now if sterni program, now you can see it's simply one, three, five, seven and nine. That's how the continue statement actually works. Let me just spend 1 minute to talk a little bit further about the range function. There is one more thing. That you should be aware of. The thing about the range function is that it can actually accept three values. As an example, let me just add my hash tag. For example, with range, you can have something like one, ten, and then two. The range function can accept three different parameters, right? So if you're wondering, what exactly are these going to be? Well, the very first number in here represent the start value, which is one the second one here will represent the stop value, which is going to be ten, while the third value right now will represent two, which will be the steps that must be skipped. By default, let me just show you how this work right now. If I was to come over here right now and then say one, ten, or two. Just as an example, let me just remove the continue sepment in here. Okay. If I go ahead now right now and print these numbers, you will see that it is now one, three, five, seven and nine y? Because we're skipping every two. The step is going to be two, so it's going to go to one, it's going to skip to three, it's going to skip to five and so on. If I was to change this one right now to three, can you guess what the values are going to be? Now it's going to be 14, seven. So if I run the program, there you go 147. You may or may not work with the range function in the future, but I just felt it's important for you to at least understand that the range function can accept up to three different parameters. The first value in here being the start, second being the stop number, and then the last value in here being the step number and keep in mind that the stop value is always exclusive. It's not going to be included in the actual results of that particular range function. Final thing to mention, final thing to mention, you may also come across a particular statement called the pass statement. It just goes like this way. Instead of continuing, for example, you will see pass. Pass doesn't really do anything. Honestly, it's really used for passing comments. Maybe the developer of the program doesn't want to execute something yet but wants to live like a node. That's what the pass statement is mostly used for. We're not going to be working with it at all. So for example, in here, I could say something like, Okay, pass and then I can add my hash tag and I can say I will add the function later. Just as an example. Let me just go ahead and run the program, and you can see right now, the program still works anyway, it's not going to it doesn't really do anything as such. That's pretty much it. Thank you for watching the video. I will see you in the next class. 25. Programming challenge: Ok, dookie, it is now time for us to try to put together everything that we've learned so far to write a program. And the program we want is the one that will prompt the user to keep entering a positive number or else if they want to exit the program, they can simply type in the words or the word done to exit the program. So basically, we're saying, Hey, user, keep adding a positive number. Keep on adding thenumber. Keep on adding number. Whenever you're ready to exit the program, just type in done, right? Now, as the user is typing in the numbers two, three, four, nine, 11, whatever it is, we want to add those numbers to a list. We want to be able to keep track of all the numbers that user keeps on providing us. So we're going to add those numbers to a list. But we also want the program to check the eVO numbers that the user has added and keep track of the very first EVO number that was provided so that we can print out and tell the user that, Hey, this was the first even number that you added, and then we can indicate what that number was. Otherwise, if the user added only odd numbers, then we can just simply say, Okay, no even numbers were found in your list. Now, once the user has added several numbers and then finally decides to exit the program, we want the program to print out all the numbers that were added by the user. And if the user never even added a single number, just say no numbers war in't hard. Okay. So let us tackle this step by step. First of all, we're going to want a list, right? Let's call that list numbers, okay? So we're going to be adding all the numbers entered by the user. We're going to add them to this list called numbers. So obviously, we're going to make use of the input function to, you know, prompt user to add numbers, right? Okay. And we're going to have to create a wild loop in this scenario because we want the user to keep on adding a number or providing a number until they choose to exit the program. So we can say, you know, while true, and then the user keeps on adding a number. And then we want to check if the number that was provided by the user was actually an even number. Okay? So we're going to have to have some sort of like an El statement that will check if the number, if it's defeasible by two, you know, if that equals zero, you know, then print, even number, you know, stuff like this. You know, otherwise, just say no, even numbers were found. But what else? Remember that we also want to keep on adding the numbers that user has provided. We want to add them to the list called numbers. So we are going to have to append the user input to numbers list, right? And we're going to make use of a new function here called Append. Alright, don't worry. I'll show you how that works. What else do we need to keep track of? That's pretty much it at this point, right? So First of all, we create our list called numbers, and there we're going to have to prompt the user to add the numbers, check to see if the numbers are even. Once the very first even number has been found, let the program keep track of that and then at the end print out what that even number was. And then, of course, also at the end of the program, most user has decided to exit it, simply print out all the numbers that user added. So let us do this, right? We're going to move this away and let us begin. So first things first, let us create our empty list that will contain all the numbers provided by the user. So numbers equals, and then empty, okay? So the first step we're going to do. Now, let us prompt the user to add the number. So I'm going to say while and now true colon now we want to prompt the user to provide some numbers. However, we'll have to assign users input to a variable. So let's say user on underscore input. Okay, this represent whatever the user adds. User input in here equals to input brackets, and now let us prompt the user to enter a positive number. Okay? Or in brackets, they could also just type type done to finish. Okay? So far so good, we've assigned numbers. We have our empty list right there. This will collect the numbers provided by the user, and now we've created a loop to prompt the user to keep on adding a positive number or type done if they decide to finish. So what we're going to do right now is let us check to see if the user has actually typed the letters done or typed done. Okay, because we don't want the product to keep on running if the user decides to type done. So I'm going to say if user On the score input is equal to done. If these are actually types done, what are we going to do? You guessed it, we are going to break the loop. All right, let me add some space in here. Okay? All right. So we've checked to see if dieser has typed in done if they did break the loop. However, if they typed an actual positive number, what do we want the program to do? We want the program to add that number to the numbers list. Okay? So how are we going to do this? We need something to represent the numbers that will be provided by the user. In this case, right now, don't get it twisted. The user input variable in here is simply representing the input in here, enter a positive number or type done to finish. That's what it's representing in here. The actual numbers themselves will have to create a new variable for that. So for the variable in here, I am going to say num. Okay, num is going to be equal two, I NT, remember the integer function that will convert whatever the user types in. So it's not going to be integer and now underscore input. Okay? So basically, whatever the user types at this line right here where we say enter a positive number, if the user types in 5.2, for example, okay? Convert 5.2 to an integer, which will now be five and then assign five to the variable called number NUM. Now the next step will be to add this number that user has added to the numbers list. And how do we do that? I'm going to type in numbers and now check this out dot append. And now in brackets NUM. So right here, this append is going to be the function that will now begin to add the numbers added by the user to the numbers list. Let's go over it one more time. So first of all, we have our empty list called numbers. Let's create the loop called Wil that will prompt the user to keep on adding a positive number. However, if they typed done, if the user input is equal to done, break the loop, no need to continue. But if the user actually added real numbers, assign those numbers to a variable called Num NUM, and now start to append those numbers to the numbers list. So far so good, what else do we want to do? We want to check to see if any even number is being added. So what are we going to do? We are going to create a four loop. Okay? That loop is going to go through all the numbers in the numbers list and then find the very first even number. So check this out. I'm going to say four num N in what? In numbers. That's right. Now, let's add a colon. Let's create I L statement. So if num divided by two equals to zero, what do we want the program to say? We want to program to say print. And now our brackets. Let's add our F string, and then we can say, found the first the first even number. And then, of course, curly braces, and then NUM. Okay? Otherwise, if the number that was added by the user isn't even simply break. Let's just break it, okay? And now we can add our statement to do something we can say s colon and now we're going to say print when you saying no even numbers were entered by the user. And there it is. However, again, the identation is very, very important. Keep in mind that this s is under the four, okay? So it's not Is rather. It's the four s statement in here. So just make sure that you don't make the mistake theidntons are going to be very, very, very, very, very important. So it's basically four and then num in numbers. If num divided by two equals zero print, we found the very first number and then print out what the number is. Otherwise, simply break the loop once that number has been found, once the very first positive number has been found. Sorry, the first even number has been found. And then if no even number was added by the user at all, they just simply print out no even numbers were entered by the user. Okay. Last but not least, will now have to print out all the numbers the user has added at the end of the program. So I'm going to come out here right now, add the new If statement and simply say if number. So if the user actually added some numbers, so if numbers, guess what, simply print. And now we're going to say F and now encodes the numbers you entered R and then curly braces. Now, numbers. Otherwise, so that's going to be s. Simply prints. That is, if the user did not add any numbers, we can say no numbers were entered by the user. Oh. Let us try. I'm gonna go ahead right now. Run the program. And, oh, so we do have a bit of an issue right here. It says line 23. Okay, print F. What did I do wrong here? Oh, there shouldn't be any space. Sorry. There shouldn't be any space right there. You see how just a single space can ruin everything. It's kind of infuriating, but, yeah. So whenever you're using the F strings, make sure there is no space between the F and your code. So let's try that again. Run. Okay, so I'm going to add one. Alright, let's add three. Let's add four. Okay, let's add five. Let's add eight. Let's add six. And now I'm going to type in done, Enter, and there you go. The first number first even number was four, and now we've typed in the other numbers which are one, three, four, five, eight, and six. So you can see right now, the program actually works. So let me run through the program one more time. First of all, we created a list called numbers. This will hold every single positive number added by the user. Next, we created the while loop that will continually prompt the user to keep on adding a positive number, or if they choose not to, they can simply type in the word done to finish the program. So we now needed to check if the user actually typed in done. And by the way, let me even try that. Okay, let's run, and let's just type in done instantly and see. Well, there you go. So the program works very, very well. So it actually check to see, hey, did this user type in done? Oh, it did. Okay, so if we typed in done, then simply break the loop. We don't need to run the loop any longer because the user has typed in done. So in that case, right now, in this scenario where the user has typed in done, basically, all of this all of this stuff right here is completely irrelevant, okay? The Python program will go straight from this from the break statement, all the way down here to simply saying, Hey, first of all, all the way right here. First of all say, Hey, no even numbers were entered by the user. And then finally, no numbers were added by the user. So all the nomicals integer, is going to skip all of that. But what if the user actually typed in a number? What do we want the program to do? Well, we want to add that number to our numbers list. So we need to assign the user input to a variable. In this case, right now, we created a variable called Num that will represent the input provided by the user. And we also added the INT. In fact, let's try this and see. I'm going to say 4.5 Enter and oh oh, oh, oh. Okay. There is a bit of an issue right here, do you have an error message? Don't worry. I'm going to show you how we can handle errors in the next video. Okay, B, let me run this one once again, okay? And type in, let's say, five, okay? Type in five, type in eight, done. Okay, so it still works fine. So we made sure that we've assigned the numbers added by the user to the variable called NOM, and then we appended NOM to the numbers list using this particular function writing numbers dot append and num, okay? Now, because the user actually added some numbers, we wanted the program to check if any of those numbers were actually even. And by doing so, we had to create a loop that run through all the numbers in the numbers list. And then if the number was divisible by two with no remainder, simply say, Okay, we found the very first even number, printer that number. Otherwise, if the user did not add any even numbers, simply say no even numbers were entered by the user. And at the end of the day, simply print out the numbers that were added by the user. And if the user did not add any number, simply say no numbers were ended by the user. That's exactly what we did here. So, feel free to go through this program as many times as you need. Hopefully, it was relatively straightforward. And of course, as always, if you have any questions about anything we've done in here that you're not, you know, you're exactly sure, always feel free to reach out I'll be more than happy to answer whatever questions that you have. So Jomini Vernick Video web will now begin to take a look at how we can handle errors in our program. 26. Functions section intro: Come to the brand new section where we're going to take a look at functions, and I guarantee you that no course on programming will be complete without talking about functions, okay? Functions are extremely important, and believe it or not, we've actually been working with some functions, and we've even created some of our own functions already. In this course. You just don't know it yet. So in this section, going to focus on functions. I'm going to show you how you can create your own custom functions, and then how you can use something known as the parameters, okay? What do we want our function to work with? What kinds of data types, things like that. We're also going to take a look at keyword arguments, local and global variables, and so much more. And just like with the previous sections, there's going to be a coding exercise at the end of the section. So please do attempt it. Hopefully, you'll be able to pass. If not, don't worry, I'm going to provide you with the solution. So without wasting any more time, let's begin talking about functions. 27. Defining & calling functions: All right, so welcome to this Bn new section where we're going to take a look at functions, right? And so far so good, we've actually been working with quite a number of pre built functions within Python. So functions like the print, input, range, and so on, they all perform a particular kind of operation. However, what if we wanted to create our very own custom functions? How can we do that? Well, to do so, we are going to have to define the name of the function first of all, and then decide whether or not that function is going to accept what we call parameters. And then we can also say, Okay, this function will add these numbers or it's going to do something. And then finally, we can choose to return a value within that function. So it's going to be something like this. You'll have DEF, which is going to be the function to define the name of the function, and then you can choose to have parameters inside of that function, okay? And then we'll have the function body. So, you know, add A plus B, add this, do this, divide this, check to see if this loop is true, stuff like that. Basically, whatever the function will do. And then finally, we can then choose to return a value, which by the way, this is going to be optional. We don't always have to return a value whenever we are creating our own function. Let me give you a very, very basic example. So let's say for we wanted to create a function that will print the words hello world, right? We can say DF, and then I can call my function Grit. So the name of my function is going to be GET and then I can add my brackets at the colon and then end hub. By the way, notice that I haven't added any parameters inside of the brackets in here. Parameters are optional, okay? However, I'm going to come in right now and then define what the function will actually do. I'm going to say print. Now I'm going to provide the words hello world. And now all I would have to do is to simply call the function. And how do I do that? I'm going to say greet. And then the packets, and there it is. So now if to run the program, there you go, it simply says hello world. So once again, we started off by giving the name greet to our function. And then we now told what the function should do. The function should print hello world whenever we call or use that function. And then right here, we simply call the function by simply saying grit and then in brackets, and there you go. So this right here is one of the most basic kinds of functions we can create. But that's it. Jerman Vanex Video Ware will now begin to take a look at function parameters and arguments. I'll see you then. 28. Parameters & arguements: Well, welcome back. Now, let's talk about parameters and arguments. See, a parameter is basically an input that we can add to a function in order to customize its behavior. In this case, right now, in the previous example, the name of the function, which is grid, it didn't have any parameters at all. It's basically empty. That's why when we said, Okay, print and then simply hello world, and then we call the function, all it simply did was just simply say hello world. But what if we now want to add a particular parameter inside? In this case, right now, I'm going to come in here right now and say name. Okay? So the perimeter right here is called name, and now I want my function to say hello and then the name that will be provided. So I'm going to come over here right now. Let's use the F string, and now I'm going to add my curly braces, curly brackets rather and call it name. Okay? So now, check this out. When I'm calling my function right now, I can add what we call the argument. When you're passing a value to the function, it's going to become an argument. Please note the difference between parameters and arguments. Up here, when we're first defining the function, we're going to use the parameter. The parameter is basically all saying, Hey, we're providing you with this input, and I'm going to use this input to customize how the function grid will behave. However, once the function has been created, we've told what the function is supposed to do. In this case, right now, hello and then name the perimeter. When we call that function, when we want to provide a value, it's going to become what we call an argument. So in this case, right now, if I wanted to greet, let's say, Alice, for example, right? Alice right now is the argument, and if I was to run the program, there you go. It says, Hello Alice. What if I wanted to greet, let's say, Bob? Right? Bob, let's go ahead, run the program, and there it is. Hello Bob. These are perimeters and arguments. However, we can also have multiple parameters in our function. So far right now, we've only been working with one single parameter, which is name. What if we wanted to create a function that will add two numbers together? How are we going to do that? Well, we can start off by defining the function name. So I'm going to call this one add numbers. Add numbers will be the name of the function. And now inside, we're going to provide two parameters which are going to be A and B, colon, okay? Now, return A plus B. We haven't talked about the return function or return statement yet. What return does here is that it's simply going to return the value of a particular operation. In this case, right now, is going to return the values of A and B when they are added together. That's what the return does. So now I'm going to come in here and say result. Okay? Result is going to be equal to add numbers. Okay? And now inside, I can add the two numbers I want to add, let's say five and six. And now all I will have to do is to simply print the result. And there it is. If I want my function, there you go 11. You see how this works? First of all, we created a function, but using the defined function called add numbers. That's the name of our function. And then we said, Okay, our function is going to receive two parameters, A and B. And now with the return, we're basically defining what the function is going to do. It's going to return the value of A plus B. Now, all we need to do is to create a variable that will store the value of our function. So now the variable here is called result, which is equal to ad number. The name of the function call in right now with the two values which are five and six, and now we simply printed out result, which is equal to 11. That's exactly how parameterize arguments and the return function also works. By the way, before I round up the video, a very quick word on the return statement. It's used for two main purposes, okay? The first purpose is typically to exit a function. So you can use it to stop function execution. Second is to return an actual value. So as an example, let me just show you real quick. If I said define and then greet. Okay, let me add empty parameters in here. And I said, print hello. And then I said return Alright, but then after return on another line, I also say print. And then, let's say, let me say, like coats, hello Jack you can even see right now that within our replet the text itself is kind of fitted out, meaning that Python will not execute this particular line. So if I was to run, you can see right now, it does absolutely nothing at all, but that's because I haven't called the function yet. So let me come over here and say greets and then brackets, and there you go. So if I was to run the function right now, you can see all it says is going to be hello. Why? Because return right here signifies that, hey, stop the execution of the function, it shouldn't go beyond this particular line. And that's why we only have hello. However, if I was to remove the return statement, and I run the program again, you can see right now it says hello and then hello Jack. So this is another use of the return statement, but you can use it to stop function execution, or like I showed you earlier, you can use the return statement to return a particular value. Thank you for watching the video, I will see you in the next class. 29. Keyword arguments: Well, come back. I want us to now talk about keyword arguments, right? Python functions, they basically allow us to pass arguments using parameter names. Parameter names are what we refer to as keywords. They'll make a code more readable and flexible, especially when we're dealing with many parameters. Now, so far we've been dealing with parameters like A, B, X, Y, but let me show you something else, okay? I'm going to create a new function and let's call this function describe, underscore person, okay? So this function is going to describe somebody's name, age, and let's say city, right? So let me add the brackets in there. And now in here, we can add the keyword argument. So I'm going to say name, age, and then let's say city. Okay. And now for the actual function itself, oh, sorry, I forgot to add the colon. And now the actual function itself, what's it going to do? We wanted to print, Okay, John is 30-years-old and lives in London, just as an example, right? So I'm going to say print, bracket and now the F string. Okay? And now coats and now curly braces. Name space is curly braces again are now call the age perimeter. Bracket is old. And I'll let Python finish the rest for me. And there it is. So basically, the function will say, Okay, Alex is 100-years-old and lives in New York City, just as an example. So check this out. I'm going to call the function so you can see right now I'm going to call Alice. Thank you, Python for providing this suggestion. So I'm going to say Alice is 25-years-old and lives in New York. So if I run the function right now, you can see it says, Alice is 25-years-old and lives in New York. Pretty straightforward, right? However, check this out. I can also call the function by providing the values for the parameters themselves. Look at this, okay? I can say describe person. And now in brackets, instead of saying, Okay, Alex, 30, London, instead, I can say age equals to 30. And now coma. And then I can say the name equals Bob City is San Francisco. Did you see what I did right now? Instead of providing the actual values for the arguments directly, I just said, Hey, I'm going to pass the values into the keyword arguments. So age will be 30. Name is Bob, City is San Francisco. And, of course, please pay attention to the fact that Bob and San Francisco are strings. That's why they have the double codes. While 30 is an integer. It's not a string. That's why there are no codes. So if I run the function, you can see right now it says Bob is 30-years-old and lives in San Francisco. Despite the fact that here, age came first. We didn't say name equals Bob and then age or city. No. That's because Python is simply going to use what we have described in the actual function itself. It's not going to matter the order all the arguments. Python doesn't care about that. All Python cares about is, Okay, you've given me the keyword arguments, fine. And now this is the order. It's going to look at the function that says, Okay, print the name first and then is 8-years-old and then lives in city. So these are what we refer to as the keyword arguments. It's not likely we're going to work with them later on in this course, but nevertheless, this is something that you should at least be aware of when you're working with Python. So thank you for watching the video, I will see you in the next class. 30. Local & global variables: Well, welcome back. One last thing I want us to talk about before we round up the functions section will be the concept of variable scope. See, in Python, or in fact, in any programming language, we have both local and global variables. Variables have the scope which will determine where they can be accessed. Now, variables that are inside of a function will be local to that function, and you cannot access them outside of that function. While variables that are defined outside of any function are global as an example, okay? X equals, let's say 12, right? This right here is what we refer to as a global variable. Why? Because it's on its own. It's not inside any function or any loop or any if else, it's on its own. It's just that X equals 12. However, if I created my own function and I said define, underscore, modify, variable, bracket, colon, and now I said, X equals three. The X right here will be what we call a local variable because it is inside of the function which is modified on the score variable. So the global variable of X is 12, the local variable of X is three because, again, this three right here it's inside of the modifivariable function. So let's take a look at this, okay? I'm going to tell my function to print out the value of X, okay? And now, I'm going to call the function modify variable. What do you think the value of X here is going to be? The value of X here is going to be three because we're simply calling the function modified variable, which already has the value of X to be equal to three. So if I run my function, you can see it's right there three. But what if I wanted to call the global variable X equals 12. I want to print out that value. All I need to do is just call the default print command or print function four, Python, and then simply say print X. Now, if I run the program again, you can see three and now 12. Again, because this print right here, it's not the same print that's under the modify variable function. This print right here is meant to produce or print out the value of X, which is the local value that's inside of the function, modify variable. However, this print X w here notice again the indentation. Is not inside of this function, it's outside. So the X here is going to be the one that belongs to the global variable, which is 12. That is the difference between global variables and local variables. If you wanted to get this particular value of X, which is three, you will need to always call the function that houses or that holds that particular value, which is, of course, modified variable. But the global variable, you can access it anywhere as long as it's not inside of the function. However, we can actually modify the value of our global variables inside of a function. Yes, we can do that. Check this out, okay? I'm going to go back to my function in here, right? And just underneath, I'm going to say global X. Just by using the statement called global, we can modify the global value of X. So now I'm saying global X, X equals three. In this case, right now, we are simply modifying the global variable. That's what we're doing right now, we're simply going to modify the global variable of X. So now, if I called, if I run my program, you can see right now that first of all, the function modifier variable X equals three will produce three. And because we have now modified the global variable of X to now be three instead of 12, even when I call the print function that is outside of the modified variable function, it's still going to produce three because, again, inside of this function, we modified the global variable X. We changed the volume 12-3 by using the keyword global right here. So this is how you can also modify the global variables inside of a local function. Thank you so much for watching the video. I will see you in the next class. 31. Data structures section intro: Welcome to the data structures section. And in here, we're going to focus on three main types of data structures. We're going to talk about lists, dictionaries, and sets. And, of course, I'm going to highlight to you the differences between these three. So you're going to learn exactly what makes a list different from a dictionary. You're going to learn exactly what a set is. And then once you've learned how to create your own sets and dictionaries and lists, we're going to learn how to incorporate loops into them. So say, for example, you have a list of passwords and you wanted a loop to go through each password and do something, you're going to learn how to do something similar in this particular section. And, of course, there's going to be a wide variety of different coding exercises, so please do attempt them. There are also going to be two programming challenges, which, of course, I'm going to walk you through on how to solve them. And, of course, just like with every other section at the end, we are going to have the main coding exercise which I want you to attempt. So please make sure you do so without wasting any more time, let's jump right in. 32. Lists: All right, so let's begin by talking about lists. Now, lists aren't anything new to us. We've actually worked with quite a few lists already. We've had lists of names, lists of cars, and so on. But one thing about lists is that they are what we call mutable. Meaning that the items or elements that you have in a list, we can actually modify them, right? So we can add to the list. We can remove from the list, and we can modify whatever elements that already exist within that particular list. Now, over here, I do have a list of compromised passwords. So, the usual password, one, two, three, four, five, QW, TY, and so on. Now, I want to show you a few things that we can do in here, right? I can say print, okay? And now in brackets, I can call the list compromise passwords. And now I can provide the index number of the item that I want to list. In this case, right now, it is index number zero. If I run my program, you can see that's going to be password. So the first thing I want you to understand is that the items in a list, they all have index numbers starting from zero. So if I was to print index number two, what do you think the answer would be? Yep, it's going to be QWERTY. Okay, that's the first thing I want you to understand. Next is the fact that we can perform various types of functions on our list. For example, I can say, let me call the list again, and now I can add this function here called append. Append right here in technical terms, is what we refer to as a method. Methods are also functions, but they are very special kinds of functions. We'll talk about methods a bit later. But for now, just understand that we do have very special functions that we called methods that you can apply directly onto lists and they will do something. So right now, what a pend would do is that it will add whatever new item we want into our list. As an example, I can open up my brackets. And let's say we add swordfish. Okay? This is another very, very popular password used by a lot of people, okay? So I've appended Salt fish to the list. So if I go back right now and I print my compromised passwords, you can see Sordfsh has been added to the list. So this is the very first method or function I want you to understand that you can use with your list. The next one is going to be what we call the extend function. See, a pen with a pen, you can add you know, individual items, elements, and so on. With Extend, you can actually add all elements from another list. So as an example, I have my first list right there, compromise passwords. And let's say we've created another list of compromised passwords. Okay? Equals, and now let me open up the brackets in here. And let's add some fresh batch of passwords. Let's see. We have one of the very common passwords people often use. Okay, let's say one, two, three, A, B, C, how about that. And let me add one more. And let's call down one dog, cat, bone. You know, just something, right? Okay, so we now have two different lists, right? One for the compromise passwords and then another for the new compromise passwords. So check this out, okay? I can come here right now and say, compromise passwords and now extend. And now in brackets, I can say new on this compromised password. So basically, I am attaching the new passwords into the old passwords list. And now, if I run my program, now you can see that 123 ABC and dog cat Bone have been added to my compromised passwords list. Now, another method I want you to be aware of is going to be quite the opposite of append which is going to be remove. So let me first of all, cut out our first or second list rather and I am going to modify this line right here and then say, remove. And now in brackets, just as an example, okay, I can provide what I want to remove, which is one, two, three, four, five. So let's say I wanted to remove this particular element very, very easy. I'll just say Don't remove, run, and now you can see that 12, three, four, five is no longer in the compromise passwords list. So very, very straightforward. Another one we can do is to simply clear. We can clear the entire list as an example. So if I was to come over here right now, change remove to Clare, right? I don't need to have anything inside the brackets. So just Clare and Iron, there you go, it is going to be empty. There is nothing inside of the list anymore. So there's quite a few other methods that you can walk with. I think it's about 11 of them. But let me give you two more, right? Another more we can use is going to be reverse, right? So basically, what this will simply do is that it will reverse the items in the list the order. It's going to reverse the order. So if I run this, there you go, it starts from Uh, let me in and goes backwards all the way to password. And then one more that I want to show you that can be very useful would be the copy method. So as an example, over here, let me just come right here and I'll create a backup, okay? So backup and let's say underscore accounts, okay? I can assign the backup to my variable backup accounts and I can now say equals, and now compromised passwords, and now dot, copy, and now in brackets. Alright, so basically, what I'm doing right now is I would like to make a backup of this list, this compromise password list and then assign that to the variable backup accounts. So now if I was to print the backup accounts and I run, you will see right now that we have the exact same list, the exact same items. So this is what you would use to create backups or copy the items in a particular list. So like I said, you do have a few other ones like sort, pop, and so on. We're going to talk about methods a bit later in the course. But for now, that's the introduction to lists and the methods that you can use to work with them. Thank you for watching. I will see you in the next class. 33. Using loops in a list: Continue working with our lists, and I want us to try to write a program that will simply print out the high activity days. Now, what you see on my screen is a list of login attempts per day. So let us write a program that will go through this list and then print out the high activity days. However, to determine what a high activity would be, let's say login attempts that are ten or more. Okay. So to do this, let's create a variable that will store the high activity days. So I can say high and then underscore activity on the score days. Okay. Now this is going to be equal to let's say attempts. Okay? We want to create a four loop that's going to go through the list, okay? So we have our brackets in here, and then I can say attempt, Okay, four, remember now for attempt in where are we checking the number of attempts? It's going to be in the login attempts list. So I'm going to say in the login attempts. And now we haven't talked about this yet, but you can also include an if statement directly inside of a four loop. So we want to create a loop that's going to go through our list, and then if the attempt is ten or more, we're going to print out a high activity day. So attempt for attempt in login attempts. Now if the attempt is greater than or equal to ten. You got that, okay? So now, what should happen? Let's simply print. And then in brackets, we can say something like high activity days, colon, and then coma, high underscore activity days. So now if I run the program, and there you go. The high activity days, we have ten, 12, 15 and 20. So this is how we can run a four loop to go through our list and then print or do something else. So I'm going to provide you with an exercise, a challenge in the next video. Please go through it and attempt to answer the questions. Thank you for watching the video. I will see you in the next class. 34. Dictionaries: Let's talk about dictionaries. And just like with lists, you can modify, you can add, remove elements in your dictionaries. However, the core difference between your lists and your dictionaries is that unlike in your list where you have individual elements or values or items in your dictionary, we're going to have what we refer to as a key value pair. So as an example, I can have a dictionary called user info, and now equals I'm going to now use the curly braces, okay, we typically use the curly braces for dictionaries. So in here, I can have information like the username, which would be admin. So right here, username is going to be the key while admin is going to be the value of the key. We can also have a password, okay? And let's say the password here is let me in just as an example. And we could even have another one, like, let's say, last log in. Okay, last login, call on, and I can have a date, let's say, 2024 10-22, just as an example, right? So do note that whenever you're adding your key value pairs in your dictionary, the end of each line, you will have the comma. But then for the very last key value pair, there is no need to add the comma at the end, right? So just take note of that. So this is what we refer to as a dictionary, right? We have our key value pairs, username, the value is admin, we have the key password, the value is let me in. And of course, we have the key last login, and then we have the date there as the value. So from here, I could decide to print out the information. I could come down here and then say print. And now if I wanted to print, let's say, the user name, I would first start by saying user underscore info, and now the regular brackets in here, I would simply say user name, and that's it. So now if as to run, you can see admin has been provided. So this is how you can print out the values of the keys in your dictionary. You simply state the key whose value you want to print and the program will do the rest. Well. Now, just like I said with dictionaries, you can easily add or modify the elements in your dictionary. So, for example, if I wanted to add some new information like, let's say, the email of the user, all I need to do right now is I can come down here and simply say user underscore info, Okay. And now I will have the regular brackets, and now I will simply add the key here, which is going to be email. And then very simply, I'm just going to say equals, and now I can add the email address right there. Let's just say admin at labcyber.com. Alright. Just as an example, right? So if I was to come down in here right now and print the user info, let me just remove the key there. So we want to print out all the user's information in here. I run the program, and there it is. It's going to print out everything. So username is admin. The password is let me in. The last login is this date, and then the email right there is admin at labsyber.com. We could also update. We could also update the information in our dictionary. So once again, as an example, if I wanted to update, let's say the password, right? So I can say user Oder score info, and now in brackets, of course, the key here is going to be password, right? So password. And now I need to provide the new password. So let's call it SOD fish. SOT fish. And there it is. If I run my program, now you can see that the password has indeed changed to SOD fish. So just like with your lists, you can add new key value pairs to your dictionary, you can also modify the values of existing keys. Also, just like with lists, we do have a variety of methods that we can actually apply onto our dictionary. So the very first one here I want to talk about here is going to be the G method, which will retrieve the value associated with a key. So for example, let me remove all this information in here, right? If I wanted to print the user info, or when I get the user name, the value of the key user name, all I need to do right here is I can say print user dot info and then dot G. All right. And now in brackets again, I will now add the key which is user name. So this right here should print out the user name. If I run, there it is admin. So just like with your list that we used a pen, copy and so on. We have the exact same methods also for dictionary, and get right here is one of them. Another method is going to be the keys where you can simply print out all the keys that we have in a dictionary. So let me remove all the information in here. So if I run, there you go. We have all the keys which are username, password and last log in. We also have the opposite which would be values. So it sort of keys. I'm going to change this one over here to values and if I run, now we're going to have all the values listed in the output. Now, we could also create a loop that will go through our dictionary. And how can we do this? Well, we can use the four loop because we have a dictionary. Remember that the four loops work well with lists, dictionaries and so on. So I'm going to say four, and now I'll have to say Key, okay? So for key and then comma value in, and now user underscore info, and we have to add another method called items items and now brackets, colon, and there it is. This is how you will create your loop that will run through your dictionary. So it's going to say four key comma value, and then in the name dictionary, and then you're going to add the items method to it. And now all we have to do from here is to simply print out the information. So I'm going to say print. And let's use the F string. So our regular brackets first. And now the F string and now coats, curly braces to hold and display the key, and now we'll have a colon, and now the other curly braces to hold what to hold the value. And now if I run, there you go, use the name Admin, password let me in and of course, the last log in date right there. So you can also use loops to run through your dictionaries, your lists, and so on. So that's it for dictionaries. Joy in the next video, where we'll now take a look at what we refer to as nested dictionaries. I'll see you then. 35. Nested dictionaries: And back. In the previous lesson, we talked about dictionaries, but now we're going to go a step further to talk about nested dictionaries. So basically, dictionaries inside of another dictionary. So far, we've been dealing with one single user info. But what if we had multiple users, right? How can we create this? So what I'm going to do right here is, let's change the name from user info to user credentials, right? This is going to be the new name for a dictionary, and now curly braces. We can now add the very first NSTA dictionary that will contain the information for Admin. So I'm going to come right here, type in Admin. That'll be our first user. And now the credentials, curly braces. Let's go with password. Password in here. The colon, and then we can go with admin 123. Alright? So the first user here, admin, the password is going to be admin 123, and we can add another credential which would be the last log in. So last log in let's have this one in our codes. Let's log in. Now colon. Let's add the dates. Let's say, something like 202-04-1019. Alright. Just as an example, there is a bit of an arrow right here. Let me see what exactly am I doing wrong? Ah, over here with password, the column should be outside. I made a mistake there. Okay, much better. So this is the first Nesta dictionary we have admin that has the key value pairs. It has the key password, and the value for a password here is going to be admin 123. And then the other key here is going to be last login, and the value is, of course, 2024, 1019. But we can add another dictionary. I'm going to say comma. And now we can add another user. Let's call this person user one. And I'm just going to let repl do the rest, fill out all the information in there and there you go. So the password day is going to be user one, one, two, three, and then last login is also No, let's change the date here to something else. So let's go with 06 and then maybe a 14. All right, for variety. So this right here is what we refer to as a nested dictionary. Dictionary is inside of another dictionary. And of course, we can access the information pretty straightforward. So for example, if I wanted to print out the password for the Admin, how can I do this, okay? I can simply say print. And now in brackets, I'm going to say user underscore credentials, okay? And now the regular brackets and inside double coats, I'm going to say admin. Okay. And now we want to print out the password for the admin. So brackets again, let's add password. And there it is. So if I run the program right now, you can say it says Admin one to three because by saying, Hey, print out from the user credentials dictionary, we want you to print out the value of the key password belonging to the user admin. And that's why we have Admin 123. Or I could say, okay, print and brackets, user credentials, and let's print out the last login for user one. How are we going to do this? Again, very straightforward. I'm simply going to come in here and say user one, and now new brackets and then simply put in the last login key, last on the score, login. And there you go. So run, and there you go 202-04-0614. So this right here is an example of nested dictionaries. And this can be particularly useful if you're trying to create functions or programs for firewalls or a pastor check in tool or a tool that check user credentials, things like that, right? You can use Nested dictionaries for those kinds of programs. So thank you for watching, and, of course, I'll see you in the next class. 36. Sets : Continue with data structures, and the next is going to be what we call sets. Now, on my screen right here, it appears that I have two different lists that are similar in the elements inside of them. However, notice that the compromise passwords uses the regular brackets, while the new compromise passwords uses the curly braces. Now, believe it or not, this is extremely important because the regular brackets indicate these items are in a list, while the curly braces will indicate that the items are not in a list, but inside of a set. So the natural question right now would be, well, what is the difference between a list and a set? There's quite a few of them. The thing is, with your items in a list, you can order them. You can order them. You can reference them using an index number, while in a set, they are not in any particular order. They are ordered, right? In fact, I prove this to you. If I was to print right now the compromised passwords, right? And then I also print out the new compromise passwords look at the order. For the actual list, you can see it follows the same order password, 1234 QWERTY. But with the set, it's almost kind of random. It set off at 123, four, then went to ABC 123, it went to Monkey, and so on. So the thing is with sets, the items are not in any particular kind of order. Another major difference is that your list will allow duplicates, right? So for example, I could add in my list 123, four again. However, if I was to do the same in my set, you will notice the difference. So if I run, again, you can see right now in the list, 1234 is repeated. While in the set, 1234 is listed only once. It is not allowed. You're not allowed to have duplicates in your sets. And this is why whenever you're trying to create a function or a program involving, like, a list of items that should be unique, like, let's say, passwords or email addresses or user accounts, you want to use sets for those as opposed to list because you know that, okay, no two users can have the exact same credentials. So it'll be more ideal to use a set. So sets are typically a lot faster to run through. The program can run through a set a lot faster than it would a list. Now, both are mutable. Your lists and your sets are mutable, meaning that you can make changes. You can add elements, move elements, you can modify them. And that's pretty much it. But just like with your lists, we also have operations or methods that we can use on our sets, we actually call them mathematical operations like union intersection, difference. Let me just show you, okay? Let me remove this right here. And now, let me come over here and let's work with two different sets. Okay? I don't think I need this line anymore, actually. Okay, so right here, we have set one, we have set two, and then both have two IP addresses. Now, I could decide to find IP addresses that are common in both sets, right? So I could say, for example, common underscore IPs will now be equal to set one, and now union. Okay? So intersection, right? Intersection and now set two. So intersection right here is what we refer to as a operator that will find the common elements in both sets. So if I was to come up right now and then say print, common IPs and I run, you will see 10.0.0.1 is common in both. That's why it is printed out. But we also have what we call the Union mathematical operator. So I can change a symbol right here to the union symbol. What this will do, it's kind of like the extend method for list where you'll add the elements in one list onto another. So right now, if I run, you can see we now have all the IP addresses. However, take note that again, because this is a set, the 10.0.0.1 IP that's present in both will only be listed once, right? We have another operator in here, which will be the difference operator. Just use the minus sign. Now, what this will do is that you will find elements that are present in one set, but not in the other. So if I run this right now, you can see that 192 one.one.01 is an element that is present in the first set, but is absent in the second set. So we can do the opposite, okay? I can say set two minus set one, and now this will give us the 19216 eight.one.102, which is, of course, present in set two, but not present in set one. We also have methods like your add, remove, Clare. So as an example, if I wanted to add a new IP address to set one, all I would need to do here is, I would just say set one and now dot add, and now in brackets, can add the new IP address, so I can say 192.168.0.10. All right, just as an example. So now, if I was to print out set one, run, and there you go. You can see right now that 192-16-8010 has in fact been added to set one. You can also have other methods like remove, clear, and so on, feel free to look this up in the Python website if you want it to. So that's it for sets thank you for watching. I will see you in the next class. 37. Programming challenge sets and loops: Come back. Now, I want to walk you through an exercise that has two tasks. And on my screen, you can see we have a dictionary of different logs. You can see the key value pairs there, the username, and then the IPs, and then the statuses, right? So the two tasks are as follows. First of all, we want to identify and print out all the unique IP addresses in the logs. And then, second, is to look for a special IP address that might exist. If it exists, then we're going to print out something. If it doesn't exist, we can print out something else. So you're more than welcome to pause the video and actually attempt to solve these two tasks on your own. One hint I will give is that we're definitely going to have to work with some sort of a four loop I was going to go through each log and then especially look for the val of the IP addresses, and we're going to have to pass the results into a set because remember that with sets, we're not going to have any duplicates. And since we're looking for unique IP addresses, sets would be the ideal way to go. So you can pause the video and try it on your own. If not, I'm going to walk you through on how we can solve both tasks. So first of all, Task one, we need to identify all the unique IP addresses. So one thing we could do right now is we could simply create a set that's going to store these unique IP addresses. And I'm going to call that set unique IPs. Okay. And now, what I'm going to do is because it's a set, I'm going to open up my curly places. But now think about it, okay? We want to go through each log. And then look for the value of the I P key. So check this out, okay? I'm going to say log, and now I'm going to open up my brackets because we want to target what the key, which is IP. So essentially, we're saying, Okay, for each log, let's access the IP, and now I'm going to introduce my four loop by saying four log in logs. Right? So what we've done right now is we've created the four loop that will go through each log in our logs dictionary, and we'll specifically look for the value of the IP key. So since we've passed everything into a set, all we need to do right now is just simply print. And then in brackets, we can say something like unique IP addresses, colon and then simply add a comma, and now unique underscore IPs. And there it is. I'm pretty sure you might have expected that this will take a few more lines of code. So congratulations if you were able to solve this on your own, regardless of whether or not it was two lines or three lines or four lines or five lines because there are other ways how we could solve the task, but this is easily the shortest and the most effective way. So let me go ahead and run the program right now just to make sure it works, and there you go. Awesome. So we have everything in there, 182, 168, 11, one dot five, one to two, one dot four, and then one dot three. Awesome. Cool. Alright. Next is going to be task two. Okay? So what are we looking for in here? We're looking for a special IP that's equal to 182168, one dot four. So we can say special IP equals 19216814. So what are we going to do right now? We'll have to use the Is statement because we're saying, Okay, if this special IP address was found in this, do this, else, do something else. So I'm going to say if special underscore IP in what N where in our unique set. Unique underscore I'm sorry, unique IPs, rather unique underscore IPs. What do we wanted to do. We wanted to say print, and then we can say, you know, special IP address was found, okay? Else. Ooh, sorry about that. And then we can say else Print. And then special IP address was not found. And there you go. Okay. Let's go ahead, run the program, and there it is. The special IP address was indeed found. Now, if I change four here to let's say ten, just to make sure that it's actually working, and I run the program again, there you go. Special IP address was not found. So once again, congratulations if you were able to solve either tasks or both tasks on your own. If not, don't worry. It's all part of the learning process, but I'm going to give you an exercise right now that you'll have to attempt on your own. So I will see you in the next class. 38. Programming challenge dictionaries and loops: Welcome back. So I have decided to add one more task to make it the third task, and it's going to be a bit more complicated. And this task is going to involve counting the number of failed login attempts per user. So essentially, we're going to go through each log, count the number of times a particular user failed in their status, and then simply print out Ale has, you know, four flures, Bob has one filure and so on, you know, something like that. So you're more than welcome to pause the video and attempt to solve this task on your own. If not, let's go through the two together. So like I said earlier, I have my notepad file in here because I want us to tackle this logically, okay? So let's go step by step. The very first question here is this. Are we going to be dealing with a set, a dictionary, or a list? Let's think about it, right? Obviously, it cannot be a set because sets do not accept duplicates. So if we're going to use a set in here and Bob had one failed login attempt, the second time Bob has a failed attempt, the set will not accept it because remember sets do not accept duplicates. So sets will not work here. It cannot be a list either because think about it, okay? We need to associate the user name with the number of times they have failed, and it's dictionaries that are great for working with key value pairs. So the key here will be the user name, and then the value is going to be the number of times that particular user name has failed in their status. So we're going to be working with a dictionary. So we need dictionary to store user name and their respective fail login attempts, right? Okay. Now, what do we need to do to actually find out the number of times each user has filled? We need to create a four loop. So for loop to go through each log, right? And then a loop, when it goes through a log, what are we looking for exactly? We want to look for the status that says failure. We're not interested in the success. We're interested in failure. So we can say if the status. So if status, equals to failure. What do we want to do? We want to identify the user name associated with that failure. So let us extract the user name, okay? We're going to extract the username and then we can store that in a variable called, let's say username, right? So we can store the user names in the variable, username. So once that username has been found, let me correct this one extract. Okay, so once a username has been found to have a status or failure, there are two possible scenarios here. Think about it, okay? It's either this is the very first time that username has had a failed login attempt, or it could be the second time or third time or fourth time. Essentially, it could be it's not the first time. Okay? So we need to create conditions for both of them, so we can say if it's the first time Okay? We can say, right, the user name here, the score is zero because it's the first time, okay? However, if it's not the first time, if it's not the first time, we need to add one, okay, and then also add it to their previous record. So if it's the very first time, you start off from zero, you get one. But if it's not the first time, if it's the second time or third time, we're going to simply add one to your previous record. I hope that makes sense. And then finally, we can simply print the failed login attempts. Okay? So you can pause the video again and try to solve this task on your own. If not, let's write the program together. Okay, so what are we doing, first of all? We are creating our dictionary. So I'm going to call the dictionary failed attempts, okay? Equals, and then curly braces, is going to be empty because nothing is in there for now. So next is going to come the four loops. I'm going to say for log in logs, colon. And now, what are we looking for? We're looking for we're looking to check to see if the user name has the status of failure. So specifically, we're looking to check if the status is a failure. So I'm going to say, I log, and now we want to target the status, right? So if the status in here is equal, Oh, sorry about that. There should be outside. So if status is equal to what to failure. F. If it's equal to failure, what are we looking for, we want to extract the user name. So I'm going to say user name will be equal to. And now let's extract the name. I'm going to say log, and now in brackets, user name. Let me add the codes right there. And there you go. Okay, so what have we done so far? First of all, we've created the dictionary failed attempts. Now, we've created our four loop to go through each log in the logs and then look for any log whose status is equal to failure. When you find that status that's equal to failure, let us extract the user name and then assign it to the variable user name. Now remember, there's going to be two conditions. Is this the first time this user name has the status of failure or is it not the first time? So, check this out. I'm going to say, if Use a name Okay, is not. How do we check if it's the first time? If it's not in the failed attempts? Okay? So by saying, if this is the first time that we're actually discovering that this user has a failed record, so we can check that by saying use the name is not in the failed attempts. What do we want to do? We want to say failed attempts. And now in bracket, use a name is going to be equal to zero. However, if it's up, sorry about that. Let me failed attempts. Attempt. Actually, I do apologize. I've made a mistake with the attempts. That she be T for the S ta ta ta ta. I do apologize. Let me add my T there and add my T there. Okay, so, however, if it's not the first time, if it's the second time, third time, what are we doing? We are saying failed attempts. And now, use a name. Use a name is going to be equal plus equal to one. Because we're going to be incrementing the value. If it was one before, now it's going to become two, if it was two before, now it's going to become three and so on. I want to point out the fact that notice the second failed attempts here, it's outside. It's not inside this If statement, okay? Because this right here, line 21 22 is just to check to see if the user name did not exist previously. And now, the second field attempts here, this is to check to see if indeed the failed attempt has already been recorded previously and now we're incrementing by one. So all we need to do right now is to print Okay. And then make sure this is outside, print. And now in brackets, we can say something like filled filled login attempts per user, and then cool on, and then we can simply now add filled underscore attempts. And there you go. Okay, so hopefully this should work. I'm gonna run the program, and there you go. Awesome. So, Bob was three. Alice was two, and Dave was one. I think this is correct. Bob has one here, one, one that's three. Okay, Alice has. Alice was successful here. Okay, this is a failure failure, two, okay. And then Dave is one. And, of course, Carol did not throw because Carol had only one log, and she was successful. So that's pretty much it on solving this particular task. So once again, we created a dictionary called failed attempts. We created a loop to go through each log and then say, Okay, if we do find a status that's failure, we want to check first of all, want to extract the username associated with that failure, and then check to see if the username has been in our failed attempts previously or if it's the first time. If it's the first time, I assign them the value of zero, and then if it's not the first time, simply add one to their previous value. By the way, a confusion you might be having here is, hold on, Alex, why is it that here on line 21 22, if it's the first time that the record is a failure, that the NM has a record of failure, why not say equals to one? Y Z O? The reason is because on line 23, we, regardless, are going to add one to the value, whether or not it's the first time or it's the second time, so it doesn't even matter. I hope you get what I'm saying, right, because this is going to go. So if it's the very first time, right, first of all, the program will say, Okay, your field number of Ts is zero on line 22, and then on line 23, it's going to add one regardless. That's why we assigned zero in here and not one. If we assigned one here, then automatically, whoever had just one failed attempt will now have two against their record. And to prove it to you, you can see right now Dave has just one failed login attempt, right? If I came here right now and I change zero to one instead and I run the program, look at that. Dave now has two that's why we made this 10 first, because we know that on line 23, on the very next step, one will be added to that user's record regardless of whether or not it's their first time or their second time. So hopefully, you understand what we did there. Thank you for watching, and of course, I will see you in the next class. 39. Authentication system project: Alright, so welcome to the programming challenge where we are going to create for ourselves a very basic authentication system. We're going to apply everything we've learned so far from variables to functions, to loops, and of course, most recently dictionaries, alright? Now, I have already written out the program. This is the output. So the way the program is going to work is like this. At the very beginning, the user will be presented with three options to either register, login or simply exit the program. Now, if they say three, it will immediately exit the program and will end the entire authentication system. However, if they were to go with one and press Enter, now they'll be X provide a user name. I'm going to go with John, let me provide my password as password, and there you go. Registration will be successful. Now, if I was to go and type in one again, and I tried using the exact same username John to create a new account, the system will check to see that, Oh, John already exists, try a different username. So our program is going to check to see if the username already exists. Now, for number two, if we try login in, it's going to ask for the user name. Now, if I say John, right, and then I provide the right password, you can see right now it's going to say login successful. Welcome back. Now, if I tried logging in again with the exact same username, John, but this time I used the obviously wrong password, it's going to say invalid username or password, please try again. So this is going to be our basic authentication system. This is how it is going to work. And what I want to do right now is to bring over my Notepad file in here just to kind of give you an idea of what we're going to be working with. So obviously, we're going to need a dictionary that will store the user credentials because we're going to have to compare and see if the new username that the user wants to input has already been created before, if the password is correct, things like that. And we're going to be using a dictionary because we'll have to create the key value pair, using them and password ware, using them will be the key, and then the value of the key will be the password. So dictionaries are going to be useful here. We're going to need two different functions. The very first function will be to register the user, and then the other will be to log in the user. Now, for the actual function to register the user, what do we need? First of all, we are going to have to ask the user to provide the user name and then we'll have to have some sort of an EL statement that will check to see if the username already exists. If it does ask for another user name. However, if it's a brand new user name, then the program can proceed to ask for the password. Once the user has provided both username and password, we will then have to create the username password key value pay. I'm going to show you how exactly to do this. Once this is done, we can then say that the registration has been successful, so pretty straightforward. For the function to log in the existing user, again, we will have to ask the user to provide the username and password. And then we would have to check to see if the username matches the password that's already been stored in the dictionary. If it's false, then we're going to print invalid username and password. Otherwise, if it's true, if the password and username match, then we can say login successful. And then finally, the third stage will be to create the actual authentication system where the user will be presented with three options to either register, login or quit. And depending on which option user chooses, we would have to call the relevant function. So we're going to build this authentication system from the ground up. Are you excited? I hope you are. Let's jump right in. 40. Step 1 build the registration function: So the first thing we're going to do will be to create the function that will register the user. However, let us create a dictionary that'll be used to store the user credentials. So for the dictionary, we can call it user underscore credentials. All right, equals, and then we're going to have our Colly braces. All right. Now for the actual function to register our user, in fact, let me just add the note there real quick. Function to register user. Okay. Let's define the function. I'm going to go death and then let's say register underscore user. Call on. Now, what do we need? First of all, we need to ask the user to input the user name. We can assign that to a variable. Let's call the variable user name equals. Now let us prompt the user to provide the user name input, and then we can say enter your user name. Okay. Just as an example, let's press Enter. Let me leave some space in here between the colon and the codes right there to have some space in. So right now, we need to check to see if the username that the user provides actually already exists. Let me just add a node in here on a separate line. Let's add the note, check to see if username already exists. So how are we going to do this? Well, we can do so by using the If statement. I'm going to say if user name and now in the user credentials. Again, Python has already given us the code. If user name in the user credentials, Colon, let us print username already exists, please choose a different user name. I think I like this. However, I'm going to remove the return. We don't need that. So however, if the username doesn't exist, if it's a brand new username, we can then add our s. What else do we now need to do? We need to ask the user for their password. I'm going to say else. Let us create a variable password and then assign that to the inputs and then in brackets, let's say, Enter a password. Call on, and then let's add the closing code, and there it is. From here right now, the next step that we'll have to do is to create the key value pair for username and password. Now, how are we going to do this? Well, we're going to do so by calling the dictionary user credentials. Okay. Now check this out. I'm going to open up my brackets and I'm going to provide the key which will be user name and now equals to password. This is how we can create the key value pair. You provide the name of your dictionary and then in brackets, you provide the name of the key equals to its value, and there it is right there. Then the final step will now be to simply print registration successful. And there it is. Now, let us try something. I am going to call this function and let's see if it actually works. I'm going to say register user, brackets, colon, and there it is. Let us try this. So we don't need colons in here. Apologize. We don't need colons right there. Let's run the program. All right, enter use a name. I'm going to say John, let's enter a password. Let's just say John for the password. John, Enter once again and it says registration successful. However, notice that the program has ended, and that's because we don't have an actual loop that will continually run to keep asking the user to provide a new user name, a new password. What can we do to run this program indefinitely? Well, we can introduce the while true loop. Over here, just underneath the register user function, I am not going to call in a loop that says while and then true. However, indentation is going to be very, very important. I am going to tab all of this to make sure that everything falls under the wild true statement. Look, let me add my column right there, and there it is, now we're going to have the program running continuously. Let's check to see if it will actually check if the user name we provide already exists. Let's run the program again. All right. So this one I'm going to go with Mark. Password is password. Okay, fine. Registration successful. Now you can see it's asking for another user name. Let's try Nancy, Password, let's go with SOD, Fish. It's working. Now, let us try using Mark again, Enter and now it says, username already exists, please choose a different user name. Awesome. We can see right now that the first part of our program, the one that checks to see if the one that registers user and checks to see if the username already exists, you can see right now that it's working perfectly fine and that is awesome. Join me in the next video where we're now going to move on to stage two, where we're going to create the function to log in the user. 41. Step 2 Build the login function: Welcome back. Now let's move on to the second stage where we're going to create a function two, log in an existing user. What I'm going to do is I'm going to push the function here all the way down to the very end and outside, let's add a new notes that says function two, login the user. What do we need? Let us first of all, create the name of the function that will be def, let's say, login underscore user. And let's add colon. What are we going to need? We're going to need both the username and the password. Let us say username equals and now input, we're going to prompt the user to provide their username. Enter your username. Colon, let's add some space right there. Okay, next will be the password, I'm going to say password equals and then enter your password, produce straightforward. We're going to have to now check to see if the username actually matches the password that we had in the registration section. I'm going to add my note real quick. Check to see if user name and password match. So how do we do this? The good old if and now user name and user underscore credent sorry about that. I use a name in user credentials and because both username and password have to be checked and user underscore credentials and now the brackets, use a name equals to the password. See what we did here, we're checking to make sure that if the username that's already been stored in the dictionary user credentials and now the key value pair for the username and password, if they actually match, then we can print, I'm going to say print let's just say, welcome back. A very, very, very simple. Else, if it's not a match, we can print. Now we can say, invalid using my password, please try gain. All right. There it is. Pretty straightforward. What I'll do right now is I'm going to call both of the functions register user, and then also log in on the score user. However, we need to pay very close attention to the fact that the loop currently resides inside of the register function. It is not outside. We need to run our loop independently of the register function and the login function. What I'll do is I am going to cut out the wild true cut it out, and then I am going to paste it outside on its own while true. However, everything else must fall under it. I'm going to tableate everything under let me do this again and there you go. There it is. You can see right now that the user credentials and then the wile loop are outside independent. Now, the Wil true, the entire loop begins with registering the user and then logging in the user, checking to see if there are matches, things like that. This should work. I'm going to go ahead right now to run the program and let us see. I'm going to provide the first user name. Let's go with Mark and then password will be admin register successful. Now let us try to log in as Mark. I'm going to say Mark and then admin Sorry, Mark and then Admin, and there you go. Welcome back. It works successfully. Now it's gone back to the registration phase. If I try to now create another account with Mark, it's going to say username already exists. Please choose a different username. Let's go with Nancy. Password for Nancy, let's go with password. Okay, now you can see it says, sorry, invalid username or password, please try again. The reason why this happens is because the way the program is written out right now, it simply goes step by step by step by step regardless of whether or not the previous step was successful. Over here, you can see it set off with the registration phase that worked. I then moved on to the login phase that worked. It now came over here for the login phase again. Sorry for the registration phase again, I saw that Mac already exists as a user. So it said please chose a different user name. However, it keep asking the user to create another sname over here, it went straight to the login phase where it check to see if Nancy existed. Nancy did not exist. That's why it said invalid us name of password, please try again. Right now, it has gone back to the registration phase. If I say Nancy again, now you can say it's asking Nancy for the password because it's in the registration phase. Now by typing Admin, it says register successful. Now let us try logging in because Night has gone to the login phase. I say Nancy Nancy's password is admin, but I'm going to type in something different, Enter, and then it says Invalid us name of password, please try again. We can see right now that the program works like 80%. The registration phase and login phase work, however, if we were to ideally, when we're trying to create a new user account, and if we are trying to use a username that already exists, the program should not move on to the second stage, which just to log in the user until the user has actually provided a brand new user name for the registration. We're going to fix that in the third and final part of this assignment. 42. Step 3 creating the authentication system: I'll come to the final part of our assignment, and that is to now create the actual authentication system. That will prompt the user to either register, login or maybe just even exit the system. Now, what I'm going to do, first of all, is I'm going to remove the functions in here, register and login. I'm also going to remove the while loop right here because we're going to run that in a different location. Now I am going to change the indentation and make sure that the functions register user and login user on the same line. This right here should be under the Login user function and there it is. Let us begin by creating the authentication system. I'm going to add my notes and call this one the main menu. Now we can define our function in here, authentication underscore system. And there it is. What do we need here? We need to prompt the user to either register, login or exit. We're going to print out a couple of statements. Let us begin by printing out and I'm going to say, let's say basic authentation system. This will be at the very top. Then next, let us print out the very first option for the user. We can say one register will be the first option and then print option number two, this will be two dot log in. Then the final option in here would be three and then let's call that one exit. There it is. We've got our three options in here. Now, the user will have to add either one, two or three. Let us create a variable that will represent that particular option. I'm going to call this one option, option is going to be equal to input, and now let us ask the user enter your choice. We're asking the user right now, Hey, provide us with what you want to do. We're going to have to create the if scenario where we'll check to see if the user chooses Option one, which is register, we can call the register user function. If they choose option two, we can call the login user function or if they exit, we can simply end the loop. I am going to say right now that if option right here equals, and now I can say one. Colon. What do we do if this cheeses option one? Well, we can call the function register underscore user. Very, very straightforward. Now, if which is going to be if. If it was option two, what are we going to do? We are going to call the function login underscore user. Now for the last one, if option equals three, what do we do? We can print. Then code, we can say exiting the system. And there it is, and then we can say else if the user maybe provides option four, five, six, which don't exist, then we can say, else, print and then in brackets, we can say invalid choice, and then we can say, please choose from options one, two, or three. There it is. There it is. Now finally, we can run the authentication system we can do this by simply saying authentication system, and there it is. There is one thing that we haven't added just yet. I don't know if you can spot what it is, but that is the loop. Because right now as it is, this program will run just once, but we want the user to keep on either logging in or registering or exiting. What do we do? This is where we will have to call in the Wild true loop. But where do you think we're going to put it? We are going to put it just underneath our authentication system function. Here is where I'm going to say wild true. Now remember, everything in here will have to fall under the wild true statement. So Make sure your identation is correct, and then we're going to have to add the break statement as well to end the loop. Now, where are we going to end the loop we end the loop where the user chooses option three, which is to exit the system. Just underneath in here, underneath print, I'm going to say break and there it is our program hopefully should work without any issues. Let me go ahead right now and run it. We do have an issue right here and that's because the break is outside the loop and look at that. Oh, wow, indentation is so important. You can see right now that if all of this isn't the same indentation as the while statement. So, let us go back over here and indent them properly and there it is. You can see even, I make mistakes with my indentation as well. So let's make sure that everything else is indented properly. Let's try running the program one more time. Thankfully, it works now. Let us test it out. I'm going to go with option number one. First of all, let us register Mark and his password is admin. Awesome. Now let us try going to option number two to log in, use the name. Let's try Nancy as an example, password, let's say, Bob. But now it says invalid name, using the name of password, please try again. It's obviously working really well. Let's try again to register this time as Mark once again check to see if the program will detect that Mark already exists, so Mark and it says, Okay, name already exists, please choose a different user name. Okay. Let us try login in this time as Mark. Let us provide the password which is admin and it says, welcome back. I actually worked. Let us try login again as Mark. But this time, let us use a different password, a long password and then it says, Invalid user name of password, please try again. It appears it is working properly. The final test is to exit, enter, and there you go. Exiting the system, the loop has ended, the program has ended. There it is. We have successfully created a program, which is a basic authentication system that would allow a user to either register login or simply exit the program. Of course, we've added checks to ensure that no duplicate accounts can be registered. We've also added checks to ensure that user names and passwords will match, and there it is. Congratulations. I hope you enjoyed this assignment, this challenge. Thank you for watching. I will see you in the next class. 43. File handling section intro: Welcome to this section where we're going to be talking about file handling, and believe it or not, this is officially the beginning of the more advanced topics on the Python programming because in here, you're going to learn how you can write programs that can create files and even modify them. And perhaps even more importantly, you're going to learn exactly how to handle file errors. So say, for example, something happened in your program, it didn't quite work out the way it was meant to work out. You're going to learn exactly how you can modify or design your program in such a way that these kinds of errors are handled in a professional manner. So this is going to be a very, very interesting section. And, of course, you're also going to learn the different types of methods that we can apply to our files, such as how you can read from a file, or maybe even write to a file or maybe even append or modify a particular file. And of course, you're also going to learn about handling the file errors which I've talked about already using the multiple except blocks and also the finally statement as well. So without wasting any more time, let's jump into talking about file handling. 44. Reading and writing to files: All right let's talk about file handling and right now as it is, the only file we have been working with is the main dot py file. It's just one Python file and nothing more. However, we could create other types of files. What if we wanted to write a program that would ask the user to provide their username or password, and then we store that user information in a separate file. How can we do that? Well, we can do that in a variety of ways. That's what you're going to learn here. The first thing I want to show you is how to create a file. There is a statement called with and then a function called open brackets. What this will do is that the open function typically accepts two parameters. You will have to provide the name of the file that you want to work with, and then the second parameter will be whatever it is that you want to do to that file. Maybe you want to read to the file, write to the file, append the file, and so on. As an example, let me say, I want to open up a file called example dot TXT. It's a text file now comma, and now I have to provide the operation. Do I want to open this file? Do I want to read the contents of the file? What do I want to do? I want to write to the file, so I'm going to use W to represent the right function, the right operation. And now to finish this, I'm going to say a file. This right here, this is typically the syntax. You will say with open and then in brackets provide the name of the file, and then the operator relates W for write, R for read, and so on and then say a file. Now I'm going to jump over to the new line and say file dot w. This is going to be the function that we're going to use to actually write the string or whatever it is that we want to write to the file. In brackets, I'm going to say hello world. This is what I want to write to the file. Now, check this out. I'm going to go ahead. Now notice so far I still have only one file, the main dot PY. But if I was to run the program, you now see it says example dot. Even though I didn't have this file created before, whenever you're writing to a file, if that file hasn't existed before, then Python will automatically create that file for you. Now, if I was to open up my example to text, you can see right now it says hello world. What if I come over here right now and I write another right function and I say, this is awesome. What do you think is going to happen? Let's go ahead and run the program. If I go back to my example, doc TXT, now you can see it says hello world, this is awesome. Now it has added this is awesome to our file. Let me go back and close this. Let me remove the second file function. Now, we can see that hello world is in fact in example dot TXT. But what if in our console, we wanted to display the contents of example dot TXT. What I'll do here is I'll say with open again and now in brackets, once again, the name of the file, example TXT. Now we're going to use the R function instead. This is what we use for reading the file. I'm going to say as file, colon. Now, what if we wanted to print out the contents of the file? I can assign a variable called content equals, and now I'm going to use a function file dot Red. This function right here file dot red we read the contents of our file. It's going to pass the contents to the variable content. Now all we need to do from here will be to simply say prints and then in brackets, what are we printing, we're printing content. Okay. Now I'm going to run and there it is right here in a console, you can see we have hello world being displayed. This is basically an introduction to reading from files, writing to files. One question you may have is, okay, can we actually create our files without using the with statement? The answer in fact is yes, we can do so without using the with statement. How would we do this? Well, let me just first of all, remove the programming here. What we need to do, first of all, is to say file equals, and then open. This is going to be the actual open function itself. Remember that open accepts two parameters. Now we'll have to open up our example dot TXT, now what we want to do we want to write to the file. I'm going to say W. Let me just remove let me delete this example dot text file. Let me delete it. We're starting from the very scratch. Now, I have told Python, I want you to create this file called example dot TXT. What do we want to now write to the file? I'm going to come in right now and say file dot write. Because now I'm writing into the file and then in brackets, let's provide the string hello world. Now what I will have to do is I'm going to have to close the file. When you're opening up a file or you're reading into a file or you're into a file, by default, the file needs to be opened in order for that operation to occur. What you always want to do is you want to ensure that the file is closed once the operation has finished. If you don't do this, this can lead to programs leaking out memory and it can be very vulnerable to different types of attacks. So to close the file very simply, file dot W dot close. And that's it. Now, if I run the program again, you can say example that text is over there, I can open up the file and it's right the hello world. The reason why we prefer or most developers prefer to use the W statement is because with the With statement, let me just undo all of this. With the W statement, you will notice that we didn't actually have to close the file manually. Python is smart enough to know that, when you're using the W statement, as soon as you've performed whatever operation it is that you want to perform on the file, close it automatically. That's why developers often use the with statement. You don't have to close the file anymore because Python will do so automatically. Plus, it's also cleaner, you have less lines of code. That's why the Wi statement is typically used in conjunction with the open function. Thank you for watching. I will see you in the next class. 45. Reading and writing inputs to text files: Let's now talk about how we can read from and write to text files. Say, for example, we wanted to create a program that will accept an input from the user. Let's say the user has been asked to provide their username and then we want to store that username on a separate file. How can we do this? Well? First thing first is, let us assign a variable that will accept the input from the user. I'm going to call my variable username equals and now the input function. Now enter your user name, pretty straightforward. Now remember from the previous lesson with open Syntax, I'm going to say with and now open and of course, remember open will accept two parameters. First of all, the file you want to work with and then the operator, I'm going to create a file called users dot txt. And then we're going to add the W functional idea to write and then I'm going to say a file. Now what do we want to do? We want to write the username to the file. What I'll do right now is very straightforward file dot, write. Now in brackets, user name. Then just for good measure, we can print out something after the user has provided the username. Let's say print and I will say user name has been added to the file, just as an example. Let's go ahead right now. Run the program. Enter your username, I'm going to say Alex, press Enter and now it says username has been added to the file and you can see users textiFle has been created and if I open it, you can see it's right there Alex. So far so good. However, if I go back and I run the program again, and this time, I used a different username, I said Alice, I press Enter. If I go to my users or text the file, you can see right now that Alice has overwritten Alex. That's because whenever you're accepting inputs from your users, you're storing them in a text file. When you use the right operator, it will simply overwrite whatever it is that existed in that file previously. What if we don't want this? What if we just want to keep on adding files or user names to our file without overwriting the previous ones? How do we do this? First of all, let me clear my history, remove our users dot text the file. Let's pretend like we're starting from scratch? Instead of using the W function, I'm going to use A, which represents a pen. With a pen, we can keep asking the user to provide a new user name and once they keep on adding new user names, they will be stored in our file and the previous user names will not be overwritten. I'm going to run the program again. Provide my username, Alex. Now you can see we have our users dot txt file, Alex, just like we did with the right operator. But now if I go back, I run the program again and this time, I say Ale. What now happens, I go to my user dot textifle and then you go, we have Alex, we have Alice. You can see Alex was not overwritten. By the way, if you wanted to store your usernames in separate lines, all we need to do right here is we're writing the file username. We can just say plus now check this out. You want to say forward slash, I'm sorry, backward slash and then N. So this right here, this is what we use to create lines, whatever you're adding will be added onto a new line. So what I'm going to do here is let us remove the users DotexiFle again. Let me run the program one more time. Alex, press Enter. Let's run the program again. This time, I'm going to say Alice. Press Enter. Now if I open up my users do textiFle, now you can see Alex and Alice are on separate lines. This right here is the difference between your right operator and your append operator. But what if we wanted to read the data, the user names from the file? How can we do this? Well, I'm just going to come down in here, say with open again, brackets, and I'm going to say users TXT, then what are we going to do? Codes R. Then as file, Colm now we want to create a four loop. I'm going to say four name for name in file. For the user names for the names in the file, what do we want to do? We want to print. I'm going to say print and then we can say, use a name. Colon and now I'm going to add coma and now name dot strip. This is a new function and it's going to have its own empty brackets and there it is. Now if I run the program, Alex, and there you go, use them as being added to the file. Now because of our function over here, we're reading the file, it's going to say user name, Alex, user name Alice, and then user name Alex again because the user textiFle has Alex Alice Alex. This is how we can read the contents of our file. By the way, the dot strip right here, this is a method that's very often used whenever we are handling files. Now, we use it to remove any leading or trilling whitespace characters a string. This may include things like your spaces, stabs, or even new line character. It's just a very efficient way of ensuring that whatever text we are outputting or printing out, it is in the most efficient manner without any unnecessary space. That's why strip is very often used whenever we're handling text files. Now, it is completely optional. You don't have to use the strip method, in fact. If I remove the strip method and just closed my bracket normally, if I run the program, it'll work the exact same way. Let me provide a new name. Let's say Mandy. As an example. There you go. We have user name Alice, sname Alex, name Mandy there it is, it's still going to work. But you can see right now that we do have the extra space, the lines between sname Ale, username Alex, use name Mandy because we are using the new line character, the new line string here to add each input on a separate line. That's pretty much it for reading and writing to text files. Thank you for watching. I will see you in the next class. 46. Read and write methods: Well, come back. Now, before we move on, I wanted to let you know that there are different methods for writing and reading our files. Now, so far, we've been dealing with the file dot method, which is, of course, very straightforward. So if I run the program right now, it's going to create my example dot THD. I open it up and there is Hello World. Now, file dot write is useful and it's used whenever we're writing individual or very simple strings to our file. Hello world is very straightforward, it's very simple. But what if we wanted to write multiple strings or we want to write large amounts of text. File write will no longer be used. We'll have to use a different method. As an example, if I had a list of names, let me create my list names. Let me add the first name, Alex and let me add the new line character, slash let me allow Python to add the remaining names. We have Alex, Bob, Caroline, Dave, and Fred. If I wanted to write these strings, I can no longer use file I'll have to use a different method. What I'll do right now is I'm going to go with the usual width, open, and then brackets and then remember, we'll have to open up our file example dot CxT then W of course two, write we have our colon and then I'm going to say, sorry, as file and then our colon. Now you can see file dot right lines and then our names. Write the file dot write lines, this is used whenever we're writing multiple strings or we're writing large pieces of text onto our file. If I run a program right now, and I open it up then you go, we have the names right there. Please do be aware of this. In the future, if you're going to create advanced programs that will require writing large amounts of text or strings to particular file, you want to go with the file write lines as opposed to the usual file dot write. Just like with writing, we also have different methods for reading. Now, so far, we've been using the very simple read the file dot read let me just copy some code in here. Let me pass this in here real quick. So far we've been dealing with code like this. We'll have file and then we'll say content e calls file dot read and there it is. If I run a program right now, very, very simple, we have Alex Bob, Caroline, Dave, and Fred. Okay. Thing about using file dot RED is that it's going to read the entire file at once, which is great whenever you're working with very small files. However, if you're dealing with large files, it will still work. Don't get me wrong. I will still work, it's still going to read everything. The problem though is that because it's going to read the entire file at once, it will have to use a lot of memory in order to load all the text in that file at once. That's the problem. It's not very efficient whenever you're dealing with large amounts of text in a file like big files. You want to use a different method. Now, let me show you. I'm going to create a new program right here and say with open. Now let's assume that example TXT, in this scenario has large amounts of text, it's a very large file. I'm going to go state the usual out, add our filename and then R and then say a file. Now, check this out. I'm going to say line equals file dot read line. Notice the difference. It's no longer file read, it's now file dot read line, and then I can create my loop and say while line. While our file is open, it has text in it. We want to print the line. We can also then say line equals file dot read line. And here it is. If I was to run the program again, you will see right now that we have just about the exact same outputs. You're not really going to notice the difference because we're dealing with a small file regardless. However, please do keep in mind in the future, just like with the right method, if you're going to be dealing with large amounts of files that you need to read from, you want to use dot read line. If it's a very small file, file dot Read would be used. It's ideal for those scenarios. However, we are not done. There's another method which is the file dot read lines. What this does is that it's going to read all lines at once and we'll return them as a list of strings where each element is a line from the file. In other words, it combines the benefits of both your read and your read line by reading the whole file at once, just like the file dot read. However, it's going to store each line as an individual element in a list. So let me give you an example. I'm going to go back in here. I'm going to modify this to change line two lines. Equals file dot read lines. Now instead of using the Wil loop, I'm going to switch this over to the four loop. I'm going to say four line in lines, colon, and now we can simply prints in brackets line. Or better yet, let's add the strip method in order to remove any unnecessary spacing. There it is right now, if I was to run the program, there you go. Alex, Bob, Caroline, Dave, and Fred. Again, you're not going to notice the difference when you're dealing with a very small file. Once again, do keep in mind that if you're dealing with large files, you want to go with either the read line or the read lines. The last method I want to show you is going to be the for line in file. The purpose here is to read the file line by line in a memory efficient way. I'm going to go back in here. Let me remove the lines. We have with open example dot TXT, RS file. Now I will say for line in, I could say file, For line in file, now simply print line dot strip and never to run the program, there you go. This approach is actually very similar to using your read line, but it's actually more concise and considered the best practice for reading large files because each iteration retrieves a single line, so memory usage is going to be low. To summarize, if you want to read from a small file, the usual read our file read will be the best. If you're reading from a large file, you can go with your line in file. It's better than your file dot read line, and also better than your file dot read lines. Now, I also just want to point out that in addition to the modes for your read mode, your append mode, your write mode, we also have additional modes like your read and write mode, your write and read mode, as well as your append and read mode. As an example, instead of R here, I can say R and then plus. This is all referred to as the read and write mode. This is going to open up a file for both reading and writing. Keep in mind though that the file must exist and content can be read and modified. Your file must already exist in order for you to be able to use this particular mode. We also have the write and read mode, which is going to be W and then plus. This right here will open a file for reading and writing but it was going to replace any existing content. Then finally, of course, you have your Append plus mode, which is the append read mode. This will open up a file for reading and writing, appending new content at the end without changing any existing content. You're more than welcome to play around with these modes and see how they modify your file. Thank you for watching. I'm going to see you in the next class. 47. Handling file errors: As you continue to progress in your journey as a Python programmer or as a programmer in general, there is a habit that I want you to adopt and this habit is going to involve writing programs or designing programs that are able to handle potential errors in a graceful manner. See the thing is, most programmers out there, they create all these great programs that can do this, do that. However, if something goes wrong, maybe the user of the program added some type of an input or maybe they used numbers instead of letters or something like that. Many times these programs end up crashing because the programmer hasn't designed the program in such a way that if such potential errors do occur, the program knows how to handle it. We have talked about handling errors briefly when we talked about loops, but now we're going to go a bit more in depth. On my screen right now, I have a very simple program that reads the file, the example dot TXT. But note, however, that this program, the short block of code, it relies on this file, example dot TXT actually existing. What if I maybe made a mistake or the use of the program the use of the program, made a mistake. Instead of example dot TXT, we have example dot TXT. Now, this file doesn't exist. It's not in my directory. If I run the program, now you can see we have an error. Python is telling us that, hey, sorry, no such file or directory, file not found error. This doesn't look good. We want to write our programs in such a way that if such an error does occur, it's going to handle it in a very graceful and professional manner. What we want to do right now is let me go back in here and add example dot TXT. We're going to use the Try except block. We've talked about this when we talked about loops, but let's talk about it again. What you want to do is in the block of code where there is a potential for the error to occur, such as over here, one thing you want to do is you want to start off by saying try. Try and now make sure it's indented properly because the code after try should be under it. Now we're saying, Okay, try to open this file example dot THD. If it exists and all that, you can print out the contents of the file. However, what if the file does not exist? Now we can say except and then under except, we can say print, and then we can say something like file not found. Let's add a column right there for except. There it is. Basically, we're saying, Hey, try to run this block of code if for one reason or the other, the file isn't found and simply printout file not found. If I run the program right now, everything works properly because the file does exist. However, if I change the name of the file and I run the program, you can see very professionally it simply says file not found. That's because we're using the try except block. While this works well, to go deeper, I want you to start using what we call the common file related exceptions. There's quite a variety of them. Python has specific exceptions for many of the common file related errors. The first one and the most common one is the file not found error, just as we have over here. All you need to do is instead of just simply saying try except where you have the except, you can now type in file not found error. This right here, we're telling Python to specifically check to see if the error is caused by the fact that the file was not found. If I run my program again right now you can see it still works perfectly fine. However, this is a much cleaner and more efficient way of handling the error because over here, we're specifically checking to see if the error occurred because the file doesn't exist. The try except block on its own handles all errors. Whether the file wasn't found whether it's a permission arrow or maybe it's a directory arrow or maybe it's an open system error, it will handle everything. So yes, it will work quite all right. I will handle the arrow. But again, a cleaner way will be to specify the kind of error that you're actually expecting. So we have the file not found error. We also have the permission error. Maybe, for example, the file in question is, can only be accessed by an admin. Now it's trying to be accessed by a regular user. Obviously, you're going to have the error saying, Hey, sorry, you don't have permission. In this scenario, all we simply do is we'll just say, you know, sorry, you do not have permission to view this file, and that's all we would need to do. So if you're expecting a permission error, you want to go with the accept permission arrow block of code. We also have a directory arrow. Maybe the user was trying to open up a directory that doesn't exist again. Simply come in here and then you type in is a directory error, and there it is. And now from here, we can specify the exact print message. So in this case, it will be something like Sorry. You don't have permission to access this directory or SOI. This directory doesn't exist, something like that. Then the last one is going to be the operating system error. This is typically the general exception for other file related errors. Maybe the disk is full, maybe it's an input output issue, something like that. So all you need to do right now is just say except, and then O error. Then you can just type in something like, sorry, an error has occurred or an open system error has occurred, something like that. That's exactly how that would work. Once again, you want to adopt this professional approach towards constantly making sure that your blocks of code whenever there's a possibility of an error occurring, they have been designed in such a way that they're able to handle any potential errors by making use of the try except block and then for the accept statement itself, you can specify the exact error that you expect might occur. Thank you for watching. I'll see you in the next class. 48. Multiple except blocks: Welcome back. In the previous video, we talked about handling arrows and also the different types of specific except file related errors that we should be working with. However, what if we wanted to check for multiple types of errors? Maybe in addition to the file not being found, it's possible that the file was found then maybe the permissions for the file are not correct or maybe the directory wasn't correct, something like that. We want to create a program, a block of code that can handle multiple types of errors and also print out different error messages for each error. As it is right now, I have one error which is the file not found error and it's going to print file not found. But what if I wanted to include the permission Al as well. It's very, very straightforward. All I will need to do is just simply open up another block and then just say accept and then permission Al add my colon and now I can print in brackets and then say, sorry, you do not have the permission. To access this file. That's all I got to do. Let's add one more arrow, so I can say except, and then I can say OS arrow. Maybe there was a problem with the opening system, and then I can my colon and then I can say print and then in brackets, there is an OS arrow. Now, what if I wanted to add one more block of code that will handle any other potential type of arrow out there? All I need to do is to again say except now the key I'm going to say exception as E. Basically, this line will handle any other type of error that could potentially occur. Then I'll just simply say put out a general message and say print, and then I'll just say an unexpected arrow as occurred. Now we want to stress the E here. This E is going to represent the arrow. What I'm going to do right now is I'm going to use F string as usual. And then curly braces, I'm going to add the E right there. The last line in here will handle any other type of error and the system would say an unexpected error has occurred and then you'll specify what type of error because of the E variable in here that we created over here and that's it. I'm going to run my program, and of course, you can see, it's going to work perfectly fine. This is how you can create multiple except blocks that will handle multiple different types of potential errors that might occur. For watching. I will see you in the next class. 49. The finally statment: When it comes to opening files with Python, remember that we don't always have to use the with statement. The reason why we use with most of the time is because with with statement, automatically, the file will be closed once it has been accessed. In this scenario right now where we're simply working with the open function to open up the file, we have to manually close the file by saying file that close. Otherwise, the file will remain open and that could lead to potential errors. The question right now is, how would we handle for errors in this particular scenario? Well, we'll have to make use of the finally block. Now, at the very top, I'm going to start off with try as usual, and then remember, of course, everything has to fall under try. Now on line five, this is where we're going to add the accept. Now let's look for the file not found error. Colon. If the file has not been found, we can simply print file not found. However, what if the file was in fact found, there is no error. What we're going to do right now is we're going to say finally. Now if file, hold on. Now we're saying, finally, if there was no error message at all, if the file was in fact found, what do we now do? We can simply print out the content and now we can also close the file. In fact, for good measure, not necessary, but for good measure, we can just simply print file closed, just as an example. Of course, if I run the program right now, you can see it works perfectly fine and it even says file closed. This program on the surface right now looks good. There were no errors, the file was found, it's printed it out, and of course, the file has also been closed. However, we're not done yet. Because in fact, if I was to deliberately change the name of the file so that we do get the file not found error, if I run my program, Oh, you can see right now, we're still having the usual unpleasant way of displaying or handling errors. It's like the try except block of code did not work at all. What is going on here? Another thing you need to keep in mind is that if you're not working with the width statement, not only would you have to manually close your file, but you also have to initialize the file at the very beginning. Because right now as it is, Python doesn't even really know what file itself is, what's the default value of file? A file isn't equal to example that takes if the file wasn't found, how would Python know that, there's actually an arrow in here, what will be the default value that the file variable needs to have? This is where we will have to at the very top, say file equals none. Right now at the very start, we're initializing the file variable, we're assigning it to the default value of none. This is a common practice when dealing with resources that may or may not be successfully allocated. Using non here would allow for a very safe default value that will prevent any accidental operations on an uninitialized variable. This is another thing you have to keep in mind. Right now, if I run the program one more time, it works well. If I change the name of the file deliberately to produce the error message, you can see right now it handled the error gracefully. This is exactly why when it comes to handling files, we typically use the with statement because with the with statement, you don't have to initialize your variables or your files and you don't have to close them manually either. The W statement is so powerful, it will automatically initialize our files and variables and also close the file automatically once it has been accessed. But nevertheless, you should be aware of this if you find yourself scenario or any programming code where the width statement wasn't used, how would you handle errors? How would you close the file manually? Of course, how would you also initialize the file manually? Thank you for watching. I will see you in the next class. 50. Libraries and modules section intro: Welcome to the libraries and modules section. And, of course, in here, you're going to learn how to work with them. What exactly are libraries and modules? Think of them as basically programs that have already been written by other Python developers. Keep in mind that the Python community is a big one. You have developers who have written their own kinds of code. Maybe there's a programmer out there who has already written their own code on how to encrypt a password. So instead of you having to write such a program from scratch, you can simply import that program onto your own program. And then modify to do something else. So that's kind of the whole point of working with libraries and modules. So in this section, you're going to learn the different types of libraries and modules out there, the differences between libraries and modules. And of course, we're also going to have different challenges and coding exercises as well. So without wasting any more time, let's jump right in. 51. Introduction to libraries and modules: Welcome to a brand new section, and here we're going to be talking about libraries and modules. So what exactly are these? Let's paint a scenario, right? What if we wanted to write a program that will hash plain text, right? So, say for example, we wanted to store passwords in a file. Obviously, we'd want to hash them. We don't want to store passwords in plain text. So how would we write a program that will hash these passwords? There are two options, okay? We could either decide to write the program from scratch, which might take a lot of time or very simply, we can use something called a module that already has the code that we need to hash our text. So a module is typically a Python file that already contains code. It could be functions, variables, classes, statements, you name it, that serves a specific purpose while a library, as you might now have imagined, is often a packaged group of these modules organized around a broader goal. So you can think of modules as very specific functions that serve a very specific purpose, while a library would be a collection of such modules. As an example, we do have the datetime dot pi. This is a module that has functions specifically for walking with dates and times. We also have a library called the HH Lib, the HH Library, which would be the library we would have to use in order to hash our plain text. Now, what do we have in the hash library? We have different types of algorithms. We have the MD five, that'll create a 128 bit hash. We have the Shall one, that'll generate a 160 bit hash, Shaw 256, which will generate the 25, six, and, of course, the Shell five and two, which will generate a 512 bit hash. There are also other types of functions within this library like the hash leap dot u, that will accept two parameters, name data which we can then use to create a hash with a specific algorithm name. So moving forward, libraries can either be built in. We call them standard libraries. This will come default with your Python installation or we can also use libraries that are external. We refer to them as third party. These libraries don't come with your Python installation, so you'll have to install them manually yourself, and they're developed by the Python community. So one more time, modules have very specific code that serves very specific function while libraries will be a collection of these modules. One other thing I want you to realize is that within the Python community, you may also hear of another term called packages. Packages are somewhere in between modules and libraries in that they are also a collection of modules, but the modules tend to be very closely related in purpose. They're not quite as broad as libraries. So for example, you can have your email package and in the email package, you will have modules like email dot message, your email dot mime, your email dot Utils and so much more. Another advantage of packages is that they can allow us to organize these modules in a hierarchical structure by using folders and sub folders. So each package typically will contain the int PY file. Which tells Python that, Hey, this particular directory, it's not a library. It's actually a package. This is how we can help Python differentiate between the package and the library. The package will have a file specifically called the ini dot py file. So that's it for libraries, modules and packages. German Vinexway we will not begin to work with them. 52. Creating a custom module: Begin working with the standard modules and libraries within Python, I want us to create our very own custom module and then use that module in one of our programs, okay? So this is our main program file in here, the main dot pi. What I'm going to do is I'm going to create a new file, okay, and then call this one math on the score ad. Dot PY. Okay? This is going to be the file that will hold our very own custom module. Now, the module we're going to design will be one that's very simple, is going to take two numbers and then add them together. Okay? So remember how we created our custom functions. We used the fine function. And now I'm going to add the name of the function, which will be add on the square numbers, and then two variables. It's going to take two numbers and add them. We can use any letters to represent them. I'm going to go with F and V, just as an example and then add my column at the end. Now, what do I want the function to do? I want to add F and V. So I'm going to say return the value of what of F plus V. And there it is. We now have a custom module that will add two variables, F and V, and then we turn the result. Okay. If I was to go back to my main PI file now, and I want to make use of this module that we've designed in the method ad Pi file, what I'm going to do right now is I'll have to use the function called Import. This is the function we now need to use to import our custom module. So it's going to be the name of the file, which is math on the score ad. Okay? And now, what do we need to do? We need to provide two numbers. Okay? So I can say number one equals five, and then number two equals seven. Okay? Just as an example. Alright. Now, we want to print out the results of adding number one and number two. So, oh, by the way, do forgive me. There shouldn't be any space between number one and number two. Do forgive me. Since it's a variable, okay? So no spaces in the variable names, do forgive me. So we want to print out this result. So I can assign a variable called result to be equal to what? I am now going to pull in the custom module, which was math on the score ad Okay. And now, dot, what was the function? The function that we had in our method Ad file is add underscore numbers. Okay? So now I'm going to go back in here to my main file and now say add underscore numbers. And now in brackets, what are we adding? We are adding number one and number two. And there it is. All I need to do right now is to simply print out the results. I'll say prints, and then in brackets, let's add a code, and then I can say the sum is, and I can add comma and then simply say result, and there it is. And now if I run the program, there you go, it says the sum is 12. So to give you a quick recap, first of all, we created a separate file, and notice, by the way that the files are in the same folder. Okay, that's very, very important. So we created a file called Mth Underscore ad, and this was going to be our custom module. We defined the name of the function to be Add underscore numbers. It takes in any two variables, F and V, and then we return the value of adding those two variables. So going back to our main file now, the first thing we needed to do was to import that custom module, which is math underscore ad in here. We now need to provide the values of our two variables. I don't want you to get confused. Don't think that, Oh, because in our custom module, we used F and V. Therefore, in the main file, F should be equal to five, and then V should be equal to seven. Why are we using number one number two? Always keep in mind that these variables, these parameters in here, they represent basically nothing. It's just a way to tell the function of AtlPyn that, Hey, within this function, we're going to accept two variables, and then we're going to add them together. We could have used any letters in here. We could have used B, C, Z. We could have used X. We could have used anything. We could have used number one. Number two, we used any name, anything to represent those parameters, okay? So don't think that, Oh, whatever parameters you specify in here must be the same in here, no, okay? All Python should know is that, Okay, this function is going to take in two variables. It's going to take in two numbers and then add them, okay? So in here, we said number one equals five, number two equals seven. And now this is the tricky part, okay? Line six. We want to print out the results, so I created a v called result, which would now be equal to first of all, the name of the module which is math dot add and then dot because we want to append the function that we created. The function here is Ad underscd numbers. So we appended it to math on the score ad, and now in brackets, simply number one, number two, and then the final thing is just print out the results. So we said the sum is and then result and that's how we got. So congratulations. You've written your very first custom module, and you've successfully been able to import and make use of it, as well. Thank you for watching the video. I'll see you in the next class. 53. How to wok with a standard module : Welcome back. So the previous lesson, we successfully created our very own custom module, and we were able to import and make use of it in our main file. But now I want to show you how we can import some of the standard libraries and modules that comes with Python. So let me go ahead and remove all of this code, and I'm going to leave the keyword Import, the main function, and the name of the module we're going to import is called random. This is an inbuilt module within Python that would allow us to make or generate random numbers from a sequence. In fact, let me show you a bit of the documentation in here. You can see Python has a built in module called random. And the thing about random is that it's so powerful, it has so many methods that comes with it. Take a look at the choice method. This will return a random element from the given sequence. I'm going to make use of this particular method to generate a random letter. So check this out, okay? I'm going to first of all, create a variable called letters and then equals two, and I'm going to type in all the letters in here, A to Z, okay? So we want our program to generate a random letter from this list, okay, from this sequence. So, how are we going to do this? Well, I'm going to create a new variable called random dos called Letter, okay? And now in here, I'm going to say random Remember, the sequence, first of all, we'll have to add the name of the module or library, so it's random in here. And now the method, which is choice, is going to be dot and now choice. And now in brackets, letters. That is all I have to do. And now, what do we do we print out and I can say the random letter chosen is. And then I can add my comma and then simply add random underscore letter. And there it is. That's all we have to do. So now if I run the program, there it is. The random letter chosen is M. Okay. Let's try running the program again. Maybe we'll choose a different letter I should. And there you go. Now it's C. Again, now it's A. One more time now it is V. You can see right now it actually works. So a quick recap, what did we do? First of all, we imported the standard module called random that has a list of different types of methods that will generate a random letter. So the first thing we did was that we created a letters variable that will hold all the letters from A to Z, and then we assigned another variable called random letter that will now hold the actual a letter that will be generated. It's actually a random letter that was generated. And then we said random bin the name of the module and then appended the method which is dot Choice, and then in brackets, letters. And now we simply printed out the random letter that was chosen in line four. So that's it, thank you for watching the video. I will see you in the next class. 54. Password generation with random and string modules: Well, come back. In the previous lesson, we successfully learned how to generate a random letter by making use of the random module and also the random dot choice method. A question you might have here is, well, what if we wanted to generate multiple letters and not just one? What we have to do is to first of all, change the method from random dot choice to random dot choices because now it's plural. We're dealing with more than one letter. Another thing about random choices as a method of function is that it's going to accept two parameters instead of one. The first will be what we call the population, which is basically where we're pulling the letters from. And then the second is going to be the actual number of letters we want to generate. So in this case, right now, I'm going to say K equals three. All right. And now I can change the printext from letter to letters and then change E to R. Now if I run the program, we have KJ, I can run it again. We have QMW, right? So you can keep that in mind. However, we want to move a step further to generate a random password. So we're no longer just dealing with letters. We're now going to combine letters with uppercase and numbers and so on. And I'm pretty sure you've seen random password generators before. Maybe it's on the website where you're asked to create an account. You want to create an account, and then they offer to generate a password for you, which you can then change later. I'm going to show you how we can create our random password generator, and we're just going to use five lines to do this. In fact, four lines, just four lines, okay? You don't believe me. Take a look at this. So first of all, we're going to keep the Import random because obviously we want to randomize the letters and numbers, but we're going to import a library here called string because we're going to be manipulating our strings, which will include letters, numbers, and so on. So we have Import string, and now I'm going to create a variable that'll represent the password. I'm going to call it password equals. And I remember from the previous lesson, I'm going to say random, ok dot Choices, because now we're going to be dealing with multiple letters. And now in bracket, remember the first parameter for this function or method is going to be the population. We have to tell the function where to pull the string from the letters from. The thing about the string library is that let me just show you over here, the string module rather, we have a variety of methods, or in this case, we call them constants, okay? So we can say string dot Ask the score letters. This will pull in letters that are both capital, lowercase. And then we can specify. We can say string dot Ask lowercase. This will pull in only lowercase letters, and then we have for uppercase, we have for digits, hex digits, punctuation, and so on. So we have all these methods, all these constants that we can work with when importing the string module. So let me drag this one away. What we're going to do right now is for the random choices, we want to pull string dot ask and now underscore letters. Okay? We're going to pull random letters. And then in addition, plus, we also want to pull in what digits, we want to have numbers in our password as well. So basically, we're telling the function, we're telling Python that, Hey, for the choices of letters, for the password, we're going to pull in letters that are both upper case and lower case and then also digits as well. And then, of course, the second parameter, we need to specify how many letters digits are going to be in our password. I'm going to say K equals, and let's go with ten. Okay? So a pretty strong password. And now, all we have to do is what? Just print. We're going to print. And I'm going to say generated. So generated password. Okay. And then let me add my comma and then password. That's it. We're going to generate the password. And now let's try it, okay? So I'm going to go ahead right now run the program, and there you go. We have K, we have six, we have capital F, we have six, zero Smoleta, H, J, zero, capital W, and then capital. So you can see right now, it successfully created a password with ten characters. We had upper case letters, lower case letters, and a few numbers as well. But what if instead of having the characters separated by a comma and codes, we want to join everything together, so we have just one single string. How are we going to do this? We will have to make use of a special function called the join function, okay? It's actually a method. So what you want to do right now is over here where it says random the choices, we're going to add this very special function called dot join, and it goes like this, okay? We're going to have a single codes, and then dot join, and now we'll have to open up a bracket to cover everything in here, and there it is. So what this is going to do is that it's going to generate the password, but then all the characters will be joined to get there. Now, if I run the program one more time, there you go. Now we have it looking much better. I can run it again, and there you go. Run it one more time, and there you go. So it's kind of amazing how with just four lines of code, we've been able to create our very own random password generator. We, of course, had to make use of two standard modules, the random module and then the string module, and of course, two additional lines of code, and there it is. So that's it. Thank you for watching the video. I will see you in the next class. 55. The datetime module: Let's continue working with the standard modules. And the next one is going to be very, very important because not only is it a very useful module, but it's also relevant to the world of cybersecurity. And I'm talking about the date time module. This is a module that you can use to display things like your time, the current date. You can manipulate date and so on. So let me show you how to work with it. I'm going to remove all of this and simply say import and then date time, okay? Now, let's assume we wanted to log the current date and time. Going to come down in here and I'm going to say current underscore time equals, okay? And now we'll have to call the module which is date time, dot, and now the method, which will be used to actually display the current date and time. And it's also called date time dot now. It's kind of interesting that in the module daytime, you do have a function called day time now that's actually used. So all I have to do from this point right now is to simply say print. And then in brackets, I can say the current date and time is space, and then you can add my comma right here and then just say current on the score, time and let's see. I'm going to run the program, and there you go. The current date and time is 27th of October 2024, and that is the time right there, as you can see. However, I want to introduce you to something else. See, given the fact that over here we have daytime and then the daytime again, it's kind of redundant, right? I mean, the code still works perfectly fine. Don't get me wrong. The code does work. But if you want to be very professional about this, one thing we could do is we could say that, you know what? From the daytime module, let's import specifically the date time method. So what I'm going to do right now is over here, I'm going to say from datetime, import daytime. And now the beauty is that over here, we no longer have to say daytime dot but you can just remove all of this. And just simply say daytime dot now. Because right now we've told Python that, hey, from the module called daytime, I want you to import specifically the class or the method called datetime now if I run the program again, you can see it still works perfectly fine. It's just that this is a little bit more professional and a much better way of writing your code. Now, I want us to take this program a step further and actually combine the daytime module with the random module. So we're basically going to generate random activities like login, logout, file upload, password change, you know, things that a user might do on a computer system, we're going to generate them randomly and also generate them with some time stamps, okay? So I'm going to go back in here. And just after the daytime module, we're going to now import random. Okay? Now, let us indicate the different types of actions that we're going to randomize. So I'm going to say actions equals, and now we can create our list. And let's add a variety of them, okay? Let's first of all, go with, Log in comma. Let's go with Logout as well. Okay, we have some suggestions in here like register, reset password, and, okay, I think I like these four. I think four is enough, okay? So different actions like login, logout, register, reset password. So we're going to randomize these actions. Let me just add an underscore between reset and password. Whenever your strings have more than one word, if it's like two words, always use an underscoe to join them together, it's a much better way of writing out your strings. So one that I'm going to do right now is over here, I'm going to keep the current time equals to daytime dot now. However, we also need to create a variable that will hold the randomized action. So I'm going to come over here right now and say user underscore action equals now random dot choice. And then in bracket, the actions. And there you go. And all we have to do right now is just to print that the user performs saw action at a particular time. So we can do this. I'm going to say print and now the F string, and I will say the user performed and now our curl braces to represent the action. So that'll be user on score action. And now I can say at and then the curly braces, current underscore time. Add the closing codes, close the bracket, and that should be it. So let's go ahead right now and run the program, and there you go. The user performed register at S and so. Let's run it again. Performed login this time around, okay, performed a set password. As you can see, so it is working. Now, what if just like in the previous lesson, we wanted to have more than one action being performed by the user? What are you going to do very simply? We're going to come down here, change choice to choices. And then remember, we'll have to indicate how many actions we want to randomize. I'm going to say K equals two, and now over here, I can say the user performed let's just keep the print text as it is. Let's run the program again. And now you can see it says user performed, you said password login at this, run it again, and now it's login and now Logo. So that's how you can work with the daytime module in accordance with the random module to just generate random actions at different times. Thank you for watching the video. I'll see you in the next class. 56. Working with an external library: Round up this section on libraries and modules, I need to show you how you can work with an external library. And the one we're going to be working with is going to be a very, very powerful library, and it's going to be the cryptography library. And in fact, let me drag over the documentation. So this particular library, it's developed and maintained by the Python community. You can go to cryptography dot IO if you want to learn more. But right here, they provide us with ideas on how to actually import the library and specifically a particular function, which is the Fernet function. Okay? This is what is mostly used for encrypting and also encrypting messages. So let me show you how we're going to work with it. And I've added some notes in here because it's very important for you to understand step by step how our program is actually going to work because to be honest, it can get quite confusing, okay? I'm not going to lie. Remember that in encryption, we can encrypt a key, right, we're going to use a cipher, we can then use to encrypt our key. But then we can also decrypt the message that has been encrypted, okay? So the way it works is that, first of all, by using the frente function, we'll have to generate the key that we're going to use for both encrypting and encrypton and then we can pass that key to our cipher. Remember in the world of encryption, we're going to use ciphers that combines an algorithm and the key for the actual encryption and encryption. So it's basically four steps. First of all, we generate our key. So we say frente don't generate key. This will generate the key, and then we can pass the key to our cipher. And now to encrypt the actual message, we will say cipher because now it's holding the key and then encrypt. And then in brackets, our function message dot encode. And then the opposite to decrypt, we can say decrypted message will be equal to Cipher dot decrypt. And now in brackets because we're decrypting what the encrypted message. So we're going to pass that in and then add the last method dot decode to decrypt our message. So let us start from the very beginning, okay? First thing I'm going to do is to import. So I'm going to say from cryptograph P dot Fernet, we're going to import the class of Fernet. Okay? That's the very first step. Now, step one, remember, is to generate the key. So I'm going to say key equals, and now fern generate the score key. This will create a key. And now we can say cipher will be equal to Fernet and then key. At this point in time, I would like us to, first of all, print the generated key that we've created. So I can come in here right now and say print, and then in brackets, let's say generated key. Okay? So narrated, and then key, can add my colon and then add a comma and then key dot the code. All right. So this right here, this particular function here is what we're going to use to show the key as a string. Now, next step will be to actually encrypt the message. So what do we do? We can say message equals. Let's add the message. The default here says hello world. Let's make it a bit more complex. I'm going to say hello world. This is a circuit message. Okay, I think this is fine. This is a circuit message. Okay? So this is the message that we're going to, first of all, encrypt and then decrypt. So I will now come in here right now and say encrypted on the scope message. Now we're going to encrypt the message, and how we're going to do that, we are going to say cipher dot encrypt, and then in brackets, we're going to pass the message and the method dot encode to encrypt our message. So far so good. Next step will now be to print. We can print the encrypted message right now, so I'm going to print it in brackets. Let's add our codes, and then I can say encrypted message is colon and now add my comma and then simply say encrypted on the score message. Okay? So far so good. The last step, of course, after encryption will be to decrypt. So I'm going to say decrypted Underscore message equals to what? Cipher dot the crypt. Then in brackets, we're going to pass in what the encrypted encrypted underscore message, and then dot the code. Brackets, close the last bracket, and there it is. So last step right now just be to print decrypted message is and then decrypted message. Now I can see here we do have Oh, there is no closing bracket, to forgive me. I made a mistake right there. And hopefully, this should work. Let me go ahead right now and run. And there you go. So right now you can see on my screen. It says, Loading the Nix environment. So the beauty about Python on working in plet is that even though this is actually an external library, it's going to automatically import the library for it's basically installed the library. And now we have the program running, and it says the generated key was this, okay? That's a long key. So the encrypted message is now this right here, very, very long as you can see. And of course, the encrypted message is hello world. This is a secret message. Let's try changing the message to something else. And I'm going to say Python is amazing. Let's run it again, and there you go. So now we have a different generated key and, of course, a different encrypted message. So that's how to work with the cryptography library. And just to give you a quick recap on again, first of all, we imported the module furnit from the library cryptography. Then, of course, in the world of cryptography, we're going to have a key that we use for encryption and decryption. We generated the key right here with the use of the Fn Dogenerate key method, and then we assign that key to our Cipher. We printed out the generated key by making use of the key decode function. And then to encrypt the message, we have to, first of all, add what the message actually is. The message here is Python is amazing. And then we say, Okay, encrypted message will be equal to now the function cipher dot encrypt, and then in brackets message dot encode. So the dot encode method right here will convert the plain text or string into bytes, okay? And now the dot encrypt will take the encoded message. I will then encrypt it producing an encrypted version. And of course, the output will be equal to the encrypted on the score message. And then the last step right now is to actually decrypt the message. So cipher dot decrypt right here. We'll take the encrypted message and then decrypt it, returning it back to the original format, and then the doot decode, right here. This method will convert the decrypted bytes back into a readable string format. And, of course, finally, we printed out the actual decrypted message. So that's it, thank you for watching the video. I will see you in the next class. 57. Encryption & decryption tool project intro: Welcome to the next Mini Project. And over here, we're going to build ourselves a tool for encrypting and decrypting files. Now, the way it works is that once the user has run the program, they will be provided with four different options. You can see on your screen, they can either generate an encryption key, encrypt the file, decrypt the file or simply just exit the program. So if I go with option number one right here to generate an encryption key, now you can see that we're going to be prompted with the question enter the file path to save the key. Now, the point of this is that we're actually going to provide the user the ability to either use the default key that we ourselves are going to create or they could use their own custom key. That's the whole point of this question or prompt, asking the user to provide the file path. So if the user decides to go with the default key, so I'm going to press Enter right now, now you will see that we've created a file over here called Encryption underscoolk dot key that now has a particular key for encryption. So now if I was to go back to the program and I typed option number two to encrypt a file I have created a sample tx file in here with some random text. So let's say, for example, we wanted to encrypt this file. Now I'm going to provide the name of the file, sample dot TXT, press Enter. And now we can provide a name for the output file. So we can call this one encrypted under sco file dot TXT, ok. And now we press Enter. And now we can enter the path of the encryption key to be used. Again, we're going to go with the default key, so I'll press Enter and now our file has been encrypted and has been saved to another file called encrypted underscore file DTXT. You can see this is the file right here. So if I open it up, right now, you can see that this is the encrypted version of our sample DTXT file. Now, if I go back and go with option number three to decrypt a file, okay? And now let us decrypt the file that we just encrypted, okay? So I'm going to go encrypted Underscore file dot TXT. This is the file that we just This is the newly encrypted file. So now I press Enter. And now what's the name of the output file? We can say decrypted, underscore file dot TXT. We press Enter, and then we press Enter again to use the default key. And now we have decrypted the file and send it to a new file called Decrypted Underscore file dot text. And if I open it right here, now you can see we have gotten the exact same text back again. So basically the way the program works is that we're going to provide a file, in this case, right now is going to be sampled on TXT. We're going to encrypt it using the encryption key that we've generated. We're going to store it to a file called encrypted nscoFLdo text, and then we're going to attempt to decrypt the file and then save the contents to encrypted Osco FL Do texti. And as you can see right now, it works perfectly well. So I'm going to show you exactly how we can build out this particular kind of tool. I'm going to divide the entire project into three different topics. So the first one, we're going to create an encryption key then the second video, we're going to create both the encryption and decryption keys, the functions. And then finally in task number three, we will add the user interface. Without wasting any more time, let's get started. 58. Task 1 create the encryption key: Alright, so let's begin. And the very first thing we're going to do is we're going to import the required library, and that's, of course, going to be the cryptography dot Furnt. So I'm going to come in here and say from and then cryptography dot furnt and now import net. Okay. Now, we want to write a function that we can use to generate and save our encryption key. So let's go ahead and create a function. I'm going to call this one define and let's say generate underscore key, and now in Bracket. So this is going to be the name of our function which we're going to use to generate our encryption key. Now the thing is, whenever you're generating your keys, whenever you're using this function, we can actually add a particular kind of parameter, which will be the file path. We can create the name of our file. So what I'm going to do right now is I'm going to say file, and now underscore path Okay, will now be equal, and now we can add the name of the file. In this case right now in codes, I'm going to say encryption underscore key dot key. This is how you want to save the file for your encryption key. It's going to be the name and then dot key. And I'm going to add my column at the end. Very, very important. So now to generate the actual key itself, you can see we's already given us the information. I'm going to say key equals and now fernt now dot generate underscore key, and now in bracket. So this right here is what we're going to use to generate the actual key. We're going to use this method, which is the fern to generate key, and we're going to save it in a variable called key. So now, let us create the actual file itself. So I'm going to come in it right now, and I'm going to say with, okay, W and now open. So we're going to create our file, and now in brackets, I'm going to add the file path, okay? And now comma, and now the right mode. So it's going to be WB, and now I'm going to say S K on the score file. So this right here will open up a file in what we call a right binary mode. Now, you may be wondering what exactly is WB. It's similar to the write mode, which will just be W, which we're already familiar with however, in WB, you will write data in a binary format instead of plain text. So basically, this means it will write data as raw bytes rather than characters. Now you might be wondering, okay, so what exactly is the point? What is the advantage? Well, if you write binary data using your regular text mode, which will be W the program will attempt to interpret the bites as characters, which can very often result in encoding errors, data corruption, or maybe even loss of data integrity. So using WB is always recommended whenever you're working with encryption, image files, or any non text data to ensure the data will be correctly stored in its original bite format without any alteration. All right, so now we'll have to write the key to the file. So I'm going to come over here right now, and I'm going to say key on the score file, and now the method write, and now what are we passing in? We're passing in the actual key itself. So this right here, we'll write the key to the file, and then it's done, but we can also just write something to print out. So let's just say print. And now in brackets, we can use a string. And I'm going to say codes. Let's say encryption encryption key saved, too, and now we can add the file path underscore path, and there it is. So we have successfully created the function for generating our encryption key. But now we'll also write another function to load the encryption key from the file. So I'm going to come in here. I'm going to say define load underscore key and now in brackets, I'm going to add a file path. And now this will be equal, of course, to encryption key encryption underscore key dot key. So now from here, I'm going to say width and now open going to add the file on the score path. And now we want to read. We want to read from the file. So what do you think we're going to use? We're going to use RB as opposed to WB. So it's now going to be codes. It's going to be RB. Let me add my coma right there, and then close the bracket and now as key on the score file. Had a column. And now, finally, to read the key from the file, we can simply say key equals, and then key, underscore file, dot, the read method, and now in brackets, and now we can simply return our key. So the function we've just created right here, the load underscore key will simply read and return the encryption key stored in the file, and of course, this is going to be the key that will be needed for both encryption and decryption. Jelly next video where we're going to go ahead right now to write functions to encrypt a file and also to decrypt the file. 59. Task 2 creating the encryption and decryption functions: Welcome back. So in the previous video, we successfully created the encryption key, and we gave it the file path of encryption onscorekey dot key. And we also wrote a simple function to load the key. So now, let us write the functions which we're going to use to actually encrypt our files, but also decrypt them. And by the way, I just noticed over here on line nine that I did not add my colon at the end. So please if you haven't done that, be sure to do that as well. And then also on line five, I missed another colon as well, so please do add the columns to line five and Line nine as well, okay, so we are going to write a function to encrypt our file. So I'm going to come down here and let's say define. And let's call the function encrypt, encrypt, underscore file, okay? Now, think about this, okay? Our function because we're going to encrypt a file, we're going to require three different parameters, okay? The first parameter will be the actual file itself, which we're going to be encrypting. So let's call that file the input on the score file. Okay? Now, we're also going to need to create a file that will contain the encrypted version. So let's call that file the output On the sco file. Okay. And then finally, we're going to need the actual key which we're going to use to encrypt the input file, right? So let me add my colon at the end. So the first thing we're going to do right now is we're going to create our frente object with the provided key. So I'm going to say Fernet now equals F, Fernet and now in brackets, we're going to add R key. Okay. So from here, we want to read the input file. Okay? That will be obviously the next step will be to actually open up the input file that we want to encrypt. So I'm going to say with open and now in brackets, I'm going to say the input on the score file, and now what mode we're going to go with the read binary mode, so R B, okay. And now I'm going to say a file and now, what we're going to do is we're going to read the contents of the file, and we're going to pass those contents to another variable. So let's call the variable original. I'm going to say original right now will be equal to what? The file dot read because we want to read the contents of the file, and let's add our brackets right there. Okay, we're doing great. We're doing great. Now we need to encrypt the file. Okay? And by the way, I forgot to add the column in here on line 16. So let's add a column right there. Okay, so now that we've opened up the file, okay, we now want to encrypt it. So I'm going to say let's call this one encrypted. This will be the variable to store the actual encrypted version. So I'm going to say encrypted will be equal to now Furnt dot encrypt. Now in brackets because we've passed, file dot read into the variable original. I'm going to come in here right now and simply say read null, then what is the last thing we're going to do? We would have to write the encrypted content to a file. So I'm going to say width open, and now in brackets, the output on us call file, and now the mode will be right binary so WB. And now I'm going to say as file at our colon right there. And all we have to do right now is to say file dot right because we're encrypting our file, and now in brackets, what's it going to be? It's going to be encrypted. So I'm going to say encrypted. So this will write the encrypted content to the file. And of course, we can simply add a print statement you can say print. And yeah, we can go with this one print file encrypted and saved to the output file. So from here, what we have to do right now will be to create the functional we can use to decrypt the file. So it's basically going to be a reverse of what we've just done. So check this so I'm going to come over here. And let's call our function decrypt on the score file. And then just like with encryption, we're going to need three parameters, the input file, the output file, as well as the key. So I'm going to add those, as you can see, input file, output file, and then key. And now, as usual, I'm going to say Fernet equals, and then capital Fernet, and then Backeds key. So this will be the key that we're going to use to decrypt our file. So now what we want to do, we want to read the contents of the encrypted file. So I'm going to come in here on a new line. I'm going to say with open, and now what are we opening? We're opening up the input on the score file, and now what mode R B? I'm going to say S file, and colon. So now what do we want to do? We want to decrypt. So I'm going to say the encrypted Encrypted here equals to file dot read, we're passing the contents of the encrypted file to a variable called encrypted because now what we're going to do is we're going to create another variable called decrypted, and now this will be equal to Fernet. Now I'm going to pass decrypt in brackets encrypted. So this right here is how we're going to decrypt the contents of our file. And now, what is the last step? We want to save the decrypted information to a new file. So now I'm simply going to say with open, and now in brackets, the output underscore file, and now WB a file, colon, and now simply file dot right, and now decrypted so we're going to write connect to the file. And then finally, we can just add the usual print statements. So we can say something like print, brackets F column, and let us say file decrypted and save to the output file. I think, Okay, I think this is fine. And there it is. Okay. Actually, let me improve on this one. I can say file. Okay? And now cool braces, and then the input file. Okay. And now, I will say decrypted and saved to the output file. I think this is better, and there it is. So once again, we create functions to both encrypt and decrypt our files. For the encryption function, we need three parameters. The input file which we're going to encrypt, the file that we're going to create, I will store the encrypted information, which is output file, and then the key for the actual encryption. So we created that key right here by saying find equals finite and then Bracket key. And now we opened up the original file and we stored the contents of the file into our variable called original by saying file dot read equals original. And now to do the actual encryption itself, we said encrypted equals freno encrypt and now in brackets original, and now with open because we want to store the encrypted information on a new file, the output file, we created the file right here on lines 19, 20 and 21, and then we did pretty much the exact opposite with the decryption function. So Jome the next video aware, we're going to go ahead and now create the main function for the user interaction. 60. Task 3 adding the user interface: Come to the final part of the program where we are going to create the main user interaction. We're going to provide the user with four different options. Maybe they would like to generate a key encrypted file, decryptive file, maybe even just exit. And then depending on which option they choose, we are going to have to do something. So let's begin, right? So I'm going to say define main. This will be the name of our interface, and let us print out a general welcome message. This isn't necessary, but hey, let's just do that. Welcome to the encryption, Decryption tool. Okay. Okay. Now we're going to create the while loop that's going to go through the four options. I'm going to say while and now true colon. And now let us print out state the options. So I'm going to go with print. And now number one, generate an encryption key. Okay. And now let's go with option number two, which would be to simply encrypt encrypt a file option number three will be what the crypts. File. And now the final option, which is the most boring option, exit the program. Okay, there it is. Now, since we now have the four options, let us prompt the user to provide us with which option they would like to go with. So I'm going to come over here right now, and let's go with the variable or option. Okay? So I'm going to say option and now equals and now input. So let's prompt the user to tell us what option would you like to go with question mark, okay? So let's ask the user, Hey, what would you like to do? Now, we'll have to create the IL statements for each particular option. So let us go with the first one in here. So I'm going to say if the option is equal to one, what do we want to do very simply, we can generate our key. So I can come over here right now and simply say generate underscore key, and now in bracket, what's the name of the key? That is encryption underscore key dot key. So we can run the function immediately, right? And this will work perfectly fine. However, I want to show you something, okay? This isn't necessary. But what if we wanted to provide the user with the option of actually using their own custom key and not just the default key that we've already created. So we're giving the user two options, okay? You can either use the default key which we already have or you can provide us with the path to your own encryption key. Now, how would we do this? Well, I've already written the line right here, okay? So let me just grab this, cut. I'm going to come down in here and then paste. And let me explain to you what's happening in here. So we're going to have to pass this into the variable file underscore path. And now input. So let's ask the user that hey, enter the file path to save the key. Now, over here where it says default encryption on under key dot key is still part of the string. I am just indicating to the user that hey, if you don't provide us with your own custom key, we're going to use the default key which is encryption under key dot key. Now this is very important, where it says, or encryption under key dot key. Now, right here, we are telling Python that, Hey, should in case, even though the user has been provided with the option to use their own custom key, if they want to use our own default key, then use it. So here we said or and now the name of our default key. So all we need to do right here is just to remove the default option and then simply pass in the file on the score path. And there you go. So that's option number one, all laid out. Let's move on to option number two. I'm going to come in here and say, L F, choice equals two, colon. So in here, we would like to encrypt the file. Now remember, to encrypt the file, we need three parameters. First of all, we need to ask the user to tell us the file they would like to encrypt, which will be the input file. And then also where would like to store the encrypted content. What's going to be the name of the output file? And then, third, which key would they like to use to do the encryption. So I'm going to come over here right now. Let's pass the variable input on the score choice. Oh, do forgive me. It should be LI option. No choice. I got carried away option, no choice. So input input file. Do forgive me, input underscore file. I'm going to say equals. And now let us prompt the user to tell us what's the name of the files. I'm going to say enter the path of the file to encrypt let me remove this message. So enter the path of the file to encrypt. Okay? So let's prompt the user to do that. And then output on the Sco file is going to be equal to input. And now, let us prompt the user to tell us the name of the Apple fs. I'm going to say enter the name of the output file. Column. And now, finally, what about the key? I'm going to say key on the scope path. Okay, it's going to be equal to input. And now we can say, enter the path of the encryption encryption key. Otherwise, the default will be used, okay? So I'm going to simplify the string statement in here. I'm not going to indicate the name of the default kid. I don't think that's necessary. So all we have to do at this point right now is just to also say the or encryption. Underscore key dot key. And there you go, right? So one thing we need to do right here is we need to account for errors, okay? What if the user what if maybe they provided the wrong input file or the name of the output file is incorrect, or maybe even the key isn't correct. What should happen? So let us provide the option right now to cater for that. So I'm going to say try. Remember that, and now key equals load underscore key. And now in brackets, the key path, okay? And now the most important parts encrypt On the score file. And now in brackets, what are the three parameters? We have the input on the score file, we have the output, on the score file, and now we have the key as well. So these are the three parameters. And if there is any error, let us generate this. I'm going to say, except Exception. Okay, as E. And now we can print our arrow message. So I'm going to use the F string, and then I'll say something like arrow during encryption. Okay, colon and now we can use our color braces to indicate what the particular error is, and there you go. So we have successfully created option number two with x user to provide the path of the file they want to encrypt, the name of the output file, and then also the key as well. And we've also added some error handling. Fantastic. Now, all I'm going to do is I'm just going to grab this block of code, okay? And I'm simply going to paste it because think about it, okay? The encryption and encryption process is almost the exact same thing. It's just opposite, right? So I'm going to copy all that block of code, and I'm going to come over here, and now I'm going to paste it. Okay? Now notice, though, that the indentation isn't correct. So make sure that when you do the exact same thing as I did, make sure that your indentation is correct. Make sure everything is aligned properly, and this should be fine. Okay, all we need to do right now is to change the options in here, so option number three to number two. Input file, I'm going to say enter the path of the file to what to decrypt. Okay. Output file. Enter the name of the output file. That's fine. Enter the path of the encryption key. Otherwise, okay, keep in mind that the encryption key is the exact same thing as the decryption key, so we don't need to change the text in here. You can change it if you want to, but I'm just going to leave it as it is. And finally, okay, over here as well, we just need to change this and there you go error handling is intact. And one more thing we need to change is going to be over here where it says the encrypt file. Over here, it should be decrypt file, okay? So you want to make sure that whenever you copy and paste code and you need to alter some text, make sure that you go through it properly. Otherwise, you may end up having some issues. Okay, all we have to do right now is to add the fourth option. So I'm going to come in here and say, I option equals four. We can print Xs in the program. And now we're going to have the break statement, as well, because remember that even though we provided four options to the user, they might become quite silly and decide to choose option number five or six or seven or any other option that doesn't even exist. So we need to account for that as well. So I'm going to say break and then going to provide the final Ls statement. And now we can simply say print and then tell the user, please choose a valid option. And there you go. And there it is. And now, all we got to do is to run the main function, and there it is. So hopefully, the program should work. Let me go ahead right now and run the program, and there you go. Okay, so let us test this out. I'm going to go first of all, with option number one, press Enter. I'm going to go with the default, so I'm going to press Enter again. And there you go. Awesome. So you can see right now we do have the encryption key that's been created by the program. So now, let us try to encrypt a file. So I'm going to come over here right now, and let's create a sample D TXT file. Let's add some text in here. This is sample text, which we are going to encrypt and also decrypt. Okay. So let's see. I'm going to go ahead right now and go to the program. Let's go with option number two right now. So let us try to encrypt our sample TXT file. I'm going to press Enter. And now, what's going to be the name of the output file? Let's call this the encrypted's call file dot TXT. Let's go. We're going to use the default key. I'm going to press Enter. And there you go. So let's take a look. We have the encrypted under scoeFled text, and there it is. This is the encrypted version of our sample text file. So now, let us try to decrypt the encrypted file and see if we'll get back the same text. I'm going to go with option number three. Enter the path of the file to decrypt, it's going to be encrypted UnderscoFle dot TXT. And that will be the name of the output file. We can call it decrypted, UnderscoFle dot TXT. We're going to go with the default key. And the Oh, oh, oh, oh, I'm sorry, we have an error message. It says, No such file, Dicture encrypted underscoflt xt. What did I do wrong? We do have Oh, Do you see what I did? My file is called encrypted, not encrypted. I do apologize. I need to get myself a new pair of glasses. I do apologize. Let's try winning that again. So option number three. So the file here is encrypted. Ah, on let's go file dot TxD. Now the name of the output file, I'm going to go with decrypted. On les go file, dot TXT. Default key, and there you go. Okay, so now let's take a look at the decrypted file, and you can see it work. So first of all, we encrypted sample dot TXT. We got a encrypted file, which you can see right now, and then we decrypted this file to get back the exact same texts you can see right now, it works perfectly fine. Let's try option number four to exit the program. Okay, I exited the program. Let's run the program one more time, and let's go with option number seven. And it says, please choose a valid option, and there you go. So Whoa, we've successfully created ourselves an encryption and decryption too. It's been quite a lot, but hopefully you've learned some new concepts such as using the write binary and read binary modes. You've also learned how you can provide the option for the user to use their own custom key or use the default key. And hopefully you learned a lot in this money project. Thank you so much for watching, and of course, I will see you in the next class. 61. Oop section intro: Alright, so welcome to what might just be the final section in this course. And in here, we're talking about the OOP, the object oriented programming. Sounds like a mouthful. Sounds very complicated, but don't worry. I'm going to cover all the basics in here. So this is going to be a big, big, big topic because believe it or not, if you can master OOP, you can write some really amazing programs, okay? So in this section, we're going to learn what objects are. We're going to talk about classes as well, okay? So what's the relationship between an object and a class? And of course, you're going to learn about methods. I'm going to give you plenty of examples. There's going to be a few coding exercises as well. You're also going to learn some very important concepts such as the encapsulation, inheritance, and even polymorphism. Ooh, what exactly is polymorphism? That sounds very complicated, right? It's not as complicated as it sounds. Don't worry about it. So it's going to be a very, very interesting topic, and I'm pretty excited to teach you about OOP. So as usual, without wasting any more time, let's jump right in. 62. Introduction to OOP: Come to the brand new section where we're going to be taking a look at object oriented programming, otherwise known as OOP. Okay, this is where we're going to delve into some advanced aspects of programming. But don't worry, I will be here to explain to you in the simplest manner possible. So what exactly is OOP? What you see in front of you is basically a library, right? You've got all sorts of books. You've got books on anthropology, archaeology, biology, chemistry, math, and so on. Imagine you were the librarian. You were the person in charge of all these books. Ideally, of course, you would need some sort of a system to manage these books, right? You will need to know where to put the books. You need to know the characteristics of each book, like, you know, the title of the book, the author of the book. And also whether or not the books are available, maybe they've been burred, you know, stuff like that. So, Imagine that we created a class called book, okay? And just like I said, this class can define several kinds of characteristics about each book like the title of the book, the author, the genre, the cover color. But besides the physical attributes of the book, we can also talk about the things that we can do with the book. Maybe you can borrow the book. And if we can, how long can we borrow the book for? Maybe we can even bite the book and so on. Okay, think of all these attributes under the class book. Alright? Now, Things like the title, the author, the genre, things that physically describe the book, we can call them attributes, right? We've talked about attributes already in this course. But then methods, methods will describe what it is that we can actually do with each book. Can you boy? Can you bite? So basically, the book class will define both the attributes and the methods. So in other words, the book class is like a general description of what a book might look like and what we can actually do with the book. But we're not referring to a specific book just yet. Alright. Now, each individual book in the library is what we refer to as an object of the book class. So once again, the book class will give us a general description of the attributes and methods of each book, while the books themselves, the individual books, they will be called objects of the book class. So the object attributes of book A, for example, could be the title is Python programming. The author could be, Jack, and then the color could be blue, right? And then let's take a look at book B. The title could be cyber one oh one. The author is Alice and the color is red. These are individual attributes of each book, and the attributes have their own values. The value of the attribute color for book A is blue as an example, right, and so on. So the methods, we can also, for example, say book A has a method called borrow, whereby once the book has been borrowed, the library will have to update the records to indicate that the book is no longer available to be borrowed. And likewise, imagine Book B had been borrowed in the past, but now it's been returned. We can then mark Book B as available again. So these are methods, these are attributes. So to give you a quick recap, the class is kind of like the general description of the book where the attributes and methods are defined. The object is an instance. Basically a specific book in a library, a specific object under the class, the book class. Attributes will be physical description of each book, while methods otherwise known as actions are well, essentially the actions that can be performed on each book. So Jerman Vernick video where we're going to start taking a look at some examples. 63. Createing a basic class: We're going to do right now is we're going to create a code centered around the previous example I gave you regarding the library, the books, and the attributes and methods associated with each book. So the first thing we need to do is to define the class book. So I'm going to say class book and this is how we would create the class called book very straightforward. Now, the thing is, we need to indicate the attributes that are going to be associated with every object of the book. Okay? So to do this, I'm going to say the fine. And now, in it, Underscore, underscore. And now inside of the brackets, this is where we are now going to indicate what attributes are going to be associated with each book. You typically start off with something called self. Okay? This is very, very common in object oriented programming, okay? So self is a way to refer to each individual object of the class we're dealing with. In this case, right now, it's going to be each individual book. So you're always going to start off itself, and now the actual attribute. So I'm going to go with title, author, and let's add one more. Let's say Genoa. Okay? Why not? Genoa, Colon. So far so good. Okay. Now we'll have to assign variables to each of our attributes. So I'm going to start off by saying self dot title. You see how this works right now? Self dot title equals title. Now, can you guess the next one self dot author equals author. And then, again, self dot Gena equals to genre. So far so good. I'm going to add one more attribute. It's not technically an attribute that we've defined thus far, but it will be useful once we start creating the methods that we will apply to the book, okay? And that is we want to indicate whether or not a book is available to be borrowed or not. So I'm going to say self dot available. All right, let's say self is okay, is on the score available, okay? We want to initialize this to be true at the very beginning, okay? So at the very beginning, all books are available to be borrowed, okay? Now, we've defined the attributes, title, author, gena. We've also added one of the methods, one of the parameters that we're going to use in our methods, that is whether or not the book is available. So now, let us define the actual methods themselves. And the one we're going to be dealing with here is going to be the borrow. So I'm going to say borrow and now in brackets, self, okay? We are creating a method that we can apply to the object book. So let us say that if the book is indeed available, what should happen? So I'm going to say if self self referring to what the book is, in fact, available? What do we do? Remember that once the book is available to be borrowed, we will need to update the records in the library to indicate that the book is no longer available because now it's just been borrowed, right? So we're going to say self dot is available, should not be equal to what? False, because it's no longer available. Okay? Look at line 11 again. This is because this is very, very important, okay? Right here, we're doing that for the very first time, somebody wants to borrow this book up, maybe not for the first time, but the book is currently available. So what should happen? We should say that, okay, we should update the records to indicate that the book is no longer available, and then let us print out something, okay? Let us say print. And now in brackets, I'm going to use the F string because I want to reference the actual title of the book that's been borrowed. So I'm going to open up my college braces, and I'm going to say self dot. What dot title. Okay. And now we can type in whatever it is, we want to type in has been borrowed. Okay? At the closing codes. And there it is. So two things should happen if the book is available to be borrowed. First of all, we need to update the records to indicate that the book is longer available because it's now been borrowed. And then we should print out the message saying the book has been borrowed. Ese, what is the's statement here for? What why are we indicating se? We need to indicate se because on line 11 where it says, If self is available right now at this stage, the program is assuming that the book is currently available. But what if in a scenario where the library user wanted to borrow a book that wasn't actually available in the first place. That's what the Ls statement here is going to represent. So se, if the book wasn't available initially, just simply print Print, print. And then we can just say, sorry. This book is not currently available. And I forgot to put this in quote. Sorry, this book is currently not available. Of course, we can also use the F string and self title to reference the actual name of the book, but that's not necessary. But just going to say, sorry, this book is not currently available. Okay. Awesome. Now, let us define another method, which would be to return the book. So we can borrow the book, but we can also return it. So I'm going to say define return on the score book self. So we've given the function of the method called Return On score Book and then self. So in this case, right now, what should happen if the book has been returned? Now we're going to say self dot is available should now be equal to what true because the book is now available. And let's go ahead right now and print a message and say, let's also reference. So I'm going to say F string, and then the name of the book has been returned. Alright? So let us try this. Let's put it into action, okay? So I'm going to come all the way down here. Let's say book underscore A, equals now to the actual object called book. And now inside, let us provide the name, the title of the book, first of all, so let's say Python one oh one. Okay? What's next the author? So let's call the author Alice. Okay? And then what's the genre going to be? Let's say programming, right? Programming. Okay? And now, let us create another object. This object will be represented by book on the score, B equals, and now again book, and now in brackets, let's say cyber two oh one, okay? And then the author here is going to be Jack and then what's gonna be the genre? Let's just say tech, technology, right? Technology should be the genre. And there it is. So going to come all the way down here. And now, let us call the methods, okay? We have everything in place right now. Let us call our methods, which, of course, what borrow and then return. So I can say book, first of all, underscore a dot Borrow in brackets. Alright? So first of all, we're going to borrow the book. And then let me run the program. Do you see right now, it says, Okay, Python one oh one has been borrowed because over here, it's going to indicate online 11. It's going to indicate at the very beginning that, Okay, the book is available. It's now been borrowed, so we'll have to update the records to say it's false. And now we're going to say the book has been borrowed. But now at this current stage, the book isn't available, right? So let us target the L statement. It should now print, sorry, this book is not currently available if I run the method again. What if somebody else now comes in and wants to borrow the book that's already not available? What's going to happen? I'm going to run the program. And now you see it say, sorry this book is not currently available. Okay. But what if what if I now return the book? What's it going to say? It's going to say Python one on one has been returned. And now, if I run the return method again I'm sorry, the borrow method again, one more time. Was it going to say? It's going to say Python 11 has been borrowed. So you can see right now, it works perfectly fine. So just to give you a very quick recap, first of all, we had to create a class called book now inside, we have to define the attributes associated with each book. And we did that by saying, define the score on the score in it on the score, and then the parameters or the attributes in brackets. And then we assigned each attribute to a value so self or title will be equal to title, self dot author will be equal to author, self dot genre will be equal to Gena. And then we also created a method in here that will indicate whether or not we can borrow the book. So this is basically a boolean attribute. We said, Okay, self dot is available at the very beginning should be equal to true. And now we created two different methods. The first one here would be the borrow. So what happens when a book is borrowed? If it was initially available, we'll update the records to say self is available is no longer available, so it's false. And then we should print out the title that says, sorry this book has been borrowed. However, else, if the book was already borrowed initially, we should just simply print out sorry this book is not currently available. And then the second method, what happens when the book is returned? We should now update our records yet again to indicate that self is available is now true, and we can print out the name of the book saying it has been returned. And then finally, we learn how to create individual objects in individual books. We can simply say book underscore A equals book, and then in brackets, we'll have to fill in the values for the attributes. And then we created another book, book B, which is Cyber to one Jack Technology. And then we called the Methods, and that's pretty much it. So thank you for watching the video. I will see you in the next class. 64. Creating a basic class part 2: Before we move on to a new lesson in object oriented programming, I wanted to give you one more example so that you can really understand what it is that we're doing. So let's create a different class. Okay? Let's do something related to cybersecurity. Let's say, for example, we wanted to create a class called user, okay? And the user accounts will have different attributes like the user name, the role, okay? And then let's also create a method. Say, for example, what can the user do? The user can log in. Okay? So let's do that. So I'm going to remove all of this code. And at the very top, I'm just going to say user user class. Okay? So how do we create our user? We say class and then user colon. And now what do we do? We have to define the attributes that will be associated with each user account. So I'm going to say define underscore in it. On the score. And now in brackets, what do we have? We have self because we have to make reference to each individual user account. So I'm going to say self, and then the attributes will be user name, and let's just go with role? One more. Roll. Cold on, and that's it. What's going to be the next thing to do? The next thing will now be to assign variables to each of our attributes. So I'm going to say self dot use a name equals, use a name. Pretty straightforward. And then self dot roll should be equal to what roll. There it is. Okay, so we've defined the attributes. Now, what about the methods? Let's create just one method called log in, okay? So a user can actually log in. So I'm going to say death and then log in. And then in brackets, what's it gonna be? It's going to be self. Itself. Colon. And now, what should happen once a user has logged in? Let us print a welcome message. So I'm going to use the F string codes, and then we can say, welcome and then let our Python fill in the rest of the information right there. So welcome. And then self user name. And there it is, that's all we need to do. And now, last but not least, let us create an object, basically user account, and then let's call our method. Ar? So I'm going to say admin on the score user, this can represent our first account which will be for an admin. It's going to be equal to what user. And now in brackets, we have to fill in the values of the attributes which are username and roll. So the user name in this case, right now is going to be Alex. Okay? Username is Alex. Sorry encodes, Alex, and then the role of Alex is obviously that of a administrator. So administrator. Okay. And then finally, let us call our method. So it's going to be admin scouser dot log in Brackets. And if this program works, it should say something like, welcome Alex. Let's run. And there you go. Welcome, Alex. So, this has been another example of how to create a class, how to define the attributes for each object in that class. And then we also created a method for each individual object. And then we, of course, created an actual object, and then finally called the method associated with the object. So hopefully, you now have a better understanding of OOP. Thank you for watching the video. I'll see you in the next class. 65. Encapsulation (1080p): One of the fundamental concepts of OOP object oriented programming is something known as encapsulation, which basically refers to keeping some sort of data private so that it can only be accessed or modified in certain ways. Now, this is very useful in cybersecurity because it allows us to protect sensitive data like passwords, okay? So as an example, we could write or create a class and objects that will store a user's password privately and then only provide a way to check if the input password is actually correct. So let me show you how we can encapsulate data using OOP. I'm going to keep the exact same class, okay? So class user however, I'm going to change the attributive role to password. Okay? And then obviously in here, I'm going to change our self dot role to password, and then this is going to be equal to our password as well. However, the one way to tell Python that, Hey, this particular attribute, we want it to be private. This isn't public information. This is private only to the user. And we can do that by going over here in front of the P for password and then adding a double underscores. So this right here, the double underscores will indicate to Python that, Hey, this is a private attribute. We want to encapsulate the information here. So let's now create a method which we can use to check whether or not the input provided by the user actually matches the real password for that user. So what I'm going to do right now is I am going to define our method, and let's call it authenticate, okay? So define authenticate. And now in brackets, I'm going to go with self and password, okay? And we're doing this because self here is going to refer to the user whose password we actually want to check and then the password itself that we're checking, okay? And now this is the key. We would have to return and now self dot Osco, password will be now equal to password. So we want to check if the password that's stored for the user is going to be the same as the password that was provided as an input. That's what this will do right here Line eight. So if the password matches, line eight will return true, but if there isn't a match, it's going to return false. Okay? So now we need to create our user objects and make sure that, of course, whenever you're creating user objects, the indentation is outside. It should be on the same indentation as the class itself, okay? Don't do it inside the class but outside. It has to match, okay? So over here, I'm not going to say user equals, and now user so let's say we need to provide a user name. Let's just go with guest, okay. Let's just go with guest. Something very simple guest. And now for the password, I'm going to go with S and then say SOD, Fish, one, two, three, okay? So this is the password we've created for our user guest, okay? Fine. Now, let us try to authenticate the user by using different passwords, okay? So let's try using the right password and then the wrong password, okay? So how are we going to call our method? I'm going to say print. And now in brackets, user dot authenticate. So we're calling the method right now to authenticate our user. And in brackets, we want to provide the password we want to check. So the password in here right now, let me go with the right one. It is going to be the same SOD, fish one, two, three, right? And then Brackets, okay? If I run the program right now, and oh, line 12 is an error message. Oh, we should have an extra bracket in here. Sorry about that. Let me try running it again. And there you go. So now, it says true because the password here, STFish 123 is the same as the actual password that we've given to the user. Now, what I'm going to do is I'm going to simply copy this line, okay, copy, paste. And now let us try using a different password. So I'm just going to go capital A, BC, one, two, three, and okay, that's it. So let me go ahead right now. Run the program again, and now you can see that it is false because our method has checked ABC 123 is not equal to SOTfish 123. So this is how we can encapsulate data using the object oriented programming all we really have to do to indicate that a particular attribute is private that we want to encapsulate is to have the double underscores in front of the actual attribute name. So D's, thank you for watching the video. I will see you in the next class. 66. Inheritance: Very important concept of object oriented programming is going to be the concept of inheritance. And just like in English language, we know inheritance to be when, you know, something is passed on from one person to another, right? So in Python, we can actually create specialized versions of a class without even having to rewrite code. We can basically create a child class that can inherit attributes and methods from a parent class. So I want to give you an example in here, okay? Since we're dealing with the class user, obviously in systems and networks, we have different levels of permissions, right? A regular user may be able to access certain kinds of files and then print those files, while an admin user will be able to do those exact same things that a regular user can. But because they're an admin user, they will have extra permissions like being able to say, for example, edit those files, right? So what I'm going to do right now is I'm going to create a class for the regular user but then I will create another class which will be the admin user that will inherit the methods from the regular user. So over here, we have a class user, and now I'm defining one basic attribute, and that's going to be the user name, okay? So now the next step will just be to simply say self dot user name equals equals, and then of course, user name. Now, I want to create a method specifically for our user over here. So I'm going to say define, and let's say a regular user can log in, right? They can log in. So I'm going to say log in. And now in brackets, I'm going to say self. Okay? And then when they log in, let's just do something. Let's say prints and then self user name has logged in. Let me say has has some space in here, has logged in. Okay. So far, what we've done here is we've created the user class with the attribute username. We've also assigned a method called Log in that will basically print out the user's name has logged in. However, I am now going to create another class that's going to inherit not only the attribute user name from the user class, but also the method log in. How am I going to do this? I'm going to come over here and then notice the intentation I'm all the way out. Now I'm going to say class and now provide the name of this new class. So I'm going to say admin. And then we can even say admin user as an example. Actually, let just stick with admin, okay? So admin, and now in brackets, I'm going to say user. Colon. So right over here on line nine, we have created a new class of a user called admin user that's going to inherit the attribute, user name, and then the method log in from the user class. I'm going to go one step further and now add an additional method because this is an admin user, they can do extra things. So I'm going to say define let's call this access on those called logs because they're admin, they can access the logs. So I'm going to say self, call on. And now let's just print print in brackets. I can use F string and let's say something like the self dot. User name is accessing the logs. And there it is. And there you go. We are done. So now, what I'm going to do is I'm going to create our admin user. So I'm going to say admin equals, and now in capital admin user. And then in bracket, let's provide the username admin. Okay? And now, let us run the methods. So I'm going to say admin dot Login brackets. And let's also run the second method, which is going to be admin dot, Access underscore logs. And here it is. So now let's see if this would actually work. Let's run. And oh, I have another arrow admin Oh, I do apologize. It should be admin over here. I got carried away. Admin equals admin and then they use the name admin. I do apologize. So let me run the program one more time. And there you go, All right, so admin has logged in, and now Admin is accessing the logs, and that's basically it. So we didn't have to specify that the admin user, the class admin is going to have the parameter or the attribute user name or the method log in. All we had to do was to indicate to Python that, Hey, this new class admin is actually inheriting the attributes and methods of the original user class. And that's inheritance for you. Thank you for watching the video. I'll see you in the next class. 67. Polymorphism: Another concept within OOP is the concept of polymorphism, which basically translates to same action, but different behavior. The thing about polymorphism is that it's going to allow classes to have methods with the exact same name, but then behave differently. So as an example, still dealing with the user class, we can have different types of users logging in both in a different manner. So as an example, let's have different rules. So I'm going to create a class for the regular user and then a class for the admin user, and then they're going to have unique ways of actually logging in. So check this out okay. I'm going to come in here right now and create class regular user. Okay. And now I am going to define very simply the method, log in and self, okay? Notice I haven't passed in any attributes whatsoever. I'm just going straight to the method, okay? So what I'll do right now is I am simply going to say print now I would just say regular user has logged in. Okay? Now, look at this, I'm going to create another class, and let's call this the admin user. Okay, call on, and now again, define and now the exact same method, log in self. But now it's going to be a different print message because now I'm going to say admin has logged in. And let me just close this, and there it is. So now for the actual polymorphism, I'm going to make use of it, let's assign user to be equal to regular users. Let's create our first objects in here for the regular user. Let's also create an object for the admin user, and of course, I'll call this one admin equals to admin user, and it is. So we've created objects for the regular user and the admin user. Let's now call them. So I'm going to say user dot log in. Brackets, and now also admin dot Log N. And there you go. So now, let us try to run the program, and there it is, regular user has logged in, Admin has logged in. So this is the concept of polymorphism where you can have two different classes. With the exact same method, but that method will act in a different manner, depending on which particular object or class object is calling that method. The method used here obviously is the login method. It's applicable to both the class regular user and the class admin user, but it's going to behave differently because it's going to print out a different message depending on which class object actually logged in. So that's polymorphism for you. Same action, different behavior. Thank you for watching the video. I'll see you in the next class. 68. Final project preview: So welcome to the final project, our final Python project. And together, we're going to build ourselves a very powerful user registration system. Now, we've built something similar already, but this is going to be much more enhanced. Now I've hidden the code. I don't want you to see the code just yet. So what I'll do is I'm going to run the program so you can see what it's all about. So we have the user registration system, three main options, register, login, or exit. So I'm going to attempt to register and I'm going to enter a user name, Let's go with Alex, okay? And I'll check this out, okay? I'm going to go with quite a powerful password. But now take a look at this, okay? It says password is not strong enough. Password must be at least eight characters long. So we do have a password checker tool. But check this out okay. I'm going to go again to register, okay? Let me come down in here, press one. Enter a different user name this time. Let me go with Boris, okay? And now look at this. Okay, I'm going to go with Bis 123. Boris. So this is pretty long. This is more than eight characters. I press Enter. But then it says password, it's not strong enough. Password must include at least one special symbol. So our password checker tool is going to check for four main criteria. It's going to make sure that the password is at least eight characters long, has an uppercase letter, has a number, and also a special symbol, okay? So I'm going to attempt to register again, okay? This time, I'm going to go with user name Alice. And now let's go with an actual real password. I'm going to go with Alice one, two, three with the add symbol. And there you go, it says, User was successful, and now I'm going to attempt to log in. Okay? Take a look at this. You're going to love this, I'm going to log in as Alice. Okay? And now password, Alice, one, two, three, with the add symbol. And now, Login was successful, but take a look at this. We have a post login menu. So our log in users will be able to access a log in menu where they can view their logs or simply log out. So now, I as Alice, if I wanted to view my logs, I press one, and there you go. It says right now that the logs for user Alice. Alice successfully registered on this date, and then Alice also successfully logged in. So we're going to implement basically an auditing system that will keep track of every user and every activity on the system. Also, take a look at this, o we're going to have the users TXT file that will store all the user names and their corresponding passwords, but take a look at this. The passwords are going to be hashed. They're not going to be in plain text. We're going to implement a hashing function to hash the passwords for security. So you can see right there, we're going to implement some hashing, as well, but that's not we're also going to create an audit log file that will keep track of all the logs on the system. So you can see right now the registration field for Alex, registration field for Boris, Ale successfully registered, and then Ale successfully logged in as well. So this is going to be a comprehensive advanced registration system with a passwor checker tool, and also it's going to have hashing all the password will be hashed as well as an additional security feature. So here's the thing, okay? I'm going to provide you with a text document with some useful tips and hints if you want to attempt to write this program all by yourself, okay? So attached to this video, I'm going to provide you with a text file with all the useful hints and tips. I'll tell you the general thought process. And if you choose not to do that, if you just want to work with me, what I've done is I've split this entire project into eight mini projects. So I'm going to break it down step by step by step by step. We're going to be adding the features one by one, one by one, one by one. And hopefully, the way I have approached breaking down this project is going to make it easier for you to understand and also to be able to follow. So as usual, if you have any questions, maybe you're trying to build the program all by yourself and you're stuck somewhere, you can always reach out to me, and I'll try to assist you and help you in any way that I can. So I'm pretty excited to be working with you on building this user registering system. Without wasting any more time, let us begin. 69. Part 1 creating the user interface: Okay, so let us begin. And the first thing we're going to do is we're going to create the user interface that would allow the user to choose from the register, login and exit options, okay? So let's call this function the main function. So I'm going to say define main brackets, colon. And now, let us say while true. Okay, so as long as the program is running, let us display the menu options. So I'm going to say, first of all, prints, and we can say, number one, let me add my codes right there. So I'm going to say option number one, dots register. Okay. Let's close the column. And then let's print out option number two, to dot, Log in. And then option number three, very, very, very boring option, simply exit. Okay? All right. Actually, you know, let's print out one other statement at the very top, kind of like a welcome message. So let's print let's say welcome to the user registration system. Okay. And there you go. All right, so we've got the options all laid out. Now, let us prompt the user to provide us with an option. Which particular option will they want to go for? I'm going to call the variable choice to store the particular option. So I'm going to say inputs, and then let us say, what would you like to do? Okay. And there it is. All right. So once the user has provided their option, we need to create scenarios allocated to each one of them. So I'm going to start off at the very first one in here and say, I our choice equals option number one, we are going to print, let's say, registration registration. Selected. Okay. Now obviously, nothing else is going to happen for now. We will add the actual functionality later. But let's move on to the next option. So I'll say LIF, choice equals, and then option number two. Let's add a colon. So what's going to happen in here? We can say print, and then we can say log in selected Okay. And now let us add option number three, AF choice equals option number three and a colon. We can just say print, and exiting the system. Okay? And I think this should be fine. So remember, there'll be also need to add the break statement should in case the user decides to choose an option that doesn't exist. So I'm going to come in and say break and now break, enter else at our column, and now we can print. Let's say invalid choice, please try again. Okay, I like this. Invalid choice, please try again. And there it is. Okay, so let us go ahead right now to run the program. So I'm somebody going to say main and now brackets, and let's see what will happen. Let's go ahead and run the program. And then you go, Okay, let's try option number one. Okay? Register selected. Option number two, Login selected, option number three. Exiting the system. Okay, so far so good we've added the first component of our program. We have added the user interface. So Jamin next Video we will now begin to tackle each option at a time, starting with the registration function. 70. Part 2 adding the user registration function: Come back. So now let's move on to the first main function for the program that would allow the user to register. All right? So let's go all the way to the top in here and let's push the user interface function all the way down, and let's come up here. Actually, I'm just going to add a note and say registration function. Okay. Okay, so what do we call the function? Let's say register underscore user, right? Register user, brackets. Let's add a colon Okay, so we want to accept the user's username and a password. So let's prompt them to do that. All right. So I'm going to say user name equals and now input and brackets, let's say, a username. Colon. Okay. And then let us prompt them for the password as well. So password equals, input, and then in brackets, Enter a password, colon, and there you go. All right, so right now, once the user has provided the username and the password, we would like to store them in a file. We can call the file users dot TXT as an example, okay? So what we're going to do is we are going to say with and now open. Okay? Remember, we need this function to create our file. So now let us add the name of the file. I'm going to call it users dot TXT. And now, let us enable the append mode. So I'm going to say A. Okay. And then as what as file? Colon. All right. And now we can use the right functions. I'll say file dot right. And now in brackets, we can use our F string, and then let us add cool braces, user name. Let's close that, and then we can also add a colon to separate the password from the user name. So Cole braces again, and then we can add our password. And then for good measure, we can do the backward slash and then N so that each user Information will be stored on a separate line. Each user credential will be stored on a separate line, and there it is. Okay, all we got to do right now is we can print out a simple message that will say something like print, and then we can say, user registration was successful. Okay. And there it is. Okay, so how do we now integrate our registration function with the main function itself? What we're going to do right now is we're going to come all the way right here and say, I choice equals one, then we can remove this. Okay, we no longer need this print statement. And we'll just call the function. And the function that we created here was what register underscore user. Right here, I'll just say register Underscore user, and then brackets. And then there it is. So hopefully, this should work. Let's go ahead right now and test it. So I'm going to run the program. Okay? Number one, we want to register, press Enter. Enter, username. I'm going to go with Alex. Password. I'll just go with Alex again, okay? Just for a test. Enter and okay. User registration was successful, and we have users dot textfile in here. And if I open it up, awesome, you can see it says Alex Alex. So fantastic. You can see right now that it's actually working so far so good. Join me in the next lesson where we now create the function that will allow our users to log in. 71. Part 3 adding the login function: Well, come back. So now, let us add the function to allow our users to log in. So I'm going to say define, and let's go with the login on the score will be the user name. Let's add a column. Actually, let's add a note, okay? And we can just say login function. And there you go. Alright. So in order for the user to log in, they're going to require the username and the password. So let us start by saying user name equals, and then input brackets, codes enter your username. Colon. Okay. And then we can also add the password equals input, brackets, enter your password, and there you go. Alright. Now, in order to verify that the username and password do exist, we are going to have to open up our users dot TXT file. So I'm going to say with open, and now in brackets, what's the name of the file? It is users dot TXT. And then what is the mode that we're going with? We're going with the read mode. Because we want to read from the file, and I'm going to say as file, colon. And now, let us create a variable called users that would accept the file dot red lines function. Because remember, we're going to have to read line by line from each user in the users dot text the file. So I'll say users equals to file dot read lines, and now brackets, and there it is. So now we need to check if the entity credentials match any stored record. So what are we going to do? We're going to create a four loop. So I'm going to say four. Let us use the variable user in users. Okay? So now, what do we want to check? We want to check for the user name and then the password. So we can create two variables in here. We can go with the stored Okay, stored on the score, username, and then comma, we can also go with stored on the score password. Let creates two variables to represent the stored user name and then the stored password. And now, how would we check we need to first of all, check to see if the user name is correct and then also the password. But now take a look at this, okay? The users dot textifle as it is. We have Alex and then colon dot ALC. So we need to figure out a way how we can separate the user name from the password, and we know that it's the colon symbol that separates them, okay? So we can use the split function to separate the use name and the password. But we can also use the strip function. Remember the strip function. We use that to remove any white characters or empty spaces, things like that. Okay? So let's go back in here. So what we're going to do right now is I'm going to say user, and now let us add the strip function to remove all the necessary space from the user name, okay, our brackets, and now I'm going to add the split function. And what are we splitting the username and the password by? It's going to be the colon, and there you go. So we're going to split the username and the password by the colon. So from here right now, we can check. We can say if the user name is the same as what the stored underscore user name, and what password is equal as what the underscore password. What do we want to do? We want to print and say Log in. Successful and there you go. Awesome, awesome, also. And then from here, we can simply return for now, okay. Later on, we will add additional functionality. But for now, let's just return and go back to the beginning of the program. So we're going to exit the function after a successful login. And of course, we can also print just in case something did not go correctly. We can print nine brackets. We can simply say invalid using n or password. And there you go. Let me just add some space in here, and there we go. Okay, so hopefully this should work. Let me integrate it into the main function. So what we want to do right now is where it says the AF choice number two, we can remove the print statement and now simply add the name of the function, which is login on the score user and now brackets, and there it is. Okay. Fingers crossed. Let us run the program. And let us see. I'm going to go straight to log in, because we've already created the Alex username and password. So username is Alex. Password is Alex, and there you go. Awesome, log in successful. Let us try to see if we'll accept a different username that doesn't exist. So I'm going to go with Log In, and let's go with Philip. Enter password, sort, fish, and there we go. It says invalid username or password, so it is working, awesome, awesome, awesome. So we've successfully added the registration function. We've successfully added the login function as well. So now join me in the very next video where we're going to advance the program by introducing the password checker that will ensure that the password created meets a certain strength criteria. I'll see you then. 72. Part 4 adding the password strength checker tool: Well, come back. So now we need to implement the password checker tool. So far so good, our users, they can register accounts, they can login. But we want to make sure that the passwords that they used to create their accounts in the first place is actually strong enough. And we have a list of criteria. We want to make sure that the password is at least eight characters. It contains a capital letter. It contains a number, and it also contains at least one special symbol. Now, the symbols are what I've written over here, you can see. I have all of them listed out. So what I'm going to do is at the very top, okay, let's go ahead. Let me just cut this first of all, I'm going to cut that. So we're going to have to import a library, which is going to be the regular expressions library. So at the very top, let's say import and then R E. Okay? So let me just add a note that says Import for regular expressions. Okay? All right. So now, let us add a note, and I will say password Password checker function. Okay. I think that's fine. Password checker function. Okay, so let us create the name of the function. So I'm going to call it define, and then we can say is on the score strong on the score password. Okay? How about that? And now, of course, inside, we're going to accept one variable, which will be the actual password itself. Okay? And let's add colon. There you go. All right. Let us begin to add the criteria. The very first criteria here will be the length of the password. So we can say if and now the function len, which will represent the length of what off the password. Okay? So if it is less than eight, let's add our colon. What are we going to say? We're going to return false. Okay, we're going to return false. Actually false F, false comma. And then we can provide something like a feedback. Telling the user that, Hey, your password must be at least eight characters long. So let's do that. Okay. So notice right now two things that happened. First of all, we are returning fals meaning that the password isn't strong enough, and then we're also providing a feedback as well. Keep that in mind, it's going to be very, very important. Let's move on to the next criteria. So I'm going to come down here and then say if and now naught RE search, we want to search using the RE module. Let me just make sure I typed that correctly, Research. And now in brackets, we want to search to see if there are any uppercase letters within the string. Okay? So we're saying search and now Roy now codes, single codes, let's add our brackets. What are we searching for? We're searching for A to Z. So this right here is a method using the regular expressions library, we want to search through our password to check to see if there is any capital letter between A to Z. So A to Z, and then comma and then in password. Okay? So we can return fur false and why false? Because we said, If not. So if the program searches through the password and doesn't find a capital letter A to Z, it's going to be not. So we're going to return false, and then we can provide what a feedback. So I can say password must include include at least one uppercase letter. Okay? And let me add the dot right there, and there you go. Okay, now, can you try to write the code to search to see if a number 0-9 is in the password? Okay? We can say, if not, we dot search. Okay. And then in brackets R and now in single codes, I'm going to open up a new bracket now 0-1 to nine. And now we're searching for the password. We want to know if the password has a number 0-9. And of course, if it doesn't, we're going to return false. And then we can say password must include at least one digit. Let me change digits to number, actually. Okay. And there you go. So we've checked the length of the password. We've checked to make sure it has an uppercase letter. We've checked to make sure it has at least one number. Last but not least, let us check to see if it has a special symbol. So the last one, we're going to say, if not, re dot search. And now in brackets are singular codes. And now let's open up the main brackets. And now I am going to paste all the special symbols. Oh, there's quite a number of them, actually, okay? So I'm going to add that. Now comma and now password. Okay, add a colon. And now, if we don't have that I'm going to return falls. Okay. And then codes, I will say password must include at least one special symbol. Let me change character to symbol. Simba codes, and there it is. Okay, so we've checked the four main criteria. However, what if the password that was created is actually good? It's a strong password. So for that, I'm going to come over here and I'm going to return true. Okay? I'm going to return true, and we can even print out something that says password is strong. And there you go. Okay. So just to give you a very quick recap of what we've done over here, we imported the regular expressions library because we want to use a particular method of function, which is the search function to go through the password and check for specific kinds of criteria. So first of all, we check the length of the password. Is it less than eight? If it's less than eight, return false and then provide feedback that says, password must be at least eight characters long. And now we want to search to see if there is a capital letter between A to Z. So we use the method e dot search, and then in brackets R, codes, and then the brackets A to Z, and then password. And then we provide the feedback, and then we did the same to search for at least one number, and then we did the same to search for at least one special symbol. And then finally, if all four criteria were passed, we will return true, and then we would say password is strong. So now that we have the criteria listed out for the strength of the password, we now need to integrate it within the registration function and make sure that before the user's account is registered, the password actually passes the test. So right here, once we've asked the user to provide, the username and the password, before we begin opening up the user's dotexti file to store the credentials, let us introduce the password checker. So I can create a variable called is valid. We're going to check to see if the password is actually valid. Then also, we've been working with the feedback feedback or message, you can change the variable if you want to, but I'm going to go with feedback. I'm going to say is valid, underscore feedback, and I'm going to pass them into the function that we used to create. Password reach is what is strong, underscore password, and then in brackets, password. Okay. So now we can say if it's not valid, so if not is valid. So if the password they provided isn't strong enough, we can simply print, and then we can say password is not up too much space. Password is not strong enough. Okay. And then what else can we do? We can print the feedback saying, Hey, your password needs at least one number, or your password must include at least one upper gas letter. So I can come down here, add another print statement and just simply passing the feed back. And there you go. Last but not least, we are simply going to return and there you go. So that is the way how we can implement the password checker tool. We've listed out the criteria, length, number, acase letter, symbol, and now where you have the registration function, we've added, Okay, if the password isn't valid, say it's not strong enough and then also provide the feedback as well. Otherwise, simply return. So now let's check to see if it's working. Hopefully, it should. So let's run. Okay, I'm going to register. Use a name, let's say, Bob. Password, I'm going to go with capital LISA N six, one, two, three, B V. It's Enta and Awesome. It says password is not strong enough. Password must include at least one special symbol. Okay, I'm going to open up the user's dot textifle and make sure that, Okay, make sure Bob wasn't created. Awesome. Let's go back. And now, let us try to create an actual real account. So let's call Alice. Password, P O P three, four, SDR, and then the add symbol. I think this should be strong enough. There you go. Okay, awesome user. Is successful. And now, if I go back to my users TXT, there you go. Now we have Alice and her password. So congratulations. We have enhanced our program, our software to now include a password checker, as well. Join me in the very next video where we're going to go ahead and also learn how we can encrypt our passwords. I'll see you then. 73. Part 5 hashing the user passwords: Well, come back. So now let's add one more security feature to our program. And in here, we are going to include hashing. We're going to hash up pwords by making use of the SHA two, five, six, the secure hash algorithm with, of course, a data size of 256 bits. So to do this, we are going to have to import another library. And this one is going to be the hash lib. Let me just add a node here and I also import for hashing. And there you go. All right. Now, let us add it at the very top here the function to actually hash the password. Let me just add a node function to hash passwords. The name of the function, I can say define and we can go with hash password. Now, of course, inside the brackets, we're going to be working with the password parameter and then I fed in my column. I'll take a look at this to hash the password, you can simply say return Now, the name of the function itself in the hashlib module, its hashlib. Now dot sha two, five, six. Now in brackets, what are we hashing? It's the password. Now I'm going to use the method called code, open up new pair of brackets, close the previous brackets and now dot now finally we can had hex diigest add closing brackets again, and there it is. This is a particular line that you can memorize. This is the function that will actually hash up passwords by making use of the hashlib module. All we need to do right now very simply will be to integrate it into the actual registration function. Let us come all the way down here at the very end, where the password was successful, here where it says print feedback, return just before we begin to write the password to the users or text the file, let us now hash it. After the writen statement, I am going to add a small little note and we can say hash the password. All right. So what we want to do right now is very, very simply, we can create a variable called hash underscore password, and then we can pass the actual password that we've just hashed. I'll say hash on the score password and now in brackets, very simply pass word and there it is. All we have to do very simply is where we're writing the password. Instead of the original password, we'll now have to write the hashed password. Here it says file or write F, user name. Now in here instead of password, we're going to change that to the variable hashed on the score pass word, and there it is we've successfully added hashing. Let us see if this would actually work. I'm going to go ahead right now and run the program. Let us register. I'm going to add a new name in here. I'm going to call this one, let's say, David. Okay, password. I'm going to go with a very strong password in here, SOD, fish, SOD, fish, and then 234 at, and then sterics symbol, there you go. Now let's open up the users of textifle and C and wonderful. There you go. This right here is the hashed version of David's password, which is SOT fish, 234 at and the Asterics symbol. So wonderful, we have added hashing to our program. It's becoming more and more advanced, and now jump in the very next lesson where we're going to add our audit to feature CU then. So I just wanted to give you a quick update. Yes, we've just added the hashing function. However, when we test it out, you might notice that it doesn't work. So if I was to go over here right now and I wanted to register a new user and I said, let's call the new user Victor, right? Victor. And then I added a password, Victor 123, and then VC and then the ad symbol, and I press Enter. It does say that user registration was successful. However, if I try to log in as Victor again, so I have the user name. And now if I type in the password, Victor 123, Vc at you will see right now that it says invalid username or password. So for some reason, the login function it's not able to detect that the username matches the password. And this is one of the reasons why you really want to test out your code whenever you've added new functionality. So I realized that over here on line 60, where it says, where we have the login function, it says that if user name equals stored user name and password equals stored password, then we can allow the user to successfully log in. You can see right now that this isn't going to work because as the code is written right now, it is comparing the password to the plain text format and not the encrypted version or the hashed version rather. That's why we're having the issue. So we're supposed to come in right now and say, stud a name and now we stress the hash underscore password. And now in brackets, password. So this right here, we are now telling the program and telling Python that, Hey, compare the hashed password of the user and not the plain version. So it should work right now. If I was to go back and let's create a new account for the user. Actually, let's stop the program, okay? And I'm going to run it afresh all over again. So now let me register a new user, call the user Mac and then Mac one, two, three at Mac for the password. And now let's try to log in as Mac again. So I'm going to say Mac. And now Mac one, two, three at Mac. And now you can see the login was, in fact, successful. So this is, again, a reminder that whenever you add new functions and new lines of code, you want to test before begin adding more and more lines of code to your program. So please do go ahead and update the line where it says, If you use Nemicol start username and password, you want to make sure that it's the hashed version of the password that we are comparing. Thank you for watching, and of course, I will see you in the next class. 74. Part 6 adding the audit tool: Welcome back. So now we're going to add a really cool feature, which will be the audit tool feature where we can keep track of all the activities on our system from users, logining and creating accounts and things like that. So let's go ahead and do that. And for this to happen, we need to import a special library and module, which is going to be the date time. So going to come in here and I'm going to say from daytime library. Let us import the daytime module. Okay? And I can add a note in here that says input for date and time. Okay. Now, let us create our function, and where can we place that? You know what? Let's place the function just before the registration function, okay? So I'm going to come all the way down in here, add the note, and we can call this one the function to log user activities. Activities Act. Did not spell that correctly, Activts. Okay. Alright, I'm going to call this function defined, and then log underscore event. That's going to be the name of the function. And then the parameter will of course be event. Okay. Add a colon, and fine. So this is where the magic is going to happen. Let us pass the actual date and time into a variable called time stamp. Okay? I'm going to say time stamp equals. And now we're going to make use of a function from the daytime module. This function is called datetime dot now. Okay? So this is the function that will retrieve the actual date and time, okay? But now I'm going to add a method in here dot STR FT. This method right here is what we're going to use to actually format the date and time. So whether we want to display the year, the month, the day or maybe just the month day. And then the time, maybe we want to display the hour, the minute, the second, things like that. So we're going to use this method, the STR FT method. And now in brackets, we need to indicate how we want the date and time to be formatted. So inside, I'm going to add my coats. So let's do it like this, okay. We're going to display the year, the month, the day. That will be for the date. And then for the time, we'll simply display the hour minute and then the second. How about that? Okay? Sounds good. So to do this, I'm going to use the percentage symbol, and now Y. This will represent the year and then we can add minus and then percentage, and then M for the month, and then minus again, percentage for what for the day which will be D. Okay? And now a space, and now for the actual time itself, I'm going to go again, percentage, H for the hour, okay? And now instead of the minus, we can use the colon because I think it's better to use colons whenever you're dealing with actual time, in my opinion. We can use colon and then for the minute and then colon again, and then percentage or S, and there you go. This is the format, your month day, and then the hour minute and the second, and I think that should be fine. Now we got to do right now is we now need to start logging in all these activities. Okay, so what we're going to do is for the registration function, when the user has registered, we need to log the event, okay? Now, we need to come over here and say with open because we want to create an actual text file that will represent the audit log. So I'm going to say with open. And then in brackets, let's call the file audit. On the score log. Underscore log dot TXT. So this will be the name of the log file. And then what we want to do? We want to append it, okay? So I'm going to put the A mode, and then as what? As Log on the score file, and then add colon. And then what we want to do? We want to write to the Log file. I'll say Log on the Score file. Dot right. And now in brackets, we can use our F string, and then we can simply add colon, and then I'll add my bracket, colli braces, timestamp, and then the event, and this should be fine. There you go. So we're going to create a file called Audit Log dot TXT. And when we write into the file, we're going to write a timestamp. And then also the event. So maybe the user created an account at January 6, 2024 at 5:30 P.M. And 14 seconds, something like that, you know, so that's exactly what we've done over here. Awesome, awesome, awesome. So now we need to make sure that each activity is, in fact, logged. So down here, the first activity is going to be the registration, right? So let's check. Actually, you know what? We can even log when the password wasn't strong enough. How about that? Okay? We can also log that. So right over here where it says the print feedback, I can just come over here right now, add a new line and say log underscore event. And now in brackets, we can simply say registration field for user user name. Okay. I like this. What do you think? I think this is fine. Registration field for the user user name. So the registration wasn't successful because the password wasn't strong enough. I like that. So we've locked down over there. So now, let us log the event in a scenario where the registration was actually successful. And where is that? We have it on line 56 here that says print the user registration was successful. So just underneath it, we can now add our log underscore event. And now in brackets, we can simply say F and then let's have coats and then in our collie braces, we can say use a name. Okay. And then use a name successfully registered. How about that? And there you go. Okay. Now, what else do we need to log? We need to log when users have also logged in. And we have that over here under the login function where it says login successful. So in here, I'm going to create a new line, and now we can log the event, so log underscore event. And then in brackets, we can say F, string, and then add our coats, colli braces. So use a name, okay? Well, let me put some space in here. Use a name successfully logged in. And there you go. Is there anything else that we need to log? Okay, where it says the invalid username or password, we can also log this event in here as well. So we're logging everything, everything, okay? So log event F, and then we can add our codes, and then we can say failed login attempt for the user, and then with the user name and there you go. Okay. I am liking this thus far. Is there anything else we need to log? We can also log in the third option. Where the user actually exits the system, it's not necessary, but, you know, we might as well. So where it says print, exiting the system, we can just also log this. Actually, let me just do this before the print statement. Option number three, let's just log underscore event. And then we can just say something like system exit. Okay. And there you go. Okay, I think this should work. This should work. And I'm going to go ahead right now and run the program, ok and let us see. I'm going to register a new account, so I'm going to go with number one, press Enter. Let's add a user name. So I'm going to go with Tracy. Okay? And the password, Tracy 21, OP, hashtag. And there you go. Okay, awesome. So it was successful. Let's first of all, make sure that under the user's D TXT, we have Tracy. That's the hash password. And now the other Dc TXT, there you go. Awesome, awesome. Awesome. Tracy, successfully registered. And, of course, you can see the format right here, 2024, the tenth day of November at 9:01 08. So we successfully added the log feature as well. Thank you so much for watching the video. I will see you in the next class. 75. Part 7 adding the view logs function: Come back. So let's add one more feature. And this time, it's going to be a feature for our log in users. We want them to be able to view their own personal logs. So this is going to be in two steps. First, we'll have to create a function that would allow them to view their logs. And then, second, we'll have to create a post login menu. So our log in users, they will have a separate menu where they will have the option to view the log. So this is going to be in two parts. First of all, let us create the function allow them to view the log. So I'm going to do that just underneath the main login function. So let me just add a note in here that says function, sorry, function to view logs. Okay? I'm going to call this function view unders call logs. And of course, we're going to pass in the parameter user name. Okay, add my colon. Alright, so let's first of all, print out like a general message saying something like, you know, logs for user and then, you know, the particular user. So I'm going to say print. Okay? And then let's have a F string, and then let's have codes. And then let me add my backward slash N, and then we can say logs, four. User, and then we have our coll braces, and then we can have the user name. Okay, not necessary, but it's nice to have. Alright, now we're going to open up the audit log D textiFle because it's from there, we're going to pull in the logs. So I'm going to say with now open and now in brackets, what's the name of the file? It is audit unders called Log TXT. And now, of course, we want to have the read mode. Okay, because we're going to read from the file and we're going to say as Log On Score file at our column. Okay, so now we're going to go through the logs and the file. So let's pass them into a variable called logs. And now Log Underscore file dot redlines. Okay. And there it is. Okay. So now we're going to have to create some sort of a four loop that's going to go through each individual logs. And what exactly are we looking for? We're looking for any logs that will match a particular user name. So I'm going to come in here right now, right? And I'm going to say user logs, okay, underscore logs is going to be equal to, and now check this out. I'm going to open up at my brackets in here. I'm going to say log dot strip and the reason why I'm using the strip function is again, I want to remove any unnecessary white space or margins from the user names, so I'm going to say log or strip. And now I'm going to say four log in logs. So we're searching for each individual log in our logs. And what are we looking for? We're looking for if the user name is in the log. So we're looking for a very specific username in our log files. So now if we do, in fact, find a match for the log for the user name, what do we now do? So we're going to say, if user undercool logs add our column, and now I'm going to say four log in user Underscoe logs, what we want to do? We simply want to print. So I'm going to say print and then log, and there you go. And of course, we need to create a condition. What if no log was found? We can simply say else, and now we can print. We can say no logs were found. In your account because remember this is for the login user, and there you go. So just to give you a very quick recap, right? We defined the function as view dos call logs, and we passed in the user name. And now we printed out a general message saying, you know, logs for the user. And then the user name. And then we opened up the other log the text file. We're going to read from it as a log file, and then we created a variable called logs that will actually use the method read lines for each particular log file. And now in here, we created our four loop that's going to go through each individual log in the logs and then see if there is a match for that user name. Now, if there is in fact a match, we can simply print out the log for that particular user. Otherwise, we'll simply say no logs were found in their account. So there it is. We've successfully created a function to video logs. Join me in the next video where we're going to create the post login menu for our log in users. 76. Part 8 adding the post login menu: Well, come back. So now we're going to create the function that'll display the post login menu for our log in users with options. So let us do that just above the function to view the logs. So right here. I'm going to say function to display Post log in menu. Okay. Alright, so as usual, we're going to call this one, let's say, post on the score login on the score menu. I think that's a good name. And, of course, inside, we're going to pass in a perimeter user name at a colon. Okay, so we're going to create a while statement. So while the user is, in fact, logged in, let us now print out the options for them. So first of all, I'm actually going to print out, like, a regular message. So I can say something like login, I'm sorry, post login menu, right, Post, dash, login menu. Okay, and let me add my backwards N so that this is on the new line. Okay, so now we can print out the options. There's only going to be two of them. Okay? So option number one will be to simply view my logs. Remember this is personalized. It's for the log in user. And then option number two, well, logout I guess. And there it is. So we're going to have to ask the user which option they want to go for. So I'm going to say choice equals and then input, and then we can say something like, you know, what would you like to do? Question mark. Okay. So now we're going to have to create the IL statements for each scenario. So I'm going to go with the first one if if choice equals one, what do we want to do, want to view the logs, and of course, they use a name. So we're going to call in the function, which is view logs user name, so very, very simple. And then we can say, if choice equals two. And then what do we want to do in here? Well, we can simply print out a message that says logged out successfully. But then don't forget we want to log every task. So we're going to add the log event statement in here. So log event, and then we can say something like Fstring and then we can say user have a collie braces and single codes, user name logged out. And there you go. And, of course, don't forget we need the break statement shot in case the logged in user decides to, for some reason, choose option three, four, five, or any other option that doesn't exist. So we're going to say break and then else, we can just simply print out a message that says, invalid choice. Please try again. And there it is. Okay, so we have successfully now created the function that will display the post login menu. However, how do we actually link it to the login function? Because once the user logs in, we want to call this function. So what do we do? We're going to go over to the main login function. And now let's look at the code. Where has user logged in? It says right here, login successful. So right here where it says the login successful, I'm going to come over here right now and now call our function. So here, I'm going to say post underscore login, underscore menu. And, of course, in brackets, we're going to add the user name and there you go. So that's it. I think we have successfully finished writing our code. Let's now test it out. I'm going to run the code, okay? And I am going to login, first of all, as actually, you know what? I'm going to try something else, okay? I'm just register a new user. Okay? And I'm going to call this user Boris. Okay? And then pass will be Bois one, two, three with the ad symbol. Okay? So now let Boris log in. So Boris and then Boris one, two, three, with the ad symbol. And there you go, Yes, we can see Bois now has the post login menu, and can Bois view his logs, but going to press one and there you go, Logs for user bodies. Bodies successfully registered at 8:10 35. And then he successfully logged in at 8:10 45. And if we look at our users dot TXT, there you go. We have bodies with his hash password. And then over here we have the Alex successfully registered. John successfully logged in. Obviously, these are tests that I did off screen. But you can see right now that the program is in fact working successfully. There are a few other tests we can run. We can make sure that if I typed in option number five in here, okay? I typed in five and I pressed Enter, it can say invalid choice, please try again. So let me try to log out now and you can see it in fact successfully logged out. And if I press three at the main menu, you can see right now it has exited the system and program is in fact working properly. So there it is. Congratulations. We've successfully created a pretty advanced user registration system that would allow users to register. We've also implemented a password checker to ensure that the password is strong enough by having at least eight characters long, having one Opa case letter, one number, and also a special symbol. And, of course, we've also added additional security features to hash the password so that they're stored in a hashed format. And, of course, we also created a post login menu for the log in users where they can then view their own personal logs. That's it has been fun. Thank you so much for watching. I'll see you in the next class. 77. Final project conclusion: Alright, so congratulations. We've successfully built ourselves the user registration system, and I just wanted to give you a few more tips. Sul in case you wanted to maybe further enhance the program or make a few adjustments here and there. Instead of hashing the passwords, for example, you can try to encrypt them instead. So you can try to remove the hashing function and then replace it with the encryption function. Obviously, you will have to import the cryptography library in order to be able to do that. And then you can even add an additional option for the log in user besides them viewing the logs. You can create a function, a simple mathematical function that can calculate some sort of, you know, multiplication or addition. You can ask the user to provide two or three numbers, they provide them, and then the function can do some sort of a math, can maybe multiply them or add them, you know, something very, very basic. You can also add that as an extra option for the log in user. So there's a lot of different ways how you can further enhance or modify the user registration system that we've just built. So be creative, think outside the box, look for ways on how you can improve or modify the program. And if you do come up with some very interesting ideas, I'd love to hear from you. I'd love for you to share your code as well, share your code with the community, share your code with the rest of the students taking the course, as well. So that's pretty much it for the project. I hope you had a lot of fun, building out the user registration system, and hopefully you've also learned quite a lot, as well. So thank you for watching. Thank you for building out the user registration system with me. I'll see you in the next class. 78. Course Conclusion: Congratulations. You've come to the end of this course, Python for cybersecurity. And hopefully you found the course to be very useful, informative, educational, but also fun, as well. And hopefully, at this point, you now have the skills to write some programs with Python. Now if you have any questions about anything we covered in this course, as usual, I'll be more than happy to assist you and answer you and help you in any way that I can. Do not hesitate to reach out to me. And if you found this course useful, please don't forget to leave a written review for the course and let the world know that you enjoyed the course. So that's it. My name is Alex. It's been a pleasure. If you're going to take some other of my cybersecurity courses. Hopefully I will see you in those courses as well. But if this is goodbye, let me just say I wish you all the very best in your Python journey and also in your journey as a cybersecurity professional, I wish you nothing but success and plenty of even more success in the future. So Lassa, thank you so much for taking the course for Marlin and I'll see you next time. Bye.