Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Tuesday, 20 September 2022

How Indentation works in Python

 Lets say we want to write a python program to check whether a variable has a value 10 or not.


Code : 

var = 10

if (var == 10):

print(' A is 10')


Error :

File "<ipython-input-53-61659dc01be1>", line 3

    print(' A is 10')
    ^
IndentationError: expected an indented block

>> As we can see above we get the above error. This means the print statement is not as per expected indentation. Lets see if put it in the right way like below :

var = 10
if (var == 10):
  print(' A is 10')

In above code i try to shift/move the print statement a bit towards right side with a couple of spaces it could also be possible with tab and it works fine now.

 A is 10
In [ ]:

It prints the expected statement now as above we can see the actual cell is displayed .


Wednesday, 15 August 2018

Lambda function in Python

To understand Lambda function we need to see a normal function in Python first, Lets begin:

Lets define a function first and then we will convert it to Lambda function

-----------------------
def add(x, y):
    return x + y

print(add(2, 3))
-----------------------

Above function name is add, it expects two arguments x and y and returns their sum. Notice how we are printing and calling add function in last line

The output of above program is below:

-----------------------
5
-----------------------

# Now let us convert this function to Lambda function in Python

----------------------------------
add = lambda x, y : x + y

print (add(2, 3))

----------------------------------

The output of above code is below :

--------------
5
--------------

So what on earth is Lambda then ?

Basically lambda function is used for creating small, one-time and anonymous function objects in Python. In lambda x, y: x + y; x and y are arguments to the function and x + y is the expression which gets executed and its values is returned as output. Lambda functions can be used wherever function objects are required. They are syntactically restricted to a single expression. Semantically, they are just syntactic sugar for a normal function definition. Like nested function definitions, lambda functions can reference variables from the containing scope.



Function in Python

We will now write a function in Python :

-----------------------
def add(x, y):
    return x + y

print(add(2, 3))
-----------------------

Above function name is add, it expects two arguments x and y and returns their sum. Notice how we are printing and calling add function in last line

The output of above program is below:

-----------------------
5
-----------------------

Python if Statement

Lets write a Python code where we see how if statement is used

-----------------------------------
num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")

-----------------------------------

The output of above code will be below

-----------------------------------
3 is a positive number.
This is always printed.
This is also always printed.
-----------------------------------

Explanation of the code :- Watch how we use num variable 3 and print it as positive, notice the second time positive is not printed because -1 is negative not > 0

The range() Function in Python

Lets use Range function of Python to write below code to print range of numbers between 0 to 4

------------------------------
for i in range(5):
    print(i)

------------------------------

The output of above code will be :

------------------------------

0
1
2
3
4
------------------------------

Now lets use Range function again of Python to see another scenario where 10 is not printed in the range of 5,10 but 5 is printed so it includes first number not the last
------------------------------

for i in range(5,10):
     print(i)

------------------------------

The output of above code is below

------------------------------
5
6
7
8
9

------------------------------

The given end point is never part of the generated sequence; range(10) generates 10 values, the legal indices for items of a sequence of length 10. It is possible to let the range start at another number, or to specify a different increment (even negative; sometimes this is called the ‘step’):

So lets take a scenario of below code :

---------------------------------

for i in range(0,10,3):
    print(i)
---------------------------------


The output of above code is 

------------------------------
0
3
6
9
------------------------------