Conditional branching and functions in Python. In this video, we will learn conditional branching and functions in Python. Finally, we will create a simple calculator project. Most of the programs are more than just a simple list of commands to be run over and over again. programs can make decisions and execute some commands only if certain circumstances apply. They do this using a special Python keyword called if, and it's optional counterpart else.
In some cases, you will need extra conditions in between if and else. In that case, we use the key word as if. To understand this, we will create a simple program that accepts two numbers as inputs and checks whether the first number is greater than or equal to or less than the second number. Open the Tony ID and load the conditional statement. P wave file from the downloaded repository. Now run the script.
First, I'm going to enter x as five and y as seven, the output shows x is less than y. Let's test one more condition before moving to the code explanation. Entering x and y is equal to phi returns an output x is equal to y in the code. First, we prompted the user to enter the numbers X and Y using the input function. We then store the values after typecasting them to an empty job. Next, the key word if followed by the condition x greater than y is given.
This means that only if the condition is true, any statements inside the F will work. Here, the output is obviously the string x greater than y. As there are multiple conditions involved, we need to use the LCD condition in between. In this case, we need to check for the equality condition. Please remember that x equals y and x double equals y are different, while the former assigns x to y. The later checks whether x and y are the same and return either true or false.
Finally, we use the else keyword. Note that here we haven't put any conditions for this. Please remember to put the colon and proper intends for an error free program. A function is a block have organized reusable code that is used to perform a single related action functions provide better modularity for your application and a high degree of code reuse. As you already know, Python gives you many built in functions like print, etc. But you can also create your own functions the store functions are called user defined functions.
There are simple rules to define a function in Python. function blocks begin with a keyword depth, followed by the function, name and parenthesis. Any input parameters should be placed within these parenthesis. You can also define parameters inside these parenthesis. The code block within every function starts with a colon and is intended. The statement written of expression exists a function, optionally passing back an expression to the caller.
A return statement with no arguments is the same as return none. Thus, the syntax of a function definition will look like this. defining a function only gives it a name specifies the parameters that are to be included in the function and structures the blocks of code. Once the basic structure of a function is finalized, you need to call the function in order to execute it. Open a new blank script and type the following def my underscore function print of inside codes Hello from a function that was Save and run the script, you won't be getting any output as there is no function call. Thus add my underscore function at the end of the code and rerun it to get the output.
To learn more about functions, please check out the links in the resources section. A project is always the best to understand the implementation of concepts. So now we will create a simple calculator with all the programming concepts we learned till now. Open the simple calculator.pi file from the download repository in Tony ID and run it select the multiply operation but entering three Now enter the first number as 30. And second as to you should get an output 30 as just two equals 60. Now, let's check out the code.
For each mathematical operation. We have defined separate functions. Each function accepts two parameters as arguments. They are the first and second numbers. Each function uses the return keyword to give back the result of the operation. Next, we use the print function to print out the options.
It then takes in the input from the user. Next, we have to collect the numbers. Here we need to typecast string to an integer so that we can later use the numbers to do mathematical operations. Next, we created a conditional branching for each operation. The condition was checked with a double equal to the option Later. In the end, we also added an else statement to prompt the user that the input is invalid summary.
In this video, we covered the following topics, conditional branching functions and Python, a simple calculator project. In the next video, we will learn about modules in Python.