13
Jun

nth Line of Pascal’s Triangle

Question:

Write a program to print the nth line of the Pascal’s Triangle

Program:

Explanation:

Pascal’s triangle is a triangle where each number is equal to the sum of the one or two numbers above it:

Output:

12
Jun

Print squares of numbers till 15 (lambda, map, list comprehension)

Question:

Write a single line of code to print squares of numbers till 15 using lambda, map and list comprehension

Program:

Explanation:

The lambda operator or lambda function is a way to create small anonymous functions, i.e. functions without a name. These functions are throw-away functions, i.e. they are just needed where they have been created. Lambda functions are mainly used in combination with the functions filter(), map() and reduce().

The map function is the simplest one among Python built-ins used for functional programming. These tools apply functions to sequences and other iterables. The filter filters out items based on a test function which is a filter and apply functions to pairs of item and running result which is reduce.

So here the output of the anonymous function for squaring x when x ranges from 1 to 15 is recieved.

Output:

11
Jun

Program using Lambda Function

Question:

Consider variable  x. do the following operation. Add 2 to x and square the result. Again add 3 to x and square the result. Do using anonymous functions in python

Program:

Explanation:

The lambda operator or lambda function is a way to create small anonymous functions, i.e. functions without a name. These functions are throw-away functions, i.e. they are just needed where they have been created. Lambda functions are mainly used in combination with the functions filter(), map() and reduce().

Output: