Conditional statements and methods in python

Spread To Friends

Today we are going to learn how to implement conditional statements and methods or functions in Python. 

 

In the previous tutorial we learnt about variables and data types. If you didn’t read that then follow the link below and learn that carefully because in every step you have to use variables and data types in your program. 

 

So without any further delay let’s continue with today’s tutorial. 

 

What are conditional statements ?

 

a conditional statement means some conditions to be implemented on programs it can be if and else. In real life we also use this type of conditions, because programming is based in real life so in programming there also are conditional statements in programming even they play a heavy role on programming without conditional statements a program cannot be completed. These conditional statements are in Python also . In every Programming language there should be conditional statements present mostly it is if , else if and else. In Python if and else are the same but instead of else if there is elif   though the words are different but the meaning and the word functionalities are exactly the same as other languages . There would be a lot of work where we have to to work on conditional statements like if and else. The syntax to write conditional statements in Python are below:

 

  1. You have to use an expression after writing if.
  2. The second step is to come in the next line and leave an indent . That’s the basic Syntax of python to enter in any expression.
  3. In the elif statement you have to use exactly the same as you did before or for the if statement.
  4. What is statement you don’t have to use any expression it will default recall if the if statement and the elif statement was not called

 

A basic syntax of conditional statements is given below:

 

i = 10

if i < 10:

print(“i is less than 10”)

elif i == 10:

print(“i is equals to 10”)

else i > 10:

print(“i is greater than 10”)

 

Here in the elif statement we used this “== “ , this operator refers the checking functionality , means when we have to assign any number to any variable then we use the single equal but when we need to check the variable, if there is the variable we want, there we use the double equal operator.

 

One thing I want to say about try and except statements. The try and except statements are similar to the conditional statements though that the try and except statement is also a kind of conditional statement but the same as if , else at all. The conditional statements and the try except statements are completely different from each other as their work functionalities are also different even it is far different than if , else conditional statements. So let’s learn about try , except statements , also to clarify the confusion. I think you have a question in your mind about where we will use the try except statement right ?

 

Let me clarify for you… 

 

So You can easily understand what try and except means , if you don’t then I will clarify for you now. The try statement represents the trying expression. Means it is saying that try something… here something is , what you want to try. 

And except means if the try expression fails to execute then the accept operator will be called immediately and automatically to execute and the error can be shown or any work process will be done based on your need so that’s the concept of try except blocks in Python.

 

 It can be changed in terms of other languages like Java there try is available but instead of except block there catch is present you can also print the error which is coming in the try statement, anyway we’re talking about Python language so we don’t want to go to other languages right now . 

 

So that’s it about try except block I think that you will never be confused about this try except block and if else statements. One more thing that if you don’t want to execute anything in if or elif or else statements while it is true then you can use the pass keyword. It will pass the statement from the conditional statement. In this way if you don’t want to execute anything while something is true or false then you can use this keyword to pass the execution state of elif or else statements.

 

Now let’s talk about methods or functions in Python:

 

Functions or methods are used to separate multiple works in a program. The use of methods are extremely high in every programming language. 

 

These methods or functions are the most used components in programming language. Methods provide us to develop multiple works in a single program with separation .

 If another person reads the program he can easily understand the words of each function or method. You can assign any name to the function. But it is recommended that the function should be named as its allocated work so that everyone can understand what the function does even you also can understand it when you come back to your program after a while. The methods could not automatically Run when you run the whole program. You have to call all the function in your program to run the function or method. Because methods are separated from the real program so the variables which were declared in the program are also separated from the function but if you want to access those variables in the function then you have an option to do that. Option is to pass variables through the method so that you can access the variables in the function and you have to receive those variables in your function which you have declared when you call the function in your program. A basic function is is mentioned below:

 

a = 2

b = 4

def createsum(a,b):

c = a+b

print(“the answer is: “+str(c))

createsum(a,b)

 

You can also do one thing with function that if you want something from the function then you can call return in your function then it will return something that you want to return to your main program from the function. One example for this kind of function is mentioned below: 

 

a = 2

b = 4

def createsum(a,b):

c = a+b

return(c)

 print(createsum(a,b))

 

you can copy and paste these codes to your console and can run it easily or you can also write yourself Too.

 

That’s all for now , see you in the next interesting tutorial , until then stay tuned with me and don’t forget to get signed up .

 

Thank You,

 

Other posts: Python Loops and Methods

close
Oval@3x 2

Don’t miss these updates!

We don’t spam! Read our privacy policy for more info.


Spread To Friends

One thought on “Conditional statements and methods in python

  • November 7, 2020 at 9:36 am
    Permalink

    Spread this post to your every friend so that they can access this.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *