Python For Beginners | Sara Al-Arab | Skillshare
Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
29 Lessons (2h 58m)
    • 1. Introduction

      1:27
    • 2. Installing Python

      5:18
    • 3. Why Python?

      2:04
    • 4. Values & Types

      6:00
    • 5. Variables

      4:53
    • 6. Variable Names

      4:35
    • 7. Statements

      3:22
    • 8. Operators & Operands

      8:37
    • 9. Expressions

      8:52
    • 10. String Conversion

      10:33
    • 11. User Input

      5:28
    • 12. Comments

      1:49
    • 13. Comparison & Logical Operators

      9:19
    • 14. If Statement

      9:23
    • 15. Application on If Statement

      7:49
    • 16. Try & Except

      5:38
    • 17. Logical Expressions

      2:55
    • 18. Functions

      11:28
    • 19. Math Functions

      5:56
    • 20. Application on Math functions

      11:19
    • 21. Random Functions

      6:20
    • 22. Create Your Own Function

      8:34
    • 23. Fruitful & Void Functions

      4:19
    • 24. While Loop

      5:49
    • 25. For Loop

      6:21
    • 26. Some Loop Algorithms

      6:35
    • 27. Continue, Break, & Pass statements

      5:23
    • 28. Word Reverse

      5:30
    • 29. Project

      2:11
  • --
  • 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.

72

Students

--

Projects

About This Class

Welcome! Are you new to programming? If not then I presume you will be looking for information about why and how to get started with Python as it's easy for beginners to use and learn. Python is a powerful general-purpose programming language. It is used in web development, data science, creating software prototypes, and so on. Fortunately for beginners, Python has simple easy-to-use syntax. This makes Python an excellent language to learn to program for beginners. So jump in!

Meet Your Teacher

Teacher Profile Image

Sara Al-Arab

Computer Engineer

Teacher

Hello, I'm Sara. I'm a computer engineering student at LAU. I'm currently working on web develpment and programming and seek to share my information with as many students as possible!

See full profile

Class Ratings

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

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

Your creative journey starts here.

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

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

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

phone

Transcripts

