Quantcast
Channel: Cloud Training Program
Viewing all articles
Browse latest Browse all 1891

Python Methods and Functions Q&A: Day 4 Live Session Review

$
0
0

This blog will cover the Q&As of Python for Data Science (AI/ML) and Data Engineer Training covering Python Methods and Functions Q&A: Day 4 Live Session Review This blog will help you to clear your concepts with Methods and Functions.
We also covered hands-on Lab 11Lab 12Lab 13Lab 14, and Lab 15 out of our 25+ extensive labs.

The previous week, In Day 3 Live Session, we have covered the basic concepts of Loops and Control Statements.

So, here are some of the Q & As asked during the Live session from Module 4: Methods and Functions.

Functions in Python

A Function is a sequence of statements and instructions that performs a particular task. A function is like a black box that can take certain input(s) as its ​parameters​ and can output a value after performing a few operations on the parameters. A function is created so that one can use a block of code as many times as needed just by using the name of the function.
Functions allow us to encapsulate lines of code to create custom processes that can be reused anywhere throughout the script.
In Python we have three types of functions:

  • Built-in functions, such as help() to ask for help, min() to get the minimum value, print() to print an object to the terminal, You can find an overview of more of these functions here.
  • User-Defined Functions, Functions that are defined by the users. Eg. The add() function we created.
  • Anonymous functions, which are also called lambda functions because they are not declared with the standard def keyword.

Q1. What are functions in Python?
Ans: Python function is a block of code that is executed only when it is called. To define a Python function, the def keyword is used def is a keyword, not a command. Python functions are neither pass by reference nor pass by value, they are pass by the object.
Example:

functions

Q2. How to define a Function: User-Defined Functions (UDFs)?
Ans: The four steps to defining a function in Python are the following:

  1. Use the keyword def to declare the function and follow this up with the function name.
  2. Add parameters to the function: they should be within the parentheses of the function. End your line with a colon.
  3. Add statements that the functions should execute.
  4. End your function with a return statement if the function should output something. Without the return statement, your function will return an object None.

User-Defined Function

Check Out: Our blog post on Python Control Statements.

Q3. How To Call A Function?
Ans: Once you have defined a function, you can call it from another function, program, or even the Python prompt. In the previous sections, you have seen a lot of examples already of how you can call a function. Calling a function means that you execute the function that you have defined – either directly from the Python prompt or through another function (as you will see in the section “Nested Functions”).
A ​function call​ takes the following form:

<function-name> (<value-to-be-passed-as-argument>)

Call your newly defined function hello() by simply executing hello(), See below:

Call A Function

Q4. What is a lambda function?
Ans: Lambda Function, is also known as Anonymous function is same as a regular python function but can be defined without a name. This function can have any number of parameters but, can have just one statement.
Example:

lambda function

Q5. What is an inner function?
Ans: A function that is defined inside another function is known as an inner function or nested function. Nested functions are able to access variables of the enclosing scope. Inner functions are used so that they can be protected from everything happening outside the function.

inner function

Read More: About Python Data Types.

Q6. What is Call by Value?
Ans: In call-by-value, the function receives a copy of the argument objects passed to it by the caller, stored in a new location in memory.
You pass values of parameters to the function, if any kind of change is done to those parameters inside the function, those changes are not reflected back in your actual parameters.

Call by Value

Q7. What is Call by Reference?
Ans: In call-by-reference, the function receives a reference to the argument objects passed to it by the caller, both pointing to the same memory location. you pass the reference of parameters to your function. if any changes are made to those parameters inside the function those changes are getting reflected back to your actual parameters.

Call by Reference

Q8. What is a Return Statement?
Ans: A return statement is used to end the execution of the function call and “returns” the result (value of the expression following the return keyword) to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned.
Syntax:

Return Statement

Also Check: Our blog post on Data Science Career Roadmap.

Q9. What is the usage of the help() and dir() function in Python?
Ans: Help() and dir() both functions are accessible from the Python interpreter and used for viewing a consolidated dump of built-in functions.

  1. Help() function: help() is a super useful built-in function that can be used to display the documentation string and also facilitates you to see the help related to modules, keywords, attributes, etc.
  2. Dir() function: dir() shows a list of attributes for the object passed in as argument , without an argument it returns the list of names in the current local namespace (similar to locals().keys() ) .

Functions vs Methods

A method refers to a function that is part of a class. You access it with an instance or object of the class. A function doesn’t have this restriction: it just refers to a standalone function. This means that all methods are functions, but not all functions are methods.

Consider this example, where you first define a function plus() and then a Summation class with a sum() method.

If you now want to call the sum() method that is part of the Summation class, you first need to define an instance or object of that class. So, let’s define such an object:

Functions vs MethodsRemember that this instantiation is not necessary for when you want to call the function plus()! You would be able to execute plus(1,2).

Q10. How do I use strings to call functions or methods?
Ans: There are various techniques to achieve this, but the best way is to use a dictionary that maps strings to functions. With this approach, strings do not need to match the names of the functions.

Read More: About Python Data Visualization.

Q11. What is __init__?
Ans: All classes have a function called __init__(), This method is automatically called to allocate memory when a new object/ instance of a class is created__init__ is a method or constructor in Python.
Here is an example of how to use it.

__init__

Related References

Next Task For You…

Python’s growth is very promising in the near future. Gaining the right skills through the right platform will get you to the perfect job.

We are launching our course Python For Data Science (AI/ML) & Data Engineers (Python For Beginners) which will you help and guide you towards your first steps to Python. Join our Waitlist for the FREE CLASS now.Free Class Python

The post Python Methods and Functions Q&A: Day 4 Live Session Review appeared first on Cloud Training Program.


Viewing all articles
Browse latest Browse all 1891

Trending Articles