Python for newbies part 2

Conditions in Python

Hello everyone, today we're going to continue python programming, in the previous blog post (~_^) we saw how to install python,
Pycharm, how a program works in a simple way and what a programming language is, variables and a lot of little things.
I want this series of blog post to be clear and simple,
to know how to program basically without having to watch or read many hours of videos and tutorials.
In this blog post i'm going to talk about the conditions that allow us to refine the programm that we created in the previous blog post.
For practical reasons, we will start from a program adapted to everyone and not only to the welders with the example of the coated electrodes.

How do the python conditions work?

Python for dummies part two, Blog post for Youtube channel of Wallid Guergour



If the weather is good,
then I go out
Otherwise I stay at home


This structure we will find in our code and it doesn't matter which programming language we use. So we will see the structure of all this in python. To begin, we're going to create two variables, the first one our wallet, wallet to which have assigned as values for example 3000 dollars, We can go to the line then create the second variable which would be the price of an object, here a computer, computer_(underscore)price, if you followed the previous blog post you will have understood why an underscore is used here. The value of the computer is 1200 dollars.

Python for dummies part two, Blog post for Youtube channel of Wallid Guergour

Then we will check if the price of the computer is less than 1500 dollars.
The price of the computer corresponds to the variable "computer_price" then I want to check if with print(computer_price < 1500)

Python for dummies part two, Blog post for Youtube channel of Wallid Guergour

The price is less than $1500.

Python for dummies part two, Blog post for Youtube channel of Wallid Guergour


So as in mathematics, I will use the sign strictly above or strictly below, or even two == to check for a perfect equality This is called a Boolean, which returns true or false. So we're going to start by looking to see if it's inferior in this way, If I make a print of all this, it will return true.

First we will get the verification we want to do, then we used the keyword (if) which will allow us to start the condition. So, if (small space) we are going to paste here the verification (computer_price < 1500 and to start the block of condition we put two small point like this (:) If I go to the line now, I could carry out a series of instructions, if this condition is true. For example, print ("computer price is under 1500$").

Python for dummies part two, Blog post for Youtube channel of Wallid Guergour

I start the program and you can see that the message is displayed.

Python for dummies part two, Blog post for Youtube channel of Wallid Guergour

Now what will happen if I change the value of the computer price to 1600$? You agree with me that here the condition will no longer be good, therefore we could create an "else" just below, You open the conditional block as usual and can do something else, for example, print( No, the price is more than 1500$)

Python for dummies part two, Blog post for Youtube channel of Wallid Guergour

Now I launch the program, and have this result displayed, what is important to remember is that they have checked if the price of the computer is less than 1500 if it is the case they have carried out an instruction otherwise we will do other things, for example display a message.

Python for dummies part two, Blog post for Youtube channel of Wallid Guergour

What is interesting is this sign, with, we can check if it is lower <, higher > or a perfect equality ==. You can check if the price of the computer is different by putting for example != (exclamation point and equal) You could display here "No the price is not 1500$" and here "Yes the price is 1500$". I start the program. And have to well, no the price is not 1500$.
Python for dummies part two, Blog post for Youtube channel of Wallid Guergour
Python for dummies part two, Blog post for Youtube channel of Wallid Guergour
And if now I set the value 1500, the program will see that the first condition is not true and will therefore take refuge in the second condition.

For example we could check here if the price of the computer is much lower or equal to the "wallet" variable, but if the person with 1500$ can still buy it, well it's not very reasonable since he or she will have 0$ at the end but well.. We will put here, if computeur_price <= wallet: For example here I can very well take the value of "wallet" and take away the price of the computer and reassign it to the variable wallet, wallet = wallet - computer_price Python for dummies part two, Blog post for Youtube channel of Wallid Guergour
For those who want to do it even faster there is a trick, you can do wallet -= computer_price Python for dummies part two, Blog post for Youtube channel of Wallid Guergour
If this condition is false then we will switch to this condition "else" with as answer, print("The purchase is impossible, you only have " ~here I will inject the value of the person's wallet~ + srt(wallet)) And lastly, it will display the wallet after the purchase. print("wallet") Python for dummies part two, Blog post for Youtube channel of Wallid Guergour
Now we can also make several checks for the same condition, here we have to check if we could buy the computer with the value of "wallet" but we could also refine all this, with the keyword "and" and check in addition to that if the price of the computer is more than 1000$, our program will first check this and then go on here. Python for dummies part two, Blog post for Youtube channel of Wallid Guergour
Python for dummies part two, Blog post for Youtube channel of Wallid Guergour
And if both are true it will be able to execute the different instructions.
next update I will explain how to make a lists with Python.