1. Introduction: Hello everyone, welcome to Python for beginners, My name is satellite up, and in this course you'll be learning the basics of Python. Python is a high level programming language used for general purpose software engineering. It's a server-side language, which means it runs on the server and is responsible for processing the logic behind user inputs, interacting with databases and other servers. Initially developed in the late 19 eighties by Guido van Rossum. Button has been around for decades alongside other server-side languages like Java and C. Been awesome, mottled pattern after the English language, eliminating unnecessary syntax to make it easier to read and write the other programming languages. But it is an open source language like Java. And in recent years it has increased in popularity during, due to its use in data science fight and also has a, has a strong community around bison, sorry, around machine learning, data modeling, data analysis, and artificial intelligence with extensive resources and libraries look for these purposes. And yes, the rumors are true. By then has been named after the British comedy group Monty Python. Although it's a high-level language and can do complex stacks that sorry, tasks by them is easy to learn and have clean syntax. Therefore, it's recommendable for both beginners and experienced programmers to start with. So jumping. And I hope you really enjoyed this course. 2. Installing Python: So before we start, let's first download Python and data or by the environment. And so first, let's go ahead and install Python and go to Patreon.org and click on Python does is downloads the latest version of Python on your PCs. And once you click on it, you should see that it's starting to download. And once it's downloaded, just click on it and add it to the power and then install it. And so now we wait until it gets installed. Then settable successful should show up when it's done, and then just go. And now, just to make sure that we've downloaded, let's go to the command prompt and type python. You should receive by latest version that you've downloaded. And now we've entered the Python interpreter that say I print something like print hello. And it should print for me hello. And you can see that our Python interpreter is working. However, for this course, we will not be writing our code inside this command prompt. We'll be using a text editor. And one of the text editors that I recommend is Visual Studio Code. And so we'll be using Visual Studio Code and our course. And if you, if you don't have it, you can download it. It's so easy to download. You can just go and type on google, download Visual Studio Code. Go to Visual Studio Code.com. And you can download on the version that you aren't either for Windows or for math. And now let's go ahead. I've already downloaded which also to go. So let's go ahead and open a current. So this is Visual Studio Code. And so first let's go ahead and set up our workspace pencil. In order to do so, we're going to save workspace. To save our workspace. And let's go ahead and create a file. And let's call it Python. And inside of that file we're going to save that workspace. Let's call it also fighter. So now, as we do that, we've saved a workspace inside this newly created folder. You can see it right here. So this is the workspace we'll be working on. And now let's add a folder inside of this workspace. So let's just start a folder. Let's call it Sample. Let's add a folder. Sorry. Let's add a folder. And you can see that now we have that folder that we created inside of our workspace. And now let's just add a Python folder. Exercise one dot py. So Python files are formatted by at applied extension. And let's go ahead and try to put print. Hello. And the moment you start typing, you should do. This. Green button should appear. And so let's go ahead and play, runs her own code. And you should see this code should work fine. And so you can see that we've printed hello. So this is the same as going to the command prompt. However, using Visual Studio code will just write a bunch of code. And then when we finish our code would just click on that rung. And Visual Studio code would run this on the command prompt for us. And so this is where we will be working in this course. If you are having trouble setting up your Python environment, you could use an alternative if you are a beginner. But later on you'll need to setup this Visual Studio Code text editor. But however, temporarily you can use an online Python compiler. So you type into Google Python online compiler and you'll have to program IS.com. And similar to that, which procedure code, you will just enter a bunch of code and just click one. And whatever you run should appear in that shell. So now let's go ahead and start with our course. 3. Why Python?: At the heart of creating software is being able to use the right tool for the right job. In this course, you learned about Python programming language. The most recent major of version of Python, sparse and three, which we shall be using in this course. However, person too, although not being updated with any other than security of this, is still quite popular. And so we ask ourselves this question, why do we use Python? Well, for starters, poison has many different things we can use for. It can be used for general web development and building web applications. Well, Python is one of the simplest programming languages. And I mean that in a good way because Python has pre-built libraries and web frames including Pyramid, Django, and Flask, which you might have heard of, is especially great for using an on back end web development projects. Shortening the amount of time you spend on projects by allowing you to repurpose chunks of code. Moreover, button could be used for startups. This seems like an odd item to include on the list, but it's true. Startups and especially tech start-ups, love Python because it's easy to use and scalable. And I mean scalable, like take for example, Dropbox. Moreover, there are many different things Python can be used for in mathematics, in system scriptings and software developments. And so the list goes on and on. And so for this course, Python will be written in a text editor. It is possible to write Python in an integrated development environment, such as Virtual Studio code, which we'll be using in this course, and which is particularly, particularly useful when managing larger collections of Python points. Ten. So now I would like to welcome you to this course and I hope you enjoy it. 4. Values & Types: Now let's go and head to our first lecture, values and tabs. And so the value is one of the basic things of program works with. It's like letter or a number. So a number could be an integer. It could be what we call as a floating number of loading number is anything that has a decimal point. And it could be a string of words or characters. So if we attempt to printed c, this value, when fully to the Python interpreter would return for us 142. If we try and bring 22.4 at will, I'm rooting for us this value. So these are values. This string is a value, and if we attempt to print it there by the interpreter will return it for us. And so if we want to know what types of these values are, we have several types. So we have strings and they belong to the teletype STR strings, such as this example right here. Hello world. We have integers that belong to the end because they are integers, though they have typed in. And as we said, numbers with a decimal point belongs to that type called float. A number that has a decimal point. Even 1.00 shoulders know something after the decimal point, but it has that 1.00, it is considered as a floating point. And you know, these, these types of these values using the interpreter. So you just calling that function called type. And you enter whatever value you want inside that type function. And it will return for you on the type of that value. So as we said, this value, which is a string of words, hat is of class SCR. This number which has no decimal point is of class integer end. And 3.2. Offloading void because it has a decimal is of class looks. And when you type, let's say you sometimes want to type a large integer. And you might be tempted to use commas between groups of three digits, such as one million. One million has two corners. And this is not a legal way to write that integer. However, it's not an error by the, doesn't give you an error. It reads that code, however, it doesn't give you the output that you desire. So it would read that one as a integer, one, come on, 000, which is 0 basically, and then comma 0, use u, which is, so this is not what I expected at all. Python interprets until print, sorry, 1 million as a comma-separated sequence, as we said, print spaces between 2.5 spaces here. And this is the first example we have seen on semantic error. The code runs without producing an error message, but it doesn't do the right thing. And now let's go ahead to use pseudocode and try to visualize these values. And let's go ahead and open a new file. So this is a far I already created. And let's go ahead and create a new file. And inside that file, let's call it values dot pi. And so we can either write our code here or just conduct erotic through the terminal. So this is our terminal. And let's just try using first writing the code and our Python file. Let's say I want to print five. And let's go ahead and run that. And now as you can see, virtual pseudocode ran our Python code, the terminal. And you can see that it printed five. And so let's say I want to know what is the type of, let's say 19.2. And an order to visualize the results, we have to include the print. In order to print for us what is the type? We'll go ahead and save it and run it. And so now when you run it, will able to C5, which you print it first and then the type of 19.2, which is a fluke of class, or another way to do that is through writing directly to the Python interpreter. And so inside our terminal, write Python in order to access the particle interpreter. And so right now I'll just draw it in type and let say, I want to write a petition. Hello. And as you can see, Director gave me the type of hello. Let's say I want to print hello. World. Same. Print for me, lower. Let's try to do a semantic error just to visualize how it would go. And say 12, right? And so if we print that, you can see that the US 1-0-0 AND not that number. And as we said, this is a semantic error. And so that's how we can visualize and bright values in Python. 5. Variables: And now we move on to variables. One of the most powerful features of a programming language is the ability to manipulate variables. A variable is a name that refers to a value. Programmers generally choose names for the variables that are meaningful and document what the variable is used for. An assignment statement creates new variables and gives them battles. So as you can see here, the variable that we have is called message. We named any valuable for any name and give it, gave it a value of a string, hello world, string that contains HelloWorld. And this is another variable we can see is an integer because it's held a towards a value that has a type integer. And the same for the variable pi, which holds, as you can see, a closing number. Because as we said, holding number has a decimal point. And we can see here that pi has a decimal point. And display the value of a variable. We can just use a print statement. And as you can see, we have a variable n and we want to print the value of n. And so to do so we just write into the parcel interpreter, print the variable n, and it will Antoine print for us? No, it would print for us the value that an hold, which is 20, and the same for pi, it would print for us not pi, but the value that this valuable holds. And the type of a valuable is that type of the value it refers to. So you want to know the type of a variable. We will just say we want to know the type of the value which holds because the variable doesn't have a type. The type of a variable is what is the type of the value that it holds. And so the type of the message that we saw earlier here, which is a string, class string. And the same for m is an integer. And seem for pi, it's a float. And so let's go to Visual Studio Code and try to create our own variables. And let's go ahead and create a new file. Let's call it pi. And now let's create our first valuable that, that would be an integer. And let's say, I want my guy up to 45, this is x. Let's say I want it to be a floating number, c, 32.6. Let's have a third variable, onset and let it be a string. It doesn't matter if it's single or double quotations. It either way. So let's say, I want to write a book. And so now let's go ahead and try to print these variables. Let's say I want to print x and then print y and z. Sorry. Let's go ahead and run this code. And now you can see that we have x which is 45, Y, which is 32.6, and that which is a string. Welcome. And let's say I want to know the type of C. So go ahead. I wanna print the type of set. So go ahead and save it. And let's run. And now you can see that the third print is the type of this variable z, which is an STR, which stands for string. And as we said, we can also use the Python interpreter to write the same code. However, when our codes become more complex, it would be tedious to write everything in a Python interpreter would be more beneficial to ride our coast and files and just run the program. And so let's try and write x equals to three f. Let's change x. We have already X, say S equals to three. And now I want to print. And as you can see, we are given three. And so that is how we use variables in Python. 6. Variable Names: We previously mentioned that programmers generally choose names for the variables that are meaningful and document what the variable is useful. However, we should be careful while giving the variables names because there are certain restrictions that we must be. And so first of all, variables can contain both letters or numbers. However, they cannot start with a number. They can start with a letter, but numbers can be in the middle or at the end. It is legal to use uppercase letters. While giving a variable a name. However, it is a better idea to start to get used to begin variable names with a lowercase letter. Moreover, the underscore character is often used when we have names that have multiple words, such as hello world. Since the space character is an illegal character while giving a variable name, we could use the underscore character. Instead of that whitespace character. There are several other illegal characters that we can't use while giving a variable a name that we'll see throughout this course. And if you give a variable and illegal name, you'll get a syntax error. So let's see some of the examples that would give us a syntax error. The first example. And you can see that because we started our name with a number, it gave us a syntax error, invalid syntax. And the same for the second one, because we gave it an illegal character, which is the add. And this is one of the new illegal characters we've introduced. It gave us a syntax error, invalid syntax. And now you might ask yourself, why did the last example gave us a syntax error? Everything seems to be fine. We didn't start it with a number, which is the name with a number, we didn't have an illegal character. All of the characters are of a radical. So what's wrong? And so it turns out that class, the name class, is one of Python's keywords. The interpreter uses keywords to recognize the structure of the program, and they cannot be used as variable leaves from. So Python preserves 35 keyword. And you might want to keep this list heavy. If the interpreter complains about one of your variable names and you don't know why, see if it's on that list. So now that's open, which was close to you. And let's try to experiment with these variable names. Let's open a file. The file will call it names dot py. And so let's try to give an illegal name. Let's say 98 message, hello, string that says hello. And so let's try to print the message. And now as you can see, we have a syntax error here. Because we started with, and let's try to give it an illegal character, the add for example. And let's try to print it. And let's say the first k. And now as you can see, we have a syntax error because we added an illegal character. And now let's try to give it a reserved keyword name. Let's say all of the reserved words are if, other than class that we saw in the example. So if we try now, we can see that it also gave us a syntax error because there is one other reserved groups. So be careful while giving your Bibles name. And consider that there are reserved words and that you should start with always with alphabetical error. Sorry, letters. And don't add any illegal characters. 7. Statements: Now we move onto statements. Statement, because a lot of code that the Python interpreter can execute, we have seen two kinds of statements that print being an expression statement and the assignment statement. When you type a statement and interactive mode, the interpreter executes it and displays the result. If there is one, script usually contains a sequence of statements. There is more than one statement. The results appear one at a time as the statements execute. So for example, here we have this line of code. And this is a script that has three statements, where two of them are print statements and one is an assignment statement, and the produced output is one and then two because we have two prints. However, the assignment statement does not produce any output, is just an assignment statement. And so as we mentioned, assignment statements. And assignment statement consists of an expression on the right hand side and a variable to store the result of that expression when it's executed. And you can change the contents of this variable and a later statement. So let's say we have x equals 9.3 and y equals to ten. And then a literal and the code, we decided to change the value of x. And so we wanted to put x equals to 2.2. And so the old value of x changes and the 2.2. now there's no value that is 9.3, that value is gone. We only have 2.2. And so the new value of x variable is 2.2. And so now let's go to Visual code and visualize back. So let's go ahead and create a new file. Let's call it T, that pi. And let's create a new variable and that's called x, which has a value of 2.2. Let's go ahead and print x. And now let's go ahead and see our results. Now, as you can see, we have 2.2. Now, let's go ahead and change the value of X into, let's say 3.2. And now let's go ahead and print x again. And let's see. Now, as you can see, x was 2.2. But then we change the value of x to 2.2 and so on. We printed it. They did not print 2.2. But it printed 3.2. And so this is one of the few statements that we saw important and we'll see a lot more in throughout the course. 8. Operators & Operands: Moving on to operators and operands. Operators are special symbols that represent computations like addition and multiplication. The values the operator is applied to are called the operand. The operators, as seen here in the table. Plus has the oppression to add, to add. So it performs addition. The dash performs subtraction. Start, performs multiplication, slash performance. Division. Two stars perform experimentation. So it say two powers two gives us four. So we just add two here and two here. And we get two to the power two, which is four. And finally, the percentage, which is the remainder, or which some call the modulus. And so let's, for example, this is an example here. We have a variable called minutes, which is 59. And if we try to divide the minute by over 60, we'd would get a decimal number. However, there has been a change in the division operator between Python two and Python three. And Python three. The result of this division is a floating point, as we can see here. However, the division operator in Python 2 would divide two integers and truncate the result into an integer, as you can see here. So it would ignore the decimals and just give us the integer part which is 0. And we can obtain the same answer in Python 3 using the floor division. And so what is the third division, the upper division rounds or number two, the small to the closest, smallest integer. So let's say you have this example right here where we want to divide minutes over 60 and we have this decimal. However, we just, we only wanted to give us the integer. And so to do so, we apply that for division. And it only gives us 0 the integer value. And finally, using the remainder or which we also called the modulus operator. The modulus operator works on integers and the remainder when the first operand is divided by the second. In Python, the modulus operator is a percentage sign. And so let's say I want to find the remainder of dividing 18 by three to do so with just on the add 18 modulus three. And if you want to find, sorry, hit the quotient. We use the floor division because if it is a multiple, which would give us the right answer. However, if Santa multiple and to give us the right answer with some, sorry, not the right answer than the integer part with some decimal. So we just want the decimal integer part. And so modulus operator is used for many mathematical problems that we see throughout the course and later when you start programming. Seriously. And so now let's try to experiment with these operators. Let's go ahead and create a file. Let's call it Operation dot py. And let's go ahead and create a variable. Let's call it hours. And let's give hours of value of six. And let's create another Bible, let's call it. And let's put the rate at two. And let's say I want to find the payments. And so the payments is the hours multiplied by the rate. So to do so, create another bad with variable payment, which is ours, multiplied by the rate. So let's go ahead and print that value payment. And let's see what that gives us. And now, as you can see, it gave us 12. So six multiplied by two. Well, let's try finding the modulus on dividing an integer by a value. Let's say I want to find which is say, example 16. And now let's go ahead and print that value. Cause if we don't print out, nothing would happen. Because this is an assignment statement, it's not a print statement. Assign statements don't show us anything. They are only found inside the Python code. And so let's go ahead and print. And now, can you give us one? So the remainder of dividing 16 by three is one. So that indicates something for us, which is that three is not a multiple of 16. And now let's go ahead and try to use the third division. So first of all, let's try to use the division. First of all. So let's say equals two, let's say 60 and divide by five. And let's go ahead and print. And let's go ahead and realize our code. And now as you can see, it's 3.2. Let's say I want to just say S3 and not 3.2. And so to do so, we use the floor division. So now let's go ahead and rerun our code. Now. We have to see only the three, not 3.2. So we have the 1213 and not 3.2. Now let's go ahead and try our last operator. Let's use the power before the power just, let's just give this psia Look on addition and subtraction. So the x equals five, y equals six, and x plus five multiplied by y k. And we'll see more. Tourists look at parantheses In the next lecture. But just for now, see what does that give us? And so let's run that. And now as you can see, it gave us 60. And that was because x plus five and times six is six. And now moving on to our, to our last operator, homage to the power, let's say, let's print, let's say two to the power four. So to do so, two to the power four can solve. We can directly write our statements inside the print. We don't have to store them in variables. And so let's go ahead and print. Now, as you can see, two to the power four is indeed 16. And so these are the arithmetic operators. We'll see more operators throughout the course. There are also as interesting as this. 9. Expressions: And now now we reach expressions. And expression is a combination of values, variables, and operators. A value all by itself is considered as an expression and so is a variable. So are all the following examples here are all the expressions. So we have 12, which is a value, this is an expression, X is an expression x plus 12 and expression, this operation right here is an expression. And operations have an order of precedence. So each operation has its own precedent over the, over the other. And so the highest precedence rule to the lowest president rule goes as the as shown here, the parentheses are always respected. So the prices are the main, has had the highest president. And after that you have the exponentiation as tourists to Opower. And after that, we seek their multiplication, division and remainder. And after that, we see the addition and subtraction. And finally, we move on to the, from the left to the right. So this is from the highest precedence to the lowest precedence. And now let's go and see some examples on this on Visual Studio. So let's go ahead and create a file. Let's call it the pi. Let's create a variable. Let's create another variable. Let's give it x plus ten times five plus one, all that inside a variable. And that's multiplied by five divided by 25 plus one. And so right now, let's go ahead and add a print statement. So let's go ahead and visualize what just happened. What is the result of our code. And so you can see that it's 606 and we have a floating number because we use a division somewhere in our code. So once we use the division, our value is converted into float. And so it became ten plus 1020 times 500 plus 101. And times six gives us 606. And now let's try to move that parenthesis and see if something in our code changes. So let's go ahead and run our code. And as you can see, indeed something did change. And why is that? That's because remove the parentheses. So before when we had the parenthesis, we had precedence over the multiplication. However, now that we remove the parentheses, the, sorry, the addition. So when we had a parenthesis, we had the precedence of the addition of x plus ten. But now that we remove the parenthesis, we have the President's Algorithm multiplication. So ten times five would operate first and that added to X. And so you can see that the parenthesis to have a really high effect on how operators might go. Now let's go ahead and try another example. So let's try to print two to the power one. So the power has precedence over the addition. So we have two to the power 1 first, then we add two on, we don't add one plus one, and that change the power. So it's two to the power one plus one, not two to the power two. And so the answer would be three and not four. So as you can see, the answer is three. Let's try another example. And let's try to print three times one. And so as we said, first we have power and then the multiplication. So one to the power three is one. And then three times one is three. Let's just change that for mountain not to get confused with that three here. If it were to be the, the multiplication first, we'd have four times one is four to the power three, which should give us huge number. However, because we have that experimentation with a higher precedence, we'd have one to the power three times four gives us four. And see if it actually gives us 4x. And yes. Let's try another example. Let's try two times three minus one. So of course we don't subtract first when we multiply two times three is six months 15. So you could talk to me, subtraction first, which would be two times 24 should be wrong. So let's go ahead and answer would be here. And so the answers file. Let's go ahead and try another example. Let's try six minus three x1. And so since we have the precedence from, sorry, from left to right. So we don't first subtract three minus one and then subtract six months, 36 months to sorry, no, we subtract from left to right, six minus three and then three minus one. And so we have to obtain here too, since the quantities P and Q must wants to and not the other way around, NO3 minus 1261244 is a wrong answer. Let's go ahead and try to see. And as you can see, indeed it is two. And if you remember, we talked earlier about the modulus operator and how it is important for us in, when we start to write some complex code. And so one of the advantages of using the modulus is to be able to extract the right most digit. So the right-most digit is on that side. And so let's say I have a variable which is 265, and I want to extract the right-most digit, the five. So to do so, let's see monastery. And if you think about it, if we divide any number by ten, the decimal point would move backwards one step. And so we'd obtain the remainder, which is that number five. And let's try to visualize. And as you can see, we got five. The remainder is five because the remainder of dividing 265 by ten is that number that moved by the decimal point, which is let's say L12. Obtain the last, sorry, the first 2, first book, most digits. So new, so we, and so 100, because 100 rolls it to point to the left. And so you can see that we have 65. And so these are some examples on how operators have precedence over other operators. In Python. 10. String Conversion: And now we move on to string operations and type conversions. So to start with string operations, the plus operation works with strings, but it's not an addition in the mathematical sense, as we saw earlier. Instead, it performs concatenation. And what competent concatenation means as joining the strings by linking them. And so, for example, let's take this code right over here. We have first 20 seconds is equals to 25 and both are integers. So when we add those variables, we're adding those integers. And so the answer would be 45. However, let's say first ten. However, ten is entitled string. And second is 161 succeeds inside of a string. And we want to add those two variables. We're not adding integers. Here, we're adding strings. So it doesn't matter what is inside that string. It can be a number and a character, word, anything. It doesn't matter. What matters is, when we add those two variables, we're just adding the ends of these two strings. And so we obtain ten, which is inside of the first string, and then one which is inside that next strip. Now, let's say I want to add those number alone one to obtain that number, o to obtain 170, which is ten plus 116. What do we do then? How would we fix that? And we'll see that in a minute. But before that, we have also the multiplication operator, which also works with strings by multiplying the contents of a string by an integer. So if we have a variable called first, which is hello, and another variable which is second, and which has a value for which is an integer, and that's a string. And so if we multiply that integer by that string, we repeat that string four times because the value of that integer export. But we can multiply two strings together, that would give us an error. And so now, going back to what you said, you wanted to add that 162, that term and not obtain that value. And here comes the convergence. And so we have an integer conversion and a string conversions, which we'll get to in a minute. And so the integer conversions allows us to convert integers and between Int and float. And of course there are other types that we can convert to. But for now, we are talking about integers. So an integers we have built in functions which are the int and a float. And so let's say I want to obtain a float number. I don't, it doesn't matter if it has a decimal point or not. I just wanted to have that decimal point. Sorry. It doesn't matter if it has an ID number after the decimal point. I just wanted to have that as oboe and sort of knew. So I convert one of my variables into a float, because as we said, once we have a float which we are adding two another number, the answer would. Be converted implicitly to afloat. And so 22, Whitman converted to offload. It's converted to 22 and only added the answer would be also afloat, just running 120, 2.0. And if you see the type of that, let's say x is an integer i we saw earlier in this class. And if it is of class float. And now we move to the, sorry, string conversions. And so we were, we were talking earlier. Now, what if we wanted to add two numbers, but there are inside of strings. Could we do that? So we could do that by using the int and a float functions to convert these strings into integers. So we have that extra squeeze, 1-2-3, But it's inside of a string. And we have, let's say, I want to add one plus one or x plus one. And so do so we convert that into an integer. So we have a new variable y and which is the converted value of x. And so 1-2-3 is now no longer of type string is of type integer. And when we add it to any other variable, which is an integer value for float, it will give us a valid answer. However, you will get an error if the string does not continue characters. So for example, if we have x equals to one to three, which is all numeric. But we don't convert it. That would give us an error. A type error can convert int object to STR implicitly. And another error would be trying to convert a string which has no numeric characters into an integer. That would also give us a value error. And so that's why we should be careful while converting. And so one more thing, errors in Python starts with a traceback. And what is a traceback? Traceback gives us the dream. Apply that. And so now let's go to Visual Studio Code to try an experiment with these conversions and these strings. So let's go ahead and create a new file. Let's call it cooperation 2.5a. And let's try first to concatenate two strings. So S1 equals to hello and has two inputs, two. And now Let's try to concatenate these two, S1, S2. And let's try to print the value. Let's print S3. And now as you can see, it has three Hello World concatenate. So let's try to give it some space to do so, just add some space here and it can be here or at the beginning of S2, doesn't matter. And so now we have space between them, which is best. Now, let's try to concatenate. I don't know, two integers, 123. And plus five to 4-5-6. 4-5, six. And now let's try to see the result of that. And let's print. As you can see, it's concatenated 12345. Now let's say I want to add those as integers and not as a string. So to do so as four. And that's fine. And let's go ahead and print that value. And unless you can see, we obtained the addition of 1-2-3, 4-5-6. Now, let's try to add, let's try to add S3, which is a string with nonnumeric characters, and S4, S3, and S4. And let's see what does that give? And so we can see that it gave us because both are strings. It gave us helloworld, 1-2-3, didn't give us any error, which is what we totally expected. But let's say I add so much is S3, which is a string. But plus let's say four, 5-6. What does, now, what does that mean? So you can see that there's a traceback error at line 19, which is a type error, can only concatenates strings, not it, two strings. So we cannot concatenate a string with an integer, can only concatenate a string. So that is an adverb. And finally, Now, let's just try the multiplication. Ds9. Let's say four times. And let's paren paren night. What does that give us? You can see that hello is printed four times because we multiplied by four. And so these are some of the string operations and some of the type conversions that we saw using the end to add integers that were initially strengths. 11. User Input: As we reached this stage, our Python code is because we would become a little bit more interactive. So sometimes you would like to take the value for a variable from the user via their keyboard. Python provides a built-in function that is called a book that gets input from the keyboard. When this function is called, the program stops and waits for the user to type something. When the user presses return or enter, the program resumes and input returns what the user type is a string. And before getting input from the user, it is a good idea to print a prompt telling the user what input. You can pass a string, the input to be displayed to the user before pausing for. So let's say I want to get the name of the user. I prompt my input with this string. What is your name? And add this slash n, which represents a new line, which is a special character that causes alignment. So we don't see that. We only see what is your name and wherever we write, the user rights would be written on a new line. And so when we get that name, you'd like to print that name. And so we can write on hello comma, the variable. So when we do that, print automatically concatenate these two. And so we don't, and you don't have to have a space here. Print will do that for you. And so as you can see, the input string that we provided first is who are you? And wherever we write this on a new line, because we added that special character that is invisible, that is only read through the code and the darkly understands that we have to go to a new line. And after that, we print Hello space. And the name which the user provided. If you expect the user to type an integer, you can try to convert the returned value to choosing the int function we previously talked about. So let's say I want to get the age of the user. So as we said, the input gets us the value in string value. So an order to, let's say I want to do some arithmetic operation with that variable. So first I have to convert it to an integer or a float. But if the user types something other than a string of digits, we might get an error that we saw earlier. Value because we're trying to add a string to an integer, which is wrong. So let's go and try to experiment with these and Visual Studio code. So let's go ahead and create a new file and call it input dot pi. And let's try. Let's try. First, prompted with a string that says I want to get What is your favorite color. And so let's just add a sludge line. Sorry, that slashed the others. And it must be inside the string, not outside. So and after their user prints, that prints enters their favorite color, I want to print that favored color. So in color and comma. But first we have to say that and put the knowledge user. So we save it in a variable. Let's call it color. And we ended here. And so let's try. And now as you can see, we have that prompt. What is your favorite color? And we can type here. So let's say my favorite color, blue. So I enter presenter. So as you can see, as I hit enter, the program is you look at the code and we have a print statement, and it printed color, blue and set blurrier. And so inputs are very important to interact with the user. And we'll be using them. You'll be using them many times when developing web applications or any other thing for that matter. 12. Comments: As programs get bigger and more complicated, they get more difficult to read. Formal languages are dense and it is often difficult to look at a piece of code and figure out what it is doing or why. For that reason, it is good idea to add notes, your programs, to explain in natural language what the program is doing. These nodes are called comments and employ, sorry, in Python, the start with the hashtags. So after anything, after that, hashtag symbol is ignored by Python. And so let's say we have this kind of code right here. And we want to find the percentage which is, which has this operation. And let's say someone just entered undo so that code and understand what is happening. Why are we multiplying by a 100 and dividing by 16? So one way to explain to the user why this is happening is to add a comment. And so we add a comment which could be above or beside that code doesn't matter because everything after that hashtag is ignored and write a comment explaining what this code might be doing. And so here we are computing the percentage of the hour that has elapsed. And just one more comment. And don't add that hashtag here. Because if you add that hashtag here, you'd lose that line of code. It would be considered as a comment. And so comments are really beneficial to understand the code that you're seeing in front of you. 13. Comparison & Logical Operators: So we've seen previously arithmetic operators and now we'll get introduced to a comparison and logical operator. But first, let's go ahead and see what are first the Boolean expressions. So a Boolean expression, an expression that is either true or false. The following example uses the equal operator, which compares two operands and produces true if they are equal and folds otherwise. So here is a Boolean expression. Five equal, equal five. And because five is equals to buy, it gave us true. However, if we said something like five equal equals six, the statement is not true. So this gives us false. And true. And false are special values that belong to the class bool. So they are not strings. So if you type True and the Python interpreter, it will give you the class bool, which stands for Boolean, and the same for false. So true and false are of type Boolean. And so now we go to the comparison operators. We have several comparison operators. We have the equal, equal, we have the not equal to. So if you have x and y, we can give a comparison operator that acts as the print on y or a is different than mu. We have less than or less than or equal, greater, or greater or equal. And although these operations are probably familiar to you, the Python symbols are different from the mathematical symbols for the same operations. A common error is to use a single-site equal instead of the double sign. Remember that equal is an assignment operator. An equal, equal is a comparison operator. So here in that example, we have a equals ten and b equals two. Funny, if we say that a is equal to b, that would give us a false because a is not equal to meet. However, if we have a statement that says a is different than b, then that would give us true because it is indeed different. And so all these operators only return Boolean values as either true or false. So now we go to the logical operators, and the same thing for the logical operators. They only return true or false. So we have three logical operators, either not or. So. Let's say we have x equals to five and we enter a statement not x less than ten. So let's first examine that statement, x less than ten, is that true? So if x is less than ten, that would return for us true because indeed x is less than ten. So not true is the opposite of true, so-called so regard forms. Since that is true and re-entered and not logical operator. So it would negate that value. So true would become faults and folds, let's say. We added not greater than, greater than ten. So x greater than ten is false and not false would be true. Now, moving on to the next example, we have x less than, sorry, greater than 0 and x less than ten. So we have one operation here and one here. Comparison operators, sorry. So that statement is true because indeed x is greater than 0. And this over here is also true because X is less than that. So true and true would give us true and order. Get through. Both statements must be true. Let us say that, but for was false. So true and false, which would give us the same as that what folds And was that was true, false and true would also give us for, let's say both are false, false and false would also give us true, sorry, false. Now, in the last example, right here we have x less than ten, sorry, x less than 0, or x greater than ten. So let's say we have x greater than ten. This is false. However, also, x less than 0 is posts. So false or false would always give us folds. Let's say we have here x greater than ten, greater than 0. So true or false will give us true. So let us say both are true. That would also give us true. So in all, you only need to at least one condition to be true. However an and you need all the conditions to be satisfied, they have to all be true. Okay? Now, strictly speaking, the operands of the logical operators should be boolean expressions, but Python is not very strict. Any non-zero number is interpreted as true. So here we have 17 and true, and that is true. This flexibility can be useful, but there are some opportunities that might be confusing. You might want to avoid it until you are sure you know what you are doing. Now let's go ahead to pseudocode and desperate these logical comparison operators. So let's go ahead and create a new logic dot py. And let's get an input from the user and store it in a variable x. And let's say we want a, it doesn't want to enter a number. So let's just use the integer function to convert it. And now let's say we want to see if that integer is between a range. Let's say we want to make sure that it's between 010. So let's go ahead and run our code. And let's say I enter 50. So as you can see, it gave us folds, So it's not between that range. Let's go ahead and try that again. Let's put 11. And now it changes to true, which means that it is inside that trait. Now, let's say we have another input. We can put more than one input, the same code that doesn't affect another integer. And now let's say we want to see the other integer is equal to x. So he wants to, integer y is equal to x. So let's go ahead and run our code. So let's say I enter five. It will give me true cost 20. And now the program is asking me to answer another integer. So let's say intersects. So again falls, which means that the other integer data entered is not equal to f. Now, let's say I want to add another state. Or y is different than 0. Now, since we'll have sorry and Brenda program. Now, let's say I enter five to nine. It will give me true. And that's because nine is different than 0. So these are some of the ways that we can use logical comparison operators. And you'll see that as we go on in this course, we'll get introduced. Lou spanned other functions that we'll make use of these operators. 14. If Statement: In order to write useful programs, we almost always need the ability to check conditions and change the behavior of the program accordingly. Conditional statements give us disability. The simplest form is the if statement. And so the boolean expression after the if statement is called the condition. We end the if statement with a colon character and the lines after the if statement are indented. So this is the condition right here. X must be greater than 0 in order to execute any statement that is after that. And of course it has to be indented. If the logical condition is true, then the identity statement is executed. If the condition is false, the indented is just skipped. So let's say if x is not greater than 0, it would skip the statement and just print whatever is aligned with this. So it will just continue reading the code, as we can see here. So x is greater than 0, it prints positive. X naught greater than 0 continues to execute the code without printing. And statements like this are called compound statements because they stretch across more than one line. So you can have more than one statement, but it has to be along that line and it has to be indented. There is no limit on the number of statements that can appear in the body, but there must be at least one statement. Parallels else. What is the meaning of having an if statement? And occasionally, it is useful to have a body with no statements. Usually as a place holder, holder for your code that you haven't written yet, you just keep it for later. So in that case you can use the pass statement which does nothing. So you just keep that condition for later and you can later just come back to it and change it to whatever you want to do that. And if you enter an if statement in the Python interpreter, the prompt will change from the three chevrons to the three dots indicating that you are in the middle of the block of the state. However, you have to indent in order to run the code. So you have to indent when you enter an extinct. And after that, a blank client at the end of a block statement should be added, otherwise, Python will return arrows. So as you can see here, added an ifStatement. We invented. Everything seems good to. Now. However, we didn't add a blank as we did here. So neuroscience index error, invalid syntax. And as you can see, in addition, is a big deal in Python. If you don't, then you get an indentation error. Let's see here we have x equal to pi and then we want to write another command. Y equal to six. However, we added a space before adding y equal to six. And so here, Python gave us an indentation error. Everything should be aligned, everything that is below, and conditional statements should be indented and so on. We can see here we have an if statement and we did. And then below that if statement. So it gives, it gave us an indentation. Error by them is expecting us to indent after we enter a conditional statement. A second form of the if statement is the alternative execution, in which there are two possibilities and a condition determines which one gets executed. So let's see here, we have this code and we want to see if x an integer is even or odd. And to do so, we divide it by two and the remainder is 0. It is in fact even. Our evidence not then x is odd. So we only have two conditions and only one should be true. It can't be both. So you can't run both. Only one will be executed. Sometimes there are more than two possibilities and we need more than two branches. And one way to express computation like that is through change conditionals. And so we add an elif statement. Ellis is an aggregate abbreviation to else-if at exactly one branch will be executed, there is no limit on the number of lf statements. If there is an else clause, it has to be the end. So he felt an else clause, it has to the end. We can't add down. However, if we have an LF, we can add as many statements as we want. Each condition is tracked in order. If the first is false, then X discharge and so on. If one of them is true, let say this statement is true, then it would execute this statement and the corresponding other statements won't be executed even if it's true. And this is another example which we can notice that we only have elif statements. We don't have an else, and that's totally okay as long as we don't have an else and then an LFO below it. So we can add as many other if statements as we want. And then if you want to end it, we can add an else and we can also ended by another. However, if we add an else, it is declared that says that this is the end of this if statement. And see another example. So this is another example. If x is less than y print class. So this is the statement that we have x is less than y. If it is less than y, then we'll print Less. So x is less than, less than y. If no, it would go to the other statement. And then we check this condition if it is true, if it is a footprint greater and if it is not, if it's not greater or less than, of course it's going to be equal. And then the program runs the other codes that are below it. And one conditional can also be nested within another. We could have written the three branch example like this. The outer conditional contains two branches. The first branch contains a simple statement which is print equal if x is equal, x to the second branch contains another if statement, which has two branches on its own. So if x is less than y, then print Less else and else by alchemy, x is greater than y because x equal and snouts less than, of course it's greater. And although the indentation of the statements made the structure apparent, nested conditionals become difficult to read very quickly. In general, it is a good idea to avoid them when you can. And as you can see here, if we have f x equals 2x equals to y equal to y, x equal to y. Print equal principle. And then district board, all of the other statements, the else-if statements and continuous the code that is below that if statement. And if it is not, then it checks if x is equal to y, sorry, greater than, less than one. And if it is, it would print that statement. And if it's not, it would go to the state. And we could have written a more simplified and conditional statements. So we could have written the following code using a single conditional. Let's see it. This is the code. If x is greater than 0. If x is less than ten, then print that x is a positive single-digit number. However, we could have just written it using one single conditional, which is if x is greater than 0, which is the first condition. And we use now a logical operator and x is less than ten a column. And then we print the same statement. And so this would be simplified nested conditional statements. And so conditional statements give us the ability to check conditions and change the behavior of our program accordingly. 15. Application on If Statement: And now let's go ahead and try to give an example on how to use if statements. So far, let's call it theta dot py. So already happens, which is called statements one. And let's say we want to build a program that asks the user if he is over ten years and if he speaks English, and if he has both conditions verified, he can create an account. First the user to give us his age. Enter your age. And so now we want to see that age is greater than ten. So we add an if statement and say if h is. And first, because we know that input returns for us strings. So what should we do first? Let's go ahead and convert that to an integer. Because if we compared a string, that give us if h is greater than ten. Now, we enter the first if statement. If it is greater than ten now we have to check if he speaks English. So if He is greater than, is greater than ten, we'll ask him another question. So another question would be, does he speak English? So let's create a variable. And we ask the user to give us another input, which is head. Sorry. And let's just specify for him, how does he need to answer? So let's say I yes or no answer. And so now we know that the answer is either going to be yes or no. So if that answer is a yes, so if English is yes, so we can compare strings. If it is, then we can say print. And now we print. You can create an account. And now let's see. If he doesn't speak English will give him another chance. And say, let's ask him if he speaks Spanish and say this program supports both English and Spanish. So if he doesn't speak English, will add another statement. To know. We'll ask them another question. So in that case, we'll ask him, do you speak Spanish? And so now, let's say, let's add another if statement inside that L is equal to yes. Then let's go ahead and also print that he can create an account. So now, let's say he doesn't speak either English or Spanish. And that case we just add an else. And in that case we print. You can build and create an account. And let's go ahead and test our code. Before we tested. So we have the main If statement. And so we have to complete that too. So if he is not greater than ten, so it will also print that you cannot create an account. So if he is under ten, kids would skip all these statements and just directly say, sorry, you can create an account. So let's go ahead and run our program. So first of all, let's just try to choose an integer that is less than ten. And we can see that we cannot quit. Now let's create, let's enter a valid age. Let's say 20. Now, do you speak English? Yes, I do. And in that case, you can create an account. Now let's just draw, again. Let's enter 20 English. And that case they speak Spanish. And in that case, yes, I do. You can create that cap. And so if we can't spin speak English or Spanish, so it's obvious that we cannot create an account. No. No. So we cannot create an account. And so you can notice here that they didn't give us the statement. You cannot create an account. And the reason for that is we didn't add an else statement for the spot. So we just have to add an out-of-state. And now you can see that our code is becoming more tedious and we can spur many of these conditions into only one. And to do so, we'll just replace n by blank. So do you speak English or Spanish? So now we have two languages, and that would spur as many code. And now we just say if B, if language is equal, equal to yes, so if you speak English or Spanish, then you can create an account and the language is known. So you don't speak both languages. So you can't, you cannot create an account. And so you can see our code now is more simple. So all these, and just we now have the country you can see now our code is more readable, more tidy. If he spins any of these two languages, just create an account. Or love, doesn't matter. You cannot create account that his under age just you can also create an account. So let's try an example on that. And let's see, yes, I do. You can create an account. And so you can see if statements are really important to our problem and how to stretch from one output to the other. 16. Try & Except: Now we reach catching exceptions using try and except. Earlier we saw code segment where we use the impotent and end functions to read and parsed and integer number entered by the user. We also saw how touches doing this could be, let's say the user didn't give us a number. And so if that happens, the script immediately stops, all the code stops. It strikes with a traceback, and it doesn't execute the following statements. So that could be a problem. And so there is a conditional execution structure built into Python to handle these types of expected and unexpected errors called Try and expect. So you as a programmer should expect that the user might enter an invalid input. And so you have to try to run this code. And you'd have to expect that if the user did enter an invalid code, you have to handle this exception and that exception, a statement. So the idea of try and except is that you know that some sequence of instructions may have a problem and you want to add some statements to be executed if an error occurs. This extra statements in the expect, sorry, Except block are ignored if there is no error. So let's say there is no error. The statements in that except block would be ignored. You can think of the triad expect feature and the Parthenon As an insurance policy on a sequence of statements just to ensure that your code will run smoothly and will not be stopped in its tracks, and don't execute the following statements. And so Parthian starts by executing the sequence of statements in the try block. So it will give, get from the user and input. And if all goes well, it skips the except block and proceeds. If an exception, of course in the try block. So let's say we entered character. Python jumps out of the try block and execute the sequence of statements in the except block. So let's say we have all other statements below that h, sorry, input. They will all be ignored and we will jump to that except statements. And so handling an exception with a try statement is called catching an assumption. The except clause prints out an error message. And in general, catching an exception gives you a chance to fix the problem or try again, or at least the program gracefully and not abruptly stop in the middle of the code. And so now let's go to our previous example we were working on, where we ask the user to enter his age and then saw a piece spoke English or Spanish. And so now let's say that the user did not give us his age. He gave us his number. Sorry, his name. Let's just try and during until my name instead of the age. You can see now we have a traceback error, a value error invalid literal for int. So the int function couldn't parse that variable because it was n string and not an integer. And so to avoid, and you can see that we didn't contribute the code. We didn't ask him if he spoke English or Spanish. The code just stopped abruptly. And so to avoid having such problem, we could try to use the try and except. So let's try to parse that integer. And so one important thing is after the trial you have to have a semicolon and then indent afterwards. This is similar to the if statement. We have to indent in the tri-state wants to. And so let's try to parse that integer. And if an error occurs, we'll ask the user to re-enter his age. However, this time will give him. Please enter your about it as a valid number. Okay? Now you can see that this code, this solution is not perfect. We'll see later on throughout the course how to make it more optimized. But for now, let's just try using the try and except statements just to see how they work. And so let's try to enter an invalid input. And you see we get a signal, Please enter a valid. And so we are given a second chance to enter a valid age. And right now I entered about age. And now you can see that we forgot to convert our entity. So don't forget, this statement is very important. So let's just start. Now. You can see that the code runs smoothly. And so a try and accept statements are clauses, sorry, are really important to handle exceptions throughout our course. Sorry, our code to make it more smooth. 17. Logical Expressions: Now let's see short-circuit evaluation of logical expressions. So when Python is processing a logical expression such as this expression right here, it evaluates the expression from left to right. And because of the definition of n, if x is less than two, the expression right here, x greater or equal to two is false. And so the whole expression is false regardless of whether this value right here is true or false. And on Python texts that there is nothing to be gained by evaluating the rest of our logical expression. It stops its evaluation and does not do the computations in the rest of the logical expression. When the evaluation of a logical expression stops because of the overall value is already known. This is called short-circuiting, the evaluation. And while this may seem like a fine point, the short-circuit behavior leads to a clever technique called the guardian pattern. So let's first consider the code sequence and the Python interpreter here. The third cal, calculation fail because Python was evaluating x over y, which was 00 in division, causes a runtime. We can't have 0 in the denominator. And so it did evaluate. But the first and the second example should not fail because the first part of the expressions was x greater than or equal to two, was evaluated to be false. So whatever does, the other statement was, it doesn't matter because it will always be false. However, in this case it was true because it was. So we had to evaluate the other expression. And so we can construct the logical expression to strategically place a guard evaluation just before the evaluation that might cause an error as follows. And so the first logical expression, x greater or equal to two is false. So the evaluation stops at the end. It doesn't execute the other expressions. At the second logical expression, which is right here, x greater than two is also a story true, but why different than 0 is false. So we never reach x over y. And the third expression, the y different 1000 as after X over Y calculation. So the expression fails with an error because it evaluated that expression first and then check if y is different than 0. And so in the second expression, we say that y, the fringe NCO acts as a guard to insecure. So to ensure, sorry, that we only execute x over y if y is non 0. 18. Functions: In the context of programming, a function is a named sequence of statements that perform a computation. And so there are many reasons to why we use functions in our code. And one of these reasons is that creating new functions gives you an opportunity to name a group of statements which makes your program easier to read, understand, and debug. Functions can make your program smaller by animating repetitive code. Later if you make a change, you only have to make it in one place. And when you define a function, you specify the name and the sequence of statements. Later you can call the function by name. We have already seen one example of a function call, and It's the type function. The name of the function is type, and the expression in parenthesis is called the argument of the function. The argument is a value or a variable that we are passing into the function as input to the function. The result for the type function is the type of that argument. So whatever we type in as an argument, the dysfunction, this type function should return for us the type of this argument, this value or this variable. And it is common to say that a function takes an argument and return aerosol, and the result is called a return value. And so Python provides a number of important built in functions and that we can use without needing to provide the function definition. The creators of Python wrote a set of functions to solve common problems and included them in Python for us to use. The max and min functions give us the largest and smallest values in a list, or what we call the argument. So here is a string that has, that has characters in it. And so when we enter the string into that max function, that function will see this string as a list of characters. And so it will return for us the character with the maximum number. And so characters and Python are encoded into numbers. And we'll see that later when we go into advanced data structures. And so you can see that the maximum character in this world is the w. Because when in alphabetical order as ranges from a which has the, which has the smallest encoded number, reaching, which has the highest encoded number. And you can see that the min function gave us the smallest character in this world on this string. Which is the space. So the space has the smallest, not. Another very common built-in function is the len function, which tells us how many items are in its argument. If the argument to learn is a string, it returns the number of characters in that string. And so you can see here, Hello world has 11 characters, including the whitespace character. These functions are not limited to looking at strings. They can operate on any set of values, as we will see in later chapters. And keep in mind, you should treat the names of built-in functions as revert, sorry, a reserved word. And so Python also provides built in functions that convert values from one type to another. The int function takes any value and converts it to an integer if it can, or complaints otherwise. So here we can see that when we give the int function and a string that contains integers, it gave us an integer which has the same. But however, when we gave a string that has characters, which are alphabetical characters, we gave, Sorry, we are returned within value error because we cannot convert alphabetical strings into numbers. Another function, sorry, another role of the int function is that it can convert floating-point values, two integers. However, it doesn't have a decimal part on the integer part, so we have 5.999. We only are returned with the integer part and all of the decimal part is thrown away. And of course it works on positive and negative numbers. Then the same four legs upper-right here. If we enter negative 55, it to treat it as negative 55. And another function is the float function that converts integers into integers and strings to float pointing numbers. So if we have an integer as an argument, we are returned with the floating point version of that integer. So if you have a T5, we already turned with 55. And the same if you have a string of integers, of course, we, whether it's an integer or a decimal, doesn't matter. It will return us the floating version of that number. And finally, the STR converts, the acf function, converts its argument to a string. So whenever we enter as an argument to the STR, it is returned inside as string. And so these are some of the built-in functions into Python. And we'll explore more and more as we dive into this course. Now let's go ahead to Visual Studio Code, and let's just try and experiment with these built in functions. So let's create a file that's called IT functions of pi. And so let's have a variable that is taken from a user. And let's say we want to have a number. Any number. We don't care if it is decimal or integer. And so let's say we want to convert this number with a decimal integer into a floating number. So in order to do so, we use the float function. And now let's print just to visualize our results. And so does return a floating number. However, we cannot visualize that in a script code unless reprint that valuable, it returns that value to that variable x. And so now let's run our code. And let's enter a number. Let's enter five. And so you can see we have 0 which is a floating. Let's draw another example. Let's try negative numbers. Minus six. Can see now it's 16. Now let's try entering a decimal number 6.3, and it remains 6.3. However, if we try to enter something like an alphabetical character, we can see that we have an error. And we've seen how we can see the types of dysfunctions. That's just try to see what's the type of the float function. So let's just print type of the float function. And since we don't want this anymore, let's just commented out. And so we add a hashtag commented. So let's go ahead and run our code. We don't care what do enter it here. However, the type of the float function is of class. Now let's say we want to print a statement that says you enter this number. So let's say you enter this number and you want to print that number. And so how can we add that number to our print statement? And one way to do so is by using the STR function. So we add this string to another string, which is going to convert that number into a string. And so right now, let's go ahead and run our function. If we try to enter five, you can see that our print statement work. There are no errors. You enter this number which is 5, which is already convert to a float. We can remove that statement and keep it as an integer the way we entered it. And let's say we entered five. You enter this number which is five. And finally, let's say we want to add the number that we turn to, let's say five. So we create another variable. And to add it, we have to convert it into an integer, assuming that we entered an integer. And so int x plus, let's say five. And now we want to see the result is the value plus five. So let's go ahead and try to enter as a variable this number plus five, which is ten. So let's try. Let's try converting it to a float. And let's see what happens. Five. And you can see that it is converted to a full, because when you add a float to an integer, the answer is implicitly converted into a float. And so these are some of the built-in functions that we can use in Python. 19. Math Functions: Python has a map module that provides most of the familiar mathematical functions. And some of these functions include ceil function that returns the smallest integer greater than or equal to x. So let's say x is 5.55.5 would give us six. And let's say steel, sorry, x is six. It will return for us six, because six is the smallest number that is equal to x and 5.5 is the smallest number, that is, sorry, six also is the smallest number that is greater than 5.5 is the smallest. If we assume that it was 77, is wrong because there is a smaller number that is closer to x, which is 5.5. And the floor x also has the same concept. However, floor x returns the largest integer less than or equal to x. So let's say x is 5.5. Floor x would return for us five because I am sorry, because 5.5 is less than 5.5 and is the largest to it for is not the answer because there is a larger number. I'm closer to 5.5, which is five. Copy sign returns x with the sine of y. So let's say x is five and y is minus five. So copy, sorry, would give us minus five, but we'll just take the sign of why they don't have to be identical. X could be three, y could be minus six. And it would take the negative sign. Fabs, x returns the absolute value of x. Let's say x is minus five. This function would return for us five. And if it is five, it will remain fun. And factorial x returns the factorial. Clear from its name, AF mode x slash y. So y would return the remainder when x is divided by Y. And this is similar to the mod r, the remainder that we saw earlier. And our somatic operators, it has the same, similar concept. Xs. Xs returns on e to the power x. As you remember, this arithmetic operator is a power. And so this function returns for us e to the power x. Let's say x is three. E to the power three. There are more course and we'll just skimming through some of them. These are some of the functions that are used and trigonometric equations, mostly, which include radians and degrees of x. So let's say we want to convert. We took from the user on radiance and was converted to degrees and vice versa. We could use these two functions, and of course, the cosine sine tangent functions that return for us that sine of x. And from its name, we should know that x should be a radiance value and not a degree as value. And of course, we have the power and the square root. So square root returns the square root of x. So in another way, and then another meaning radical acts and power x comma y on, similar to the two stars at arithmetic operators. So raises x to the power of y two, and y is four, so two to the power four. And so in order to use these functions, we have to import it. So there is the math module that we should comport in order to use the functions. This module contains. Module object, which contains the functions and variables defined. And to access one of these functions, you have to specify the name of the module and the name of the function, as you can see here, separated by a dot and known as a period. And this format is called dot notation. And so first, as we said, import math, the math module. And then after that you could use these functions. And if you print only math python interpreter TO turner, return for you that math is a module which is a built in function. And you can see here that this statement right here is trying to convert degrees value by dividing it by 360 and multiplying it by two pi. And the expression matches that mattered pi gets the variable pi from the math module. The value of that variable is an approximation of pi, accurate to about 15 did. And so when we enter the statement, we're basically converting this degrees into radians. And as we said, there is already a function that does that and does that, sorry, in math, however, we just wrote stated to show you that you can also use the math.pi expression. 20. Application on Math functions: And now let's go ahead and use these malfunctions and lead them through an example. And let's say we want to build a program that asks the user to give us the radius of a circle and it's center and central coordinates. And let's say we also ask the user to give us a point. And we have to see if the check, we have to check if that point is on the circle, or if it's outside of this circle, or if it is inside of that circle. And so let's begin by taking the radius as an input from the user. And so we need that x and y coordinates of the center. And so we need x, z and y z. So x coordinate of the center. And of course we need the y-coordinate of the center, x, y. And now let's also ask the user, as you said, to give us a point and each point has an x and a y. That's assuming we're working on a 2D plane. So we just did the x and the y. Ok, so X1 and Y1 refers to that point of, let's say point, let's give it a name. And the coordinate of 0.8. And now, let's say, aside from checking if this point is on a circle or not, let's give, provide some information about the circle, such as its area. And so the area is two pi r. And to use the math.pi, As we said, first of all, we need to import the math module in order to use this functions. And so area is pi r square. So math.pi times r square, which is math dot power, will use the power function, which is x comma y, and x here is the radius because its radius squared. And let's say we want to provide the perimeter of the circle, which is two pi r. So two times pi, which is math dot py times radius times radius. And now, how would we know if that point is on the circle or not? So first of all, we need to calculate the distance from this point to the center. And to do so, we need to use a formula that says that the distance in a 2D plane as radical x1 minus x2 squared plus Y1, Y2 squared. So first of all, let's calculate the difference x minus x. And so the difference in the x coordinate, x. X1 or X2 minus X1, it doesn't matter. However, what matters is that we need it to be absolute. So we go ahead and use the method absolute function, which is tops. And we want our x, 0 minus x one to be positive. And the same goals. And the same goes for the y-coordinate. So one coordinate, to be clear, that is an O and Al-Jazeera. Ok. So you don't get confused. And so now we found the x and y. And the rule says it's radical dx square plus d y squared. So now the distance is radical is math.sqrt. And so we use that square. And inside of that radical resettling need each one to be powered squared. Lambda to the x to the power of two plus the power d y to the power of two. And so as you can see, that's a radical. Axial minus X1 squared plus y 0 minus Y1 squared. All and celebrate it. And now this is the distance at that point is outside of the circle. We need to check. We need to compare it actually to the radius. So here we'll use the if statements that we discussed earlier and make use of them. And so we'll add an if statement and say distance is equal to the radius. We'll print that the point is on certain. However, and you can see that the intensity, and you can see that we have to end up. And now let's add another condition. Olive, distance is greater than the radius. That should mean that the point is outside of the circle. So the point a is and inside the circle. And else, else meaning that it's not greater, not equal. So of course it is going to be less than. And else we'll say point a is inside of our circle. So in side of our circuit. And so after we print were the point stands, let's go ahead and print that area and the perimeter. Perimeter, sorry. And let's go ahead. And just The perimeter and of course the area. And so we have to specify what does this number so the perimeter of the circle. And as you said, in order to add this number, this is a number, actually a floating point. We have to convert it into a string. And so now we cannot do the same for the area. So the area. And we can do something else to avoid using that function. And this is, so we just use a comma and it will automatically work. So let's go ahead and just change perimeter to area. And so now let's go ahead and let's just, for the sake of green clear, let's also print on the distance. The distance between the area, sorry, the point in the circle, the distance. The circle, point a. And say, this is the distance. And let's go ahead and see if our code works smoothly. So let's say the radius is five and the coordinates are 0 or the circle. And let's say, let's say we wanted to be on the circle. So let's give it a coordinate of 5-0, x is five and y is 0. And now you can see that we have a traceback error which says that must be a real number, not STR, which indicates that we needed to convert our inputs before using it. So let's go ahead and convert them. Slow triangle. See, it works. So five is the coordinates for the center and 5-0. And as you can see, is on the circle, this is the perimeter. This is the area of a circle. And the distance between the circle and point a is five, which is indeed the distance, sorry, the radius of the circle. And so let's say that we don't want the exact value of the area and the perimeter, as you can see, it has a long decimal. So let's say we want only the rounded value. And so now we can use the ceiling or the floor. So let's say we want to round it to the nearest smallest number. So that is using the floor. And to do so, we'll use the F4 function. And so four enables us. Of course it's not that floor. And same for the area. Let's say we want there to be a ceiling and on the floor. So let's go ahead and try running our code again. And radius 5050. And as you can see, we rounded the perimeter to the smallest number and the area of the circuit to the highest theorists number. And so these are some of the functions of the math module that can be beneficial when building a program that needs to use mathematical equations. 21. Random Functions: Given the same inputs, most computer programs generate the same output every time. So they are said to be deterministic. Determinism is usually a good thing since we expect the same calculation to you, the same result. For some applications, though, we want our computer to be unpredictable. Games are an obvious example, but there are more. Making a program truly non-deterministic turns out to be not so easy. But there are ways to make it at least seems non-deterministic. One of them is to use algorithms that generate pseudorandom number. Numbers. Pseudo random numbers are not truly random because they are generated by a deterministic computation. But just by looking at the number, it is all but impossible to distinguish them from random. The random function is only one of many functions that handle random numbers. The random module also provides functions that generate pseudo random numbers. The function random returns a random float between 01, including 0, of course, but not 1.00. So it can give you 0, but it cannot new 1.00 at each Tommy call random and get the next number in a long series. So if we, of course we have to import then in order to use its functions. And so let's say we use the random function. And we can have any of these as an output. Of course, not all of them would be the output, only one of them, but these are just an example of what that output could be. Another function is the rand function that takes the parameter's low and high and returns an integer between low and tie, which is including both. So this is the parameter for low and that's for her. And it would give us any number between those two numbers and including those two numbers should can give us five, it can give us stand. It can give us 76 any number between those two. And let's see what specify a limit. And so let's add another panel, that third parameter to. And that indicates that, let's say any number between 210. However, it should lead by arrange of two. So 246810, We don't want the odd numbers, we don't want 3579. And another function is to choose an element from a sequence at random. And so you can use the choice function. And so in a choice function, you'll add a parameter, a list that, sorry, a sequence that we'll talk about. We'll see further in the course. And once you add that parameter, random.choice will choose any item in this sequence. It can choose 1234 or five, or six. And let's say we don't want it to choose one but two. So we add a second parameter and specify that we want to items. And so it will choose at random two items or three, for example, as you can see here. Now let's go ahead to real pseudo-code and try to use these random functions. So let's try to write great, again using the random module. So let's call it a file name. And let this game be that he, the user enters digit between 010. And if that digit is equal to the random number that we create, if he predicted the right answer, then he wins the game. And so to do so, first, we need an input from the user, but it has to be between 010. So please enter number retreating 010. And now let's go ahead and create our random number Y in order to use it. That's import random module. And let's go ahead and choose a random integer between 0. And let's say that just narrow it down in order not to take so many time getting the right answer just for the sake of giving you an example. And so now, if x equals equals two, I will print that you want. And we'll see what should you have written in order to win the game. And we'll print, try again. And now let's go ahead and try our program. And let's not forget to convert int x. Sorry. Let's try choosing four. And let's just put a slash and in order to visualize it better. And let's try adding four you want, because I predicted right answer. Let's try adding Moon. And we lost this time. And so this is one interesting way of how to use the random functions and our codes. 22. Create Your Own Function: So far, we have only been using the functions that come with Python, but it is also possible to add new functions. A function definition specifies the name of a new function and a sequence of statements that execute when the function is called. Once we define a function, we can reuse the function over and over throughout the program. And so this is an example of function that I created. The name of the function is print names. And the rules for function names are the same for variable names. Letters, numbers, and punctuation marks are legal, but the first character can't be, you can't use a reserved word as the name of a function. And you should avoid having a variable and the function with the same name. The first line of the function definition is called the header and the rest is called the body. The header has to end with a column and by Has to be indented. The body can contain any number of statements. And the value of print names is a function, objects, function and that's the object print names. And the type of this function is class function. And the syntax for calling the new function is the same as for built-in functions. So you just type in the name of the function and the statements inside that function will be executed. Once you have defined the function, you can use it inside of another function, as you can see here. So this program contains two functions. So two function definitions, print names and repeat names. Function definitions get executed just like other statements, but the effect is to create function objects. So the statement inside the function does not get executed until the function is called, like hearing. And the function definition generates no output. There is no output. It just prints, as you might expect to have to create a function before you can execute it. So when we type in the name of the function that goes to that function, repeat names, and that goes inside of that function to know what are the statements. And that it says that it's calling another function which is print things. And so as it reaches the first statement and goes to that statement, that function, and that goes inside that function and it says prints this thing and then print that thing. And so we'll see this function printed twice, because recall that twice. And as you can see here, we printed this function twice. And in order to ensure that a function is defined before its first use, you have to know the order in which statements are executed, which is called the flow of execution. Execution always begins at the first statement of the problem. Statements are executed one at a time in order from top to bottom. Function call is like a detour and the flow of execution. The next statement, the flow jumps to the body of the function, executes all of the statements there, and that comes back to pick words of top. That sounds simple enough until you remember that one function can call another. While in the middle of one function, the program might have to execute statements and another function, as we saw earlier. But while executing that function, that program might have to execute yet another function inside our main function. Fortunately, Python is good at keeping track of where it is. So each time a function completes the program, the program picks up where it left off in the function that it called. When it goes to the end of the program, it terminates. So as we said earlier. So here, as you said, when it goes to that function, it says that it's calling yet another function. However one it goes to that function and execute that function. It then returns to the main function, executes the next statement. And so some of the built-in functions we have seen required arguments. For example, when you call methods sign, you pass a number as an argument. Some functions take more than one argument. Math.pow, for example, takes to the base and the exponent. And inside the function. The arguments are assigned to variables called diameters. And the same holds of competition that applied two built-in functions, also applied to user defined functions. So we can use any kind of expression as an argument which are assigned to variables called parameters. And the argument is evaluated before the function is called. So for example, let's take, for example, this function right here, which takes as an argument a variable, let's call it name. It could be a string and him anything. And then we print that name. And however, here, when we call that function, we pass in an argument which is a string and multiplied it by four. And we said, we saw earlier that we can multiply strings by numbers. And that would result in mud and repeating that string four times. So it helps spam, spam, spam, spam four times. So this argument would be calculated and then we print it. So let's go ahead and try to create our own function in Visual Studio code. So let's go ahead and create a new function. Let's call it function, sorry. And you, Let's call it function 2.5a. And remember, we worked previously on a game that from a user and input. And then, so he won't or not. So let's go ahead and try to create a function out of this. Let's go ahead and copy it offers. And now, how might we tried to create a new function? So let's first, as we said, in order to create a new function, we need to begin with there. And let's say when the function is called. And so as a parameter, we need to get the user's input to see if he wins or not. And we add a column, and now we go ahead and create our random number inside of that function. And we'll add all these statements inside of that function. So let's just be careful for the indentation. So created inside of our created function, a variable, a random number, and we take as an argument the input of the user. And so now if we run our code, nothing will happen if we enter a number. And that is because we did not call our function. So in order to Visual C, if we want or not, we need to call our function. And so we call it and add an argument which is x. So now let's go ahead and run our function. And let's try to add four. And you can still see that we still have an error name when is not defined. And that's because as we said earlier, we cannot call a function before we created. Firstly create our function, and then we call it. So now we try this again and enter four. We won the game, the program work. And so this is one way to create a function and use it. 23. Fruitful & Void Functions: Now we reach fruitful ongoing function. When you call a fruitful function, you almost always want to do something with the result. And to use this result, you need to have a return value. So to return a result from a function, we use the return statement and our function. So for example, this is a function that we created and in which if we enter the radius, it would return for us the radio, which is pi r squared. And so in order to return that area, that result, we use the return statement and I'm beside it, we specify what do we want to return, what variable inside of that function do we want to return? And sometimes it is useful to have multiple return statements, one in each branch of a conditional. It has a conditional. You cannot have two returns below each other without a conditional separating them. Otherwise, it will just return the first return statement. And the second return statement would be ignored because when you return, you are exiting the function. So here is an example of a function that gets as an argument any integer. And if it is and negative, it returns negative x, so it turns it into positive. And if it is already positive, adjust returns the integer itself. Now when we reach void functions, void functions might print something on the screen or have some other effect, but they don't have a return value. If you try to assign the result to a variable, you get a special value that is called non. So if you try to print name and you assign a name to this function, then this functions is a void function. It would just print None. And if you see, if you print the type of that non, non is an untyped. So we've already seen functions that are void. So now let's just try to create a fruitful function. So function three dot pi. And let's say we want a function that sees if an integer is even or odd. And so first let's ask the user to give us an input, an integer, enter a number. And of course that is returned integer. Let's create our function. Let's call it EBIT, which takes as an argument and number. And let's go ahead and check if it is even or odd. And so to see if it's even, we just say this number divided by two gives us a remainder of 0. Then return true. So that's going to return a boolean value. Else meaning that it is odd, return false. Now, let's go ahead and see if that function is true. Sorry, is even or not. And if it is even true, and if it is odd, we'll get a folder. So let's go ahead and run our function. And let's say I enter five. It's false because five is odd. That's enter 66 is even. So it's true. So this is one way of using a fruitful function. 24. While Loop: Computers are often used to automate repetitive tasks. The breathing, identical or similar tasks without making errors, is something that computers do well. People do poorly. Because iteration saw common. Python provides several language features to make it easier. And which, one of which is what? The while loop. But first, let's go ahead and talk about reassignment and updating variables. As you may have discovered, it is legal to make more than one assignment, two, the same Bible. And new assignment makes an existing variable referred to a new value, and it stops referring to the old value. So here, the first time we display x, the value is five. And the second time we display x, its value is seven because we change its value in the second assignment. Reassigning valuables is often useful, but you should use it with caution. If the values of variables change frequently, it can make the code difficult to read and the boat. And a common kind of reassignment is an update where the new value of the variable depends on the old. Means get the current value of x and add one, and then update the ax to the new value. However, if you tried to update a variable that doesn't exist, you get an error because Python evaluates the right side before it assigns a value to x. So if you don't have x, this would give you an error. So before and so before you can update a variable, you have to initialize it, usually with a simple assignment, x equals to 0. And updating a variable by adding one is used, sorry, is called an increment. And if you try subtracting, subtracting one, this is called a decrement. And so now let's check the while statement 1-4 of iteration and partner is the one state. And so here is a simple form that counts up from 0 until nine. And so first, let us see the flow of execution for white stigma. And so first of all, we have to evaluate the condition which yields either true or false. So we have this valuable and equal to 0 and we have to evaluate this condition, which is n less than ten. And so we can see that it is an n is less than ten. And now the second step, if the condition is true, we'll go inside that loop and execute the statements inside that while loop. And however, if it's not true, if it's false, we exit the while statement and continue the execution on the next statement. And so, as you can see here, we'll start by printing zeros and then we'll increment n by one. So it will become one. And so the loop goes on and on until we reach ten. And so when N becomes ten and re-evaluate that condition, we can see that this condition is no more, is no longer true. It becomes false because ten is not less than ten. And so it exits this loop and prints done. And so we can also use L statement with while loops. And this is an example on doing so. So here we have a while statement that evaluates the condition if count is less than five, and if this condition is true with print count is less than five. And count is referring to the variable that we initialized before using it inside our loop. And so you can see, and the output that you want to 34 were actually inside that loop. They printed that sacred. And when the count at each tile account is incrementing, count reached five and really evaluated that condition. We saw that five is no longer less than five and went to the else statement, which made us print that that count, which is five now, is not less than five. Recall that variable that we are incrementing each time that changes. Each time the loop executes and controls. The loop finishes the iteration variable. And if there is no iteration variable inside our while loop, the loop would maybe highly repeat forever, resulting in an infinite loop. And so that's an example on how we could result in infinite loops. Here we have a while true statement. That means that that distrusted and will always, this loop will always execute because true is always true, it cannot change. And so this is an example of an infinite loop. Another example is we're using an variable which is equal to one. And we're seeing that this variable is equal to one. However, we are not changing anything inside of our while loop regarding that variable. And so it's not an iteration variable. And this also results in an infinite loop. 25. For Loop: Sometimes we want to look through a set of things, such as a list of words, the lines in a file, or a list of numbers. When we have a list of things to look through, we can construct a definite loop using a for statement. Recall the wild state with an indefinite loop because it simply loops on until some condition becomes pool. Whereas the for loop is looping through a known set of items. So it runs through as many iterations as there are items in the set. And so this is an example. And the syntax of a for loop is similar to the while loop. In that there is a four, the statement and the loop body. So instead of having a while statement will have a for statement and of course a column, and then we indent, and then we have the body of that loop. And so the valuable colors is a list of three strings. And the for loop goes through the list and executes the body once for each of these three strings. Resulting in that output. Translating this for loop to English is not as direct as the wide, but if you think of colors, as I said, it goes like this. Run the statements in the body of the for loop once for each color in this set, name colors. Looking at the for loop four and so forth, are reserved words in Python. And color and colors are only variables. For the for loop. The variable color changes for each iteration of the loop and controls when the for loop completes the iteration variable step successfully through the three strings stored in the color variable. So first we have the red, and we go through that first iteration and print color red. And after this iteration is done, we move on to the next string, which is blue, and print that statement color blue. And when we're finished, we exit that for loop and we print done. And you can see the output here. You can also look through a string. So strings in Python are considered also as a list. So for x in the string, so for every character in the string, which is stored in x, we're going to print x. And so the output will become my printing on each line, each character of that string. And to look through a set of code as specified number of times, we can use the range function. And the range function returns a sequence of numbers starting from 0 by default and increments by one by default, and ads at a specified number which is written as a parameter. So let's say we have this code right here. For x and range. So by saying for x in range from 0 till five, because six is not included and because we have already 0 by default. So it already is five from 0, sorry, it is already six from 0 to 50123456. So we're repeating this code, this loop six times. And we can also specify that we want the range not from, not six tons, but ranging from a number to a number. And so we want x to be starting from two and ending at 55 because six isn't included. And so we start from 2345. And finally, we can use else also and for loops the same use them in the while loops. And so the else keyword in a for loop specifies a block of code to be executed when the loop is finished. And so here you can see that this loop will execute six times because the range of six. And so you can see 123456, but starting from 01 finishes, it goes to the else statement and prints. Finally, finish. And finally we have the nested loops. We can have a for loop inside another for loop. And the inner loop will be executed one time for each iteration of the outer loop. So here's, here's an example. We have this outer loop which has four in this list. We're going to have another loop inside it, which is for y in this other list. And for this iteration will go into to print x comma y. And so what would be the output? Let's see. So first of all, we went, we go to x and the outer loop. And X is red for our first iteration. And so for our first iteration, we're going to go to the second inner for loop, and we go for y and y for our first iteration inside that inner loop is Apple and so red apple. And now the inner loop iterates for the second time because we also dealt listed at the end, we have still banana, cherry. And so now it prints again, read, we're still attract, we're still at the first outer iteration, red banana. And then we have the last element, which is cherry. And now when we finish this inner loop, we go ahead and change x to go to the next item which is big. And so the whole cycle is repeated again until we reach the last element and the outer loop, which is tasty. And so this is one way to use nested loops and for loops. And we can see that we can use them also. And while loops. 26. Some Loop Algorithms: Now let's go ahead and see some loop algorithms. But first, let's see how loop patterns are generally constructed. So to construct a loop, we generally first initialize one or more variable before the loop starts, as we saw previously. And then by performing some computation on each item in the loop, possibly changing the variables in the body of the loop. And that's one main thing that has to happen if we don't change that variable, it's not an iteration variable and some of our loops might become infinite. And so looking at the resulting variables when the loop completes is one important step because these variables are going to decide if we exit the loop or not. Because based on these variables. And we're going to reevaluate our condition and decide whether we reiterate the loop or not. And so one example of loops, counting. And so let's say we want to count how many times are we looping through a for loop. And so to do so, we first initialize a variable count to 0. And inside that for loop, each time we run through this code of sequence, we add this assignment, count equals count plus one. We are incrementing the count. And so when the, all the loops are finished, we print count and see how many times did we loop over this region. And so we can see here, because we have this list which has six items and we're iterating over these six items. So we're going to have a count of six. And similarly, we have summing loop. However, the sine loop, SMS every, also all the items inside the list. And so let's say we have this list of integers and we want to find the sum of all these integers combined. And so to do so, we first initialize the variable sounds outside of the loop. And then each time we are going through the loop, we add that variable, that integer to some variable. And so each time we're adding up these integers, and let's say the first time we added five plus variable, which is 0 first initially. And then the next iteration, we already have five inside that variable and we're adding to its empty one. And then third iteration, we have 76, which is the sum of these two and adding to it 21. And when we finish the, the slope will print sum and we'll have the sum of all these integers. And neither the counting loop nor the summing group are particularly useful in practice because there are built-in functions like land and some that compute the number of items in a list and the total. Of these items in the list respectively. And now let's move on to another example. And let's say we want to find the largest variable in a list or the minimum. And so we can do so by having a maximum or minimum loop. So the variable largest is best thought of as the largest value we have seen so far. So before the loop, we said the largest to the constant none. None is a special constant value which we can store in a variable to mark the variable, the variable as empty. Before the loop starts, the largest value we have seen so far is none, since we have not yet seen any values. And so as we start our loop, we check this valuable largest is none or is greater than the current number inside of our list. And so if it is none that indicates worse that we are still in our first iteration. And so if it is none, we'll just go ahead and give that variable the first number. The first number in that list is the largest number. And that's for the current largest number. And now going to the second iteration, we are going to the second number on our list, which is 71. And so we go to that condition and C, S largest non, No, it's not. Now it's the first number. And now is it larger than the first one? The one previous? Yes, it is largest. Sorry. 71 is indeed greater than the largest. And the largest now becomes that second item. And now going through the third one, we're going to ask ourselves the same question. And we've seen that no, 71 is still greater than that item. So the largest stays the same. The 71. And we go on and on until we reach the end of the loop, and sorry, at the end of the list. And by the end of the list, we should have gotten the largest number in our list, which is 92. And now the minimum is similar, just that, we now want to find the smallest. And so now we check if that number is smaller than the smallest number, that similarly we have None at the beginning. And for the first loop, the smallest will be the first number in our list, just 72. And then throughout the loops, that smallest will change until it finds the smallest number. And here is a simple version of the Python built and minimum function. So we have a minimum function, Bolden and Python and a Maxwell function. And so let's just have a simple look at that. It's similar to the one we've just seen. Just the only difference is that it will return for us the smallest number rather than printing it. And so you can see you have none. Then we check if that's most is not smaller than the smallest. And we'll replace the smallest by this value. 27. Continue, Break, & Pass statements: Now let's go ahead and see what our break, continue and past statements. So a break statement terminates the loop containing. So sometimes you don't know what time. No, it's time to end the loop until you get halfway through the body. In that case, you can write an infinite loop on purpose and then use that break statement to jump out of the loop. This loop is obviously an infinite loop because as you said, the logical expression, expression on the wall statement is a logical constant true. So we enter the loop, we test that expression which is always true. So it always going to ask a rubric, the loop or not. And so we are going to input which is a number. However, if we enter done, we'll check if we enter done. And if we did, we're going to break that loop. And so when we break it, we just exit the loop. However, if we actually entered a number, we're going to repeat the loop and enter, and enter another number until we enter done an exit the loop. So let's go ahead and see an example on that. Another example. So here's an example on a for loop. And so let's say we're going to check every letter inside this string, every character for every letter. That letter is h. We're going to break that loop. And so as you can see on output, we only printed the first three characters. And when we reach age, we stopped and in print anything else. And a similar example on a wild loop as having a variable which is ten. And every time we enter that loop, we're going to print that variable and then decremented. And if that variable was to reach five, we're going to bring that loop. And you can see here that we didn't reach five, we only reached six. And as the variable became five, we broke the loop and printed. Done, sorry, good-bye. What is a continue statement? The continue statement is used to skip the rest of the code inside the loop for the current iteration only. Unlike the breakthrough, the break look, breaks the loop. However, here in the continue statement, the loop does not terminate, but continues on with the next iteration. So lets see the same examples we saw on the break, but on here on the continue statement. So we have this for loop. And we're checking every letter. And if we reach age, we're going to add a continuous state. And as you can see in the output, when we are, when we reached age, you can see that we re one to the next iteration and we didn't reach that statement, we print the current. And so that's why we can't see the H here. And similarly in a wild loop, if we use a variable which is five and decremented every time. And we said one-year reached for continuity. So go back to the next iteration. And so you can see when we started with five and then we documented and then which for we didn't print current variable. As you can see, that's why we don't have four. And so it went to the next iteration. The next variable is three, and so reprinted 321. And then we finished our loop and printed good. But now moving on to the last statement, the pass statement. So the pass statement is a Null operation. Nothing happens when we add a past statement to our code. The past is useful where in places where your code will eventually go but has not been written yet. So let's say you have a function, as we said earlier, and say you don't want to write that function right now. You just want to add it for later. And so in order to add that function, you just write that past statement as in that function will not do. And the same way we use that past statement in the function, we can use it in a loop. So here's an example where we have a for loop. And we said that if that letter and parsec is H, we're going to pass. And so here, that pass has no effect. However, it will print that pass block. So we went to P, nothing happened. Then why? Nothing happened as really h will go inside that if statement bus, nothing happens, then print this block and then print H. So it printed H, nothing happened. Normally. The only difference is that we had this if statement and inside that we have that print. This is a passbook. However, let's say we remove pass. This output is going to stay the same. And so past statements, as I said, are only useful when you haven't written yet what your code is going to do. 28. Word Reverse: Now let's try to make use of these for and while loops and these break and continue statements by building and program. So let's try to build a program that reverses a word. So, and first let's go ahead and create our own file. Let's call it reverse dot py. And let's say we want, each time the user enters a word, we want to reverse that word. So we need the user to give us an input. And this input is before we reverse it. And now we need to reverse that input. And to do so, we need to go for every character inside that word. So for every letter and word. And we're going to create a reverse bird. So as we said, let's initialize our reversed word. And each time we get that character, we're going to add that letter to this reverse, sorry work. However, we don't add the reverse plus the letter because if we did so, then let's say the first letter, let's say was W. Then we add o, so it would be W0. So we're just going to return the same word. So in order to reverse it, we're going to add the letter, then the reversed word, so the order matters. And so after we're done, we're going to print that reversed. And that's after reversing it. And let's go ahead and visualize what's going to happen. So before reverses, say I enter my name. After reverse, you can see that the word is reversed. Now let's say we want to, we want the user to keep on giving us words until he types done. So we don't want to run the program each time. So to do so, we're going to add a while loop. And let's give it an infinite loop. And if the user does enter a break statement, sorry. If the user enters and done statement, then that means that we're going to get out of this loop. So if this word that the user entered is equal to done. And let's go ahead and break out of this loop. However. And you can see that this is giving us either because we have too in that now. And when we exit that while loop, we're going to print done. So we added a while loop which is impaired. However, we added a break statement which breaks the loop whenever we type in done. And so let's add another feature. And that enables the user to add a comment. And so if you add a comment, nothing would happen and you just enter another world. And so how could we do that? So remember that comments. Let's try to add a comment similar to that Python Common. And so Python common starts with a hashtag. So let's say our word starts with hashtag. How can, how can we check that our words start with hashtag? So as we said, words are, every string is considered as lists. So in order to access the first item, which is the first character, we had 0 because the items are numbered from 0 till the last item. So afford at 0. So we're at 0 means that the character of the word at the beginning is equal to hashtag. We're going to consider this as a comment and just go ahead and reiterate this loop. So let's go ahead and try to run our code. And let's see the Kirk's. So let's say this word. You can see that we have the before and after. And now it's asking us again to add another word. Let's say now we add a common. So this is a common and common started hashtag. So if I add this comment and enter, you can see that nothing happens. That will just give us another before rebirths statement. And so now let's say I am done. So you can see that we get out of the statement, the code, the program stops running. So this is one way we could use for loops. While loops break continuous data statement. At the same time. 29. Project: So now for the project, and the project is actually pretty simple because everything you have to do, we've already seen throughout the course. So all you have to do is just write a program that leads from the user is sentence and then returns for him the sentence reversed. So for example, if the user enters Hello, my name is Sarah. This program must return the sentence rivers, as Sarah is named my hello. However, we don't want to reverse that characters. We want to reverse the words to be careful while doing them. And this program should also count the number of characters inside the sentence. So you should also return for the user, how many characters do we have inside the sentence, including the spaces? And it has to give also the user information about what is the maximum and the minimum character inside of the sentence. And finally, this program should remain running until the user decides to exit. So for example, the program stops running when the user enters. Or done. Any other, any word that means that the program should stop. And you could use the split function in order to split this sentence given by the user into words. And these words would be stored in a list. So for example, hello.py dot split would split this word sentence according to the space. So here we have a space. So this is the first element in that list, and that is the second element in that list. So the split, split according to this piece. You can also use another approach. You don't have to use this function, but this function would make your life easier. So this is the project. Please don't hesitate if you have any, any questions, contact me. And I hope that you enjoyed this course, please. I live down and review just for me to know how can I enhance my delivery of my courses better. Thank you so much. This was python for beginners.