Master Python Programming in 2021 | Andrei Dumitrescu | Skillshare

Playback Speed

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

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

69 Lessons (7h 4m)
    • 1. Welcome

    • 2. Why Python Programming? Why now?

    • 3. Intro to Python Installation

    • 4. Installing Python on Windows

    • 5. Installing PyCharm IDE

    • 6. Running Python Code in IDLE

    • 7. Running Python Code in PyCharm

    • 8. Running Python Code at Console

    • 9. Python Basics - Variables

    • 10. Python Basics - Comments

    • 11. Python Basics - Constants

    • 12. Python Basics - Statically vs. Dynamically Typing

    • 13. Python Basics - Variable Types

    • 14. Python Basics - Numbers and Math Operators

    • 15. Python Basics - Comparison Identity Operators

    • 16. Python Basics - Boolean Variables

    • 17. Python Basics - Boolean Operators

    • 18. Strings - Basics

    • 19. Strings - Get User Input

    • 20. Strings - Casting

    • 21. Strings - Indexing and Operations

    • 22. Strings - Slicing

    • 23. Strings - Formatting

    • 24. Strings - More About Printing

    • 25. Strings - Methods

    • 26. Flow Control - If Statement

    • 27. Flow Control - For Loops

    • 28. Flow Control - Ranges In Depth

    • 29. Flow Control - For, Continue and Pass

    • 30. Flow Control - For and Break

    • 31. Flow Control - While Loops

    • 32. Flow Control - While and Continue

    • 33. Flow Control - While and Break

    • 34. Lists - The Basics

    • 35. Lists - Operations

    • 36. Lists - Methods Part 1

    • 37. Lists - Methods Part 2

    • 38. Lists - Methods Part 3

    • 39. Lists - Split and Join

    • 40. Lists - List Comprehension

    • 41. Tuples - The Basics

    • 42. Tuples - Operations

    • 43. Tuples - Methods

    • 44. Tuples vs. Lists

    • 45. Sets - Basics

    • 46. Sets - Methods Part 1

    • 47. Sets - Methods Part 2

    • 48. Frozensets

    • 49. Dictionaries - The Basics

    • 50. Dictionaries - Working With

    • 51. Dictionaries - Methods and Operations

    • 52. Dictionaries - Dictionary and Set Comprehension

    • 53. Functions - The Basics

    • 54. Functions - The Return Statement

    • 55. Functions - Arguments

    • 56. Functions - Scope and Namespace

    • 57. The Special Constant None

    • 58. Lambda Expressions

    • 59. Files - Opening for Reading

    • 60. Files - Tell, Seek, Cursor

    • 61. Files - The With Keyword

    • 62. Files - Reading a File in a List

    • 63. Files - Writing

    • 64. Object Oriented Programming - The Basics

    • 65. Object Oriented Programming - Creating Classes and Objects

    • 66. Object Oriented Programming - The Constructor

    • 67. Object Oriented Programming - The Destructor

    • 68. Object Oriented Programming - Attributes

    • 69. Object Oriented Programming - Magic Methods

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

Community Generated

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





About This Class


***Fully updated for 2021*** This Python course covers every major Python key topic, including Strings, Flow Control, Lists, List Comprehension, Tuples, Sets, Dictionaries, Functions, Lambda Expressions, Object-Oriented Programming, Working with Files, and many more!

This course IS NOT like any other Python Programming course you can take online. At the end of this course, you will MASTER all the Python 3 key concepts starting from scratch and you'll be in the top Python Programmers.

Welcome to this practical Python Programming course for learning Python, the most in-demand programming language across the job market in 2021.

I am constantly updating the course to be comprehensive, but yet straightforward.

This course, taught by a Senior Software Engineer and Professional Trainer, is the only one you need in order to master the basics of Python. After this course, you'll have an in-depth knowledge of core and advanced Python 3 and can move into specific branches: Artificial Intelligence (AI), Machine Learning, Data Science, Network Automation, or Web Development.

Why this Python Programming course?

This Python course is a unique experience. There are tons of existing Python courses you can choose from, but this course is completely different.

You'll have access to my closed and exclusive Group in order to provide you with improved, faster and better support for your course-related questions. I am always available to guide you personally and answer any questions you might have.

Have you ever programmed before? Just starting out with Python Programming and have the desire to learn the employers' most requested skills of 2020 in a practical way? Perfect. Then, this course is the right one for you. This Python course will help you go from beginner to pro in Python. You'll boost your career with hands-on Python Programming Skills!

Or maybe you are coming from other programming languages such as Java or C++ and want to learn Python? I'm here to help you and we'll put all the pieces together. This Python Programming course has everything you need to know to become a proficient Python Developer.

In this course, you'll learn the best practices of the Python programming language and how to write code the "Pythonic" way. Together we will go deeper into Python and you will understand every key concept of Python deeply. Then, after taking this course, you will be able to code on your own.

This course is based on Python 3 and doesn't require prior Python Programming Knowledge. Everything is included within the course. This Python for beginners course teaches you the Python Language fast.

Why should you learn from me?

I'm a Practitioner and an Enthusiast. Welcome to the real world! I have almost 20 years of experience as a Software Engineer. Since I've started with Programming and Networking in the early 2000s, I've been using or studying the technology almost on a daily basis. I also have a bunch of certifications in the area of Programming or Networking: Python Institute Instructor, Cisco CCNP Routing & Switching, CCNP Security, CCNP Service Provider, Certified Instructor, etc

I'm an Educator and I  know how to make a Syllabus. For the last 10 years, I've trained thousands of people on Programming and Networking

★★★★★ " "This is actually one of the best courses around regarding Python. Andrei is taking the students from the basic stages all the way to advanced ones ." by Alex Pascu

The topics covered in this Complete Python Programming Bootcamp are:

- Installing Python 3 on Windows, Mac, and Linux

- Running Python code at the console, IDLE or PyCharm

- Variables, Constants, and Comments in Python

- Math, Comparison, Identity, and Logical Operators

- Built-in Types, Dynamically vs. Statically Typed Languages

- Strings in Python

- Lists in Python

- Tuples in Python

- Sets and Frozensets in Python

- Dictionaries in Python

- List, Set, and Dictionary Comprehension

- Flow Control

- User-defined Functions and Lambda Expressions

- Working with Text Files in Python

- Error and Exception Handling

- Object-Oriented Programming: Classes and Objects

At the end of this Python Programming Course, you'll also have acquired the prerequisite Python skills to move into specific branches: Artificial Intelligence (AI), Machine Learning, Data Science, Network Automation, or Web Development using frameworks like Django, Flask, or Pyramid.

If you want to take ONE COURSE to master Python Programming you should choose this one and you'll have no regrets!

Meet Your Teacher

Teacher Profile Image

Andrei Dumitrescu

DevOps Engineer and Professional Trainer


I've been a Network and Software Engineer for over 15 years, the typical profile of a DevOps Engineer.

I've cofounded Crystal Mind Academy, a Cisco Academy and professional training center in Romania,  that focuses on teaching cutting-edge technologies to students.

I have contributed to education in areas of programming, information security and operating systems. During the last 12 years more than 20,000 thousand students have participated in-person or online  training programs at Crystal Mind Academy. 

