Python – Lambda Function

Lambda Function in Python

Lambda function is a small anonymous function. These functions are called anonymous because they are not declared in the standard manner by using the def keyword. A lambda function can take any number of arguments, but can only have one expression.

Syntax

lambda arguments expression
Python Lambda Function Tutorial

In Lambda Function the expressions are executed and result is returned.

Single Argument Lambda Function:

x = lambda a : a +10
print(x(5))

Multiple Argument Lambda Function:

x = lambda a, b : a * b
y = x(5,6) # y stores return value
print(y)
Filed under: Python

Python – Functions

Function is a block of code that works on variable input and produce output. Functions provide better modularity for your application and a high degree of code reusing.

Features of Functions

  • A function is a block of code which only runs when it is called.
  • You can pass data, known as parameters, into a function.
  • A function can return data as a result.

Creating a Function

In python def keyword is used to define a function

def myFunction():
	print("Hello from function")

Calling a Function

To call the particular defined function use the name of the function followed by ().

# Calling a Function
myFunction()

Passing Parameter to Functions

You can pass data, known as parameters, into a function.

# Passing Parameters
def myFunction1(fname="India",message="How are you?"):
	print("Hello",fname,message)
myFunction1("Huzaif","How are you?")
myFunction1()

Return values from Function

A function can return data as a result.

# Return Value
def myFunction2(num):
	return 4 * num

x = myFunction2(5) # returned value store in x
print(x)
Filed under: Python

Python – Date Time

Python Date Time can be used in Python by importing Datetime Modules. We can also work with Calendar using Calendar Module.

Importing Date Time and Calendar Modules

import datetime
import calendar

Python DateTime

Once we have imported modules then we can work on date, time and calendar in Python

import datetime
x = datetime.datetime.now()
print(x)

If you want specific Year, Month, Day then

x = datetime.datetime.now()

print(x)
print(x.year)
print(x.strftime("%B"))
print(x.strftime("%A"))

Create DateTime Object or Variable

For creating user defined datetime object use datetime module name. For Example – 

y = datetime.datetime(2020,8,19)
print(y)

Python Calendar

We can work with calendar using calendar module. For printing specific month of the year. For Example:

cal = calendar.month(2017,2)
print(cal)
Filed under: Python

Python – Loop Control Statements

The Loop control statements change the execution from its normal sequence. It can be used with While Loop and For Loop

Types of Loop Control Statements

There are three types of statements:

  1. Break Statement
  2. Continue Statement
  3. Pass Statement

Break Statements in Python

With the break statement we can stop the loop before it has looped through all the items:

While Loop Break Statement

i = 0
while i < 6:
  i += 1 
  if i == 3:
    break
  print(i)

For Loop Break Statement

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x) 
  if x == "banana":
    break

Continue Statement in Python

With the continue statement we can stop the current iteration of the loop, and continue with the next:

While Loop Continue Statement

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x) 
  if x == "banana":
    continue

For Loop Continue Statement

i = 0
while i < 6:
  i += 1 
  if i == 3:
    continue
  print(i)

Pass Statement in Python

The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.

The pass statement is a null operation; nothing happens when it executes. The pass statement is also useful in places where your code will eventually go, but has not been written yet

for letter in 'StudyGyaan': 
   if letter == 'G':
      pass
      print ('This is pass block')
   print ('Current Letter :', letter)

Python Iterator

Iterator is an object which allows a programmer to traverse through all the elements of a collection, regardless of its specific implementation.

A generator is a function that produces or yields a sequence of values using yield method.

list1 = [1,2,3,4]
iter1 = iter(list1) # this builds an iterator object
print ("\n",next(iter1)) #prints next available element in iterator
print (next(iter1))
print (next(iter1))
print (next(iter1))
Filed under: Python

Python – For Loop

Python Loops

In loops, the statement are executed sequentially till the condition satisfied. The first statement in a function is executed first, followed by the second, and so on. Using Loops we can executed block of code for several times.

Python have two types of loops

StudyGyaan- Python tutorial - For loop

Python For Loop

for loop is used for iterating over a sequence. Sequence can be String, Lists, Tuples, Dictionary.

# List Sequence Exaple
fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)

# String Sequence Example
for x in "StudyGyaan":
print(x)

Range () function in Python

The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.

for x in range(6):
print(x)
Filed under: Python