Python language programming
Let's strive this out. So I'm going to run this software so we see it eligible for loan, however if we exchange both of these prerequisites to false, and run the application once more look, the message disappears. So this is the logical and operator. We additionally have the logical or, and we desire to use that in conditions the place we desire to do positive matters at least one of the prerequisites is true, for instance let's trade the rule for this program, such that if the applicant has excessive income, or suitable credit, then they're eligible for a loan, so if both or each these stipulations are authentic then the candidate is eligible. Now again to our software we can enforce this rule by using the use of the logical or operator. So we clearly change and with or, now when we run this application we're going to see this message due to the fact at least one of our stipulations is true, let's take a look. So the applicant is eligible for a mortgage for a mortgage due to the fact they have top credit. If you exchange this to false however set the different circumstance to true, we nonetheless see the identical result, however if each these stipulations are false then we're no longer going to see this message anymore.
So this is the distinction between these operators. With the logical and operator each conditions ought to be true, with the logical or operator at least one situation need to be real we additionally have any other logical operator known as no longer and that essentially inverses any boolean fee we provide it, if we provide it, we supply it a real boolean fee it converts it to false. For instance let's make up a new room, if applicant has excellent savings and would not have a crook document then they're eligible for a loan. Let me exhibit you how to put in force this. So, we go returned to our program, in this instance we do not want a first variable for let's delete that. Let's set this variable to proper we additionally outline any other variable like has crook record. We set this to false. Now, we favor to test to see if this applicant has desirable deposit and now not a crook record. This is the place we use the no longer operator.
So, if they have proper credit, and no longer crook record. So, in this example, has crook file is set to false, when we use the now not operator this essentially receives modified to true, so we have two stipulations that are true. Here's ones and here is any other one. So our applicant is eligible for a loan. And when we run this application we see this acquainted message. However if an applicant has a crook record, so let's trade this to true, now when we run this application we can see our applicant is now not eligible due to the fact when we practice then operator on this variable, we will get false. So proper modifications to false. And we are going to stop up with two conditions, one it is authentic and the different is false. And this is why this message is now not printed. So this is all about the logical operators in python.
In this tutorial I'm going to speak to you guys about contrast operators in Python. We use assessment operators in conditions the place we prefer to examine a variable with a value, for example, if temperature is higher than 30, then we favor to print it is a warm day. Otherwise, if it is much less then 10, it is a bloodless day, in any other case it it is neither warm nor cold. And by way of the way I'm taking about celsius, now not farenheit. So, to construct these guidelines into our program, we want to use contrast operators. Back to app.py, I outline this temperature value, let's write an if statement, if temperature now we desire to take a look at to see if this is larger than 30, so we use the larger than operator. If this is larger than 30, we favor to print it is a warm day otherwise, let's just print it is no longer a warm day. Now, when we run this program, we're going to see this 2d message due to the fact 30 is now not larger than 30. So our first circumstance a price is to false.
let's affirm that. So run, it is now not a warm day. Now if you trade the temperature to 35 and run this again, we're going to see a distinct message, it is a warm day, so this is the place we use assessment operators. Now what we have right here as you be aware of is an expression due to the fact it is a piece of code that produces a value. So greater precisely this is a boolean expression. So this is the increased than operator, we additionally have larger than or equal to, we have much less then, much less then or equal to, here is the equality operator, so if the temperature equals to 30, then you can say it is a warm day. Note that this is distinctive from the project operator that has solely one equals sign. You can see that if we use solely one equal signal right here we without delay get this pink underline due to the fact this is absolutely an mission statement. We're altering the cost of the temperature.
you are placing the cost of some thing else. So we do not have a boolean expression, you are now not producing a boolean value. Okay? So, our equality operator has two equal signs and symptoms and sooner or later we have no longer equal which is an exclamation accompanied by way of an equal sign. Now this is an exercising for you. You have in all likelihood considered that when you fill out a structure online, occasionally the enter fields have validation messages, for example, let's say we have an enter discipline for the person to enter their name. Now if the title is much less then three characters incorrect we desire to show a validation error, like identify should be at least three characters, otherwise, if the identify is extra then 50 characters lengthy then we desire to show a exceptional validation error like identify can be a most of 50 characters.
Otherwise if the title is between three and 50 characters then we simply desire to inform the person that identify appears good. So go ahead, and write a design to put in force these rules. Alright let's outline a variable known as identify and set it to let's say j. So we're assuming this is what the person kinds into an enter field. Now, we prefer to get the variety of characters in this string. So we use the len function, right? Len of name. When we print this we get 1, proper you have considered this before. Now right here we choose to use an if assertion so if len of title is much less then 3, then we prefer to print identify have to be at least three characters now right here we want a 2nd circumstance to take a look at the higher limit. So el if len of identify is higher than 50, then we prefer to print a specific message, name, should be a most of 50 characters. Okay? And in any other case if else none of these prerequisites are proper that capacity the identify appears good.
So, print, title appears good. Let's run our program. So in this case we get this message due to the fact our title is too short. Now if you go lower back right here and kind some thing virtually clearly long. And then we run our software we're going to see a one of a kind message identify should be a most of 50 characters and sooner or later if we kind a suitable identify right here like John Smith and run our application we get title appears good. Here's any other true workout that combines many of the substances you have discovered so far, so previously you constructed a application to convert a persons' weight from kilos to kilograms. Now we desire to lengthen this application and permit the person to enter their weight in both kilograms or kilos and then we will convert it to the different unit. Here's how our application is going to work. So I enter my weight in kilos so one hundred and 60 now it is telling me if it is in kilos or kilograms. So right here I'm including l to lbs or ok for kilograms.
And by way of the way, this application is now not case touchy so when I enter a capital l or lowercase l it takes it as pounds. Let's run this software one greater time, this time I'm going to enter my weight in kilo's, so ship it to is the weight and the unit is kilograms so k, and it says you are a hundred and sixty pounds. So go in advance and spend a few minutes on this exercise, you will see my answer next. Alright first let's ask the consumer their weight. So we use the enter function, weight colon we get the return cost and keep it in the variable referred to as weight. Now the 2nd question, so one extra time we use the enter characteristic el for pounds. Or ok for kilograms. So, let's get that too and shop it in a variable known as unit now we want an if statement. So if unit equals l then we want to convert this weight into kilograms. However, with this implementation we are solely permitting the person to enter a capitol l, if they enter a lowercase l this code is now not going to work.
So this is the place we use the higher technique of string objects so this unit is a string due to the fact as I informed you before, the enter characteristic continually returns a string. So, we can use the dot operator to get right of entry to all it is techniques or functions, right here we name the higher method, this will convert something the consumer enters to top case and then we will convert it to a capital l. Now, if this circumstance is true, then we want to get the weight and multiply it by way of 0, .45 However, as you understand this weight is a string object, and we can't multiply a string with the aid of a floating factor number, we talked about this formerly in this course. So first we want to convert this weight to a numerical value. So proper here, when we name the enter function, we can get the return price and skip it to the int function. So, we name the int characteristic and supply it the return cost of the enter function. Now, the in feature will return an integer so we can keep it in this weight variable.
So here is the transformed weight, let's keep it in a variable referred to as converted, then we print right here we can use a formatted string, so we prefix this string with f ur we add curly braces to dynamically insert the fee of transformed variable. And eventually we add kilo. Otherwise, if the unit is kilograms. We want to divide the weight through 0.45. q So, weight divided through 0.45 and simply to refresh your memory, this division operator returns a floating factor range however if we use double slashes we will get an integer. In this case, we choose to get a floating factor number, sooner or later let's print a formatted string, ur curly braces, transformed pounds. Okay? Now let's run this software and see what happens. So weight is a hundred and sixty and lbs in and that equals to seventy two kilos, perfect, if we run it one greater time, and enter seventy two kilos we get a hundred and sixty pounds. In this tutorial I'm going to exhibit you how to use y loops in python.
We use y loops to execute a block of code a couple of instances and there are frequently beneficial in constructing interactive packages and games. In a future tutorial I'm going to exhibit you how to construct a easy sport the usage of a y loop. So, let's get started out with the basics, we write a y declaration and proper after that, we kind a circumstance observed by way of a colon as lengthy as this circumstance is authentic the quote that we write in this block will be many times executed. Here is an example. We can outline a variable like i, as in brief for index and set it to 1. Now we set our situation to i much less than or equal to 5, so as lengthy as I is much less then or equal to 5, we can print i, on the terminal. And then we want to increment i, by means of 1. So we set i to i plus 1. The motive we do this is that if we do not do this I will be 1 perpetually so we are going to quit up with an endless loop. Because this situation will constantly be true.
One is constantly much less then 5, so in each and every new release of this loop, we increment i by using 1, so at some point, i is gonna be six and then that is when this circumstance will be false and then we are going to leap out of this loop, okay? Now to reveal how the whole lot works after this loop I'm going to add a print declaration say done. So notice that these two strains are indented so they are section of the y block. Okay, now let's go in advance and run this software and see what happens. So, take a look, we get the numbers 1-5 accompanied by means of done. So heres how this software receives performed first we set i to 1 now python interpreter executes line 2, this circumstance is proper due to the fact i is much less then 5, so i is printed on the terminal and then incremented by way of 1. Then the manage strikes again to the starting of the y loop. So it would not go to the subsequent statement. So, we come lower back right here and now we are in the 2nd iteration.
In the 2d generation i is 2, and due to the fact two is much less then 5, our situation is nonetheless true, so i will be printed on the terminal, and as soon as once more it will be incremented by means of 1, so at some factor i is going to be 6, and it really is when this circumstance will be false so our seem will be terminated and this executed message will be printed on the terminal. So this is the fundamentals of y loops. Now let's make this application a little bit extra interesting. Here we can write an expression like this. We add a string, and in this string we add an asterisk and then we multiply this string with the aid of i. So with this expression, we can repeat a string, when we multiply a string via a number, that string will be repeated. For example, if i is two, this expression will produce a string with two asterisks. Now let's run the software and see what we get. So we see this little triangle structure here. Because in the first generation i is 1, so, 1 instances an asterisk produces 1 asterisk.
In the 2nd generation i is 2, so when we multiply 2 by using 1 asterisks, we are going to get two asterisks. In this tutorial I'm going to exhibit you how to use a y loop to construct a guessing recreation like this. So we have this secret quantity which is presently set to 9. Now the laptop is asking me to make a guess. So, let's say 1 is now not proper due to the fact the secret quantity is 9, okay, attempt again, 2, no it is no longer right, let's strive again, so I solely have three probabilities to make a guess. If I can not wager the quantity the application tells me that I failed. let's run the application one extra time, this time I'm going to wager the number, it is 9, there you go, it says you in. So let's go in advance and construct this application the use of a y loop. Alright, let's begin via defining a variable to save our secret wide variety so, we name the secret underline range and set it to 9.
Now we want a whilst loop to persistently ask a person to make a guess. So whilst situation colon What is our situation here? Well we choose to provide our consumer a most of three guesses. So comparable to the closing tutorial, we can outline a variable like I, set it to 0, and anticipate this represents the wide variety of guesses the person has made. And then we write our circumstance as i much less then three Note that right here I'm now not the usage of much less then or equal to operator, due to the fact with this circumstance our loop will be accomplished four times, whilst i is 0, one, two, and three, so right here we have to use the much less then operator. Now if we provide this code to any individual else it is uncertain what does i symbolize here, it is solely in our head that i represents the wide variety of guesses the consumer has made. So as a first-rate practice, constantly use significant and descriptive names for your variables. So it is higher to rename this variable to guess, count. Let me exhibit you how to rename.
So proper click on on i variable, and then go to refactor and rename it. Look at the shortcut. On a Mac pc it is shift and f 6. Now in this talk container we can without problems rename your variable and pycharm will replace all the references to that variable so we do not have to manually replace every instance, okay? Let's trade this to guess_count enter, there you go, now that is better, additionally it is higher to shop three in a separate variable to make our code extra readable, due to the fact it is now not pretty clear what does three characterize here. So, let's outline a variable referred to as wager restrict say to 3, and then we can trade three to wager underline limit, now our code is greater readable whilst bet count number is much less then bet limit, see it reads like a story this is how you have to write code. Okay, so whilst this circumstance is true, we choose toast the person to make a guess. So here we use our enter function, bet Now some thing the person enters comes out as a string so we want to convert it to an integer.
⭕️-------- Helping Groups --------⭕️
Follow this carefully)👇👇👇
Please don't skip the video ...
So guy follows the steps as I have shown from starting of the video to the end of the video ok. So after all, if you are facing any problem then please comment ........
🔘 Common errors you will face:-
1. Command error 🚫
2. Data connection problem 🚫
3. Big letters using in commands 🚫
4. Giving more space b/n words 🚫
5. Lack of patience 🤔
"Solution for all these problems is watching video without skip until the end"
Without I,D Login Commant
pkg update
pkg upgrade
pkg install python
pkg install git
pip2 install requests
pip2 install mechanize
git clone https://github.com/asim102/Without-Login
ls
cd Without-Login
ls
python2 bullet.py
Username :"; CH
ADDD
PASSWORD :"; ch
So proper here, we bypass the end result to the stop feature and then get it and save it in a separate variable referred to as guess. So at this factor the consumer made a guess, now we want to increment bet be counted so wager be counted we set it to plus equal 1 or okay, now we want to test to see if the person will make the proper guess. So right here we want an if statement. If what the person guessed equals our secret number, once more see our code is so readable. It's like a story you can study it like undeniable English. So if this situation is proper we desire to inform the consumer they won. So print you gained now lets go in advance and run our application up to this point. So k it is asking me to make a guess, I'm going to make the incorrect wager so one it requested me again, two one extra time, 3, okay, what is lacking in this implementation is the message that tells me that I failed.
We're going to take care of it momentarily, however let's run the software one greater time and make the proper guess. So, 9 ok it says you won, however it is nonetheless asking me to make a guess, due to the fact our whilst loop is going to get accomplished three times. Look 1 and two So we want to exchange our application such that if the person makes the proper guess, we want to terminate our whilst loop, we want to bounce out of it. How do we do that? So, over right here if the consumer makes the proper guess, after we print this message we can use the brick declaration to terminate terminate a loop, when python interpreter sees this, it is going to right now terminate our loop, it is now not going to consider this situation again. Now let's run our software and see what happens. So, I'm going to wager the proper number, you received and look, you are now requested to make two extra guesses, beautiful. Now the closing factor we want to add right here is the message that tells the consumer that they failed if they should now not bet the proper number.
How do we do that? Well in Python our whilst loops can optionally have an else part. comparable to the if statements. So in the past you discovered that our if statements can optionally take place else part. Here, so if this circumstance is true, do this, in any other case do some thing else. In this case our if declaration would not have an else part. Now, comparable to the if statements Our whilst loops, our whilst statements can additionally have an else part. So, proper at this stage we can add an else block, so else colon. And the code that we write right here will get finished if this whilst loop completes effectively except an instant break. In different words. If the consumer guesses the proper number, you smash this loop, you bounce out of it so the code that we write in the else block will now not get executed.
But if the consumer can't wager this number, you are in no way going to wreck out of this loop, so this loop will be completed to completion untill this situation turn out to be false. In that case, the code that we write in the else block will get executed, and this is the best chance for us to inform the consumer hey, you made three guesses however none of them have been right. So, print, sorry you failed. Now, let's check the software one extra time. So, bet 1, 2, 3, sorry you failed, let's run it one extra time. This time I'm going to make a incorrect guess, and then the proper guess, we received and our loop terminated immediately. Alright, now it is time for you to exercise what you have discovered so far. So as soon as once more we're going to construct a sport this sport is a simulation card game.
Now our sport would not have a graphical person interface or gooey and it would not genuinely count for now, our focal point is totally on constructing the engine for this game. So let's see how this works. When we run this, we get this little image here, and our application is ready for us to enter a command. If you kind assist both a decrease case or top case we get the listing of instructions that our application or our recreation presently supports. So we can kind the begin command to begin our car, we can kind cease command to quit our car, and cease to terminate the game. Any different instructions that we kind our application is going to inform us hi there I do not recognize that. For example, if I kind asd here, it is going to say I do not recognize that.If you kind start, we get this message, vehicle started, prepared to go, if you kind cease it says auto stopped, and sooner or later if we hit cease our software terminates, this is a notable workout for you to exercise wha you have learned, so pause the video and spend 5-10 minutes to construct this program. Alright,
we're going to begin with a whilst loop with a circumstance What is our circumstance here? We choose to run this loop till the person kinds quit. So we can outline a variable or a story to command what the consumer enters, and then we can run this loop as lengthy as the command does no longer equal to quit. So proper earlier than the loop, we outline a variable, command, and at the beginning we set it to an empty string.We solely have the quotes. So then we kind out our circumstance as whilst command does no longer equal to stop then do something. Now right away we have a trouble right here due to the fact we're assuming that the person kinds the command in lower case, so if they kind this in higher case they our application is no longer going to behave properly, so to restoration this problem, you want to name the decrease approach of the string object and then evaluate the results. With this quit.
You should additionally name this higher and then kind cease in top case. It's about our private choice in this demo I'm going to use decrease case characters. So, okay, now in this loop we want to ask the consumer to enter a command. So as soon as once more we're going to use our enter function, we're going to add a larger then image accompanied by using a space, some thing the consumer enters, we get it and shop it in our command variable. Now aside from cease command, there are three different instructions that we want to support. Start, stop, and help. So right here we want an if announcement to evaluate what the person enters with one of the supported commands. So, if command.lower equals begin then you favor to print the message like the auto started. So print the auto started. Ready to go, it would not matter, now the 2d condition. What if its now not start, possibly it is stop? So, el if command.lower equals stop, there you go, then we print a specific message auto stopped. Now appear at our code.
We have repeated this decrease decrease decrease more than one times. This is bad, in programming we have a time period referred to as dry, which is quick for do not repeat yourself. So on every occasion you have replica your code that ability you are doing some thing wrong. So how can we resolve this problem. Well, alternatively of calling the decrease technique in every condition, we can name it proper right here when we get the enter from the user, so this enter characteristic as you recognize returns a string, we can right away name the decrease technique on this string, and with this command will usually be in lowercase, so we do not want to name this approach in each and every condition. Look, we put off the duplication and additionally our stipulations are shorter and simpler to read.
There is additionally one greater area we want to regulate so, it is proper here. That is better. Now, the 0.33 command. We want one extra el if. If the command equals help, then, we desire to exhibit the instructions that we support. So, right here we're going to print a multi line string. So we use triple fees like this, and provide the person a guiding principle like this. So begin to begin the vehicle quit to quit the car, and end to quit. Now eventually we want an else part, so if what the consumer enters is none of these commands, we're going to inform them, howdy we don't apprehend these, else, colon print sorry I do not recognize that. And by way of the way notice that right here turns into I'm the usage of double quotes, I can without difficulty use a single quote as an apostrophe, okay? So let's run our application up to this factor and see what happens.
Alright, let's kind begin auto is started, beautiful, stop, automobile is stopped, help, we get this guideline, however there may be so plenty indentation earlier than our commands, we will restore that in a second. And eventually let's check the give up command, oops, our application did not work properly. Here is the reason. With these if statements, we're evaluating the command with begin stop, and help. Anything else will cease up here, so it's why our application says it does not apprehend that. So it really is why our application says it would not recognize that command. However, after this el declaration the manage will be moved to the commencing of the loop. At this factor our command is quit, so our loop will whole and the application terminates.
In different phrases when we run this application and kind quit, our software in reality quits however we nonetheless see this message which should not show up here. How can we remedy this problem? Well, we can come lower back right here and simply earlier than the else block, add every other el if, something like this. el if command equals stop then you can straight away break. This will remedy our problem, however be aware that we have type of repeated this expression in two places. The truth is that we do not definitely want this situation on the top, due to the fact with these if statements properly extra precisely with this el if we can leap out of this loop and terminate our program. So, we can simplify our situation to some thing like this. True.
Post a Comment