Featured Post

SQL Interview Success: Unlocking the Top 5 Frequently Asked Queries

Image
 Here are the five top commonly asked SQL queries in the interviews. These you can expect in Data Analyst, or, Data Engineer interviews. Top SQL Queries for Interviews 01. Joins The commonly asked question pertains to providing two tables, determining the number of rows that will return on various join types, and the resultant. Table1 -------- id ---- 1 1 2 3 Table2 -------- id ---- 1 3 1 NULL Output ------- Inner join --------------- 5 rows will return The result will be: =============== 1  1 1   1 1   1 1    1 3    3 02. Substring and Concat Here, we need to write an SQL query to make the upper case of the first letter and the small case of the remaining letter. Table1 ------ ename ===== raJu venKat kRIshna Solution: ========== SELECT CONCAT(UPPER(SUBSTRING(name, 1, 1)), LOWER(SUBSTRING(name, 2))) AS capitalized_name FROM Table1; 03. Case statement SQL Query ========= SELECT Code1, Code2,      CASE         WHEN Code1 = 'A' AND Code2 = 'AA' THEN "A" | "A

How to Create UDF in Python Example

In Python,user-defined function usage is to avoid repeated work. The UDFs in Python are not like C/C++/JAVA. I am sharing ideas on how to create UDF in Python.



udf in python

Python Syntax for User defined function(UDF)

Below is the good example on Python UDF.
def function_name(list of parameters): 
"docstring" 
statement(s) 
return(parameter)       

Explanation of each keyword 

  1. The keyword def symbolizes the start of the function header.
  2. A function name to uniquely identify it. Function naming follows the similar rules that are used for writing identifiers
  3. List of parameters also called as a list of arguments through which value is passed to the function. The list of parameters is optional.
  4. A colon (:) to mark the end of function header.
  5. Optional documentation string (docstring) is used to describe the purpose of the function, which is slightly similar to python documentation using comment.
  6. Python statements that perform the intended task for which the user-defined function is made. It is mandatory to maintain the indentation level while writing python statements in the function definition.
  7. In the end, an optional return statement is used to return a value (result) from the function. This statement can contain an optional parameter to return the computed result back to the function call. If there is no parameter in the statement or the return statement is not mentioned at the end of function definition then the function returns the None object.

Python Vs Other Languages

Python user defined functions
Python is one of the most popular languages in data analytics. There are many other languages that have an option to create UDFs. Even in SQL of any database, you can easily create user-defined functions.

Advantages of Python User defined Function

  • User-defined functions help to decompose a large program into small segments which make the program easy to understand, maintain and debug.
  • If repeated code occurs in a program. The function can be used to include those codes and execute when needed by calling that function.
  • Programmers working on the large project can divide the workload by making different functions.
References

One practical advice

It is always a good idea to name user-defined functions according to the task they perform.

Also, Read

Comments

Popular posts from this blog

How to Fix datetime Import Error in Python Quickly

Explained Ideal Structure of Python Class

How to Check Kafka Available Brokers