Monday, 1 August 2016

If Else block and Functions in Python

Just like every other language python has an if else block but instead of using the curly braces, we use indentation of 4 spaces to create a code block.

Here's a simple if else block in Python -

It prints out the greater number by comparing the variables a and b. And yes that's how you define variables in Python. Just type the name of the variable and assign a value, Python will determine it's datatype all by itself.

What if I want to create a function which accepts two numbers, compares them and prints the greater number? We can do that. Let's define a Python function!

All we did there was put our previous code inside the function block (by indenting it).

Returning Values from the Function


We can also return values using a return keyword instead of printing them inside the function. I've done that in the example below -

That's all about Python's If Else Block and Functions :)

No comments:

Post a Comment