I have developed documentation, labs and case studies for many training programs such as Cisco CCNA, CCNA Security, CCNP, Linux Administration, Information Security, Python Programming, Network Automation with Python or Blockchain Programming (Ethereu... See full profile

Class Ratings

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

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

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.


1. Welcome: Hello and welcome to the practical Master Python programming the complete Python three boot camp course. This is the only question it in order to master all python three key concepts from stretch . It has everything you need to know to become a proficient python developer. This course is for you. If you want to learn python the most in demand programming language across the job market nowadays it is especially designed for you whether you have no programming experience it all. You've taken other private courses and you want to level up your skills with advanced topics. We are coming from other programming languages like C Plus plus or Java. Seems my first encounter with programming. I've spent thousands off hours studying tasting and coding, and it will be my pleasure to share this knowledge with you within this Chris, these hens on course goes straight to the point without any destruction and focuses solely on how to effectively learn python programming from scratch. Now a few words about me. My name is Andre Dumitrescu and I'm a software engineer with almost 20 years experience with programming, networking and Lennox administration. I have more than 10 years experience with in person programming and the network training's . Over the years, thousands off students have been enrolled in my in person or online courses. You can find more about me on my length in page. If you want to waste no more time with incomplete scripts or tutorials, copy paste solutions or confusing source called This course is for you. When you are ready to start your journey in tow. Problem programming. Click the enroll button or check the free preview lectures. See you in the course. 2. Why Python Programming? Why now?: congratulations. If you are watching this video, it means that you want to learn Python, the most in demand programming language across the job market in 2019. Python is one off the most used programming language in the world. It's the defector standard in network automation, artificial intelligence, machine learning, robotics date our science and so on. These fields are exploding with progress and new opportunities. Learning Python could be the best decision off your career. According to Cure Bay Index off programming language Popularity, Python is the programming language, or Fear 2018 by phone hit, the most increase in rating in one year. Then, according Toa, a Steak Overflow survey, bison is the fastest growing major programming language off the planet. Even compared toa other fast growing technologies. Python is dominating the programming landscape, and it's also projected to remain one of the most popular programming languages in the future. In 2016 Python has replaced Java, is the most popular language in colleges and universities, and sees event. It has never looked back. This being said, this is the best time for you to start learning python 3. Intro to Python Installation: in this section, I'll show you how to install the latest version off python on Windows. Lee Nooks and Make. After installing Python will take a look at how to run or execute python coat. Basically, we have three options for a running python coat directly in the Python Inter Prepper, and this option includes also the official Python Idol in an integrated development environment, or I D. And we'll take a look at how to create and run Python scripts using by charm, which is probably the most popular I d for private. And finally, we have the last option in the Rex to run private screens at the console or the command line. If you already have some experience with Typhon and no different ways to executive private script, you can't skip this section. 4. Installing Python on Windows: in this lecture, I'll show you how to install Python three on Windows 10. Typically, that involves downloading the windows installer and running it on your machine. If you are using another version off windows, the installation instructions are very similar. Now, if you're using another always like Llinas or MK, you can't keep this lecture. The first step for installing Python is to visit the official private website in the download. The latest version off Python, the official private website, is www dot python dot orc. I'll hover the mouse over the ballot the menu and download python for he 0.7 dot to. By the way, don't worry if you find here another version, the most important thing is the major version, which is Version three. This will be around for years and also the content off. This course will be 100% applicable and valid. However, if possible, use aversion greater than or equal to python 3.6. After bound loading the east dollar Lex double click the file and run it. A new window opens up with a new important sitting in order to avoid future problems. Select Ed Python 3.7 tow path. After that click on, he stole. Now, after a few seconds, depending on how fast reserve machine python will be installed on windows at the end off the installation we see the message set up was successful. Now let's test whether the installation was successful. Open Windows CMD or command line by simply typing CMD in your start menu. Then type by Ivan and heat enter. You should get a similar output toe the one you are seeing going to screen right now. Okay, Vexed all at this moment, we have the latest version off Python installed. 5. Installing PyCharm IDE: by charm is probably the most popular integrated development environment or i d. For Python on Integrated Development Environment is a software application that provides comprehensive facilities to computer programmers for software development on I D normally consists off a source code editor built automation tools and the D Bargor. Most off the modern ID's have intelligent code completion and error detection. We lose by charm throughout this course when we have to develop more complex pilot scripts with pence off lines, off coat or projects that consists off many python files or more. Juice by charm comes into additions. The professional addition, which is commercial software and the license is required, and a community addition, which is free and open source software we'll use throughout this course. The community addition, which has all needed features by charm. Installation, is very simple. In this lecture, I'll show you how to install pie charm community addition on Windows. First, we go to the official website, which is www dot a jetbrains dot com slash by China and click on download. Now then, which is the community addition and click on download and the installation kit is downloading after balloting. Theo Installation keep were on it. Next. This is the destination folder, and I won't change it. Next, I check the 64 bit launcher. I have a 60 bit operating system off course. I can also check the 32 bit launcher. I want to associate dot p y files toe by charm and ed launchers. Dear Toe the path and next and by charm is installing. Here you choose early. But now, Toby, put your system. I look a boat my system later because I don't want to break the recording. And I've just installed by charm on my Windows machine. Lex tested by opening by Chairman. I do not want to import any settings because this is the first installation off by charm. There are two user interfaces themes, and I'll choose the light one. Let's keep remaining and set the forks and by charm has just started. Now I have to create a project and python files inside my project. Vicks, all for the moment. I'll show you in another lecture how to create a project and how to run by transcripts using picture. Thank you 6. Running Python Code in IDLE: in this lecture, I'll show you how to run or execute python coat. There are several ways, and we'll take a look at each off for them first. If you want to test some basic concepts or executive just a few python instructions, you can write and execute them directly in the Python interpreter. And here you have two options. The first option is to open the interpreter from Windows Command Line or CMD, and write all python instructions there. Even if it works perfectly, this isn't recommended, or at least not for beginners. This is because it lacks the advanced features like auto completion or color, highlighting off cure works or errors. It will be harder for you to write or code here. However, This is how it's done. I'll go to start type CMD and hit. Enter now I'll write Python in Press the Enter key. The Python interpreter has just started, and here we can execute our python instructions. The second option when executing python using the Python interpreter, is to use idle, which stands for integrated development and learning environment. On Windows, Idol came by default with private installation, so it's a there toe open idol just type in idle in windows, lean looks or make start menu, and you'll see something like this. This is a python shell or inter prepper, where you can write and execute short scripts and see their output immediately in real time . Each line starts with Fergie greater when signs and this is idle. Command prompt throughout this course, when we see three greater when signs will know that the coat has been executed in Python Idol legs, right? Something very simple, like print Hello by phone. Congratulations. This was your first Python script. Now let's do something else. X equals six X. What I did is that I've created a variable called X that is equal to six and then printed out X value in Python Idol. It's not necessary to use the brain function to see the value off for variable. You can just type in its name off course. You can also use the print function. We don't focus at this moment on variables or other private programming. Concepts will have a lot off time in the next lectures. For this moment, I just want to show you how to execute python instructions in idle. Now, if you want to execute an instruction again, you simply go using the arrow keys, toe that line and press the enter key, and we see how the instruction has been copied on the last line. I can modify it and run it again if I want Another useful feature is the auto complete or worth completion? The application will predict terrorist or forward a user is typing. This time I'll create another variable that stores a string. A sequence off character's name equals and Andre between single quotes. And then I write. Name that, and I am waiting. Idle will open a pop up. Will all methods and attributes available to string objects one more time? Don't focus on the language. For the moment, just focus on Python Idol. If you want to modify the completions, pop up, Wait time Goto options. Configure Iittle General and change the value from their. For example, I have 100 milliseconds for completions. Pop up Wait time in this window. You can also change other settings like phone size and so on. If you want to save your work is a script or text file. Just goto file menu and then click on save or save is, and I write script one dot p y. I am saving the script on my desktop. Also, keep in mind that you can run as many simultaneous sessions off Idol as you want without one interfering with the other. They will be completely separate working environments. I am opening another Idol station and then file open, and my script that has been saved on my desktop in our course will use Idol a lot, especially for small disks and examples for more complex scripts will use by charm, which is a professional python i D or integrated development environment. 7. Running Python Code in PyCharm: in this lecture, I'll show you how to create and run Python script in Pitre. Ah Python script is nothing more than a text file with the dot P Y extension that contains python coat by Charm is probably the most popular integrated development environment or i D . For Python, it includes great features. Such is excellent code completion and inspection with Advanced Dee Barker and support for Web programming and various frameworks sections. Django and Flask. We'll use by charm throughout this course when we have to develop more complex private scripts with tense off lines, off coat or projects that consist off many Pilon files or modules. Using PI Charm is simple. Lex Open biker After opening Pie Charm, you basically create one or more projects, and inside each project you create python scripts, which are text files with dot P Y. Extension Ah project is a directory in your file system. I'll create the project simply called. My pipeline Project by charm will create by default Vehicle Environment, which is an is elated working environment for X project by charm, is creating the project. After creating the project, I'll right click on the project name, then new and Python file, and I'll create a file cold script one. If I write script one, it will create Infect a File Cold script one dot p y. It will automatically it the dot p y extension. This is my Python script. Now I'll write some python coat. Don't focus on the code for this moment, but only on how to run the script. Using Preacher and I write X Equals five and print X Star star Fife. It will print exe raced to the power off five. In fact five Raised to the power off five. Comparing toe Python Idol where if you write the name off a variable, it will automatically print X value here in pi Term X mandatory to use the brain function if you want to display the value or for variable now, I'll execute or run the script, and the code will be run within the I D environment. There are several options, right? Click anywhere in the script and then left. Click on Iran and the name off the script, and we see the result here. UNIX own console. Another option to run the script is to go toe Ron and click on run in the name of the script, and I've got the same result. There is also a shortcut, which is by default control shift and f them off course. You can change the shortcut or any other aspect off by charmed by going toe file and settings. Note a vet. If picture in the course off coat analyzes, encounters any errors, they are immediately reported. For example, print Why? This is an error and I've done it on purpose. The coat is underlined with the red, curvy line, and if I hover my mouse over this curvy line, detailed information about the error appears as a tool tip in this case on resolved reference Why I've used on undefined variable, and we can also see information about the error in the bike charm console. These were some basics about using pi charm, but you can find a lot off. Resource is in the pie charm documentation that is also a text toe. This lecture 8. Running Python Code at Console: in this lecture, I'd like to show you another way to execute Python scripts and the vics using the shell or the command line. This is especially useful when we connect to our remote machine or server using, for example, SS age and run the Python script on that machine. We don't have access to a graphical environment, toe open, python idol or pry charm to executor escaped. In fact, this is the only way to execute the script. In this case, let's create a Python script using any editor like note, bed or even by charm. In this example, I use an editor called Note Bed Plus Plus Here, inside the editor, I'll write some Python instructions and then save the script on my desktop. Let's say a equals four and print a multiplied by four. I am saving the script on my desktop is script to dot p y. Now I'll open CMD or Command prompt, so cmd goto desktop. Using the city comment, check the content off the card into directory by running the deer comment. Okay, there is a script called Script to and then run Python and the name off the script script toe dot B Y, and we see here the result. So a, which is four, multiplied by four. The script has been executed. If you are not in the same directory with the private script, you must use a valid, absolute or relative path. Otherwise you'll get an error. For example, I'll change the directory and try to run the script again. And there is an error, no socks, file or directory and the correct way toe executor script this python and a valid path. So they stop slash script toe dot b y, and we see the same result. Also take care that inlay knocks that there could be more versions off Python installed. In this case, you should choose the Desire Python version and execute the script using that version. Lexie. An example I have on SSX Connection Toe Olympics machine in here I have a private script. This is the private script, and I'll write Python and I'll press two times on the tab Key and I see there are many versions off Python Siphon to Python 2.7 python, $3.5 and so on, and I'll execute the script using Python 3.7 by phone fadeout seven and the name of the script and the script has been executed. If I try to execute, it escaped using a wrong version, for example, Python Tool. I could get on error. In this case. There is no error because this is a very simple pilot script that works both in Python to and Python three. 9. Python Basics - Variables: in this lecture will discuss about variables. What is a variable? How do we create a variable and how to use variables in Python of Variable? Is one off the fundamental concepts off any programming language? No matter if we are programming in C C plus plus Java, ph pure python will use very a bus to start the data The application is working with variables. Are how your program Remember surveil Use Toby More clear. I'd like to give you a simple example. Let's suppose you develop a simple script that converts miles kilometers. When running the script, it will ask for the number off miles. Then the user will enter a value. Lex Satan in the script will output the corresponding value in kilometers, which is 16.9 Because one mile represents 1.60 mine kilometers. The program must save somewhere the value 10 entered by the user, and it will save it in a variable somewhere in the memory in the ram memory so we can define a variable is a name for a memory, location or address and stores available in our example. Then Variable has a name and is the variable warp implies the value stored in it can be changed. In python, you simply create a new variable by typing the variable name, the equal sign and the value that will be stored in that variable, for example, Miles equals. Then I've created a variable named Miles that stores the value off them. Now, if you want to modify the value story in the miles variable, you right miles equals Alexei. 15 15 is the new value. Start in our variable will have a long discussion about the mutable and immutable variables , which means what happens with the variable when they modify it, but we won't enter into details now. You can also create multiple variables. In a single line like this, a comma B comma C equals one comma. Five. Kalmunai. I've created three variables. A BNC with the value off 15 and nine. You can see that a is one B is five and see his nine off course. Variables can store also other type of data, not only numbers. Let's create a variable called age end of variable called name. The value start in the age variable will be 30 and the value stored in the name of Variable will be John H equals 30 and name equals John. I've created two variables of variable called age that stores the integer value off 30 and another variable called name that stores a string or sequence off characters with the value off John. Often you'll see terms like defining of variable declaring a variable or initializing variable in other programming languages like, for example, E c or C plus. Plus, there is a very clear distinction between declaring of variable and defining of variable declaring means introducing a new name and type, for example, in two miles, which means a new variable called Miles will be created and it will store an integer value and the defining means allocating value. Toe that variable, for example, Miles equals then imp Ivan. We don't have sexual distinction because we don't explicitly specify the type off data. The variable will story variable is also sometimes called a name and can store any type off value Ah, whole number or integer ah floating point number, or simply float a sequence off characters or a string a least a referenced or file and so on. Now let's see some rules about naming variable names can contain lepers, digits and the underscore character. A variable name can't start with a number. It can Onley start with underscore or letters. If it starts with an underscore, it has special meaning tope Ivan, so you should avoid it. This is a valid name for a variable. But this isn't a violet name for a variable Vicks, because we can't start variable. With a digit off course, we can use digits, but not is the first character off the variable name, so you for is a valid name. Then you must know that we can't use special characters in variable names like spaces, commas, colons, exclamation marks for emphasis, square brackets, curry braces and so on. For example, your space age equals then isn't a valid name. I've used space between your and eight and Vex not permitted. I can use on amber score, and that would be okay. This is a valid name. You should also avoid using works that have special meaning important, like least str set four and so on. So this is not recommended. This is a valid name, but as you can see, either use a special color for representing the str name Vex because str has a special meaning toe private. That's okay, but not recommended. You can use str one and Vicks Better by phone is case sensitive. It makes a distinction between lower and uppercase letter, and so are variable names. If I have a variable called Age 40 and another variable with an upper case, a also named age 60. In fact, here I've created two variables. Sometimes the Camel case notation is used. That mystery group works and then each worth in the middle of the phrase begins with a capital leper with no intervening spaces or punctuation like, for example, marks permitted value. Let's say 100. This is Camel case notation or your age equal lists. 20. Pep eight, which introduces a standard style guide for private coat, doesn't recommend using Camel case, but words separated by underscores which is known is Neko case. So the recommended way is to use a name like Max Underline permitted Underline Vale equals 100 or your underlying age equals 20 off course. You don't have to follow exactly these conventions as long as you try to remain consistent within your own code. These are best practices also recommended by Pep eight, which is the style guide for private coat. And it would be great to take a look at this document and see what are the main points you can search for Pep eight on Google in the first website will be the Pep Eight style Guide for Python Coat. Here we find many advices on how to write readable and high quality python coat Vicks all about variable basics, and in the next lectures will take a look at Comex Constants and Variables types. See you in just a few seconds. 10. Python Basics - Comments: in this lecture will take a look at comments in Python. When working with any programming language you include comments in the coat. Do not eight your work. These documents what certain parks off the code are four and the legs. Other developers you included know what you intended to do when you're out of the coat. This is a very good practice, and good developers make heavy use off comics. Without them, things can get really confusing. Comex in Python, start with the hash character and extent toe the end off the physical line. A comment may appear at the start off a line or following white space or coat, but not within a Stingley tangle. Ah, Hesh character within a string Literal is just the hash character. Comics are to clarify coat and are not interpreted by Python. Let's see examples. This line is a comment. Let's try another comment. A equals four. The entire line is also a comment and Vicks because it starts with a hash sign. If I write X equals nine here I am creating a new variable. But after that, I am starting a comment. In other programming languages, there are ways to comment on entire block off coat here in Python. If you want to comment multiple lines, you prefix each line. You want to comment with a hash sign in an I D like by charm, you can use a shortcut and vex control, plus forward slash toe comment out an entire block off coat by commenting each individual line or for that block off goat. So if I want to comment out these three lines, I can write a Hesh character in front off each instruction. So at the beginning, off the line, or I can use a shortcut for that. I am selecting the code. I want to comment, and I'm pressing on control and for our two slash, and it commented out the entire block off coat if I want toe un comment this block off coat . I am selecting the block off coat, and I am pressing one more time on control and for R slash and the block off coat has bean un commented vex all about comics in Python. Thank you 11. Python Basics - Constants: If you are coming from other programming languages, maybe wonder how do we define a constant in python? There are values in the real world that will never change. The mathematical constant pi, with three decimal places, will always be 3.1 for one. A day will always have 24 hours, and the boiling point off water at sea level will always be 100 degrees Celsius, or 212 F. These variables are known as constants by Ethan does not have anything in the language to declare a constant. A constant is declared like any other variable, using its name the equal sign in the village. In general, constant values are often written with upper case letters to indicate the vet. Let's declare some constants by equals 3.1 for two hours in a day equals 24 and water boiling point equals 100 degrees. All these are constants. I've declared them exactly like any other variable, but I've used uppercase letters 12. Python Basics - Statically vs. Dynamically Typing: in the last lecture. We've talked about the variables. Basics. What is a variable? What is the purpose off for variable, and how can we define variables? Now we'll take a look at what type of data can be stored in a variable. Python is a dynamically typed programming language, in contrast to C C plus plus or java, which are statically typed. Language is statically typed, if with a type off, a variable is known at compile time, so before running the script. And this means that the programmer must specify the type off. The data that will be stored in a variable when he or she creates the variable in a statically typed language like Java variable would be declared is, for example, in score and then defined is score, he calls them. This is how we declare and define variable in a statically typed language. A language like python is dynamically type, and it means that the type is associated with runtime values and not with named variables. So values start in variables have type and not the name or the variable itself. This means that you, as a programmer, can write a little bit quicker because you never have to specify variable types. In python, we create a variable named score simply by writing score equals. Then we can see what type off data of variable stores using the building type function. Each variable or object of variable is in fact, an object off a class, his a type of value and an I D. And they are constant doing variable lifetime. So type if score, and we can see that the value stored in this variable is off type integer, or int. If I create another variable X, a name equals hombre in the least lexei, my friends equals in our least. We'll talk a lot about the least in another section, and for this moment it's enough for you to know that, at least is another type of data that stores a sequence off object. We can check the type off the value start even named variable by typing type and the name, and you can see that the value is off type str or a stink. Then pipe my friends and we can see that the variable stores a list. If I have a variable X, A pi equals three point for one pipe off by equals float. This is afloat. Number a number with decimals. Earlier, I said that variable is a constant idee. What is that i d and how do we get it? There is a function of building function, So the function that's already defined, we just use it simply called idee. And this function has one argument index the object. This building function returns the memory address where the value referenced by the variable is start Or simply said this is the address off the variable in memory. The value start by this variable is saved in memory. At these others, we can also see a hex, a decimal value if we like. That is another building function hex and then I d off name. This is the Hexi Decimal address off the value that is start in that variable. Strictly speaking, if I write, my var equals 10 My barn is not equal to 10. Instead, my body is a reference to an integer object containing the villa tin and is located it. The memory address returned by the I. D function is a conclusion in the colloquial programmers Language X said that my verse equal Stan But in fact, my verse is an alias or a reference toe on object. Start at the address returned by the I. D building function. So at least address in the next lecture will talk about variable types. See you in just a few seconds. 13. Python Basics - Variable Types: Welcome back, everyone toe this lecture on variables and types in Python. Now that you know what the dynamically typed programming language is, let's see what basic data types do we have in private. The pipe will be associated with the value. Start in the variable at runtime. Well, quickly discuss about the main python data types, and then our lectures will go into more detail about each off them. We have numbers, integers or whole numbers and floating point numbers. Or simply float. For example. Age equals 20. This is on indexer and then miles equals 4.3. This is afloat. Another type of data are the bull. Ian's bullion are logical values indicating false to or non lexei. X equals to the type off X is bull or type off three to equal signs. Three. So three is equal to three This expression evil weeks toe and the type off through is bull We have then strings. Strings are ordered sequence off characters. Lexei name equals company. This is a stink pipe off name str Then we have really six lexei years equals in between square brackets. I have 2001 2002 in 2003 and between values, I use comas. Type off years least then have topples. Topples are similar to lists but are immutable sequence off objects where risks are immutable. So let's say 81 equals and now between parentheses. One. John and Lexei free. This is a purple, the type off. The data stored in T one is a couple. Then we have sex. They are mutable collection off a northern unique objects. Place, Greater said called vowels, equals in between curly braces. I write a E Oh, and you, this is a set or set contains only unique items or elements. Another pipe off data is the dictionary. A dictionary is a collection off a Norbert Key value pairs. Let's create a dictionary called person equals and between a pair off curly braces. I have a key cold name in the value Lexei Andre, another key called Location and the Value Lexei Europe. Another key. Cold, Let's say age and the value 40. This is a dictionary. Now that we've seen, what are the main data types Off python core programming language will move forward, and in the next two lectures will take a closer look at each off them 14. Python Basics - Numbers and Math Operators: in this lecture will take a look at the numbers and meth operators in Python. There are three types off numbers. Integers, which are whole numbers floating point numbers, or floats, which are numbers positive or negative containing one or more decimals and complex numbers . Complex numbers are used in electrical engineering, and we won't discuss about them in our course, instead will concentrate ourselves on integers and floats. First, you must understand what is an operator on operator is a symbol off the programming language, which is ableto operate on values. There are with marty cooperators, assignment operators, comparison operators, identity operators and logical operators. Now let's take them one by one. Or if Matic operators are used with numeric values to perform common mathematical operations, there are the following or, if Matic operators addition, subtraction, division floor or integer division, multiplication exponents, station or a raising to a power and modules or mood. Let's see examples with each offer Them I create a variable called price equals five, and then I write price plus six, and the result is 11. If I want to subtract from a variable, I write price minus free. Integers are positive or negative values, so I can also have four minus then. Now Lexie afloat. Let's take, for example, a equals 3.4 and b equals 2.3, and I'm calculating a plus B. This is the result a minus bi or a divided by B. We also have the floor division that always returns an integer. So if I write a to forward slashes, which is the floor division and be the result is one because it only returns ah, whole number. Then we have the multiplication lexei price multiplied by three or a multiplied by B or five multiplied by 500 the explanation or raising to a power. For example, Let's see how many I P v six there are, so to tow the power off 128. This is because on ITV seeks uses 128 Biggs, and this is the number. This is a very big number, so there are a lot off I P V six in the world. Now let's move to another cooperator and vexed. The models or simply moat eatery purse were a remainder off the division, off left or parent by the right, so only the remainder If we divide 10 by four, the quotient is too, and the remainder is also toe, so the more operator will return to, Let's try another example, eight more five end vexed three Vicks because five goes one time in eight and the remainder is free. 14 month for is to four goes three times in 14 and there is a remainder off to. If we use in a mathematical expression int and float, the result will be afloat. So five multiplied by 3.0 is 15.0. So this is a float because this is a float. Now I want to talk about assignment operators, which are used to assign values to variables. The assignment operators are simple Assignment. The Equal Sign, the Increment Assignment, the Day Clement assignment, the multiplication assignment, the division assignment, the power assignment. The module use assignment ended the floor division assignment. Let's try examples with each off for them. Lexi First, a simple assignment. A equals one. It assigns the very from the right hand side off the equal sign toe the variable on the left hand side. Then, if I write a equals a plus one first, it will evaluate the expression from the right 10 site and vex a plus one. The result will be, too, and then it will assign the value off to tow A. So a will be, too. Instead off writing a equals a plus one. I could simply write a plus equals one. In this case, it EDS one toe the current value off A, which is too. So the result will be three and will assign that result So three to a so a we'll be three. But if I write a plus equals five, the result will be eight. So, in fact, it will add five toe the value off A, which is three. So the new value off a is ate. The same happens with the deck Ament assignment. A minus equals six. This means it will substructure six from the value off A, which is eight. The result will be to and will assign her that result that value toe the a variable. If I write, a minus equals seven, a will be minus five, so it will substructure several from to, and the result will be assigned to a Lexi do multiplication assignment. A star equals five. The value off A is minus five minus five, multiplied by five is minus 25 so a will be minus 25. Now if I write a star equals one, a will remain minus 25. Let's try now the division assignment. It will divide the variable by a value and a sign. The result. Toe that variable toe the same variable. A four r slash equals five. So first it will divide a by five, and the result will be minus five because A is minus 25 will assign that value toe A So a will be minus five. No, I want to show the power assignment. Let's take another variable, be equal stool and then be star star, he quotes for What is the value off be first Python calculated two to the power off four and the Vex 16 and a signed that value. Toby. So be is 16. Lexie. The floor division assignment Lexei C equals 15 and I'll see to forward. Slash is this is the floor division operator in private on equals. Three. What will be the value off? See? See will be five Vex because it calculated 15 divided by three is integers and the result, which is five, has been assigned to see. Okay, see, he's five. Please note that we don't have a plus plus or minus minus operator in private if we want to Incremental number. We can simply write something like this, like steak C plus Equal toe one and C is six in C plus plus or in Java. We could simply write C plus place. This is not permitted in private. This is an invalid syntax. There are also some pre defined building functions that perform basic meth operations. These functions are built in or they are already defined. We don't have to define them. We simply use them. We have the diva mode function. This function finds the Kocian and they remained there, simultaneously flexed, right? So they've mud off 15 coma, for this is the cautioned and bike ocean. I mean the result off 15 divided by five and this is the remainder. So the result off 15 more five. Then we have the P. O. W function. It raises a number to a certain power. So instead, off to Star Star 10 I can write POW Toe Co Martin and I've got the same result and instead off 10 Star Star 10. I can write POW, then coma thin, and I've got the same result. When we have the around function, it surrounds a number toe a certain decimal point. For example, let's calculate 18 divided by 13 and we've got this number that has many decimal digits. If I wanted to display this number on Lee with three decimal digits, I can write around 18 divided by 13 coma. Three. So with three digits. Okay, and it surrounded the number off course. If we have a variable a one point and a lot off the jig's, I can have round A with four digits. So it displayed on Lee the 1st 4 digits. It didn't modify the variable. The variable remained the same. It only displayed the value off variable with that number off decimals. Then we have another function called some is. Its name implies it will calculate a some extreme example. First, let's create a least lexei numbers equals and then one coma. Five. Coma. 66. Call my 100 then some off numbers, and this is the result. The sum function takes. Anita Rabel is argument. We'll talk about it or Abel's in the next lecture. Then we have the max function that retails the maximum value from a least off values. So let's say Max off four to 1000 55 and minus one, and the maximum value is 1000. And there is also the mean function. Veteran tackles the minimum value so minus seven for minus 10. 500 off course, the minimum value is minus. Then that's enough for the moment in the next lecture will talk about comparison and identity operators. See you in a minute. 15. Python Basics - Comparison Identity Operators: in this lecture will talk about Python comparison operators. Comparison operators are used to compare to values, and it either returns to or Fels according to the condition the first comparison operator is equal to or to equal signs it returns. True, if both open ranks are equal, the open bionics are the values that the operator operates on. So if I have two plus three here, the plus sign is the operator that performs addition. Toe and three are the open ranks, and five is the output off the operation. Now Big Toe are equal toe operator. Let's write something like this 22 equal signs and two and it returned. True because two is equal toe. But if I write a equals five B equals six, a double equal science, be it returned force because A is not equal. Toby the same is valid, also forced things. So if I have a string lexei name equal to then and another string Lexie address equals London, if I write name double equal signs address, I'll get force. Then we have not equal toe. We use on exclamation mark and the equal sign. Let's try an example So a not equal Toby. And the result is, too, because A is not equal Toby the same four name, not equal toe address. Another operator is greater. Then it returns. True, if the left apparent is greater than the right. So let's say a great part of N B Force A is not greater than be, but a is less than B. Let's have another variable see equals five, in this case, a greater than or equal to see returns. True, because A is five and C is five, so a is greater than or equal to see a less than or equal to see returned also to, but a greater than be returned false vex. Because A is not greater than or equal Toby. These are Python comparison operators. Now let's take a look at identity operators. There are ease and is not operators they don't compare. The values start in variables, but the memory address referenced by the variables. So if we write a is B, Italy turned true if both variables are safety at the same memory address and false otherwise before trying some examples with identity operators, let's talk about the mutability and emit ability. Let's dive into some details. The value off a mutable variable can be changed after it has been created, but the value, often immutable variable cannot be changed if we try to change the value. Often inimitable variable python will create Infect, a new variable that stores the exchange value. Integers, floats, strengths topples frozen. Six are all immutable variables, and this means that they can't be changed List. Six. Sex and dictionaries are immutable and they can be changed. Let's see some examples. Here I create an integer variable afloat variable and a string variable. All these variables are immutable. Their value can't be changed. Lexie, what is the address? Where the a variable we start. This is the address. This is the address off B, and this is the address off name. And now let's try to change them. A equals to eight. As you can see, there is another arteries. In fact, this is another variable. The old a variable had this address and the new a variable has this Argus Another address and the same. He's for float B plus equal tool for idea off B. And we got this value. We can see there is another value. The new address off B is different from this value. If I want toe modify the name variable lexei name equals, then we can see that it is saved at another location. We can see how this value is different from this value, so the value often integer, float or staying variable can't be changed. But let's take a list for the moment. It's enough to know that, at least, is a sequence off ordered objects any kind off objects. So let's say names equals, and I have here three names. I use square brackets and between elements. I have comas. This is the least and the disease. It's address, and now I'll create another leased using the copy function. This function will create a copy off a list. So let's say new names equals name, not copy. Sorry, I have here names. This is the names list, and this is the new names list. As you can see, they contain the same values. So if I write names is equal to new names, I'll obtain too, because they have the same values. But if I write names, these new names, I've got force because they are different variables or different objects, they are saved at different addresses. Now let's see the last example off this lecture. I have a new least named the languages with four elements. This is our list, ekes ID's this value, and I want toe ed a new element. Toe this list. If we want toe, add another element toe a least. The upend method is used. So here I write to Lexie JavaScript. This is my least one more time we can see the new element, and now let's check its address. As you can see, it has the same others, even if I've added a new element. Rex. Because lists are immutable objects, there are also other pipes off operators, but we'll discuss in detail about them in other lectures. They are logical operators which are end or and not membership operators, which are in and not in and be twice operators used to perform be twice operations. On buying are the numbers that involved the manipulation off individual bits 16. Python Basics - Boolean Variables: hello and welcome toe this lecture on bullion. There are Boolean variables and Boolean expressions. A Boolean variable is in fact an object off the bull class, which is an end or integer subclass. So a boolean variable is a kind off integer, a more specialized integer in python. They are to bullion constants defined to always written with uppercase tea and false written with upper case F. They can also be interpreted as inter exorcise one and zero in Pilon. There is also another constant called none that is frequently used to represent the abscess . Off of value is when default arguments are not best toe a function. If we cast or convert a bull toe on end, we get one for true and zero for force. So int off through is one and ink off. False is zero. We can use double equal signs or these operators toe test on expression, and that's because the arteries off to or force is constant. It's the same during program execution. If I want to see what is the address off tool? I got this value. Then let's try four. Great. That event. Three off course. This is true. And if I want to see What's the address off this expression? I've got the same village. So in fact, it's the same. If I write for greater than three equals equals two or four greater than three East through ekes the same. The value off to is one, and the value off false is zero but true and one respectively. Force and zero are not the same objects or are not safe at the same memory. Others so true equals equals one that's true but true. He's one is force because the address off one is not the same is the address off to Let's try other examples. Three equals equals false that's false. Four between single coats equals equals for is also forced because type matters. Here we have a string written between single Cokes and this is an integer. So we are comparing a string to an integer and the result is false. Then a comma B equals one comma too. A great thought of MB be less than or equal to a and both expressions are evaluated toe false. Hey, great talk of them be be equal Sequels to the first expression is evaluated toe false and the second expression is too being a pipe off integer bull variables have all properties and methods off integers, but also some specialized ones like End and Or maybe it doesn't make a lot off sense to write something like this near coat. But you could in order to make a point, and the vics that bulls are integers. Any integer operation also works with bulls. So if I write to greater than force vexed to because one he's great, that of any zero I can write X equals through plus through X is too, because I have one plus one equals two or 100 multiplied by false. He's a zero because I have multiplied 100 by zero. Now let's talk about toothy nous off variables or objects. All python objects have an associate ID. Truth value. We get the truth value off any object by calling the bull function, and the object is an argument. So if we have one integer bull off, zero is false and bull off X is two for any value off X for a float, we have the same. So bull off 0.0 is false and bull off any other value is to for sequences, strengths, lists and Papua's. We have force if the sequence is empty and to otherwise for dictionaries and six we have force if they are empty and through otherwise and for custom classes. So classes that we defined by ourselves we must implement the Dunder Bull and Dunder Len methods. We'll talk more about this in a lecture where we'll discuss about object oriented programming. Victor code here A is one So bull off one is true. Bull off zero is false. Bull off minus one is also true. In fact, bull off any number that is different from zero is too. Now let's have a string. So bull off Alexei here, Python dextrose but bull off on empty string. Expos now for a least, likes a least one equals. And here I have on empty leased bull off least one is false, but bull off any non empty leased. So let's say one come out. Oh, come on three decks through. It's important to understand and use correctly. The truth. Iness often object because it's used sometimes when testing expressions something like this like, say, prices equals And here I have a list 100 comma 200. And now, if prices don't worry, we'll discuss in detail about thief in another lecture. Brent Prices. Truth value. It means bull off prices equals true else. Prices, truth value, false and extol. Because prices it's not an empty leased and bull off prices equals two. Now Lex empty the least and the legs executed the code one more time. Price is truth. Value is false because the least is empty in the next lecture will talk about Boolean operators. See you in just a few seconds. 17. Python Basics - Boolean Operators: in this lecture will take a look at bullion expressions and operators. A Boolean expression is a logical statement that is either true or false. Bullion. Expressions are very useful in wild loops or in if testing conditions, about which will discuss in detail in following lectures. There are three billion or a logical operators in python and or and not both end and or operators need to expressions and the final result will be evaluated toe true or false. Let's take a look first at end Operator. The bullion end operator returns toe if both expressions from the left hand side and from the right hand side return toe. If any expression is force, the entire expression is evaluated. Toe force legs, right child age. This is a variable equals six and now child H greater than zero and child age less than 18 . It will return true because both expressions are evaluated toe. This expression is through and this expression is toe. If one expression evaluated toe forced the entire expression evaluated toe force, the same python expression could be written in the following way. Zero less than child age lesser than 18. But in my opinion, this is not very readable, so using the end operator makes the code more readable. Let's see other examples. A equals equals a and free, greater than or equal to three. And it's too, because both expressions are evaluated toe. But if I write a equals, then and then a greater oven seeks vexed through and like, say, mine less than seven. The second expression from the right hand side, this one will be evaluated toe force, so the entire expression will be evaluated. Toe force. In a nutshell. We can say that the entire expression will be evaluated toe on Lee when both expressions from the left hand side and from the right hand side are evaluated toe in a boolean expression, you can use parentheses to improve your code's readability so you can write something like this. A equals equals seven, and a greater then. Then the result will be forced because both expressions will be evaluated toe force and I've used for emphases. Now let's talk about or operator the bullion or operator returns to. If any open is to eat, will return false, if any, expression or operandi. A volley weeks to force. Let's try some examples Let's say a comma B equals one Comotto. So a is one and B is toe and now a double Equal signs six or be great that of n zero. The expression has been evaluated toe. True, because at least one operas or one expression has been evaluated toe, and this is the expression that has been evaluated. Toe, so be is greater than zero. But if I write a double equal Science six or be less than zero, the result will be false. Because both expressions have bean evaluated toe force. Let's try another example. A is not equal to six or B is less than zero. The result will be true because this expression has been evaluated toe true and the last Boolean operator is not, and it will simply negate the expression both value. If I write not to equal Sequels to I've Got folks because two equals equals two evaluates to true and not true equals force, not force equals true, not true equals force. We can also have something like this. In this example, it negated on Lee the first expression, not the result off the Empire expression. Let's take another example toe make it clear, let's say a equal sequel, Swan or be equal Sequels to. Of course, this expression evaluates to true and now legs right here, not in the same expression. It will negate the first expression. So it will be false or true, and the result will be true. If I want to negate the empire expression, I must use parentheses. That's all about Boolean variables and operators. See you in the next lecture. 18. Strings - Basics: Hey guys now will start talking about strings. First drink is on order sequence off characters. It can contain any Unicode character, meaning letters, numbers, white spaces, special characters like the question mark, the dollar sign or even Chinese characters. In fact, Unicode is, um, asking super set. They are more than 130,000 Unicode characters now looks great, a variable that stores a string value. For that, we can use single or double coats. My stay R one equals and between single quotes, I write, I learn python. This is a string variable or an object off type str I can also use double coax like, say, my str two equals I learn python. There is no difference between using single or double coats. What is important to you is to try to remain consistent within your own coat. For example, I used mainly single coats. If we want to display a string at the console, we use the built in print function in python idle. When we write the name of the variable, it automatically displace it, but in pie charm or another I D. We can displayed on Lee using the print function. If I write the name off the variable, so my str one. It will automatically display X content off course. I can also use the brain function in pie charm or another I d. We must use the print function if we want to display the content off a string variable. Now I'll show you how to use coats inside coats. Let's say I want a viable Hello equals. Hi there, I'm Andre. This is an error. I've got an invalid syntax vex because I can't use coats inside coats or at least single coat. Inside single cooks, there are two possibilities. Toe have coats inside coats. We use a beck slash before the single coat character, or we define our string using double coats. In fact, we can use single coats inside double Cokes and double coats inside single coats so I can have something like this. A backslash character here and that's OK or another possibility is to use double coats instead off single coats and inside double coats. I can use single cooks without problem. This is another possibility, or we can use double coats inside single coats, something like this. There are also triple coats that are used mainly for creating doc strings, which is a way off documenting classes and functions. We'll see later how to create and use docks, drinks people. Quotes can also be used to comment out more lines if we don't want to use a hash sign for each line anyway, Pep eight recommends using a hash sign for each commented line and not using people coats. Let's try an example here in Pi charm. Lex A a equals six b equals seven. In our print, A plus B equals and after the ending single coat, I use a coma and a plus B. I'm running the scraped it displayed a plus B equals 13 and now I want toe comment out these lines. The recommended way is to use a Hesh character for each line, so this is the recommended way. But if I don't like using a Hesh character for each line, I can simply in close my commented out text or instructions between people cooks the 123 and here at the end, off the commended lines. One more time people Cokes and here I write print something here and I'm running the script . We can see it has only displayed something here because these lines are commended out. And lastly, we use people coats if we want to enter a string on multiple lines. Less greatest thing Here s one equals 123 And now I learn. Enter Python, Enter Java and go. 123 enter. This is a stink. If I write s one impressed the enter key. We can see our strength. Print this one. This is my string on multiple lines, but it seems we have something strange here. What are these? Beck slash ends. Beck slash M is called an escape sequence and is an asking coat for a new line that is being created at that point in the string, it's infected the enter key with pressed At that point, the print function interprets. And this place, Becks elation is a new line. That's why we see our string on multiple lines when we print it using the print function. Lexi Other examples Hello equals Hello, Becks. Elation World and hello. We can see the backslash in but when I print the content off the string using the print function so print Hello. I LI see a new line here a single beck slash character is used to escape characters that otherwise have a special meaning tope item, such as a new line backslash itself or over quote character. So, for example, legs suppose I want a variable called the in four end between single codes. I want something like this. Beck slash and means a new life temper in our print in for and there is no back slash and because bring interpreted beck slash End is a new line. So is on N Parky breast here? How can I display beg? Slash n literally. For that, I use a big slash character is an escape. So here I write one more backslash character. It means it will cancel the special meaning off the character that follows in this case backs elation, won't have a special meaning. And now print info. Beck slash and means a new life. I can also have something like this. So print I'm Andre is we've seen earlier. This is an error, but I can escape this single coat using a backslash, and now everything's fine. It will work like a charm. So remember Becks Lish cancels the special meaning off the character that follows Vicks, all for the moment, about strengths, basics and in the next lecture will see how can we get the user input in python applications. 19. Strings - Get User Input: Now that we are all done with strings basics, let's take a look at how to get user input. There are hardly any programs without any input. In most cases, the input comes from the keyboard. Let me show you a simple and common example. Here I am connected using Ssh Toe Olympics machine and I want toe honest all an application for of it. I'm going to use the A P T package manager off Mobuto pseudo a PT remove and map, by the way, a P T and a PTT get our python applications. And here the application is asking for the user input I must enter. Why, from yes or N from? No. This is the point where the application is asking for the user input for the purpose of getting user input. Python provides a pre defined building function named Input Input has an optional argument , which is the prompts. Think when the input function is called, the program flow will be stopped until the user has given an input and has ended the input with the return or enter key. The optional argument, which is the prompt, will be printed on the screen. Lexi, some examples. So name equals input and then print. Your name is Karlan. Comma name and I'm running. The script is weekend. See, the script displayed this thing. So the string printed on the first line and then it stopped and is waiting for my input. So now I'll right here. Lexei hungry. And it has displayed Your name is angry. What happened? The stink I've entered has been start in the name variable. And after that, the print function displayed. Your name is colon string. Concoct in ated with the value off the name variable. Now let's try to right here. Something like this. Enter your name. This is the argument off the input function Infect when executing the instructions online number two So the input function it will display s prompt at the console. Enter your name. It has displayed the string into your name. I am entering Grand gray and it has displayed my name. I also want to see what is the type off the value stored by the name variable. So print Alexei Pipe colon comma pipe off name Plexi was the type start. It is variable one more time, Andre and the K C X type is str so existing Now let's see what happens if we try to perform arithmetic operations with variables created using the input function. I'll commend out these lines. I am pressing control, end, forward slash and by charm commented out all lines. And here I'll try something else. Alexei Price equals input. Enter the price and when quantity equals input, enter the quantity in our print and then total value equals price times quantity and now bring the total value colon coma, total value and I'm running the script. Enter the price, Lexie five. Enter the quantity. Let's say to and I've gotten there. But why? Because everything looks just fine. I've got this error due to the effect that I've tried to multiply two strings. This is a string and this is a stink. It doesn't matter. I've entered an integer value. It considers it a stink. If I want toe per firm these arithmetic operation I must convert or kissed my stink toe on integer or toe afloat variable. So here in front, off price. All right, int off in price And here the same int off quantity. And now there will be no error. When I run the script in their price. Five in their quantity to total value. Then okay, here it multiplied toe. In Texas, we'll talk more about casting or converting data pipes in the next lecture. See you in just a few seconds. 20. Strings - Casting: in this lecture will take a look at typecasting or converting data pipes. We'll see how we can turn on integer in tow. Afloat ah, float into an integer, a string in tow, afloat or into an integer. This is straightforward. Infect. You can explicitly convert variables by using the name off. The built in type is a function. Let me show you some examples. I'm creating a variable called a that stores un integer variable cold, be that stores afloat and a variable cold. See that stores a stink for that. I'm going to use the input function so input enter any value and I'm entering 1.5 a store. Sending things are be stores afloat and see stories first thing not afloat. Now let's see what happens if I write float off a It displayed float value. In fact, it converted the value start by a, which is un integer, toe afloat. A hasn't been modified. A remained on integer. The float function only returned or created a new float object. So if I want, I can take that object. In another variable x a a equals float Oh fe and the B B equals in off B so Bebe stores anin take sir. The same happens for stink variables, so I can write c underline ain't equals in off. See? And here we've got in there. It couldn't convert the 1.5 string toe an integer if I want to convert it 20 texture, I must first converted toe afloat. So Lex, right? See underline float equals float off. See see stores a string value 1.5. That's a string. It's between single quotes and see Underline Float stores afloat value. And now I can write C underline in equals in off. See Underline, float and see underline in stores on Intervale. I could also have written something like this. See? Underlining equals int off, float off. See, It's the same off course. We can't always convert strings to numbers If I have something like this. Lexei name equals a and I want in off name or float off name. I've got a new air. It cannot convert a toe on integer or toe afloat. If we want to perform arithmetic operations with values stored in variables created using the input function, we must first cast them toe a numeric value. Let's try an example, let's try to convert miles to kilometers so Miles equals input. Enter the number off. Miles likes a pen off course. Miles stores a string, not an integer or afloat. And now kilometers equals miles times 1.60934 and I'll get in there. I've got a pipe air. I cannot multiply numbers. This is a number by strength. This is a stink. So if I want toe calculate the number of kilometres, I must write something like this. Ink off miles multiplied by 1.609 34 and I've got the right answer. I could also do something like this here. Instead, off using only the input function I could simply use float off and then the input function . And now the miles variable stores afloat. Five point for pipe off miles and it's afloat and our kilometers equals miles multiplied by 1.60934 and the vics much better. I take this opportunity to show you why you should never use python. Reserved works is variable names. Let's create a variable called STR equals ABC, and another variable called C equals seven. And now I want to cast see trusting We cast a numeric value to a string using the str function. This line off coat is correct. There is no air but I've gotten their Vicks because I have over eaten the key ward called str and that's not permitted here. I should have used str one or another name for the stink. 21. Strings - Indexing and Operations: Hello, guys. And welcome back in this lecture will continue our discussion about strengths. A string is an order sequence off characters. We can refer toa any item in the sequence by using AEX index number. So we have a concept off the first element off the string. The second element, the third element and so on. This is also valid for other sequence types, like risks in python, like also in other programming languages such as C C plus plus or Java. We start indexing from number zero, not one. This means that the first string element, when counting from left to right, has the index zero. The second element has its corresponding index one The third element, Index two and so on. Let's see an example, I create the string variable cold movie. And now, when I want to access a character from a specific index, I use the name off the string variable in the index number enclosed by square brackets, for example, movie off zero returns D because he has index zero, then movie off index. One off course will return age because age has index one. When counting back works from right to left, the first index will be minus one so I can have here movie off minus on, and it will return Our This is when looking from right to left. Movie off minus two Will B E and so on. Movie off minus five will be a now. What happens if we enter an invalid index? Let me show you what I mean. With an example, I create another variable called the name equals John. We can count visually that the string referenced by the name Variable has four characters with the last character. It index three 012 and three, and I'll access the character from Index for Horton. What happens then? So name off Lex a thin. And the answer is, I've got the never a stink index out offerings error. You should know that you can avoid stocks situations by using another built in function and the vexed the length function. It returns the number of characters in a stink. So if you have a very long string, you find out what's the maximum index you can use by using this function? Let's try Len off name and it returned for because the name Variable has four characters Len Off movie and E three Target 13. We have here 13 characters now that we know how a sequence such as a string or a list is indexed will move further toe basics drink operations, which are concatenation and repetition. We use the plus sign toe, concoct, innate or join two or more strings in a new stink. And the star operator Toby Pete a stink. Let's get Another Variable called Director equals Francis Ford Coppola and our movie. Plus, this is the concatenation operator, and between single or double coax, a space here was directed by another space plus director, and I've obtained a noose. Think The Godfather was directed by Francis Ford Coppola. Off course we can get that value in a new variable like say, movie one equals. And that expression and now move you on has these value. If we want to concoct innate a stink and afloat or on integer will get an error. We can only can coordinate strings. Product notebook in price equals 90.5 in our product plus price, so I want to concoct innate in display notebook Karlan in the price, and I've got an air I can only can coordinate strings two strings not to float. This is a pipe air. We can solve this error by casting afloat toe a stink For that I write str off price. Now I am concoct in ating two strings. It didn't modify the price variable. It has just returned a string so the content off the price variable is still afloat. Now let's try repetition. Who used the stark operator? So movie star five and it returned on your strength. Off course I can also have something like this Movie blasts a space here Everything between poor emphases times Fife and I've got to this value. So I've repeated these string five times. Let's try The last example likes a price equals input And here price in our price six point seven end of end price Bimes for and this is the value and this is not what we expected. In fact, price is a variable that stores a string and I've repeated this string four times. If I want to perform on arithmetic operation, I must kissed the string value We turned by the input function toe afloat something like this float off price times for And this is the value I've expected. Thank you. That's all for the moment, about string indexing and basic string operations 22. Strings - Slicing: in the previous lecture, We've seen how a string is indexed and how to use indexes. On important remark is that python strings can not be changed. They are immutable. If you need a different string, you should create a new one. Therefore, assigning toe on indexed position in the stink results in an error. I have these variable that stores a string movie off. Zero will return T, and I'll try to change that character. So movie off zero equals Alexei Simply X, and I've got the type air. Str Object does not support item assignment. This is what emit ability means we cannot change on immutable variable. In addition, toe indexing strings support also slicing. While indexing is used to obtain individual characters, slicing allows you to obtain a sub string or apart off festering. It allows you to extract data slicing, relies on indexing and works only with sequence types like strings or risks. Let's try some examples. Movie off zero colon and to this is how slicing works. It will return characters from position zero so from T and X included toe to and vex excluded so, in fact, will return the character from position zero and the character from Position one because the character from position to he's excluded. Let's try another example. Movie off toe Colon five. This will return characters from position to included to Position five excluded, so e space kenji. Note how the start is always included and the end always excluded. Slice indexes Have some defaults on or meeting First index default to zero and on or meeting second index default toe the size off the string being sliced In this case movie off Kahlan toe means characters from the beginning to the position toe in Vex excluded by default. The first index is zero now. Movies off four Colon and I don't write the second index anymore. The second index defaults toe the size off the string. Being sliced will return from Index four till the end off the stink. This means that if I write the movie off Colon in Lexie, a number any number four plus movie off the same number for Colon, this will return the entire string, and this is no matter off the value I am using here. He fire right here, too. And here, too, I'll get the entire string the same if I write here, Alexei eight. In here. Eight. It's the entire string. Another interesting example is movie off minus two Colon and the closing square brackets E three turned E. R. So characters from the second last included to the end. This is minus one. This is minus two. So this is the character included Deal. The end we've seen in the previous lecture that attempting to use an index that is too large will result in an error. However, out off range slice indexes are handled without error. So movie off and here I'll write. A very large number will return a never but the movie off Alexei. Four Colon and the very large number won't return on air. It will return from the character at Position five. Toe the end off my strength and movie off legs. Say 90. Colon will return on empty strength so we can use out off boundary indexes with slicing slices. Also support 1/3 argument. The step value when not specified the step value default. One. Lexi An example movie off zero Colon, then Colin Toe to is the third argument will return from Index zero included two index then excluded in steps off to so d e g d a. And so on. This being said movie off Colon, Colon minus one will infect Return a noose drink with X characters in the reversed order. The last character from what the initial stink is the first character even you string. The second last character is the second and so on. Vexed because Colon Colon returns the entire string but uses a step off minus one. So one element from the end toe the beginning off the stink. That's all about string slicing. See you in the next lecture. 23. Strings - Formatting: the vast majority off applications work with streaks. There are several ways to form up given string into a nicer output In Python, the new way off four month thing Strings, which is new in Python 3.6, is to use formatted string literally, or F strengths, which is a string literal that it's prefixed with the upper case or the lower case letter. If these strings may contain replacement Felix, which are expressions the limited by pairs off curly braces, while other string literally always have a constant value, four months drinks are really expressions evaluated at runtime. The part off this thing outside curly braces is treated literally. A single opening curly brace marks a replacement field, which starts with a python expression between curly braces. There can be arithmetic expressions, a convention field like casing from a stink one in Texas or a four month specify air that is appended using a colon. The replacement field ends with a closing curly bracket. Lexi on example. I'll define afloat variable, so price equals 3.5 and quantity equals, let's say, 2.8 an hour. If this means that a string literal follows and between single or double corvex I write. The price is an hour. A pair off curly braces in between curly braces. I enter the name off my variable so price and it displayed. The price is 3.5. Another example would be total value. And between curry braces, I right price multiplied by quantity. We can have arithmetic expressions between curly braces and it displayed the total value. We can also cast inside F strings. Lexei price equals. And here, Lex, right? 4.2. This is a string because I've written between single quotes. If price is a string, I must cast it toe afloat. So here float off price and it calculated and displayed a numeric value. Another way off. Four month thing strings is to use the format to method but better Lexie An example. So Miles equals 23.2, and now this is a string. The value in Miles is a pair off curly break ICS that four month and my list between poor emphases, and it has displayed the correct value. The former or template string contains a replacement, Felix surrounded by curly braces. Anything that is not contained in braces is considered literal. text which is carpet unchanged Toe the output. If I use an I d like by charm, I must print the expression in order to display it. Or I can take the output in a variable in a string variable like say, s equals the pair off curly braces inside the strength template This is the string template has been mapped with the value inside the parentheses off the format method. We can also have more bears off curly braces like these. The value the first pair off curry braces in kilometers. He's in the second pair off curry braces and now former miles. In the second argument is miles times 1.60 in mine, 34 again, each pair off curly braces inside The strength template has been mapped with each value inside the parentheses. Off the former method, this pair off curly braces has been mapped with this variable or with the value start in this variable and despair has been mapped with the result off this expression, the curly braces are just placeholders for the arguments Toby placed inside the curly brace spare. We can use positional arguments starting from zero. So the same example could be written in this way. Here I write zero in here. I write one. This is the argument from position zero, and this is the argument from Position one, and I've got the same result. As I said, the curry braces are just placeholders for the arguments Toby placed in the above example. Zero between curly braces is a placeholder for miles, and argument one or one between curly braces is a placeholder for the result off the arithmetic expression, these expression. From here we can also format very presentation off numbers. Let me show you how I'll execute this instruction one more time, but here I use a colon dot and let's say, two F, and it displayed this value instead off this failure with this large number off decimal points, look at the second curly brace bear after one, which is the positional argument. I've used a colon if specifies the format is dealing with and the vics afloat number. If not correctly specified, it will give you an error, and the value after the DOT character indicates the number of decimal points it will display. If I write here for it will displayed with four decimals It's not mandatory toe. Have the arguments in this order I can have here one. And here, zero. This argument has been mapped with this value and this argument zero has been mapped with miles off course. This is not correct. I just wanted to make a point. I'll give you one last example just to be sure that you have understood. Zero zero one three and toe. I have five placeholders. Four month, five five multiplied by 25 multiplied by three and 100. Is it Casey? This is the first argument and has been put here in here. The second argument or the argument with Index one vics here. Argument free. This is 100 hear argument with index tool so five times free and the vex 15. 24. Strings - More About Printing: almost any python application brings out some kind off output that is, in most cases available is a string variable. I want to show you one more time. In a nutshell. What are the three main possibilities? We have toe prints, strings and to form up and print other variables that are not strings like, say, floats or sinks. The recommended by Phonic Way is to use F string liters, which have been introduced in Python 3.6. Unfortunately, if you are using an older version off Python, you cannot use if string liters Lexan example. I'll use by charm, but you are free to test it in Python idle if you prefer. I create two variables a N B A. Stores afloat and be stores on integer, and I'll print if and the pair off single quotes. This is an F string literal, a ease and a between a pair off curly braces and B Ys and the B variable enclosed by a pair off curry braces. A Times B. He's a star. Be now. I will run the script and we can see how it printed out the desired string. If I want to display the result off a Times B with a specific number off decimals. I can write inside the pair off curly braces colon dot alexei three f I am displaying it as a float with free decimals. Now let's see the format method a ese and the pair off curry braces, B Ys and another pair off curly braces and a Times B Ys. And the last the third pair off curly braces dot format. Now the first argument, which is a the second argument, is B, and the first argument is a multiplied by B. And this is the output. It's the same output off course I can right here. Zero. This is the first argument one. The second argument end toe the third argument. If I want to set the number of decimals it this place here I write colon dot for f so the number will be displayed is afloat with four decimals. Okay, Now I'll share the last possibility. We have to form art and display strings, but this is not recommended because we don't have the flexibility given by F String liters or 4 march method. Anyway, in this course, if I just want to simply print the value off for variable. I will also use this method. I'll simply write print and between single or double quotes, a ease comma, a coma, then another string enclosed by single or double quotes. So be is coma B comma and the last string and a times B. He's coma, a multiplied by B, and we've got to the same result anyway. Just remember that you should use F stink literally when possible, if not the format method. 25. Strings - Methods: now that we are all done with string slicing and indexing, let's continue on by discussing about some built in string methods. Objects in python. No matter of a store, integers, floats, lists or strings have built in methods. These methods are essentially functions a text toe that object later on. We'll see. How can we create our own functions and our own methods? Now I need to do a little bit off explaining just to be sure that you understand that functions and methods are not exactly the same thing in Python. We've already used some built in functions till this point, the print function or the lin function. For example, these functions are already defined in python core, and we are just using them by calling the function. When I say calling or executing the function, I mean using its name for emphasis and optionally. Some arguments written between parentheses with coma between them. You can see a least with all python building functions at this address. When we have a function and we take that function toe on object, let's say a string object. That function becomes a method, a stink method. These methods are also built in or redefined. Working with strings is very common, so let's see some very useful methods. First, let's create a variable that stores a stink. Let's say simply, X equals. I learn python programming. If we want to see all string methods, we write a string literal or a variable that starts a string and that, for example, X that and now the auto complete function off Idol or Pike Charm will display all available methods. This is possible for any other type off object. If I have, like, say, l one, this is a least and I write Ellen dot the auto completion feature is displaying all least methods. We can also use the deer built in function to display all methods off a string. So dear and we pass in the type off object as an argument. And it has displayed all strength methods, Dear int, and it has displayed all inter exert methods their least, and it has displayed all at least methods and so on. If we want to see a short help off a method, we use the built in help function so help and between poor emphases, the type off the object. Str dot and now the name off the method like, say, fine. And it has displayed a short help off the find method. This is how we call the method the string dot the name of the method and these are of the article makes. What is between square brackets are optional arguments, so we have a mandatory argument and then toe optional arguments start and on end, and this is a short summary. The first method will take a look up his upper. This method will return a new string, with all letters capitalized, so x dot oper, the opening and the closing parentheses. Similarly, the lower method returns a new string on Lee with lower case letters x dot lower. Keep in mind that all staying methods return on your string and don't modify the original stink Stink is an immutable variable and cannot be modified. Even if we've just applied the lower and the upper methods, the initial string is still the same. No changes have been made. Another simple method is strip. It eliminates all white spaces from the start and the end off a string. Lexi An example. Let's say you have on your strength that contains an I P address. This is my strength, but I have white spaces before the I P address in the Three Spaces after the I P address and now I pay dot and Strip and it returned the string the I P address without any spaces at the beginning and EP idiot off the string. The strip method can remove also other characters from the beginning and the end off the stink. Let's create another string, Lex say str one equals and here toe dollar signs and um, I p address something like this and $3 signs characters. This is my strength. If I call the street method str one dot strip it will return the same strength. That's because it removes on Lee White spaces. But if I want toe remove another character, I call the function with an optional argument. So str on dot Strip off and here between single or double coax the character I want to remove from the beginning end from the end off the string, and it returned the string without dollar signs at the beginning and at the end of the string. Keep in mind that these can be useful sometimes another useful method is replace. It replaces a character or a sub string with another character or sub stink. So let's say I b equals I p dot strip. I've stripped all white spaces characters, and now I want to replace the DOT sign with another character I p Don't replace. The first argument is the sub string I want to replace and I want to replace the dot character for I'm replacing it with a colon and vexed the news drink off course. The original string hasn't been modified. It has just returned a new strength. This is another example. Now let's take a look at spirit method. It returns released from a string, having a separator, which is by default, a white space or a tab. I have this drink. Let's say it's the Iran equals and this is a part off a Lennox comment. So this is the name off uninjured face the encapsulation and X mark others. And now I write str on that split, we can see how it has split ID the string using the white space or the tab is a separator. In fact, it returned a least with five elements. This is the first element. The second element, the third element, the fourth element and the last element off course here. I can use any separator I want if I want to split the strength using the colon. Here I am. Pacing in the colon is an argument, and it will split based on this character. Okay, this is the first element. The second element, the third element, and so on. Another very useful method is joined. It is somehow like the opposite. Off split. If we have a sequence like a string or a least, we can join each element off that sequence in another new string. Having separator, I create another variable Mac equals and now a Mac address. I'll copy paste these value from here, and I do mark dot split and colon is the argument story here I have split. This is the least I can take the value off that least in a variable selects a l one equals mark dot split. And now I'm going toe call the joint method. First I have a separator. Let's say the new separator is dot dot Join off Ellen and it returned a string from a list off course instead off the dog character. I can have an exclamation mark, and it has joined the elements off this least. Using the exclamation mark is the limiter or as a separator. And finally, if we want to test if a sub stink belongs to a string, we use the in operator. This is a Boolean operator that returns to, or false, for example, python in X and next to if I write Python with double in here, it will return False because Python, written with double and at the end, doesn't belong Toe the sting. It's not a substance. We also have the not in operator, so python written with double in, not in X he to return through, but Typhon with a single end at the end, not in X will return false. These are only a few stink methods that are used more frequently. You can find a comprehensive list off string methods at the link. I've text toe this lecture 26. Flow Control - If Statement: Hello. Everyone in this section will discuss about if a leaf and else statements that are used for decision making. This is program flow in general and allows us to have logic in our applications and execute a section off coat on Lee when some criteria are met. Let's suppose we have some sort off booking application for airline tickets, and the application tests the balance off a bank account, and only if that value is greater than the price off the ticket. It'll continue the booking process. Reserve the Sikhs and so on. Control flow in python makes use off Coghlan's and indentation indentation is crucial for python and six it apart from other programming languages. In fact, it makes python code more readable. In other programming languages, we use curry braces to define blocks off coat, but here in python, we use indentation. The standard for indentation is for spaces. It isn't a syntax error if you use a tap instead of four spaces, but it's not recommended. Yep, eight recommends using Onley spaces for indentation. In fact, many Python ID's like by charm or sublime, replace a tape with four spaces, so infect when you press on tab, it will insert for spaces. So this is the scene tax off a basic, if a leaf Ellis statement here. If a leaf and else are python language, keywords in some condition is usually some sort off comparison operation we saw in the first part over this course. Basically, the above coat says something like this. If some condition is true, so the first condition execute only this piece off coat. If the first condition is evaluated to force test the second condition, it's very important to know that the second condition will be tested on Lee if the first condition has been evaluated. Toe force. If the second condition is true, execute only that block off coat. But if it's false, execute the block off coat that comes after the else statement or cured. So this program will execute only a block off coat the block off coat that comes after the if statement, the block off coat that comes after the a leaf statement or the block off coat that comes after the else statement. So only one block off court note that we can have only one. If statement, it's not mandatory toe always have on L if and Ellis ST Mikes. You should also know that those Coghlan's are a mandatory and indicate that an indented block off code follows. But let's see better. Some examples. We lose by charm toe. Take advantage off auto completion and other advanced features. It has off course. You can write your code in Python, Idol, note bed or any other python I D. I'll define a variable called price equals 100 and balance equals 90. This is on account Bela's If this is a python keyboard and now the test condition or the comparison operation balance greater than or equal tool price colon. This is also a cured and specifies that a new block off code follows, as you can see by the charm already indented these line off coat. So it inserted here for spaces. And here I am printing a message I'm going to use on if string we've discussed about If strings, it's string operations in a previous lecture, So f from F String. You can buy this product and you're new Ballas will be and here I am using a replacement field. So between curly braces, I have balanced minus price. Now it's waiting for the next line off this block off coat. But this block off coat has only one line. So I'll press on backspace and I'll right from the same level is the if cured else colon enter. The next line off coat is indented and I'll print insufficient funds. Please deposit in between curly braces. This is only a placeholder price minus Bela's. And here I must use on F on upper or lower case F because this is an f stink. Vicks enough Now I run the script. As you can see, it has displayed insufficient funds. Please deposit. Then what happened? The first condition has been evaluated toe force, because balance is 90 and the 90 is not greater than or equal to 100. So it executed the block off coat below the else statement. But if the price is 50 in this case, this condition will be evaluated. Toe to toe 90 is greater than or equal to 50. It will print. You can buy this product and you're new. Bellas will be 40. So 90 minus 50 let's run the script. And this is the result exactly what we've expected. Now if I delete these four spaces, I'll get an error. So this is our neighbor Indentation Air expected on indented block, and I'll press on tab by default. Bike charm in sex. Four spaces for a temp. Let's see another example. Answer equals input off. Do you want to continue? Enter yes or no. Let's suppose we have an application and we need the user input. And now, if in the first test condition is answer equals equals, yes, if the user entered yes, and I print a message, we'll move on. I am using single quotes, insight, single cooks. So I'll use a big slash here toe escape this single coat. Otherwise I'll get on air or I can use single coats inside double cooks. If this is the second condition honestly equals equals no. So if the user uncertain, no and the message will be will stop and else print invalid. Answer. What will happen when I run the script? First the script will stop and it will wait for the user input. The user will enter a string and the vets drink that is start in the answer variable will be evaluated toe the yes string. So if answer is equal to Yes, it will print this message. If answer isn't equal to yes, it will evaluate this condition if this condition is true. So if answer is equal to know it will, point will stop and the script will exit. It won't execute this line. But if answer is not equal to know so this condition is evaluated toe force. It will execute this line off coat. As a conclusion, we can say that our script will execute a single line off coat based on the results off the if or a leaf conditions around the script X asking for my input. Lexei Yes, and it has displayed will move on. Let's run the script again. Islands are no and he displayed will stop one more time and lex m per Republics and it displayed invalid answer off course. We can have more lines off code here as long as they use the same indentation. So here Lexei X equals seven print X. This is OK, but if I use another indentation, So for example, I will try to use here only two spaces. I'll get in there. We can see how pie chart has already underlined in red We have a syntax error because I've used the wrong indentation here. So one toe, four spaces and that's okay. Another improvement here would be to make the answer case insensitive Python is case sensitive. So, for example, if the user end purse yes, with an uppercase letter, it will display involved answer and the text because the first condition is evaluated toe false. We can simply solve this problem by using a string function, and that function is lower. So answer. This is a string dot lower equals equals. Yes, that means that if the user enters yes with upper case letters or only with an upper case, why letter? It will convert that string to lower case letters, and then it will compare toe the yes string written with lower case letters. And we need to do the same for the A leaf statement. So answered a lot lower. I run the script one more time, and I'll enter. Yes, even if I've used mixed lower and upper case letters, the condition has been evaluated toe through, and it displayed will move on. This is a nice example where you can use string functions about retrieve discussed in the previous lecture. Okay, that's the basics off if a leaf and else state monks and I hope they're straightforward to you. They are used in any application toe execute simple or complex tasks. 27. Flow Control - For Loops: in the last lecture we've discussed about, if a leaf else. ST Mike's Nice time to take a look at four loops, one off. The reason a computer is so useful is that it can repeat operations multiple times very quickly. In programming of four, Luper represents one possibility. Toe execute repetitive tasks. In fact, it takes arrange off values and assigns them one by one, toe a variable and executes a block off coat once for each variable. You'll see many times in python documentation. The firm eatery Able So, Lexi, What does it terrible mean in Pilon? Many objects are terrible, meaning that we can eater it over each element. In that sequence, all we need is a collection off items and the possibility off getting one item from the collection. Once Iterating, it's a way to get items out off the collection, one by one, examples off it. Terrible objects are risks, strings topples, sex, dictionaries and so on. We use four loops toe execute a piece off code for each interational. The syntax off a for loop is the following. There's the four key worth, then a temporary variable. In this case item, this isn't a cured. I can have here any name I want then in and that secured and the name off my feet are able . This can be a string least. Arrange a dictionary and so on. And then we have the four block off coat off course. This block off coat must be indented with four spaces each line off coat below. The four statement that uses the same level off indentation belongs toe the four book, then one by one item, takes a value from my collection from my eatery Able Object and inside the four block off code I have access to that value. In this example, this coat will print the elements off the least one by one. Let's see some examples in Pike Charm. I'm going to create a new file. Lexi, for one, don't use reserved works, so it's not recommended to use the four name for your file. Vics are reserved. Cured. I create the least Lexei OS from operating systems Windows. Lee knocks Mac and Andre. We'll have a long discussion about the lists, but till then you should know that at least is created using square brackets and coma between elements and all four This is a cured item. I could simply right, only I. So this is a temporary variable in. And now the name of my eatery able in this case, OS Colon. This is mandatory and now using an indented line. So using force bases by charm has automatically edited for space is 1234 All right, sprint and I'm going to use an f stink. So f industry ing Is that the name off? The item in the least is in between curry braces. Item. In fact, this is a placeholder for these variable and let's run the script. You can see how item takes a value from my least one by one. There is no problem if I write here double I and here double I heats the same. We can also reiterate over a string. A string is also unutterable object, so str one equals I learn python programming and all four char in str Juan Colon and using an indented block off coat, likes a print shark. Let's run the script and it printed each character off my string, one by one, by default. The print function EDS. A new line after each printed line So if I want here I use end. This is a non argument equals and I'm replacing that default. Backslash end with Alexei a minus sign. When I run the script, it will display something like this. So the first item off my collection than a minus sign The second item off the collection takes a space in the minus sign and so on. Flex Another example. Let's create the list. Cold numbers he quotes and hear some values like, say, 05 mine 11 91 130 in the four loop ease for I in numbers print I In fact, it will print the values off this list. Now what if I want to check for even and odd numbers? So I use a nested if this miss on if statement inside the for statement here, I need to make a test. If I moto is equal to zero, that means that the remainder off I divided by two is zero and I is zero at the second. It'll ation 59 11 and so on. Here I must use a colon, and this line off coat must be indented because it belongs to the If block Juan 234 Hells, this has the same level. Off indentation is the if cured Brent Alexei here. If old number in between curly braces and here, if even number I and I'll around the script, we can see how it displayed or the numbers and even numbers at the first interational I. He's zero. It compared the remainder off zero, divided by tool with zero and the vexed tool. So even number 00 is an even number. It the second it'll ation. I is five. If we divide five by two, we have remained there different from zero. So it executed this line, the else block off coat and it displayed, or the number five the third. The number is also an older number, and so on. Let's move on and see how we can eat a rate over a range off numbers. The range function returns a sequence off numbers, starting from zero by default and increments by one by default and ends at a specific number. So here instead, off numbers, I lose range off 100 infect the range function will return a sequence off numbers starting from zero deal 99 so 100 is excluded, and for each number in that sequence, it will do these disks. I've displayed all numbers from Zero Toe 99 with the message. If that number is on odd or even number, we'll use ranges a lot in other scripts. So I want to show you other examples. Let's say, for I in range off for comma 20 Brilliant I the wrench function will return the sequence off numbers from four till 2020 is excluded, so the last number it will print will be 19 and the last. The third argument is a step I can go in steps off to, and this maze from four included toe 20 excluded in steps off to and this is the result. There is no problem if I have here 40 in steps off minus two. So from 40 toe 20 in steps off minus two, 40 is included and 20 is excluded. Vex enough for the moment, about four loops, and in the next, lectures will dive deeper into ranges and loops 28. Flow Control - Ranges In Depth: in the last lecture. We've seen some examples using ranges. Now I'd like to dive into some more details. We use ranges each time we want our application toe execute a repetitive task. Let's assume we have a lottery application and we need to pick three random winners. In this case, we could use a range in the loop to repeat three times the process off. Choosing a random winner. Let's see a simple example for ex in a range of three colon. It follows the four block off coat, and here I'm just printing a message. Print F from F String. Choose Weiner in between curry braces X, the temporary variable off my for loop, and it has executed three times the four block off coat. In this case, the four block off coat has only one instruction, but there could be more instructions as long as they use the same indentation level. Arrange is, in fact, a type that represents an immutable sequence off numbers and is commonly used for looping a specific number off times in four loops, there is a built in function cold drink that returns ranges off numbers. Python ranges come in multiple forms the general form off. Arrange, ease, range off and we have three arguments. The start The stop in the step by default start is zero and is included. Stop is excluded and by default step is one, but better Lexus. Some examples I create a variable that stores are inch Alexei R equals Range off then, in this case, the started zero. This is the default. The stop is then and the step is one. If I want to display the numbers from the range, I must convert the range to a list. We'll talk a lot about risks in a couple off lectures. For this moment, you should know that we convert arrange to released by calling the least constructor. So least off our and these are the numbers off my rings, we can see how zero is included and then is excluded. Let's see another example R equals range off 10 comma 20 Now the start ease then and these included in the stop is 20 and these excluded off course. I leased the numbers from 10 to 19. If I want to least Onley even numbers I use here a step off to this mist from 10 toe 20 twenties, excluded in steps off to We can also have a negative step licks. An example. R equals range off. The start argument is 100. The stop argument is minus 50 and the step is minus three. In fact, I'll go back works from 100 toe minus 50 in steps off three. And these are the numbers 100 included. 50 excluded. If you want to use a range in a for loop, you don't need toe convert. Veterans toe a list six enough to write for X in our our is arrange print X. So here I let display the numbers off the rings and these are the numbers. That's all about ranges for the moment. Thank you. 29. Flow Control - For, Continue and Pass: in this lecture will take a look at continue Break and Best State monks rep are used in loops. The continue statement that was borrowed from C continues with the next iteration off the loop or, in other words, returns the control toe the beginning off the four loop. The continue statement rejects all the remaining statements in the current iteration off the loop and moves the control back to the top off the loop. Usually the situation where continue is useful is when you want to skip the remaining coat in the loop and continue Iterating. The continue statement can be used both in Wiles and four looks. Let's see some examples. I'll create a for loop that this place even and old numbers between zero and 100. So for X in rank off 100 colon and now the four block off coat. And here I have a test condition. If X moto equals equals zero, that means the remainder off X, divided by two is zero. So X is uneven number colon and here print found an even number comics and continue. When the interpreter takes this line, it will simply go toe the beginning off the four loop, so it will execute the next iteration, and it won't reach the next time. The next line off coat that drinks or the numbers doesn't belong toe the if block off coat . It belongs to the four block off coat, and that's why it's indented, is the same level. Is the If cured, I'll move the indentation for spaces, toe the left. Let's run the script, and we can see how it displayed even and odd numbers between zero and 101 100 excluded. So, in fact, 99 is the last number. Let's try another example for letter in, and here I have a string by fun Colon. If letter is equal to X 80 colon and on the next line with four spaces indented, continue print current letter. Call my letter. In this case, I eat or ate over a string. A string is a sequence off characters, and I begin letter by letter excluding the T letter Vicks, because when leper is equal toe t, the continue statement will return the execution toe the next iteration and it won't print poverty letter. It won't reach line number 12. That line is reached and executed on Lee when the continue statement hasn't been executed or when the letter is not equal. Toe de, let's run the script and the guessing how it displayed be Why h o and m. So now the letter in python. There is also the pests cured that can be used when a statement is sin tactically required . But you don't have any comment or coat toe execute. The pest statement is unknown. Operation. Nothing happens when it executes you. Use the pest cured tow, avoid a syntax air flex an example for X in range off, then in here. I don't want to implement the four block off coat. I just want toe write a comment. So something like this implement this block off coat in orderto display even the numbers between zero and 99 and vex. All this is like a to do or homework. If I try to execute the script, I'll get on there unexpected and off file while parsing so here there must be at least one instruction for of it. I can write best. This is a Newell instruction. Nothing happens, and in the future I can implement this block off coat. I run the script again and there is no error. That's all about continue, and best keywords in the next lecture will take a look at break cured. See you in just a few seconds. 30. Flow Control - For and Break: the break statement that was also borrowed from C breaks out off the innermost enclosing four or while loop it terminates or interacts the current loop and resumes execution at the next statement after the four or while loop. If break statement is inside a nested loop, this is a loop inside. Another loop break will terminate the innermost loop. Also note that loop statements may have one else close. It is executed when the loop terminates through the exhaustion off the least over which we eat or it, but not when the loop is terminated by a break. Statement. Continue Break and Ellis ST Monks in four and Wild Loops are specific Toe python. Let's see some examples. Let's create a new script for and break for number in Range off 10 Colon and here I start the four block off coat, if number equals, equals five. Colon break Break belongs toothy if block off coat. In fact, that block off coat has only one instruction. The break instruction and then at the same indentation level is the if cured green number. Let's run the script. We can see how it displayed the numbers from 0 to 4. Vicks because when number became five, this condition was evaluated toe. True because number is five and the break statement broke out off the four loop. If I had had another instruction outside the four loop, that instruction would have bean executed. For example, print instruction outside for loop. And I'll executor script one more time. We can see how it displayed all numbers from 0 to 4. And after that, it executed the line number six, which is an instruction outside the four loop Flix. Another example for letter in python colon Bring letter. And if letter is equal to oh, break here. I'm iterating over these string and I am printing letter by letter. But when the letter is equal toe Oh, I am breaking the four loop. So I am getting out off the for loop likes execute the script and they can see how it printed till Oh, it didn't print the end letter. Lex. CIA last example for X in range, Lexie 1 20 If X more 13 equals equals zero and break else print. There is no number divisible by 13 in the range. Maybe it looks strength, but the victors no error here. The else statement belongs to the for loop and not toe. The if statement in Python off for Loop can have an else statement. This is Spieth on specific. Before running the script, I want to explain to you this coat I am iterating over. The Strange from 1 to 19 20 is not included. And if there is a number divisible by 13 I'll break the four loop that May's I'll get out the for loop and I'll execute the next instruction after the else clause, which belongs to the four Loop in this case that there is no instruction. But if the four loop doesn't end by executing the break statement, it will execute the else statement and will print. There is no number divisible by 13 in the rings, so Line number 19 will be executed only when line number 17 is not executed. So the brake statement is not executed off course. If I under script, it will execute the break statement because in the range there is the number 13 and 13 is divisible by 13 and it will break so it won't display this message. But if instead, off 20 here I have 12. There is no number divisible by 13 in the strength it will display this message and Vicks because the break statement won't be executed. That's all about break in four loops. See you in the next lecture. 31. Flow Control - While Loops: in this lecture will talk about while loops. A wily loop will continue toe execute a block off coat, while some test condition remains to the test. Condition is some sort off Boolean expression that returns to or force, and the block off coat will continue toe execute. While the condition remains true in python a while, Loop can also have a new else close, then gets executed when the wild bullion condition is evaluated. Toe force. So if some bullion condition is false, it will execute this block off coat. Let's see some examples. I'll create a new python script, while one please avoid using python Key works is fine names, so avoid using while four leaf or other key works its final names. And here X equals zero. And while X less than 10 colon and the wild block off code follows using a new indentation off four spaces inside the wild block off coat, I am printing the current value off X so the current value off X East and beating curly braces. X. If I run this coat, it will execute the print statement over and over again and the X because X is zero and zero is less than 10. This is what is called an infinite loop, and in most cases this is an air. Next on the script, we can see how it continuously executes the brain function and displace the current value. Off X is zero. If you're on the script using the command line, you should press on control and see control. See, here in Pi charm, I'll press on stop and the script has been stopped. Otherwise, If I don't stop the script manually, eat will run forever. What I should do in order to get out off this infinite loop is to modify something inside the while loop in such a manner that the testing condition becomes force. So let's simply increment X by one unit in each iteration here. After painting the current value off X, I write X equals X plus one Vics of the same is X plus equals one. Now when I run the script, it will display the numbers from 0 to 9. Let's see in detail what happened. First, we've created a variable called X that stores the value zero. Then we have a while. Loop in the test condition is X less than 10 off course. This condition returns true because zero is less than 10. The wild block off coat is executed and that miss these two lines the first line displace the current value off X and after that, X is incremental by one unit, so X becomes too. Then, in the next wild alteration, the condition is to less than 10 off course. Toe is less than 10 so the block off coat will be executed one more time. It brings the car in value off X, and after that x becomes three. So two plus 133 is less than 10. It executes the bloke off code one more time X becomes four, and so on till X becomes nine nine less than 10 vex. True, it executes the brain function, and after that, X becomes 10. At this point, the wild testing condition will be turned. Force then is not less than 10. 10 is equal to 10 so the condition returns force and the wild block off coat is not executed anymore. The script execs, because there is no other instruction after the while loop here at the end of the while loop, I can have on else. Close so else colon. And here print. This is the else. State month X is not less than them. X is X and I around this crypt. One more time we can see how it displayed the numbers from 0 to 9 and when X became 10. The condition has been evaluated to false, and it has executed the else block off coat, which consists off only one instruction. The print function. If the while loop starts with a condition that evaluates to false, it executes only the else block off coat. So let's say X equals 100. 100 is not less than 10. The condition returns false, so it will execute only this line off coat is a conclusion. We can say that in Python a while, Loop can also have on else statement that gets executed when the wild testing condition is not to anymore. Thanks all, for the moment about while loops in the next lecture, I'll show you how to use break and continue in while loops 32. Flow Control - While and Continue: in this lecture will continue our discussion about while loops is we've seen earlier. In another lecture we can use, break and continue statements with four loops. These instructions, break and continue, can also be used with while loops having almost the same effect. The continue statement continues with the next iteration off the loop or, in other words, returns the control toe the beginning off the while loop. Let's see a simple example. I'll create a script that this place all numbers divisible by 13 that are less then 100 and I'm starting from 12. X equals 12 while x less than 100. I am incriminating X by one unit, so X plus equals one. If I don't increment X, this condition will never evaluate toe force, and I'll have on infinite Loop there. Now if X more 13 is not equal to zero colon and continue, I can write continue on the same line because there is a single instruction or I can use on indented line below. If and the write it on the following light, it's the same and print. Exe. First. Let's run the script. We can see how it displayed all numbers divisible by 13 that are less than 100. How did it work? I am starting from 12. 12 is less than 100. So the wild testing condition returns True X becomes 13 and then I am testing If X is divisible or not by 13 off course, I'm missing the moat operator, about which we've discussed a lot in a previous lecture. If there is a number X that is not divisible by 13 the continue statement returns the control of the script to the beginning. Off the while loop or in other works, it will go here on line number two. It will test the condition one more time. And if the condition is too, it will execute the wild block off court one more time. So in fact, it will print exe so line number five on Lee. If the number is divisible by 13 and Vicks because the continue statement is not executed. This is straight fourth. Now I want to show you a common air. If I don't increment X here online number three. So before the EAFE state month, let's say I take this line from here and increment the X variable here I have one question for you. What do you think will happen, Lexar under script toe Find out and nothing happens. Vexed because my script entered an infinite loop. I must manually stop the script, Vicks, because it will never reach this line. Why is that? First X is 12 and 12 is less than 112 more. 13 is different than zero is in fact 12 and it executes the continue statement, and it will enter the loop one more time without incriminating the X variable. So X remains 12 12 is less than 100. This condition is evaluated toe through the continue statement is executed and so on. So it's important toe increment the X variable before executing the continue instruction. But that's all about. Continue in while loops and in the next lecture will see how we can use the brake statement in while loops 33. Flow Control - While and Break: Hello, guys. And welcome back in this lecture will see how to use the brake statement in while Loops. The Break statement breaks out off the innermost enclosing four or a while loop, eat Terminix or interrupts the current loop and resumes execution at the next statement after the four or while loop if break statement is inside a nested loop, so a loop inside another loop break will terminate the innermost loop. In a previous lecture, we've seen in detail how it works with four loops, The Break statement works almost the same with While Loops. Let's see an example. I'll create a very simple game where the user must guess a number, and if the correct number was guest, he or she wins the game and the python code is while toe. This means that the wild block off coat will be run as long as I don't break out off it. Using the break statement, I create a variable Guess equals input off and the message display these guests a lucky number between one and 10. No, I have a testing condition. If in off guests, I must convert it to an integer because the input function returns a string. So guess is a string. And here I am, converting it 20 in texture equals equals eight. This is the lucky number I am printing on my six. You won and break. It will break out off the while loop. The next instruction that is indented is the same level. Is the EEF cured? Is the print function that displace a message using gun F string Literal I'm displaying. Guess was not a lucky number off course. Guess enclosed by curly braces because this is variable. Now I'll run the script. I guess a lucky number between one and 10 and I m for seven. Seven was not a lucky number. What happened? I entered seven int off seven is not equal toe eight. So the condition is evaluated to force and it won't execute these block off coat. It will execute this line. After executing this line, it will test the wild condition one more time in the condition is true. It's infect the bullion constant people and it displaced the message one more time gets a lucky number between one and 10. And this time I enter eight. Wow, I want That's because in tough guests is equal to eight. So the if condition evaluated toe through and it executed the if block off coat. In fact, these two lives here, he printed, Too dumb my six you want. And after that, the break statement interrupted the while loop and broke out off the loop, and the script dominates. The next example I'll show you is a script that finds prime numbers the parlous than 1000. I've already created the script in order to save some time. Let me explain to you what the script does. A prime number is ah, whole number that is divisible only by itself and one, for example, five and seven are prime numbers. We have to wild loops on outer and an inner loop on the first line. I create a variable. Why equals 1000. In fact, I'll go back works from 1000 toe to toe. Find prime numbers, he sighed. The first wild loop I create a variable called X that is equal toe have why, using another loop, a nested wild loop. I'm going to search for factors, and these are numbers that could divide my Y number from X toe to going backwards it's enough to search for factors from tool toe. Half Why? Which is X If, Why more X equals zero means that why can be divided by X and why is not a prime number. The break statement that will be executed in this case jumps outside the nested wild loop and executes the next line after the nested loop and X Y minus equals one. So I d equipment. Why by one unit and enter the main outer Wiley loop again. If there is no X number that D writes, why why is a prime number? The break statement is not executed and the else block off coat, which brings why his prime will be executed. Take care that the else clause belongs to the wild loop and not toe the if statement legs run the script. We can see how it found and displayed prime numbers from 1000 backwards 34. Lists - The Basics: Hello, guys. And welcome back in this lecture will start talking about the risks, at least represents the most versatile and used type off data structure in python released ISM order sequence or collection off items it can hold any object type integers, floats, strengths, other risks, Pappas dictionaries and so on. All can be stored in a list least like strings support, indexing and slicing. We can refer toa any item in the sequence by using its index number. So we have a concept off the first element off the least the second element, the third element and so on. This is also valid for other sequence types, like strengths or topples in python, like also in other programming languages like C C plus plus or java. We start indexing from zero, not one. This means that the first element off the least when counting from left to right has the index zero. The second element has its corresponding index one. The third element index two, and so on at least, is a mutable object and the Vietnamese it can be modified. We can add elements toe the least, or we can remove elements from the least without creating a new least is a reminder. String, for example, is immutable. If we want to modify a string, we must create another one. At least uses square brackets and commas between elements to separate to them. Let's see some examples. I can create an empty, leased lex a least one simply by using a pair off square brackets. Another possibility is to use the least constructor. These are two empty risks. I'll create another least my least that Holik's items off different types, so the first element is an integer. The second element of Float, The Third Element, a String and the Fourth Element, a Topple will talk about topples in another lecture. This list has four elements. If I want to get the number off elements in the least, I use the built in land function Len off. My least returns for is, I said earlier. We have the concept off the first element of the second element, and so on released is on or birth sequence. This means I can get the elements off the list using indexes. My least off zero will return one. This is the first element off the list. My list off one will return 3.2 the second element off the list. Off course, my least of three will return the last element off the list. And next the top of we can also use negative indexes. So my least off minus one will be turned the last element off the least. My least off minus two will return. Python, at least is a mutable object. We can modify a list first, Lex. Greater strength is theon equals. And here by phone. This is a string. If I want to modify the string, I'll get an air. So it's tr one off zero. I want to modify the element at position zero equals and instead off B, I want here a and this is an error. A type error. The stink does not support item assignment. If I want to modify the string, I can do it. But I must create another string for that. Let's see how it works for risks. You should also know that you can create a leased from a string. So least one equals least off. S t I won. This is my list. The elements off the least are the characters off the streak. Let's see What is the idea or the memory address where the list is? Start. This is X address. I'll try to modify the first least element. So least one off zero equals 5.5. This is a float. This is my least. We've got no error. And if I want to see ex I d. We notice we have the same object. So the initial object has been modified. These are lists basics, and we'll talk about the lists, operations and the methods in the next lectures. See you in just a few seconds. 35. Lists - Operations: in this lecture will dive deeper into python lists and we'll talk about risks. Operations The first important operation. Is it aeration? Lexie with an example. How can we reiterate over a list? I have a least that has three elements for the I P addresses, and I want to reiterate over this least to execute a block off code for each I pee in the list. For example, I have on automation script, and I want to connect to each I p and install some applications or execute a backup for of it. I'm going to use a for loop. So far, Item This is a temporary variable in my least colon, and it starts the four block off coat. In this example, I'll just print the I p address. So print I am using F string literal. So if and between single coats connecting to in between a pair off curry braces item my temporary variable. Sorry. Here I have my eye Peace. This is the name off my list, and it executed the print function for each item off the list. This is how we eat or it over a list. Now let's see, How can we can coordinate two lists. Be aware off concatenation because problems can appear. I create a new least. Let's a least one equals and it contains two elements. Toe in pictures. One Comotto, lex print. Exe, idee. Least one equals least one plus. In here, I'll add another list I want to concoct innate the second least to least one so three comma . For this is least one. It contains the old least one plus again for the second least. But when I check its i d. I notice it has another address. So infect here I've created another list. I didn't modify the first list. At least one. If I want to modify the forest least the initial least, I must use the plus equal operator. So one more time least one plus equals and another list. Five. Omar six. This is least one is expected, but its i d is the same. We didn't modify the initial list, so there is a difference between using the plus operator and plus equal operator. So they care. If you want toe ed a single element to a least, you must enclose that element by square brackets. If I write here like say, then I'll get an error I cannot concoct. Innate, at least with an integer I must have been between square brackets, even if the least contains only one element. There is also a method called extent that extends a list infected. The same is the plus equal operator. Makes an example. This is my least one least one plus equals, and I let another least that contains tow. Strength released can contain elements off different types. So ABC X y z i d off least one. We can see it has the same idea. The same results can be obtained using the extent method, so least one that extent, and I'll place in a list a B coma CD. This is least one and its I. D is the same is a conclusion you can use the plus equal operator or the extent method. Another useful method is a pent upend. EDS. A single element toe The end off the list so extend EDS. More elements and upend EDS. A single element least one dot upend off double X. It depended on element toe the end off the list. If I try to add more elements using the upend method. I won't get what I expect. What happened here. In fact, it added a single element, and the last element off my list is another list it didn't add. Two elements toe the end off the list. Lexie. The content off least one. We can see how it ended a single element. This is the difference between upend and extent. There are many other at least the methods, but we'll talk about them in another lecture. Now I want to show you something very important about the assignment operator. So about the equal sign I create a new least least one equals one Comotto least two equals least one. This is least one this is leased to They are equal and they are stored at the same address . So least one is leased to returns to in our list one dot upend I lead on element three. This is least one, and this is list too. The lists are still the same when I've appended this element toe least one. I've also modified the list too, because they point to the same address. They are references toe the same obvious least toe. Don't remove. Now I am removing on element. This is Listo. I've removed the first element with very one, and this is least one. The element has been removed. Also from least one least one equal Sequels list, too. Least one is leased to idea. Awfully. STAHN and I D off lease, too. So take care when using the assignment or equal operator. It doesn't create a copy off released, but a reference to the same address. Now let's talk about the least slicing slicing returns apart off a list and can also modify a list at least one equals. 1234 least one off zero Carlin, too. It will get on a new least with elements from Index zero and won. The start is included and the end excluded. Let's see another example. Least one off Colin three. By default, the START index is zero. So, in fact, it will return a list that contains the elements from index zero to index three excluded so zero want in toe. It will return 123 I can also use a step from index one toe index four. In steps off to this is index one. This is index three because four is not included and it returned in steps off to I can also use negative indexes least one off minus three colon minus one. It will return a new leased from index miners free to index minus one. This is minus one minus two minus three. So, in fact, it will get turned a list with two elements to every. That's because miners one is not included. Slicing also more def ice A list. So least one off zero colon toe equals seven. Comite I've modified least one. I've changed the elements from Index zero included toe index to excluded. So the 1st 2 elements with these elements I can also use something like this. Least one off zero calling two equals And here I have a coma. B comma. See Kalmadi Comma E. This isn't an error. I've extended the least with these elements. Even if the slicing operation returns only two elements, I could insert five elements. So, in fact, it has inserted these elements from index zero to index one or two excluded. At least one off colon colon returns the entire list and the least one off colon colon minus one reverses my list. In fact, I'm going back works in steps off one, that's all, for the moment, about, least operations. And in the next lecture, I'll show you li six methods. See you in just a few seconds. 36. Lists - Methods Part 1: in the last lecture we've discussed about the lists, operations like it aeration, concatenation slicing and a pending. Now it's time to take a look at the risks methods. These are functions a text on objects off type list. If we want to see all least methods, we write the name off a variable that stores a least, and that the auto complete function off Idol or pie charm will display all available methods. We can also use the deer built in function toe display, all methods off a list. The methods that start with double underscores our special methods, also called magic methods, and we won't talk about them at this moment. If we want to see a short help off a method, we use the building help function and between parentheses, the type of the object which is released a dot and the name off the method, for example, help off least dot append. The first methods will take a look at our upend extent and insect. I've already shown you some examples with a pent and extent in the previous lecture, but now I want a short review. These methods are used toe ed elements, so the end off the list. Upend EDS. A single element to the end of the least extent. EDS. All elements often eatery able object toe the end off the least and insert in sex on element at a specific index or position. Lexi examples. I create the least cold numbs from members, and the items are 1234 and five. Now I want toe ed on element to the end, off the list Nam's daughter penned and the value off that element. It can be any type off object like, say, a I ed a stink. If I want toe ed more elements using a pent, I'll get a Nair upend. EDS. A single element varies the extent the method that Ed's or extinct the least with the elements off any eatery able object. For example, moms dot Extent and I'll pace in is an argument on eatery able object that can be a least a top on a string. For example, between square brackets, this is a least six comma seven, and it extended the least with two elements. I can also pass in a string string is Anita Rabel object. So between single coats, Hello and it extended the least with the elements off the strength. So the characters off the street you'll get an error if you pass in a single element, not an eatery able object, for example. Moms don't extend off. Seven. This is a never vex because the integer seven is not on it. Terrible object. If I want toe at a single element, I use the upend method, or here I put seven between square brackets. This is a least with one element, and in fact it will add that element seven to the end of the list. Now let's see how the insert method works. It inserts on element at a specific index or position numbs dot insert. The first argument is the index zero and the value like, say, 100. In fact, it will ed the Value 100 at position. Zeer Another example. Numbs dot insect three comma 200. It inspected the Value 208th position. Three. So 01 toe and three so it dissected. Value 200 here after value to or before value. Three. If you want toe ed on item, toe the end of the least and try numbs dot insert minus one minus one being the index off the last element in other value, let's say 400. It didn't edit toe the very end, but toe the second to last position Vicks. Because it doesn't count that the least will grow by one, we can see how the Value 400 is on the second to last position. If you want to add an element toe the very end off a least, you must use the Len Function toe. Calculate the index off the last element in the list. Len Off numbs returners. Remember off elements in the list numbs dot insect off len off numbs This usually turn the index the value 17 Coma 600. This will add the Value 600 toe, the last position in the list. Another important method is copy. If you need a copy off released, you should use the copy method and not the assignment operator or the equal sign. Let's see an example. Numbers equals numbs numbers is not a copy. Off the lumps least infect. They are the same least they are references toe the same object. For example, numbers dot upend minus one. We can see how minus one also belongs to the numbs least. In fact, here, using the upend method, I've modified the value stored at earthy, addressed, referenced by both numbers and the numbs risks. If I need a copy off the Nam's least, I used the copy method. Lexei. Mom's underlying copy equals Nam's dot copy. Now numbs underlying copy and moms are different risks. We can see how they are saved at different addresses, and the moms dot append minor stool. Sorry. Here I have append miners. Toe has been upended toe the moms least, but not toe. The moms underline copy list In this lecture, I've shown you a couple ways off aiding tallest. In the next lecture, we'll see how we can remove items from a list. See you in just a few seconds. 37. Lists - Methods Part 2: we've seen so far a couple ways off adding to a list now will take a look at How can we remove items from a list? The first and the simplest method is clear. It will empty the least or remove all elements it once I'll create the least called letters using the least constructor and a string is an argument. ABC The This is my list now. Letters not clear, and I've emptied the least. If we want to remove a single element from the least, we can use the pope method. I'll create the least one more time and now letters dot pop. The pope method removes the element at given position in the least and returns that element by default. If we don't pass in argument, which is the position at which it will remove the element, it removes the last element. So in this case, it removed the last element off my list. But let's suppose I want to remove the first element. The element A In this case, I can have something like this likes a value equals letters dot pop off zeer. Zero is the index. At weeks it will remove the element and that element will be returned or it will be stored in the value variable value contains A and A doesn't belong anymore to my list. If we pass in on out off range value, it will return on error. Then he's an index out off range value. Pope index out offerings. The last method used to remove formalist is remove. It works different in the way that we pass in the value off the element we want to remove, not AEX index. This method will find and remove the first make sure so if we pass in his argument X it'll search for X and remove the first occurrence off X, or it raises value error if the value X is not present. If there are multiple value X in the least, it removes only the first match comparing toe Pope Method remove doesn't return the value. It removes Plexi on example. Let purse equal silly stuff, and here I'll apace in some letters. Also, some duplicates. This is my list, and I want to remove the value a so letters don't remove a here. I made a mistake. A is a string, so I must use single or double coats a between single cox. It has removed the first cars off. A. We can see how the first element off the least is now. Be it didn't remove all currencies off. A. If you want to remove all our currencies or for value, you can use a while loop while the value I want to remove lexei A in letters. So as long is this condition returns to so a belongs toe, the letters released. It will execute the while block off coat and I have only one instruction letters dot remove off A. Basically, it says that as long as a belongs toe, the least it will reiterate over the least, and we'll remove that value. We can see how a has been removed from the letters least. Another least method is index. It returns the index off the specified item in the list. It also allows us to specify a start and an end. So let ice dot index off. Be sorry. The name off the least is letters, not letter, and it return to zero because B is at index Zeer Letters index off d will return two because the is at index to zero one and two. If we have duplicates in the least, the index method will return on Lee. The first makes let's add some duplicates. Let purse don't extend and I'll pass in some duplicate letters. Letters dot index off the and it returned to. So this is the index off the first or Congress the first makes. But I can also provide a starting point or index. For example, letters dot index off the end for this basically says, find the index off the after the index for and four is inclusive. So 01234 So after this point and this point is included. So after the our letter and there is a D at index six and finally we can have a start and an end index. In this case, we are looking between start and end letters dot index. If B three coma, then it returned. Nine. The index off the first, be it found between index three and 10. If we want the index off a value that doesn't belong, toe the least will get an error of value air. For example, I am searching for index off. Ex Leper X is not in list Another method will take a look at his count e treaters. The number of times value appears in the list. If the value doesn't belong to the least, it returns. Zeer letters don't count off a A appears to times in my list. Be appears for eight times, but Z doesn't belong to the list. The method we talking to zero. 38. Lists - Methods Part 3: Let's see now, how can I start a list? There are two possibilities. The least sort method or the assorted building function. The least sort method sorts the items off the least in place, or it modifies the list. But the sorted building function will return a new least with elements sorted in ascending order. So if I write sorted off, numbs this building function, this isn't a least method. This function will return another least with elements sorted in ascending order. This is the new least. The initial mom's least hasn't been modified, eats the same off course we can take the new least in our variable. If we use the sort method, the least will be modified. So numbs dot sort and the least has been modified. Now the lamex are sorted. We can also sort strings in alphabetic order licks, an example, letters equals least off and some letters here. This is my letters least, and now I want to sort the list ill assorted in place, or I'll modify it. So letters that sort the least has been modified. It contains elements sorted in ascending order. If we want to sort a list in descending order for who used the same method, but we passing on optional argument in the vex reverse equals two. So let there's dot short, and this is the optional argument. Reverse equals two through written with uppercase T. It's the bullion constant and the least has been sorted in descending order. There are also other built in functions that can be used with risks. If we want the maximum value Awful East, we use the Mac's function. So Max off numbs and it returned 100. 100 is the maximum value off my list. There is also mean off Nam's or mean off letters. It used the alphabetic order that is also the some method off course. It will calculate the sum off the elements off the list. The sum is 112. However, if we have released with various types off items, Lexie numbers and strings mixed together, how does it compare a string with an integer and return the maximum value off the list? In that case, it will return a type error, saying that it cannot compare integers with strengths. Generally speaking, comparison operators don't support different data types. This is my letters least, and this is my mom's least. I'll append a string to the moms least. So Nam's daughter penned and the string He's by phone. You know, I write sorted off numbs, and I've got the type air or marks off numbs Fight fair mean off Nam's the same type air We cannot compare strings toe integers. These are the most used least methods in the next lecture will see how to transform a leased to a string and the string to a list. After that will take a look at another Python data structure called Topple. 39. Lists - Split and Join: in many python applications. We use strings, endless six. So it's very common to convict a string toy list or a list toe. A strength for that. There are two methods split and join, and they are staying, not least methods. Split is a string method that having a day limiter or separator, which is by default white space or a temp, returns a new least off the works in the string, using the limiter best. In his argument, we used split to convert per string to a list. In this example, I have a string variable that contains some programming languages with a space between them . I used the split method toe convert this string to a least. Having the white space is a day limiter. The least that was returned. His is items the world's off the string. Having the white space is a bell emitter. Join is another string method that can cut in eights joints or combines the elements off a list that is best in as argument into a string. It returns a new strength. The sting that is used to call joint on will be put in between every item in the list. We use split toe convert at least to a string. In this example, I have a least that contains some strings, some programming languages, and using the colon is a daily meter. I've created a stink. The joint method combined the elements off the least in a string, putting the colon character in between. They care that split and join. Don't modify the initial string or the least they simply return a string from released or released from a string. Let's see some examples. I have a variable that stores a string, and its name is I P's from I P addresses between the I P addresses off my string. There is a coma and I want toe get released from this strength for of it. I'm going to use the split method, Lex. Greater variable least eyepiece equals eyepiece. My string variable dot split and his argument. I must hasten the separator by default. It uses a white space. In my example. The separator is a coma, not a white space. We can see how the original string hasn't been modified. In the new least called least, Underline eyepiece has been created. The elements off the least, are the works off the streak. Now I want a stink from this list for it. I'm going to use the joint method. The joint method is a string method. So it is called on a string between single or double quotes. I have here a string colon don't join and the name off the list here I can have any eater able object in this case, least underlying eyepiece. It will return a new string by combining the elements off the least using a colon between them. So the first element the second element off the least the third element off the least in the last element off the least in a string. If instead, off the colon character, I write here ABC. It will use the ABC String between Olympics. This is the new stink I can use here any string I want Vicks, how we get a string from a least or released from us Drink 40. Lists - List Comprehension: Hi. Everybody in this lecture will talk about the least comprehension. Least comprehension is one off the python's most powerful feature, and it's essential for you to understand how to use it. In fact, it's the Python IQ way off. Constructing a list. This feature provides a concise and easy way to create the risks. Common applications are toe make new lists where each element is the result off. Some operations applied to each member off another least seaQuest or iterated ble. Let's go toe coding and see some examples. I'll create a new least called the numbers, using the least and range functions. And I want to this least to store all integers between one and 10. Numbers equals least off range off one coma. 11. Remember that when using the range function, the first argument, which is the start, is included, and the second argument, which is the stop, is excluded. Now let's assume that we want another least that contains the numbers in the first released , but doubled so it should contain 146 eight then and so on. The classical way to do this is to use a for loop in order to eat rate over the numbers least multiply each element by two and then add the result toe the newest next right. I'll create a new empty leased called Doubled. This is an empty list and the No for numb in numbers. Colon and the four block off coat so doubled my new least dot append off numb times two. And this is my doubled least. Okay, perfect. But what if we there is another way to do this in a shorter and the more concise manner hear his wear list comprehension comes into play, let's create another least called doubled numbers with the same elements, but using least comprehension. Double the numbers equals. And now I open square breaking eggs. This is the least, and inside the least I write numb time stool for Nam in numbers. Let's display the content off doubled numbers, and we've got the same result. What we do here is we create a least because we use a pair off square brackets and that means least, and in that new least we put numb times two for each Nam in the numbers list. In fact, we eat rate over the numbers least multiply each element by two and put the result in the new least. We notice how the for loop is inside the list. Okay, at the beginning, this might look very strange, but after some time and exercise, you'll find that this feature very useful and easy to use. This is just one line off coat and expert readable. Now let's move further and at a testing condition, for example, we want in or least only numbers that are divisible by three. And I had If Nam multiplied by tool mode three. This miss the remainder off numb times two, divided by three equals equals zero. Sorry, I've misspelled the variable and we see in our least Onley numbers that are divisible by three. Once again, this seems confusing at the beginning, Or at least it was for me. But as you see it more and more, you'll get used to it. Let's try another example. What if I want to create a new least that contains the numbers in the first least, but is strings, not integers? This is my first released and I want another least that contains stinks. Lexei str Underlying numbers equals a pair off square brackets and str off n here I kissed from integer to string for and in numbers, and this is my least at least off strings Now. I could, for example, join all elements off the least in a stink. If I try this using the numbers, least I'll get an error because we cannot join integers, but only strings numbers contains integers. In the last examples, I'll show you something that can be really useful sometimes. Let's assume we have two lists off names, and there are some names in both risks, off course. Instead off names you can have whatever you like in the lists. This is my first released, and this is my second list, and I want to build another least of it. Contains names present in both risks using least comprehension. We can do it easy and fast. Let's say names in both risks equals a pair off square bread cakes and I write name for name in name Swan. If name in names, too. This is my list. Let's see X elements and it contains items that are in both risks. But what if there are names written capitalized? So the first letter is an uppercase letter and the names written using lowercase or mixed case letters. How can I get in this case the names present in both risks? For this example? Let's modify the second wrist and here I write team with a lower case letter t the same for then and for John. These are my risks. For that. I create a new least that has only lower case words or names. Names, one underline lower equals and the list n dot lower for N in names. One. Okay, the name off the variable should be names. Wanna underline lower and this is my new list. It contains only names written in lowercase letters, and I write names in both the risks equals and inside the pair off square brackets. Name four. Name in name stool If name dot lower een names. One underline lower the other list and let's see the content off the list names in both lists, and we see the elements that are in both risks. Off course. I can capitalize them using the capitalize method off any string object name dot Capitalize . Thank you. That's all about the least comprehension 41. Tuples - The Basics: in this lecture will start talking about topples Topples are another pipe off order to sequence off items. Topples are very similar to lists and, like risks, support, indexing and slicing. We can refer to any item in the sequence by using its index number, which starts also from zero, not one. Unlike lists, a topple is an immutable sequence, meaning their content cannot be changed by aiding, removing or replacing elements. If you need to modify a purple, you create a new one. This is the key difference between topples and the risks. To create a couple. We use parentheses and comas to separate a limits, but better Lexie Examples on empty topple can be created using the couple construct for or using a pair off for emphasis. These are two empty topples. Please create another Purple Purple three equals one coma to comma free. This is another couple red campaigns. Three elements. Three. Integers. Just like a least a publican have elements off any type. Lexie. Another example. My topple equals and between parentheses, I have winning, takes her afloat, a string, another couple and the least This topple has elements off different types. Integers, floats, strings, topples or risks. If you want to create a topple using a single element, you have to use a trick. Although you have only one element inside the topple, you have to write a coma after it. Otherwise, it will not be a purple. For example. Topple four equals and I want a single element. Lex, a. 4.3. This isn't a couple. In fact, this is afloat. If I want to create a topple, I use a coma after the element. Even if I have a single element. This is a couple a couple with a single element indexing rules. We've seen at least six and strings are still applicable. Topples This is my topple, and this is the element at index zero. This element is at index one index to index three and index. For If I want to get an element from the least inside the chapel, for example, of the ABC String, I can write my topple off four. Vexed the least and after that two between square brackets. Because this element is at index to inside the list. Five he's at Index 06 has index one, and here we have index toe as I said earlier. A couple is immutable. We can't modify a topple. If we try to modify a topple, we get an error and I've got a type air Topple object does not support item assignment. This is the key difference between topples and the lists. These are topples basics and in the next lecture will see many other examples with topples . See you in just a few seconds. 42. Tuples - Operations: in this lecture will continue our discussion about topples Lexie. What operations are available? We can eater it over a couple using a for loop exactly like at risks. We can concoct innate two or more topples by creating a new one off course. We cannot modify the initial topples. We can slice a couple and we can repeat pap. Let's see examples. With all these operations here, I have a topple that contains the days off the week. Let's see, how can we eat right over the stop? So four day in days, the name off my topple colon and now the four block off coat. I'll just print each element off the top of so print day, and I don't want a big slash end after each element. I just want a single white space. Now let's see the concatenation operation. I have a topple the 11 comma, too. Another purple T to that contains two strings A, B and C D. And I'm going to create another Topple called T three equals D one plus Tito and the 63. So I've concocted nated, the two topples and I've created a new one. If I want to repeat a topple. I can write the three star and here Lex say three. And I've repeated the couple three times. If I want to check if an element belongs to the chapel, I can use the in or not in operators. For example, Friday in days and it really turned true because Friday is Annette element off the couple. But if I write fry in days, it will return false and they're not in operator Fry, not in days. We later, too, these two operators are used in. If testing conditions, this is all about topple operations. In the next lecture will see what methods are available toe topples. 43. Tuples - Methods: in this lecture will talk about purple methods. Because apples are immutable objects, they have fewer available methods, then lists. There aren't methods for adding or removing elements from a topple. In fact, there are only two methods. The count and the index method. Lexi examples. I'll create a topple from one eatery. Able a Topple can also be created from any eatery able object using the Topple constructor . So t one equal stop ALOF and I'll pace in a string NT to equals and I'll write some integers. These are my populace. The count method returns how many times an element appears in the top. So let's right the one that count off a and it returned. Three. Vicks because A appears three times in the popular. If an element doesn't belong to the couple, it will return a zero so X doesn't belong. Toe. My top of the index method returns to the index off the specified item in the chapel. If we have duplicates, the method returns only the first to Mitch is in the case off Lisk. We can also provide a starting and ending index do on that index off A. It will return zero because a his index zeer and this is the first Miksche, the two that index off toe. It will return five Toe has index five. If I want to see the index often on existing element, for example, 26 I'll get an error of value. Air X, Nothing topple if I don't want to get an error, but I want toe search for the index. Often element I can use an if statement. So if 26 in 82 so only if the element belongs to the couple, I'll search for its index. T two dot index off 26 and I've got no error. The built in functions max Mean Len and Sorted are also available. Topples so max off the one it will return the maximum element off the top. This is if I mean will return the minimum element and Len will return. How many elements has the couple? I can also use the sorted method. The sorted method doesnt modify the couple. It returns a new topple with the elements sorted in ascending order. If I want toe return a couple with the elements sorted in descending order I use on optional argument called reverse equals true and very turned topple has been sorted in descending order Off course the initial purples heaven bean modified. These were popular methods in the next lecture who see when to use a least and went to use a couple. What are the advantages off risks and the advantages off topples? See you in just a few seconds. 44. Tuples vs. Lists: in these lecture will talk about topples versus risks. Maybe you are wondering, Why should I bother using a couple when it has fewer available methods and locks the flexibility off a list? When you're a beginner programmer, you don't you stop us that often, but it's become more advanced in Python. You'll start to use topples frequently. Lexie. What are the advantages off a couple over a list? First, Pappas are faster and the more efficient than lists. If you haven't ordered collection off data that you know is not going to change, you can make it a topple, and it will be faster. Examples off collections that never change are the letters off the alphabet, the days off, the week, the monks off the year and many others. They are always in the same order, and the never change. Brighton uses a process called constant folding. This means recognizing and evaluating constant expressions at compile time rather than computing them at run time. So python recognizes and a volley weeks a topple it compiled time and the not at runtime. One important remark is that topples are really efficient when they contain immutable objects like numbers, strings or other topples if they contain mutable objects like lists. Populist are not so efficient, comparing to risks. Another advantage is that Apple's are safer than lists using topples. Instead, off risks will make your code safer from box and other problems. When you use topples, you know that they don't accidentally get changed. This is called data integrity. Then papeles can be used as keys in dictionaries. Lists are not valid as keys. Storage efficiency is also very important. A topple consumes less memory than a list. Here I want to show you an example. Using the purple constructor in the range function, I'll create a purple that contains one million integers. So t one equals topple off range off one million. I can write one milion like this or to be more readable. I can use an underscore instead off a coma, which is a special character. So one million can be written also like this one underline three zeros underline three zeros of X. Okay, in private, this is my topple. Then I'll create a list that also contains one million integers. So l one equals least off range off one million. Now using the cease model will talk about it in another lecture. Let's see how many bikes do these data structures occupy so important to seize and now sees dot get size off the one and sees dot get size off L one, which is my list. We can see how a topple with a 1,000,000 items off type integer occupies more than eight million bytes in the least that also has one million items off type. Integer occupies more than nine million bytes. That's an increase off memory consumption off 9%. And this beak is a conclusion. We can say that a couple is more efficient than a list, and it's better to use a topple each time we have a collection off items that never changes . 45. Sets - Basics: now we'll start talking about 66 are unknown. Ordered collections off. Unique elements are set is not a secret. Stipe in Python. You cannot access items in a set by index because there is no order. There is no concept off the first element off the set. The second element, the third element and so on. In real world, there are many collections that are unknown, ordered without duplicates. Let's take just a few examples. Social Security numbers, email addresses, I, P and Mark addresses and so on. All these are collections off unique and unordinary elements. Duplicates are not allowed. If we want tohave such collections in our applications, it's a good idea to save them is six. Asset is a mutable object, and that means it can be modified. We can add elements, or we can remove elements from the set. There is also another object type called frozen set that is immutable. Asset uses curly braces and comas to separate its elements. Lexi examples. To create an empty set, we use the set constructor s one equals set and a pair off for emphasis. We cannot create an empty set using a pair off curly braces that creates a dictionary. This is a big summary like risks and topples the elements off the set can have different types. Let's create a set that contains an integer jury afloat and the stink. One key difference is that the elements off the set can have different pipe, but must be immutable. For example, a list which is a mutable object cannot be an element off a set. I create another set s too that contains an integer afloat in the topple. I use parentheses to create a topple and Vicks Okay, all items off the set are immutable. But if I try to create another set that contains a least at least is a mutable object, I'll get a nearer are you square brackets toe Create a list. I've got the type air unhittable type list in this context on heritable means mutable. As I said earlier, a citizen unordinary collection. And that means we cannot use indexes toe get items off the set index is mean order. If I try a sawn off zero So the element with index zero I'll get on air set object does not support indexing. We can transform a least a couple and a set into the other By using the corresponding constructor I have released Want to 13 a topple and by create a sit using the set constructor and I'll a pay scene palest I can also pass in a string which is an eatery able object like, say, private programming is cool. This is s one. We can see how the very one appears only ones. Even if one appeared twice in the least, this is a stool. There is also no duplicate and this is s free. There is no duplicate and no order. A common use for six is to take a least that contains duplicates and return a set off that list. This is a least that contains some mark addresses, but there are duplicates. If I want a least without duplicates, I must transform the least toe a set and then back to a list so set off, Max. These are the Mac addresses without duplicate. And if I want to have a least, I can have Max unique equals least off set off Mex. And these are the unique Mark Agassi's. There are three unique Agassi's Her set is an a terrible object we can eat rate over a set exactly like over a least a purple or a string for item in s two colon brained esto. Sorry, Print item. These are the L M X off the set. Even if it seems that the items are in the same order, they are not. Don't count on order now it seems they are in the same order. Let's try another example. It's one equals on opening Curly Brace one hosting ABC afloat and another streak. This is my set and you can see how the items are in another order to check if on element belongs format to us it there are the in and not in operators. For example, one in this one and x two or X in X one X force. But why not? In a swan? Returned to vex enough for the moment. About six basics. There are a couple offset methods and we'll talk about them in the next video 46. Sets - Methods Part 1: in the last lecture we've discussed about sex basics six are unknown ordered collections off unique immutable elements. To stress the importance off the fact that the elements in a set have no order. I would like to give you a very short example. I create the sit s one equals one comma, two commas three and as two equals to coma one coma three. These two sects are equal even if they have the same elements. But in another order s one equals equals esto will return through. This one is is to return force. They are not the same sip anyway, It's not applicable to risks I am creating Released and l two equals to coma. One comma free l one equals equals alto. Sorry, I should have used to equal signs and l one is l two returned also false. Now it's time to see what are the most important set. Methods and operations. There is the method. It adds an element toe the set. The clear method removes all elements from the set and the copy method returns a copy off the set. They care that is in the case off topples and lists the assignment operator. So the equal sign doesn't create a copy off a set, but a reference to the same address. I'll show you shortly some examples. Then we have the discard and remove methods. They remove the specified element from the set. Barry move method searches for ever given element in the set and removes it if the element best to a very move method doesn't exist. A key air exception is thrown that this card method removes on element from. I said, if it is a member, if the element is not a member, it does nothing. Then we have the pop method. It removes and returns a random element from the set. Lexi examples With all these methods, it's one equals one comma, two comma free. It's one dot ed off ABC. This is a strength. So I've added an element to the set. It's one dot clear removes all elements from the set. Now s one is an empty sip. Now I want to show you the copy method. This is my s. One sip and now is two equals. This one s three equals s one dot copy s one and s two are references to the same address infected. They are the same object. Let's test It s one dot ed off 5.6. Esto also contains that element. We can see how 5.6 belongs to esto. But if I write s every dot Ed, then I've modified on Lee s three s one and s two Heaven bean modified. This one is s two. This will return true, but this one is s free. Will return force fix the difference between the copy method and the assignment operator lex. It removed method. It's one that remove off three. It removed the value. Three. If I place in on item that doesn't belong to the set, I'll get an exception on error. It's one that remove off then then doesn't belong toe the sit. It's one dot discard off, then won't return on error. Lexi. Now the pop method. It removes and returns a random element from the sit less creator set called names. The first element is then John and Mary, and Weiner equals names dot pop. In effect, I am choosing or in them winner. And the winner is then 47. Sets - Methods Part 2: Python has an implementation off sex that supports many set operations. We've seen some methods and operations in the previous lecture, Lexie. Now what other operations are available? All the following operations return a new set. Don't mutate the original set or six. The first method is difference if set one and set to our 26 The Difference Method returns a new set containing the difference between set one and set toe, which are the elements that exist only in set one but not in set toe. Lexi. An example set one dot the difference off set toe and it took it turned on, you said, with two elements the L Amec's that belong to set one, but not to set toe. The previous syntax is equivalent to set one minus set toe. There is a slight difference between using the method and using the operator when using the method. The argument can also be an eatery able, but when using the operator, both argue mix must be off typeset. In fact, Python will convert the e terrible. We passed in tow us it. This is applicable toe all the methods and operations that will follow in this lecture so set one dot difference and I am pacing a list, at least being unutterable. It didn't return on air, but if I write set one minus and the least I'll get a nera. Another method is the semetic difference. The symmetric difference off to six set one and set to is the set off elements which are in either off the six, but not in both. Alexey An example said one dot symmetric difference offset toe and it returned a set that contains the elements that are in either off the sex, but not in both. We can also right set one car it set toe. We've got the same result. The next method we're going to talk about is the union the Union Off tour More six is the set off full unique elements present in all six in python Union allows an arbitrary number off arguments flexi an example set one dot union off set to the same result can be obtained using a pipe or a vertical bar. Sorry, we have set one not s one and this is the union. The next method is the intersection. The intersection method returns a set that contains the elements that exists in both sex or in all six. If the method is used with more than 26 said one dot intersection off set to my six have a single element in common and X five. We can also right set one end set toe. Another method is Is this joint? It returns. True If 26 are these Joint six. If not it re Paris Force 26 are set Toby This joint if they have no common elements, said one dot Is this joint off set to it returned force because they have an element in common. But if I create another set, Alexey said, for me equals six commerce seven. Set one dot is this joint off set three will return to there are also the less less than or equal greater than and greater than or equal operators used for containment. Testing less than operator returns to. If the set from the left hand side off the operator is contained in the set from the right 10 site off the operator, less than or equal returns. True if or said is included in or equal toe another set. Lex an example. This is set one set to and set free. I'm creating a new set called Set Four equals six comma seven, Comite said. Three less than or equal set for it returned to, but set one greater than set free will return. False. Okay, these are the most important set methods. There are also other methods that maybe are not used so frequently, and you can find them in python. Official documentation Regarding six. This is a text toe. This lecture is a final thought about sex. I want to tell you that the look up time for an element in asset is extremely low, compared toa the look up for an element in a list or a topple. More than that, the look up time is constant, no matter how big the cities. This is because a set is implemented as a hedge stable, and Python does a hash look up. It doesn't scan through all the elements in the set toe. Find it. It does a D act. Look up. That disadvantage off six Comparing two lists and topples is that they consume more memory . So there is a trade off between speed and memory 48. Frozensets: in the last lectures we've discussed about six. What are they? What operations and the methods do they implement and went to use six nights time to take a look at frozen six. Frozen six are nothing more than immutable. Six. They have the same properties in behavior as six, except they cannot be imitated or modified. This means that any off the mutation operations we saw at six, like Ed Update and so on are not going to work with frozen six Being immutable. Frozen sex can be used, his keys in dictionaries or as elements in another set or frozen sip. And this can be really useful to create a frozen set. We used the frozen set function, or we create the frozen set from another eatery able object like a string least topple or set by. Pacing in the terrible is the argument toe the frozen set function. Let's see examples. I'm going to create an empty frozen set using the frozen set function. In fact, this is the constructor off the frozen set class. Now I'll create another frozen set. From a least, we can see how the frozen set has no duplicates of frozen, said can be an element off another set. I create a set s one. This is an empty set Another empty set Esto and I'll try. Toe ed is to twist. One is toe dot ed off esto. I'll get on error because a mutable object cannot be an element off a set. But if I want toe ed, the frozen sit is the element off the set. I won't get on error. Let's see another difference between six and frozen six. Set one is an empty set and now set to equals. Set one dot copy Set to is a copy off set one, but they are different objects, so set one is set to will return. False. Let's do the same with frozen six f set one equals frozen set. And if set toe equals f set one that copy being immutable They are the same object They are saved it the same memory address. So if set one is if set toe will return through all methods and operations that don't modify bursts, it are also available to frozen six. For example, Lexie, the intersection. These are 26 and using these six, I'm going toe create toe frozen six s one that intersection off as to it returned a set with the common element. And the next three, If s one. And I can use the intersection method or the end operator if s too. We turned frozen set with a single element of the common element. All these operations can be performed on mixed sex and frozen six and the result will have the type off the first opened. So s one and F s two who will return a set vexed because the first opened, which is s one is a set. But if I write F s two and s one he to let it talking of frozen set this because the first opened is a frozen set, it takes all about frozen sex. Thank you. 49. Dictionaries - The Basics: Hello, guys. And welcome back in this section will start to take a deep look at dictionaries in python. Dictionaries are everywhere in python modules. Classes, objects, even sex are all implemented as dictionaries. In python. A dictionary is an unorganized collection, off key value pairs separated by commas and enclosed by curly braces. A dictionary is a mutable object, meaning that we can ed toe or remove elements from the dictionary. If you are used to other programming languages like C, JavaScript, ruby or go, you can think off a dictionary being just like an object in JavaScript. Ah, hash in ruby or a map and go. The basic structure off a dictionary element includes two things. One is the key, and then the value. We use a colon to separate the key from the value. Values can be any python object, but keys need Toby hash a ball or immutable objects. We can have an integer. A string a couple or a frozen set is a key, but not the least or a set. The last two are immutable objects and are not allowed, is keys. One important thing to know is that keys must also be unique so keys are unique and immutable values. On the other hand, don't have Toby unique. Keep this in mind. Let's see how to create a dictionary. This is an empty dictionary or dicked. Two equals, and I am using the dicked function. Another dictionary person equals The first key is a string lexei name, and the associative value is John. Another key aches existing so it must be enclosed in single or double Cokes equals 30. This is an integer the value, or for this key, If I want toe ed, another key value pair, I simply write the name off the dictionary, and the key between square brackets equals the veil. So person off, Let's say the name off the new key. Great, he quotes. In the value is a list like, say, 7 10 comma five. Earlier I said that keys must be immutable objects, so if I want to have a key released, I'll get on air. Let's create the dictionary. One more time. With at least, is a key story. Of course, between the key and the value, I must use a colon type error, unhittable type least, but if instead, off least, I have a topple. I'll get no error. This is a purple because I've used for emphasis and there is no error in the next lecture will see how we can work with dictionaries and what operations and methods are available. 50. Dictionaries - Working With: Now let's see how we can work with dictionaries. First, Lex extracted the corresponding value off a specified key. This can be done similarly to accessing elements off released only that we don't specify on index. We specify the associative key for the value we want to extract. So first son and the key between square brackets and I've extracted the value off the key. Named H earlier, I said that the dictionary is immutable. How can we add a new key value pair toe the dictionary? This is done by simply assigning a new value toe. The new key, so person off in the new Key city equals barely. If you want to modify the value, associate it with a key. You simply write the name off the dictionary and the key between square brackets and the new value Personal CT equals London. If the key doesn't exist, it raises a key error exception. We'll see. What are these exceptions in another lecture? So person off, let's say five five doesn't exist is a key. So I've got the key ever. If you want to avoid this error, you can use the get function that returns the value Associate ID with the key if the key exists or a default value if specified and the kid doesn't exist. If the key doesn't exist and the no default value is given it returns, none person dot Get the name of the method off and Bucky. If I don't specify a default value, it will return none off course. If the kid doesn't exist and this is the case here or I can specify a default village, for example, Key does not exist. It returned that default value. If I specify on existing key, it will return the corresponding value. We can also delete a pair from the dictionary using the Dell instruction. They'll the name of the dictionary and the key, and I've deleted that key value pair. If the kid doesn't exist, it returns a keer. Let's deal it one more time. The key value associate it with the H key. It doesn't exist because I've already deleted heat. Okay, so keep Air eight. And if I want to avoid this error, I can use the pop method. Pop method removes a pair and returns the value off a specified key from the dictionary. It works similarly toe get method. So if the kid doesn't exist, it returns a key error. But I can specify a default value for this case. Let's see how it works. So name equals person dot pop off in the name of the key name. It removed that key value pair and returned the value. So name contains the value. Associate it with the name key. That bear has been removed from the dictionary. If I try to put up with that value one more time, I'll get on there because that key doesn't exist. But I can specify here a default veil like, say, no key. And there is no error and the default value has been returned. There is also the land function available, and it's the same function available to lists in topples. It returns the number off key value pairs in the dictionary. So Len off person, it returned one. Because there is only one pair in the dictionary, another method is clear. It clears out all items off the dictionary. Now the dictionary is empty, vex enough for the moment about dictionary basics. In the next lecture will see what are the most important dictionary methods and operations 51. Dictionaries - Methods and Operations: in this lecture will dive deeper into dictionary operations and the methods. The first operation will take a look at his membership operation. It disks if a key is present in the dictionary or not. For that, we use the in or not in operators. If we want to test if a value is present in the dictionary, there is no quick way around it. We must eat rate over the values in the dictionary and search for that specific one. Lexi An example. I have a dictionary called person with three key value pairs if I want to test if a key is present in the dictionary. I write the name of the key, for example, name in person, and it returned to Vex because the key belongs to the dictionary. But if instead, off name I right here X multiple times it returned. False. That key doesn't belong to a dictionary. There are three methods that deal with keys and values in a dictionary. The 1st 1 is keys method. This method is used to obtain a dictionary view having the keys off the dictionary as elements. This if you can be easily converted to a leased using the least function. A similar method is values. This is used to get the values off the dictionary. Like the keys method, it returns a dictionary view having the values off the dictionary as elements. We can convert this object to a list using the least function. And then there is the third method, called items. It returns. A list off topples each topple containing the key and the value off each dictionary. Pair Lexi Examples Person dot keys and it returned a dictionary view that contains the keys . This is not a list. This is an object off type dicked keys called also a dictionary keys of you. It can be easily converted to a list. For example, Keyes equals least off person dot keys, and this is the least that contains the keys off the dictionary. Similarly, we can use the values method, so values equals least off person dot valleys, and these are the values. Lexie, the last method person dot items it returned topples each topple contains the key and the value off each dictionary pair. One important thing is that the order off keys, values and items returned by these three methods is the same That means that, for example, the third key we turned by the Keys method corresponds with the third value returned by the various method. Also note that the views returned by these three methods are dynamic. They reflect any changes in the dictionary. Let's see an example. I modify my dictionary person off like, say, H equals 40. This is my dictionary and keys equals person. The keys Values equals person dot values. These are the keys and these are the values. And now I do person off Age equals, Let's say here 60 without calling the values method one more time, it has automatically updated the content off the values variable. We can see how inside the values variable I have the new value, which is 60. That means that the variables returned by the keys and values methods are dynamically updated. You should also know that dictionary views returned by keys and items methods behave like six index because keys are unique. This means that sex operations are available also for keys and items. Lexie An example. I create another dictionary called the one with two pairs, and the two that contains three bears. K one equals do you want the keys and K two equals the two dot keys. This is K Wan. This is Kato. These objects behave like six, so I can try a set operation. In a previous lecture, we've talked about sex operations. Let's try, for example, the union So K one. A vertical bar or a pipe Kato. And this is the Union and the result is a set. This is a sip. Now that we know how to get the keys and the values off dictionaries, Let's see how toe eater it over a dictionary. I can eat or ate over the keys over the values or over the items. Lexie Examples four K in person dot keys Colon and this is the four block off coat I am going toe print. Each key using on F string Literal makes a key colon and the temporary variable called K between curry braces. In this example, I am iterating over the keys off the dictionary. I can either it over the keys, also without specifying any method, only the name off the dictionary. So this is the same. If I want to reiterate over the values, I can have something like this so for V in person dot values And here I have value colon V . These are the values and the last example. I am iterating over both the keys in the valleys So K Kumar V. This is the syntax in person dot items key colon and between curly braces K coma value colon In between curly braces V, K and V are the temporary variables off the four loop Vicks How we eat the rate over a dictionary. Another useful method is update Update is used to extend a dictionary with the items off another dictionary. Flexi examples Be one This is another dictionary and here I have location Colon Berlin Alexei job colon developer And now I write person dot update off B one. You can see how I've extended the person dictionary with the content off the P one dictionary. In the end, off these lecture, I want to tell you one more word about order. Even if a dictionary is a noun ordered and we cannot count on order starting with python 3.6, a dictionary remains ordered in order off inception. That's all about dictionaries. Thank you 52. Dictionaries - Dictionary and Set Comprehension: Hi, everybody. And welcome back in a previous lecture, we've talked about this comprehension, which is a powerful python IQ way off. Constructing risks now in this lecture will take a look at other comprehension, which are set in dictionary. Com. Pretentious, In my opinion, these are extremely powerful python features and our most off the time under used Let's go toe coding and see how it works. The syntax for set comprehension is the same. It's for least comprehension, just that we use curly braces instead of square brackets. I'll create a new set called Names that contains some names written with lowercase, uppercase or even mixed case letters. Some the second name is en John, and then, using set comprehension, I want to capitalize each name off the set. Alexei New names equals a pair off curry braces. This means said comprehension. And now n dot capitalize for em in names, and this is my new sip. Each name is capitalized. Perfect. We see how set comprehension is similar to least comprehension. Now we'll move forever and take a look at dictionary comprehension. I'm going to create a very simple dictionary with only three key value pairs. The one equals the first key. He's a in the value is one B and to end the last keys C and X corresponding value is free. Using dictionary comprehension, I want to make each value off the dictionary double. I use a pair off curry braces exactly like I've used for set comprehension, and now I write que colon V. This means a pair off key and value. The value will be multiplied by tool four K Kumar V in the one dot items, and we see the new dictionary. Each value is doubled. We can also modify the key if we want. For example, let's change the key toe uppercase letters for that instead off key. I have key the upper, and this is the new dictionary. If you want to other condition, you use the same if statement. We've seen at least comprehension. Let's assume we want in our dictionary Onley values that are divisible by three and I write if V more free equals equals zero and our dictionary has only one item only one pair okay, perfect now related to this topic, I want to show you the building zip function, which is really useful sometimes this function takes is arguments one or more eatery Ables and aggregate them into a zip object, which is an iterated er off topples. This can be later transformed into a least off topples or into a dictionary. If the best Peter Abeles have different lengths, the heat are able with least items decides the length off the new eat aerator. Let's try an example. I create the least called years in the least called Revenue that store store revenues off the years in the first released years, and I have released with three values 2017 2018 in 2019. This is the first list now the second least who ever knew, he quotes and the values 50,000. 40,000 and 50,000. Now let's created a zip object. The operator zp off years coma revenue. This is the Zip object. If we want a least off topples who use the least constructor, this means least off and the object returned by the zip function. This is a least off topples. Then if we want a dictionary, we use the dicked constructor so instead off least I use dicked. This is the dictionary. Let's capture the dictionary in a new variable lexei sales. The zip function paired together the values off the lists in a dictionary. The items off the first least are the keys, and the items off the second least are the values off the new dictionary. At the end. Off this lecture, I want to show you one last example. Let's create a new dictionary called Profit. Knowing that the profit margin is 15% off course will use dictionary comprehension, because this is the simplest way to accomplish this task. So we have a 15% profit margin, and I write profit. This is a dictionary, a pair off curry braces. Okay, The key. The key will be the ear colon V. This is the value. I want to multiply the value by 0.15. These means 15% for K Kumar V in sales dot items. Let's see the new dictionary and we see the profit for each year. Okay, that's all about dictionary comprehension. Thank you. 53. Functions - The Basics: in these section will start talking about functions. This is a core topic in python and in any other programming language. Functions are everywhere. We've been using tons off, built in functions like print Len sorted in the methods for strengths, lists, topples or dictionaries. These methods are infect, special type of functions. A text toe objects. Now we're going to see how to define our own functions. First, let's see what is a function and why do we need functions in our application? Well, we use functions toe organize our coat in blocks than can be later reused. This is useful because we want better readability for our coat modularity and also time saving when designing and running coat. This is a keep part off becoming an effective programmer. Functions allow us to create blocks off coat that can be easily executed many times without needing toe constantly rewrite or copy paste that block off coat. Instead, we create a function that executes that block off coat, and after that we call the function. There is a term quote Dry, which is an acronym for Don't repeat yourself. Functions keep our code dry. This is opposite toe wet, an acronym for Write Everything Twice, and that's not good at all. Let's explore the syntax off, creating our own functions. A function is defined using the D. E F keyword from define, followed by the name of the function, a pair off parentheses and then a column after the colon on the next row will have the block off. Coat off this function. The block off coat must be indented one level to the right. This isn't something new for you. It's exactly what we did with if four or while statements. It's recommended to use lower case letters and the snake case for the function name, and that means two separate words using under his course. The first part off a function body is recommended. Toby A Docks Drink, which is a string enclosed in people quotes, and it explains what the function does and not how it dies. The Doc String basically creates the help section off the function. Doc String is not mandatory but is recommended after that Doc String we have whatever code we want to execute is, I said earlier. Functions are reusable blocks off coat. After defining of function, we can call that function whenever we need to run the code inside it. In order to call a function, you need toe type in that functions name, followed by parentheses. And that's all. If we just defined the function and not call it the block off code inside, the function will never be executed. Let's see some examples. I'm going to a spy charm the python i d. Using about the F cured. I am defining a new function called My Underline function. This is the snake case then I have parentheses, a colon and the function block off code follows. First, I'll have a doc String or the function help. This is a string enclosed between people quotes. This is my first function. It brings out a string and the function will have only one instruction and fix the print function. It will print out a string Hello Python world. I have defined the function and now I'll call it. I am writing the name of the function, followed by parentheses and vex. All here on line number seven. I am calling the function Now I'm going toe run the script. We can see here how our function printed out. Hello Python World if I don't call the function. This script does nothing, and the vet means no instruction is executed. The function block off coat is executed on Lee. When calling the function off course, I can call the function as many times as I want. The above function has a dark string, the help off the function we can access that help using the help built in function or using the name off the function that in Bander Duke or Underline Underline Doc Underline, underline like seat. So help my function here without parentheses. So I'll pay seen on Lee the name off the function. I am going to run the script and this is the function Help or the docks drink. Another possibility is print and is an argument. I'll pace in the name of the function without parentheses dot dunder doc exit and this is the docks. Drink now. It should be noted that functions can take in parameters. They are written inside the parentheses. If there is more than one parameter, they're separated by commas. Lex, modify our function toe, take a perimeter. So I have a perimeter called the name in here in the brain function. I'm going toe concoct innate some strings. So plus, in between single quotes, my name is plus name. Name is the function parameter. Whenever we are going to call the function, we must best an argument. Oh, the function and that argument will be further pest toe the coat inside the function. If I call the function without an argument, I'll get an error. So I am executing the script and I've got the never a pipe air, my function missing one required positional argument in the name is Neighbor. So here I must best in a stink palm. Very now I can executor script without getting an error. The function begin to doubt. Hello, Python worth. My name is Andre. Let's take a look at two terms used in this context. Arguments and parameters In Pilon. Many people use them interchangeable, and maybe there is no such a big deal. But you should know that there is a slight difference between them to when we create a function. Let's say my function off a comma be in this context. A and B are cold functions parameters. They are variables, local toe, the function. When we call the function, let's say X equals five and y equals a string. And we are facing in X and y to the function. They are called arguments. Functions are gimmicks, so a and B are parameters and X and white. Our arguments This is just semantics, Index. Okay, if you mix up with these terms, I just wanted to make this distinction clear now Big, for example, if the function has more arguments, the order we used them when pacing the arguments is important. Lexie. An example. I'm going to create a new function. Lexei function one. These function has two parameters. Ex coma. Why? And here inside the function I'm just going toe. Print the value off each parameter. So print if from F string literal and here first argument colon X between curry braces and print. Second argument Why? And I'm going to call the function so function off likes a python. This is the first argument and 55 the second argument and that around this creep we can see first argument Python second argument 55. The first part a meter X was mapped toe python because vexed the first argument and y was mapped toe 55. If I use 55 for the first argument that value will be mapped toe X and python to why. Okay, first argument 55 2nd argument by phone. 54. Functions - The Return Statement: Now let's take a look at a very important topic. Dari Turnkey wort. We use the Return Key Ward to send back the result off the function instead, off printing it out Return allows us to assign the output off the function toe a new variable. In fact, we've already used many times the return statement. For example, let's create a list with three items. L one equals one comma. Tokuma three. This is a list, and the van length equals Len off. Ellen. The land function I've just called doesn't print of value but returns the villi witches then captured in the length variable. This is just an example off a function that returns value. Let's see another example. I'm going to create a function called some one. This function takes two parameters A and B In decided the function, I'm going to create a new variable. A temporary variable called some equals a plus B. Off course. This function calculates the some off two numbers, and I'm going toe print the some so print F some colon and between curly braces, some. This is my function, and I am going to call the function. I'll pace in tow arguments like, say, three Coma. Six. When I'll run the script, it will display some colon. Nine. Now let's create another function that returns the some off two numbers. So they, if some tool a coma be some equals a plus B and using the return key word, I'll return the some. If I run the function with two arguments, the function won't display any message, but it will return a value if I run the script. It seems that the second function doesn't do a thing of X because the return value is somehow lost. If I don't want to lose the return value, I must capture that value in a variable like say, s equals and my function. And then I can print that value. I'll or under script again. We can see how the second function returned a value that value has been captured in a new variable called S and online number 12. I've printed out the some vexed the difference between a function that brings out a message and a function that returns value, Ah, function that doesn't return explicitly or implicitly none. This means that the first function called someone who he turns implicitly none. So if I write here, Alexei, return value equals someone off. Three. Coma six. And after that print have you talking value? We can see how the return value was none. So any function returns something the value off, the return statement or none. If it doesn't return explicitly, you should also know that the return statement Exits of function. The return statement is the last statement executed when calling the function. If there is any other instruction after the return statement, it will never be executed. Lexie. An example. After the return statement, I'm going toe print a message makes a message after the return and another message before the rhetorical statement licks around the script. We can see how it printed out message before the return statement, but it didn't print out message after the return statement. Vicks, because the return State month execs the function. This is the last instruction off my function. The instructions after the return will never be executed. You should also note that the function can return more values. In that case, it returns a topple I'll A modified the function to return both the some off A and B and the product off a multiplied by B so some and product product equals a multiplied by B. We turn some coma product, even the function who he talks to values. I must capture both values. So here, Alexei s Coma P in the name of the function is some end product. Now I'll execute the script. The script printed out some is six. Product is five here. Inside the function, it's not necessarily toe. Calculate the result off these operations in a variable, and after that return that variable. We can return it directly. I'm going toe comment out this coat and I'll simply right return a plus B comma a multiplied by B ekes the same. 55. Functions - Arguments: in this lecture will dive deeper into functions. Arguments In the last lecture we've seen a type off functions argument and next, the positional argument. In fact, in Python, there are five types off function arguments. These are positional arguments, default arguments, keywords, arguments, star arcs and star star kw arcs or quarks. Let's take them one by one. The most common way off assigning arguments toe parameters is via the order in weeks, their best or their position just a quick example. If you define a function like, say, my function, one with two parameters A and B when we call my function using five and 10 is the arguments . The first argument five will correspond to the first parameter A. In the second argument, Stan will correspond to the second barometer. Be inside the function A equals five and B equals. Then let's run the script. We can see how a is five and be East end if we call my function and Lee pace in then and five. The same thing happens just that in this case, inside the function A, which is the first parameter equal Stan because then is the first argument, and B equals five being the second argument. This is what positional arguments mean. It basically looks at the position off the argument and a science it toe the same parameter at the same position. If we call the function with a different number, off arguments will get a type error. For example, I'll call my function with only one argument. The same thing happens if I call my function with Alexei three arguments. I'll get the same error. Now let's take a look at another type off argument. The default argument. A positional argument can be made optional by specifying a default value for the corresponding barometer when defining the function. Let's take a look at an example, will modify this function and will make the second argument optional by giving a default value for the corresponding parameter. So here I'll write. B equals 20. How do we call this function? We call it in the usually way, with two arguments, or we can omit the second argument if we want. In that case, it doesn't throw on exception, but the second parameter will have the default value, which is 20. If I run the script, I'll get the same result. But If I omit the second argument, which is five, it won't throw on exception this because by default, the second argument is 20. We can see how a piece 10 and B is 20. Now. Let's consider a case where we have a function with more than toe arguments, and we want to make one off them optional. And the optional argument is not the last one. Let's consider this function with three parameters. A BNC and B is by default. Then we specify a default value for B, but not for C. How can we call this function without basing in the second argument? Something like my function one off, five coma 30. How could it assign these values toe the function parameter a equals five x clear but 30 will be assigned Toby or C. In this case, Python doesn't know, and there will be a syntax error. The goal here is that if we define a positional parameter with a default value than every positional parameter after, it must also have a default value. So if we give a default value Toby, then we must also specify a default value for a C C equals nine. No there will be no error. A equals five b equals 30 and C equals nine. Let's bring out the value off. See Iran the scrip off course. I can omit the second argument in this case a piece. Five b East, then the default value and see is nine X default value. But I am also free toe typing all arguments. Now let's see what happens if we want to specify the first in the third argument. But omit the second argument in our example. We want to specify values for a NC, but let be take its default veil. For that. They are another type off argument called keyword argument. Cured arguments, also called named Arguments, basically allow us to ignore the order in which we entered the parameter in the function definition, or even to skip some off them. When calling the function using keywords arguments miss, you can specify the name off each parameter and assign it a corresponding value. During the function called, I'll Raise the default value off the functions parameters and when calling the function I'll write is arguments. B equals five A equals six and C equals seven. Let's run the script. We can see how A is six b is five and C is seven. These are cured arguments and their position when calling the function doesn't matter anymore. A function with cured arguments is defined in the same way is a function with positional arguments. There is no difference when defining the function. The difference is only when calling the function Python also allows us to mix positional and cured arguments. But there is a whole. When doing this and the vics toe first specify the positional arguments and then the cured arguments. So once we use a cured argument, all arguments thereafter must be cured, too. If we don't follow this school, Python will return a syntax error. Let's try to call the function in this way. My function. One off six c equals one B equals nine. This is okay. There is no error. But if I write a equals six, then one and C equals nine. I'll get on error, Vicks, because a positional argument is not allowed to follow a cure argument. So these positional argument, which is one is not allowed Toby after a cured argument, which is a equals six. So if I used a cured argument for the first argument off the function. All arguments that follow must be cured. So here I must write B equals one and there is no error. Let's see another example. The first argument these seeks be equal Swan and the nine. This is also a nera. If the second argument is a cured argument, all arguments that follow must be off type cured. I must try to see equals mine and vex. Okay, But what if you don't know how many parameters you are going to need, you know, function and you want to specify a variable number off parameters in sight of function definition, for example, we define a function that calculates the some off 234 or any numbers. In this case, we don't know exactly how many arguments will be passed in when calling the function The python solution for this is start arcs, where arcs is in fact, a topple off potential. Additional arguments. This topple is initial empty, and this means we don't get an error if we don't specify any additional argument. When calling the function Lexi an example, I'm going toe define a new function, the F F one the first parameter is a In the second parameter is star arcs. You can use any name, but it's recommended to use arcs in the function body. I use the print function toe print, the value off the arguments, a colon and a between curry braces. This is if string literal and print if arcs colon arcs and I'll also calculate the sum I'll create a temporary variable called S equals a plus some off arcs. I am using the some built in function because arcs is in fact a topple and print. Some colon s between curly braces. I can run this function with a variable number off arguments. Next, right? If one off five I am calling the function with only one argument, we can see how a piece five this is the first argument and the topple, which is start arcs, is empty. But if I want, I can pay. Seen two arguments. Five Coma for a remains five and arcs is a purple with only one element in the sum is nine . If I want to pace in more arguments, there is no problem. It will work like a charm. We see how a piece five and arcs is a popular that contains is elements. The arguments that follow after the first argument, which is five in the sum, is 38. The last type off argument is star spar kw arcs or quarks if you need a variable number off parameters but want to use keywords arguments instead of positional arguments When calling the function, you can use a double asterisk, followed by kw arcs. Instead off arcs. Quarks stinks for cured arguments, and it builds a dictionary off key value pairs. Lexie An example. I'll create a new function, the F if toe and this function his one parameter star star, K W arcs. The first line off the function body will print about the value off quarks, quarks being a dictionary, I can test if a key exists, and if the key exists inside the dictionary, I can print Exe Vale. So let's say, if name this is a dictionary key in Quark's then print. Your name is in between curly braces, quarks off in between double quotes. I've already used single cooks for the If string literal hi, right name, and I'll call the function this way if toe off and now keywords are gimmicks, name equals John. Age equals 40 and location equals London. Now I run the script to see what's inside K works argument, and this is the output off my F toe function. We can see how it created a dictionary. The keys are the name off the arguments and their values. Are there associate ID values. So the value that corresponds to the key called name is John. 40 chorus points toe age in London. Location after that, because the name is a key in this dictionary, it printed out Your name is in the value. Associate it with the name key, which is John. This type of argument is often used when working with different external models or libraries. Also remember that the names, arcs and quarks are just conventions. You can use any name instead as long as you use the asterisk for positional arguments and double asterisk for keywords arguments. These asterisks are mandatory. If I want to write X instead off quarks, there is no problem. I'll get no error anyway. This is not recommended 56. Functions - Scope and Namespace: in this lecture will take a deep look at scopes and name spaces in python. The name space is, its name implies, is a space holding some names and by names. I'm referring toa variables, functions or classes we define. We can imagine a name. Space is a container or even a table that contains the names we defined. This way we can have the same name or variable defined in different name spaces. A name or variable can exist only in a specific part Off our coat, the portion off coat where the name exists is called scope off that name or variable, and the binding between the variable in the object or value is stored in a name space. Each scope has its own name space, so scopes and name spaces go hand in hand. In python, there are three types off name spaces or scopes. The building name space contains python built in functions. We've used a lot off A building functions till this point. Begin Len Sortie, Drink Max are all built in functions. They are available to us whenever we want to use them. The global or module name space contains all variables, functions and classes we define in our script. And then there is the local name space. It contains only names defined inside our own functions. Lexi examples Here in this script, I've used the built in name space. In fact, I've used three built in functions topple or engine print, and I've also created a global scope variable called the one The built in names are available across all files or models off our python application. And the global names can be used anywhere inside our model, including inside any function, but not outside the current file where they have been defined. Now, if I try print EXE, I'll get an error. A name. Air Siphon. We'll look for X in the global or model name space. It doesn't find it, and then it will look for it in the building name space where he cannot find it either and returns around time air. But if I do X equals, then in Iran, they escaped one more time. I won't get a never the script brings the value affects without their vex, because the Name X has been created in the global name space, so X is a global variable. We can use it anywhere inside this script, but not in other scripts. Let's see another example. I'll define a function, my funk, and inside the function I am printing the value off X. Now I am calling the function. The script printed X inside the function colon. Then, when courting the function, the Python interpreter is looking for a local variable called X, a local variable means defined inside the function. If it doesn't find it and vexed the case here, it will search for X in the global scope. There, it finds the name and uses it to paint X value inside the function. If there is no X variable defined the neither in the local name space. So inside the function nor global nor in the building name space, it will return on error. We can see how it returned a name air. We can see from this example how scopes are nested. The local scope is nested inside the global scope, which is nested inside the built in scope. If your reference a variable name inside the scope and python does not find it in that scopes name space, it will look for it in an enclosing scopes name space. It searches up the chain off all enclosing scope Name spaces. Lexie, um, Other example. Inside the function I write X equals five. When we create functions, we can create variable names inside those functions using assignments. Variables defined inside the function are not created until the function is called. Every time the function is called, a new scope is created in variables defined inside the function are assigned so that scope . When the function finishes running, the scope is gone, too. Now. In this example, inside the function, a new local variable called X is created. When calling the function, it finds a variable called X in the functions local name space and uses it that variable exists only inside the function or in the function local name space. Outside the function, there is another X variable that stores another value. Here I'll write print ex outside the function, and I run the script. We can see how X inside the Function Stores value five and X Outside the function stores value 10. This is a local variable. Inside the function and outside the function, I have another variable, a global one. Another example is when we want to use or to import a global name into a function name space. For that, we use the global keyword for that name we want to use before any other statement. Vet refers to the global variable I'll create another global variable called a equals. Then in the new function my funk one in the function, his one perimeter like, say, B and inside the function I have global okay, a glass equals B and I'll print the value off a inside the function. Now I am going to call my funk one. So my funk one and I'll place value is argument like se seven. After the function call, I want to print the value off a so print a outside the function and I'll run the script in this example. Inside the function we are importing and using the global a variable, and that means that the result off the operations done on a inside the function is available. Also outside the function we guess you how a equals 17 inside the function and a also equals 17 outside the function Vicks because a is a global variable. So inside the function I am using the global variable Python doesn't create a local one. Let's see another example. The F my funk to and Inside the function I write Print X and X equals eight, and now I'll call the function. So my funk to when running the script, I'll get on air unbound local error, local variable X referenced before assignment. Why is that? Whenever Python sees a nasu sign mint operation in sight of function, it interprets that variable is local and these happens at compile time. In our example, Python says, Okay, here is on assignment. So I'm going toe. Create a local variable called X, in this case, the local variable X that will be created. Mosques or shadows. The Global Variable X, defined earlier when wanting toe paint the value off X. It returns on error because the global variable is masked and the local variable hasn't been created yet. In fact, we are using a variable before creating it. It's the same, like writing print likes a C and then C equals six. Here I am using a variable called C before creating that variable, and Nixon never is a conclusion. If there are variables that are referenced inside the function but are not assigned value anywhere in the function that variables will not be local and python. We look for them in the enclosing scope at Runtime Vicks because there is no assignment inside the function and it will consider X global variable, not a local one, and there is sexual variable in the global scope here. X Equal stent in the last example From this lecture, I'll show you how a name can be mistakenly shadowed or masked. I'm going to write Print Len off and here a String ABC. After of it, I'm going to define a new function called Len Off X in the inside the land function I write print. Exe. Now I'll call the land function and bile pacing a string, and I run the script. We see how the building land function has been shadowed. In fact, python considered the land function defined here in the global name space and not the land function defined in the building name space. When I called the land function, it pointed out the stink best in his argument and not the number off characters off that string. This is a common air, so you should never use names that are already defined. What can I do now? I can delete the name that shadows the building name. So Del Lin, In this case, I delete the Len function that has been defined in the script. After that, I can run the land function and it will return the correct value. So, let's say print Len off and a random string. We can see how it returned the number off characters in this strength. I have called the original The Building Len function. All right, Thanks for watching it. And I see you in the next video. 57. The Special Constant None: in this lecture will take a look at a building constant called the None. None is like 24th but it means something else, and it's not an object off the bull class. It's an object off the non type, and there is only one instance off non type, which is none. None is frequently used to represent the abscess off a value there. Nothing mess and assignments toe. None are illegal and raise a syntax error. If you have worked with other programming languages, none. ISS Pythons version off you'll or nil. Now let's see some examples. Bull off. None. Three titles force so the truth iness value off. None is false. Keep in mind that none is always written with an uppercase letter. And the vein, If I write none equals false, I'll get them there. I cannot assign value to none. Type off none, and it returned. Non type Man is frequently used when default arguments are not passed to a function. Lexie. An example. I create a function called if one that has one parameter and the experimenter is by default . None. So this is a default argument with value? None if a this means. If bull off a equals tool. So if I call the function with an argument, then I'll print the value off a else brained, a function called without arguments. And I'll also print the value off a. Okay, Vicks my function. Now I can call my function with a non argument Lexie. If one and I'll pace for is the functions argument and it painted a piece for I can pass any argument here, but I can also call the function without any argument, a fun without arguments, and it didn't return on air. It simply executed the else block off coat. So keep in mind that none is frequently used when positional arguments are not best toe a function. It's all about non for the moment, but I'm sure you'll see non many times in python applications. 58. Lambda Expressions: In the previous lectures, we've seen how to define functions we use the DF cured the function name on optional least off parameters in close by poor embassies, colon and then the function body indented by four spaces. Then there is that it turned, cured of etiquette, turns of value and execs. The function Lumbee expressions are another way to create functions. They're called unanimous functions because they don't have a name like a function Hayes, and they are a single line off logical coat. Where is the Lambda cured? Which creates a lumber expression and we can think off The Lambda cured is off the D of cured from functions. Then there is an optional parameter list. The parameters are not enclosed by poor emphases. After that comes a colon, and this is mandatory and the valid expression that is evaluated and return when the lambda is called. You can think off the expression is the body off the function. Lumbee expressions can be assigned to variables or pest. Is arguments toe another function? Lexi examples. First, I'll create a normal a classical function. Let's say DF square off X and the function returns X squared so return X to the power off. Now I'll create a lumber expression that is identical toe this function. So Lambda X, this is the parameter colon, and now the expression that will be evaluated and return So x double star toe. This is the lumber expression. These London expression is just like a function. In fact, if we print the type off, the expression will get a function. So print type off and the expression we can see that X type is function is I said earlier. We can assign the value he turned by the lumber expression to a variable I'll write sq equals in the lumber expression. The Lambda expression and the function are identical. How can we use the lumber expression? For example, I can write paint sq off. Five five will be assigned toe the Lambda perimeter, which is X. When I run the script, it will print 25. So five squared. Let's see other examples s equals Lamba X comma y colon ex Blast way. These returns the sum of two numbers. Print s off. Five. Call my seven. When I run the script, it will print 12. Five has been mapped Toe X and seven to why the body off a lambda is limited to a single expression We cannot do. Assign monks inside lumber expressions. Lumber, X colon, X equals seven. This is a nearer because assigned monks are not allowed. And here I have an assignment I can't assign to Lambda all the polls. We've seen it functions. Arguments are applicable to Lambda us to we can have default arguments, keywords, arguments, start arcs or star Star quarks. I'll modify the slumber expression and I'll make X default one. In this case, the perimeter Cold X has a default value equal to one. I can call the lumber expression like this a school and no argument. It will return one. So one squared. Okay, I must comment out this line. Another common use case for Lamb Baz is when we pass a function is an argument toe Another function. Let's see an example. If my function X and funk the second perimeter is also a function, and here I return funk off X Lexie. How I can call the function Why this is the return value equals my function off. Let's say five. And now for the second perimeter, I'll create Alum by expression. So Lamba, ex Colon X start started toe so X squared it will return five squared If I paint why it will 50.25. This is an example off. A function that takes another function is on argument. 59. Files - Opening for Reading: in this section will start the serious off videos on working with text files. If you are working in the area off Network automation system, administration, data science or machine learning, Reading and writing files is very, very common in this lecture. Will see how to read text files with python in order to it or write files you don't need to import module. It's handled natively in the language. The first thing you'll need to do is to use pipelines built in open function toe. Get a file object. When you use the open function, it returns something called a file object. File objects contain methods and attributes that can be used to collect information about the file you opened and to manipulate the fight. Let's create a file object simply called F equals Open the function that returns the file object and X First argument is the final name in this example. There is a file in the current working directory called Configuration that txt Here you should use a valid, absolute or relative path according to your operating system. Please note that using a Nen valid path is a common mistake. Then the second argument, which is optional is called access mode or simply mouth and indicates how the file will be opened. The most used modes are a read Write in the Pent. We'll see examples with different modes, but for the moment, let's open the file in her it only moat. This is also the default, so if you don't specify amount, it will be opened in read only mode. You should know that Imp Ivan of File is categorized is either text or binary, and the difference between the two file types is important. Text files are structured is sequence off lines where each line includes a secret off characters and terminates with a special character called E O L or End off Line. This is coded as a big slash M. A binary file is any type of file that's not a text file, because off their nature, binary files can Onley be processed by an application that knows or understands the fais structure. We can specify if we work with a text file using the T leper or B if we work with a binary fight. Binary files are photos, pdf files, executable files and so on. By default, Python considers the file is being a text fight. Now, having the file opened Lexie how to access and print out the data within the file. The first method will take a look at is read. It reads Entry purse. The entire content off the file is a stink. Content equals f the treat and print content. Lex execute birth script, and we see how we thread and printed out the entire content off the fight. When we are all done with the file, we must close it. There is an attribute off the file object called closed. That is, too. If the file is closed and false, otherwise, like six value f dot closed and I'll execute the best script and the value off this. Our tribute is false. This means that the file is not closed. In order to close the file, I must call the clothes method f dot close, and I'll print the value off the close. Art rebuilt one more time. We see that after calling the close method the closed, our tribute is true. This means that here the file is already closed. This was the basics off reading files with python in the next lectures will see many other examples on how toe handle files with private 60. Files - Tell, Seek, Cursor: in the last lecture, we've seen how to read the entire content. Off a file is a string using the read the method. The read method can also be used to tell Python to read only a number off characters from the file and return that many characters, not the entire content off the fight. You can do this by entering a Nen Tech's ER in between part emphasis legs read only the 1st 5 characters off the fire. This is the text file I am reading, and I'll execute the Python script. We can see how it thread and printed out on Lee the 1st 5 characters. Now let's read and point out another free characters if don't read and three between Port emphases Brint content, we see how he trade in, pointed out the next three characters. After the 1st 5 characters, these three characters. Somehow Python memorized the position where it left inside the file After calling the first read method, this is where the concept Off car sir comes into play. The cursor is the position at which you are currently located Inside the file. We can get this position by calling the tell method off any file object. Brilliant f dot tell and we see that the cursor is at position eight. If we want to move to a specific position inside the file, we use another method called sick. This method takes in his argument the number of characters from the beginning off the file it which the cursor will be positioned. Let's move the cursor to position two and the then read Three more characters have got sick off to. I've moved the cursor at position two and now print f dot reid off free. I am reading three characters this is positioned to and these are those three characters that it will look it. So s T m lex execute the script and we see how it read and printed out exactly those three characters. So starting from position to if we want to move at the beginning off the file, we move the cursor to position zero have got sick off zero. Now the cursor is at the beginning. Off the fight, Brian. Theft. Don't read off one legs. Check it out. And it pointed out age a lowercase H off course. This is the first character 61. Files - The With Keyword: in the last lectures, we've seen how to open a file and how to read the content off the file is a strength. You should always remember to close the file by calling the close method. After you are done with it in this video, I'll show you another way toe open and close files. In fact, this is the really commonly used and recommended way to deal with files. I'll comment out the entire coat and do it again in another way. With open the name of the file. We have the same file configuration dot txt. We opened the file in it only mouth as file colon file dot reit and after a vet outside the with block off coat I write print file dot closed. I want to see if the file is opened or closed. Of course, if I want to see the content off the file, I must print it. We notice how it read and printed out the entire content off the file, but also closed the file automatically. We see this by printing the value off close to our tribute off our file object, and this is to is a conclusion if you are not using the with keywords, then you should call f dot close to manually close the file in our scripts will use the with keywords and won't close the file manually. Please note that outside the with block off coat and these consists off life with the same indentation level outside. The file is already closed. If we try, for example, to read the file outside, the with block off coat will get an error file dot reid I cooperation on close to five online 23 The file is already closed. This being said, you should use this structure to read or write files and not this one. 62. Files - Reading a File in a List: in this lecture will take a look at how to read a file into a list in Python. The list is the most versatile data structure, and working with a list is extremely common in many applications where file processing is required. We'll start by reading the content off the file into a list, process the least and write it back to the file. There are more possibilities to eat the file into a list. Lexie. The first example. I am opening the file using the with keywords the name of the file configuration dot txt A . And I don't specify the mode because I am opening it in Read Only mouth, and that's the default as fine. In this example, we use the same read method we've seen before and the split Linus method. The split lines method speaks the string at line breaks and returns released off lines in the string, so my least equals filed a treat dot split lines, and I'll point out my list and received a content off the file in a list. Each line off the file is, in fact, an element off the list. Another way to hit a file into a list is to use the read lines to method. This is a method off any file object. It reads until end off file and returns a least containing the lines. I opened the file again. The file has been close to, so I can't use the same object. The file will be opened in read only mode. This is the default, his file and my least equals file dot read lines bring my list and I'll execute the script . We're not this rep at the end. Off each element, we see a backslash end, which is a new line. This is one difference between using file daughter eat, dot spirit lines and the using file DOT guidelines. There is also the read line method. So without on ending S that reads just a line, not the entire file Lexi An example print filed ought read line, and I'll call the method one more time. We see how it read and pointed out the 1st 2 lines off the fight. Also notice that a file object is an eatery able object we can eat rate over it line by line, using a for loop. In this example, I write for line in file colon print line, and I'll execute the script. And this is my file line by line off course. The brain function EDS. A bex elation at the end off each printed line. If I don't want a big slash end, I can use the end argument equals and an empty string. These were some ways toe eat a file into a list. 63. Files - Writing: In the previous lectures, we've seen different ways to read text files with private. Now it's time to see how to write toe a text file. The process is simple. There is the right method that writes any string toe on open file. This method does not it a new line character, so beg slash n toe the end off the stink. Lexie An example with open the name of the file. My files dot txt. This time the file must be opened in write mode for these w access mode he's used is if my file object Colon and the with block off coat f dot right and us think just a line and I'll execute the script in the same directory with my Python script. A new text file called my file dot txt appeared. This is the text file. They care that the file will be created if it doesn't exist and over written if it already exists. A very common mistake is toe overwrite a file by using the W mouth instead off opening the file in append amount, which means adding new content toe the end of the file. We'll see some examples with a pent in a short while anyway, if I right here, let's say ABC and by execute the script again, my file dot txt has been over eaten. So take here. Let's write a second line toe the fight just a second life and I'll execute the script. We notice that it didn't add a new line Sobek selection. We need to manually include the backslash end in the stink. We rightto the file if we want to new lines. So here I should have written beck slash. And now it looks OK, lex. See a new example. I'll show you how to preserve the existing content by a pending toe the end off the file if we don't want to overwrite the file. But to upend toe the end, we must use the A mode, which stands for a pendant. So instead, off w I use a lex execute the script and we see how it ended the new content at the end off the file. This is the new content that has been upended. There wasn't a big slash end after the second line. If I want a big slash end, so on your line I must edit manually and these are the lines that have been upended When using a pendulum out, Python creates the file if it doesn't exist or a pence toe the end off the file if it already exists. Also note that we don't have access to the cursor. It always adds to the end of the fight. If I write here my fight one, this file doesn't exist. It will create the fight. This is the new fight. It starts with a backslash and then Ed's ABC on your line, and this is drink exactly as we see here in the last example off this lecture, I'll show you how to open a file for both reading and writing. For these, we use the R plus access mode. Let's see an example with open the name of the file configuration dot txt and I am opening the file for both reading and writing. Our bless is if and now if dot right line a did with our plus Becks elation and I'll execute the script and this or price, we see how it ended the content at the beginning off the file, not at the end, if we want to. It content to a specific position. We can use the cursor, which is available when using the R plus moat. Lexi An example. If not seek Lexei. Five. I am moving the cursor on position Fife and then f dot right, like, say, 100 and we see how it throughout the value 100. Starting from position five. 12345 is, I said earlier. We can both read and write to a file when opening the file using our plus moat. If I got sick then and then print if not read. And I am reading three characters starting from position then and these are the characters a white Space W and I. You should also know that when using the AARP last smote, the file must already exist. Otherwise you'll get a nearer python doesn't create the fight. He fire right here. Configuration one that the X day off course This file doesn't exist. I'll get a never no such file or directory configuration One, not txt. Okay, this is basically how we write text files with python 64. Object Oriented Programming - The Basics: in this section will start diving in tow, object oriented programming or Opie. There are several different programming paradigms, and up until now, in this course, we've been using the procedural style off programming, which is a type off imperative programming. This style off programming involves providing the city's off instructions toe follow in pre defined order. This is mainly achieved using variables, functions and flow control. Object Oriented programming, or Opie is another programming paradigm and is based on classes, objects, attributes and methods. Python has been on object oriented language since its creation. It's infect a multi paradigm programming language, meaning it supports different programming approaches, including the procedural and the 00 P. Object oriented programming is an approach for modeling concrete real world. Things like cars or accounts as well is relations between things like companies and employees or students and teachers. Opie more delicious cereal world entities is softer objects, which have some data associated with them and can perform certain functions. It allows programmers to create their own guild awards data types. Let's see what all of these new terms I mean class is a kind off data pipe, just like a string integer or least when we create an object off that data type, we call it an instance off a Class A class is a factory for creating objects without knowing it. We've already used Opie and Vex because in python everything is an object. Everything is an instance off some class if we create an integer, a string or a function, in fact, we create objects or instances off those classes. X equals five. Here I've created a new instance or an object off type int. If I create a least, I've also created an object and X type is list. This is the same for functions. A function is nothing more than an object off class function. The data values which restore inside on object are called attributes, and the functions which are associate it with the object are called methods. We have already used the methods off, some built in objects like strings and risks. Here, if I write l dot upend. In fact, I am calling a method off. At least object all of these Artem ethics, or are three books now returning to our own defined classes. For instance, we could have a class called person and an object or instance could represent a person with attributes like name, age, address and with behaviors like walking, talking, running, which are all methods. I should also mention that in Python, Opie doesn't allow us to do anything we couldn't do without Opie. It doesn't unlock any functionality we didn't have before. Object oriented programming allows us to structure our coat in a human way off thinking with O. P. We create coat that is more usable, repeatable and organized. Okay, this was an introduction toe, Opie, and we'll see in the next lecture how to define our own classes and create our own objects with attributes and the methods. 65. Object Oriented Programming - Creating Classes and Objects: in this lecture will see how to define our own classes and create our own objects with Opry books and the methods at the beginning. It could seem overwhelming because it's not just about learning some syntax. It's really about a new way off thinking and writing coat. Let's dive into the syntax off object oriented programming. And don't worry about completely understanding everything I show you right now. Take it step by step and give it some time. If you get stuck, this new approach off programming could take longer for you to completely understand. In order to define a class, we use the class cured, then type in the class name. I'll create a class called Robot. Now they care that the convention is to use the Kimmel case notation for class names. This means each word in the name off the class will be capitalized. So if I have a class called my robot, the convention is to use my with a nup. Er case M and robot with an upper case are This is the Camel case convention. If there is a single work, we write it with an upper case. Leper. Let's name our class simply her, but then is for any other block off coat we've seen so far will type in a colon on the next throw. Using one level off, indentation will have the content off the class for the moment. I just want an empty class, so I'll write best. This is an empty instruction, but X mandatory. In order not to get a syntax error, a class like a function must contain at least one instruction. So this is my robot class from class. We construct instances or objects on instance is a specific object created from a particular class. We'll do that by typing the name off the object, the equal sign and then the name off the class and a pair off parentheses. Lexi Eireann equals her, but we can see how pie charm automatically completes the name off the class. Here are one is object off class Robot is with functions. On the first draft of the class definition, we can type in a documentation string or doc string in between people. Quotes toe provide a hint about the class functionality, so let's enter some text in between triple corks. This class implements robot toe, get the value off the dock string. We use the name off the object that in Double Underscore or Dunder Duke, print the name off the object that double underscore duck, and we can see how it displayed the class docks Drink. This is the basics off creating classes and objects and in the next lecture will see how toe ed characteristics or attributes to our robot class. 66. Object Oriented Programming - The Constructor: Now let's add functionality to our class. Lex. It some characteristics or are three books to our robot. There is a special method called in it also called the class constructor. The word in it has to underscores on either side, and this is called dunder method or a special method. Any time we create a new instance off a class python will automatically look for this method and call it. We don't explicitly call it X called automatically anyway. It's not mandatory for a class toe. Implement the unit method. It's optional. A few seconds ago, I said Method, What is a method? A method is a function defined inside the class. It will be called in conjunction with an object using the syntax object the name that method Here, for example, I've used the upend method off least object. I'll start defining the unit method D F. Double underscore. You need double underscore Big toe. Our eat method. The class constructor, the first parameter is the self cured. In fact, if we use an I d like by charm, it will automatically edit the self name basically connects this method toe the easterners off the class. The object that calls the method speaks of the instance off the object itself. Then we passed in any attributes or characteristics we want. Toe ed toe are objects in this example, ER, but has a name and a built here, Let's say simply year then in the function body, I say self dot name equals name. Self dot, ear equals year. This can be very confusing at first, but I'll try to clarify. Here I've created to art rebukes. They are name and year, and they are on the left hand side, off the equal. Sign the name and hear from the right hand side. Off the equal sign are the method parameters. So this is the object, our tribute and this is the method barometer. Now if I run the script, I'll get an error missing. Two required positional arguments, name and year. This is because when creating the object, I must specify X attributes. The first attributes is the name Lexei Marvin, and the second attribute is the ear. Let's say 2040. This robot is from the future and it's working. There is no error. Now let's take another look at the minute method by convention who use the same names for objects are three books and the methods arguments. But if I want, I can right here and is the method argument. And why is the second argument off course? I'll modify the following to lines. This is the same. Nothing changed, and it will work like a charm. The article books off the object, our name and ear and the method parameters are in and white anyway. As I said earlier by convention, we used the same names for objects are three books and methods arguments I just wanted to show you How are they connected and how method parameters are assigned to self dot. Attribute. I can tell you that this was also for me confusing when I first started to learn Opie. Now, if we want to access the object are three books we type in the name off the object dot in the name off the attribute her about name Colon and here I write are run the name off the object dot in the name off the attribute. We can see how Pie Charm Auto completes the Arctic rebukes names so name and it displayed robot, the name Marvin in terminally attributes are safe in a dictionary where the key is the name off the attribute in the corresponding value the value offer that our tribute to see this dictionary we use the attribute Dunder Addict off the Object Dictionary that stores the art . Critics are one that double underscore dicked, and this is the dictionary that stores the objects operate Bix the name off the activities , the key and X corresponding value. The value off that our tribute at the end Off this lecture, I want to give you one more detail about the self cured other programming languages based. These is a hidden parameter toe, the method defined, for example, in C plus plus or java that is the this keyword here in private, we must declare it explicitly and by convention, its name, his self. It must be the first parameter off the unit method or any other method we define on a class instance. Self isn't a riel python language cured? It's just a convention, and if you want, you can use these or any other name instead. Off self. There is no problem. If I use this is in C plus plus or java instead, off self. That is no error. We can also use any other name for self self being the object itself. If I right here, let's say d here t and here T Vicks also right? Anyway, I don't recommend you to do so. Just stick with the convention. 67. Object Oriented Programming - The Destructor: in the last lecture we've discussed about the unit method, which is the class constructor. If you are coming from other programming languages, maybe wonder if there is also a destructor in python. These tractors are a very important concept in C plus plus where they are going. Essential ingredient off writing really safe coat that involves allocation and the allocation off resource is in just a few works. A distracter is a special function that is called when the lifetime off an object ends. The purpose off the destructor is toe freed. The resource is that the object may have acquired during its lifetime. In python, these tractors are needed much less. Because Typhon is a garbage collector that handles memory management. We won't dive deeper into cases where an explicitly used off the destructor is needed. Just keep in mind that there is also in python a destructor. But in the vast majority off cases, we don't need to define it explicitly like we do for the constructor, which is the dunder innit method. Anyway, just for you to know it, The destructor is the Dunder del method, and if we define it, it will automatically be called when destroying the object and the allocating resources like a memory. So let's create a destructor. DF double underscore del double under school. This is the destructor. And here I simply write, print are but died and I'll execute the script. We can see how the robot has been created here by calling the meat method and at the end, off our script, the distracter has been called and it printed out robot died. 68. Object Oriented Programming - Attributes: in the last lecture. We've seen how to create instance attributes using the class constructor or the need to method another way to create Eastern SAT tributes is using common methods. Lexi An example. I am going to create another method called set energy. The first parameter is self. This is an eastern, it's toe the object itself, and the second perimeter is energy. Now, in the Method body, I am creating the attribute Self dot, Energy equals and RG. This new attribute, called Energy, will exist only after the method is called. If I paint the dictionary where all attributes are stored, we see that there is no our tribute called energy. My object has only to our tribe it. Now let's call the method are one that said energy off. Let's say 100. We can see how the new attribute appeared. We get the value off the instance. Attribute by writing the object name dot and the attribute neighbor are run dot energy. Another way to get the value off on our tribute is to use the building. Get 80 tr method. I'll comment out this line and instead off our one dot energy I write, Get 80 are off the name off the object and the name off the operative it so energy. And I've got the same result. If you try to get the very often attribute that doesn't exist, an exception is raised. Lex An example. Print are one dot producer off course. These are tribute doesn't exist, and I've gotten attributes. Error. Robot Object has no attribute producer To avoid this error, you can call the get 80 TR method by passing in a default. Value is 1/3 argument. So here I write, get a TT are off our one the name off the object. The second argument is the name off the Attribute producer, and the third argument is a default value that will be returned if the attributes doesn't exist. Robot without Producer. Now when I run the script, I'll get no air, and it returned to the string robot without producer. So this default value. All of these articles books we've seen so far are cold. Instance article books, and they are unique for each instance or object in python. There is also another type of far tribute called class attribute that is defined at the class level class attributes are at three books that are owned by the class itself. They will be shared by all the instances off the class. Therefore, they have the same value for every instance we define class attributes outside off all the methods. Usually they're placed at the top right below the class. Heather, if we want to access the attribute, we use the notation class name dot class, attribute, name in my example, I want to keep break off how many robots I've created. For that I'll create a class our tribute called Population. This is like a counter, so each time on your about is created. The population class attribute will be incremental by one here, outside, any method I am creating the class attribute population equals zero when creating an object so robot the UNITA method will be automatically cold. So inside the neat method I write robot dot population plus equals one. Here I am incriminating the population by one. Now let's create more robots. R two equals robot off Calvin and the Bill Thierry's 2030 Another robot R three equals robot off Guel and the Bill Thierry's 2020. Now if I want to see the robot population I right or about population coma or about dot population. Here I am accessing the class attribute and it printed out robot population three. This attribute is shared by all instances. We can also access the attribute using the notation object name dot class, our tribute name and it's the same value. But if I try to modify the class attribute using the notation object name dot class, attribute, name In fact, I am creating a new instance. Our tribute I am not modifying the class attributes likes an example Our one dog population plus equals blacks a 10. This is just an example. After running the script, we see how the class attribute has value three and the instance Our tribute has been modified and has the value 13. So here, in fact, I've created a copy off the class are to be built is an instance our tribute 69. Object Oriented Programming - Magic Methods: we'll continue our discussion about object oriented programming with magic methods. What are magic methods? Their special methods that you can define toe ed magic or twin reeks your classes. They are always surrounded by double unders course, and they are also called special or dunder methods. They're special or a magic because you don't have to invoke them directly. The invocation is realized behind the scene. There are a bunch off magic methods, and you can find them in the python. Documentation at the section Special Method Names. This is also a text toe this lecture. All these are magic methods, one off. The biggest advantages off using Python's magic methods is that they provide a simple way to make objects behave like building types. Let's take an example. I do four plus five, and it returned nine four between single quotes plus five between single quotes. It returned 45 off course is a string because it's enclosed by single quotes. How did it work? How did private know what to do here? The plus or the addition operator is a shorthand for a magic method called Dunder Head that gets automatically called on the first opened so If the left operandi is an instance off the in class Dunder head does, um, automatically addition, if excess drink it does string come combination? In fact, there is a magic method for each operator like addition, substructure in multiplication, raising to a power and so on. We can define these Mexican methods in our classes. Toby Ableto print at substructure or multiply objects. I've just done a small modification to our her about class, defined in a previous lecture. Here I have two instance artery Buicks. Name and price. Now, if I try toe ed or compare to robots, I get on air. Maybe for robots. It doesn't make sense toe at objects off type robot. But for other classes like, say, a bank account, it makes sense to it to accounts in order to get the summer off their Bella sees or to compare to a conchs. So do these. You have to implement the corresponding Mexico methods first, let's try toe ed. Two roadblocks are run, plus are, too, and I've got the type air UN supported, opened type, for addition. In fact, it says that I didn't define a magic method for this operator. If I try to compare toe RR box I'll get the same ever. Now let's try to display information about a robot so print are one, and it printed out this message. This isn't a human readable message. Now let's go back to coding and see two examples. In the first example, I am goingto overload the Bamber str method that is automatically cold when trying toe print on object and the Dunder ed method that is automatically cold when using the plus or the addition operator on objects inside the class. I'll define a new function called double underscore str double underscore. This is the bander, or the magic method that is automatically cold when printing on object as usually Self is the first parameter. Now I'm going to create a temporary string variable. My underline str equals my name is plus self dot name plus and my prices plus str off self dot price. Here I am converting on integer or afloat toe a stink. That's because the price will be an integer or afloat, but I want to concoct innate the price toe This stink. If I don't convert it to a string, it will return on error and I am returning my str variable Now when printing the object, it will automatically call this function next right and we can see the difference. It pointed out. My name is Marvin and my price is 150. Now let's define the second magic method. The F double underscore ed double underscore. The first parameter is self in the second parameter. He's other self is the object that calls the method the open from the left side, off the equal sign and other the open from the right side. What does it mean by adding two robots in this example? By aiding tourer box, I want to get the some off their prices so I create a variable price equals self, not price. This is the operas from the left hand side off the equal sign, plus the opened from the right hand side off the equal sign other the price and I'll return the price. Now when trying toe ed toe or a box, I won't get a never it will automatically call this method and will return the some off their prices. So print are run plus are too, and it printed out 250 the some offer their prices. I've just implemented toe magic methods, but there are a lot off magic methods. And if you want to implement that operation on your objects, you have tow implement the corresponding Mexico method. That's all, for the moment, about magic methods. Thank you.