DevScript Saga

Welcome to DevScript Saga!

A little Helping Hand for Devs

Actually Python is much vast. As we are dealing with basic Python, these tutorials are sufficient for one to program in Python. Today we will discuss remaining points like Python Lamba functions, Pass keyword and how to send emails using Python.

Python Lambda Functions

Python allows us to not declare the function in the standard manner, i.e., by using the def keyword. Rather, the anonymous functions are declared by using lambda keyword. However, Lambda functions can accept any number of arguments, but they can return only one value in the form of expression.

The anonymous function contains a small piece of code. It simulates inline functions of C++, but it is not exactly an inline function.

Syntax: lambda arguments : expression     
 Example 1:  
x = lambda a:a+10  # a is an argument and a+10 is an expression which got evaluated and 
#returned.  
 
 print("sum = ",x(20))  

#output:
 sum =  30 


#Example 2: Multiple arguments to Lambda function
 x = lambda a,b:a+b 
# a and b are the arguments and a+b is the expression which gets 
#evaluated and returned.
   
print("sum = ",x(20,10))   

#output :
  sum =  30
 

  Why use lambda functions?

The main role of the lambda function is better described in the scenarios when we use them anonymously inside another function. In python, the lambda function can be used as an argument to the higher order functions as arguments.

Python Pass

In Python, pass keyword is used to execute nothing; it means, when we don’t want to execute code, the pass can be used to execute empty. It is same as the name refers to. It just makes the control to pass by without executing any code. If we want to bypass any code pass statement can be used.

Syntax:
pass  
# Example
 
for i in [1,2,3,4,5]:  
    if i==3:  
        pass  
        print "Pass when value is",i  
    print i,  

 #Output:
 >>>   
1 2 Pass when value is 3  
3 4 5  
>>> 

Python Sending Email using SMTP:

Simple Mail Transfer Protocol (SMTP) is used as a protocol to handle the email transfer using python. It is used to route emails between email servers.

Python provides smtplib module, which defines an SMTP client session object used for the purpose of sending emails to an internet machine. For this purpose, we have to import the smtplib module using the import statement.

The SMTP object is used for the email transfer. The following syntax is used to create the smtplib object.

#Syntax
 import smtplib    
smtpObj = smtplib.SMTP(host, port, local_hostname)    

It accepts the following parameters.

  • host: It is the hostname of the machine which is running your SMTP server. Here, we can specify the IP address of the server or localhost. It is an optional parameter.
  • port: It is the port number on which the host machine is listening to the SMTP connections. It is 25 by default.
  • local_hostname: If the SMTP server is running on your local machine, we can mention the hostname of the local machine.

The sendmail() method of SMTP object is used to send the mail to the desired machine. The syntax is given below.

smtpObj.sendmail(sender, receiver, message)  

Example:

 import smtplib  
 sender_mail = 'sender@fromdomain.com'  
 receivers_mail = ['reciever@todomain.com']  
 message = """From: From Person %s 
 To: To Person %s 
 Subject: Sending SMTP e-mail  
 This is a test e-mail message. 
 """%(sender_mail,receivers_mail)  
 try:  
    smtpObj = smtplib.SMTP('localhost')  
    smtpObj.sendmail(sender_mail, receivers_mail, message)  
    print("Successfully sent email")  
 except Exception:      
print("Error: unable to send email")  

Sending email from gmail :

There are the cases where the emails are sent using gmail SMTP server. In this case, we can pass gmail as the SMTP server instead of using localhost with the port 587.

Use the following syntax.

$ smtpObj = smtplib.SMTP("gmail.com", 587)   

Here, we need to login to the gmail account using gmail user name and password. For this purpose, the smtplib provide the login() method which accepts the username and password of the sender.

Its the end of our 7 days of Python.Now its your turn to practice for programs. So keep calm and love Python Programming. Learn new things . We will be meeting soon with new tutorial series or blog. Keep visiting Downloads section of TechNext for placement papers. If you wish for any study material , then mail us or mention in comment box. Till then Good Bye. Happy Coding.

Remember: You are programmed unless you program a program for yourself.

Leave a comment