© Sangeeta M Chauhan, Gwalior

Slides:



Advertisements
Similar presentations
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Advertisements

 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
CIS Computer Programming Logic
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
An Introduction to Python Blake Brogdon. What is Python?  Python is an interpreted, interactive, object-oriented programming language. (from python.org)
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
By James Braunsberg. What are Modules? Modules are files containing Python definitions and statements (ex. name.py) A module’s definitions can be imported.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 5 Function Interfaces 4/18/09 Python Mini-Course: Day 2 - Lesson 5 1.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Python Functions.
Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new.
P YTHON ’ S C LASSES Ian Wynyard. I NTRODUCTION TO C LASSES A class is the scope in which code is executed A class contains objects and functions that.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
Python Let’s get started!.
LECTURE 2 Python Basics. MODULES So, we just put together our first real Python program. Let’s say we store this program in a file called fib.py. We have.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
REEM ALMOTIRI Information Technology Department Majmaah University.
Functional Programming
Lesson 06: Functions Class Participation: Class Chat:
Chapter 9: Value-Returning Functions
Topic: Functions – Part 1
Topics Introduction to Functions Defining and Calling a Void Function
3 Introduction to Classes and Objects.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Lecture 2 Python Basics.
Python Let’s get started!.
Introduction to Python
COMPSCI 107 Computer Science Fundamentals
Topic: Functions – Part 2
JavaScript: Functions
Functions CIS 40 – Introduction to Programming in Python
Functions.
JavaScript: Functions.
Functions in C Mrs. Chitra M. Gaikwad.
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
CHAPTER FOUR Functions.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Chapter 3 Simple Functions
Python Primer 2: Functions and Control Flow
Functions, Procedures, and Abstraction
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Functions, Part 1 of 3 Topics Using Predefined Functions
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Lesson 06: Functions Class Chat: Attendance: Participation
ERRORS AND EXCEPTIONS Errors:
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Python Functions Part I Presented by : Sharmin Abdullah
Topics Introduction to Functions Defining and Calling a Void Function
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
G. Pullaiah College of Engineering and Technology
Topics Introduction to Functions Defining and Calling a Function
Functions, Part 1 of 3 Topics Using Predefined Functions
CSC1018F: Intermediate Python (Tutorial)
COMPUTER PROGRAMMING SKILLS
Functions, Part 1 of 3 Topics Using Predefined Functions
CISC101 Reminders Assignment 3 due today.
ITM 352 Functions.
def-ining a function A function as an execution control structure
Functions, Procedures, and Abstraction
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Python Basics. Topics Features How does Python work Basic Features I/O in Python Operators Control Statements Function/Scope of variables OOP Concepts.
Functions John R. Woodward.
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior FUNCTIONS IN PYTHON By : Mrs Sangeeta M Chauhan , Gwalior 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior By: Sangeeta M Chauhan

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior FUNCTIONS IN PYTHON Def Function is a piece of code written to carry out a specified task. You can Pass Data(input) known as parameter to a function A Function may or may not return any value(Output) 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

There are three types of functions in Python Built-in functions The Python interpreter has a number of functions built into it that are always available. They are listed here in alphabetical order. User-Defined Functions (UDFs): The Functions defined by User is known as User Defined Functions. These are defined with the keyword def Anonymous functions, which are also called lambda functions because they are not declared with the standard def keyword 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior Functions vs Methods : A method refers to a function which 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. 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Structure of Python Function def functionname ( parameters ): "function_docstring“ function_body return [expression] 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior DOC STRING Python Docstring is the documentation string which is string literal, and it occurs in the class, module, function or method definition, and it is written as a first statement.  Docstrings are accessible from the doc attribute for any of the Python object and also with the built-in help() function can come in handy. 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

After execution of the program/function >>> Info.__doc__ 'This Function is to display info about author.' 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Without execution of Program/function >>> use_doc.Info.__doc__ 'This Function is to display info about author.' >>> 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Example of Function Definition Example for Creating a Function without parameter In Python a function is defined using the def keyword: >>> def MyMsg1():    print("Learning to create function") 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Example of Function Definition Example for Creating a Function with parameter >>> def MyMsg2( name ): "This prints a passed string into this function" print (name ,” is learning to define Python Function”) return 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Calling function MyMsg1 () To call a function, use the function name followed by parenthesis: >>> MyMsg1() Output : >>> Learning to create function Calling Function MyMsg2() twice with different parameter >>> MyMsg2(‘Divyaditya’) >>> MyMsg2(‘Manasvi’) Output Divyaditya is learning to define Python Function Manasvi is learning to define Python Function Parameters 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior By: Sangeeta M Chauhan

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

?????Arguments Vs Parameters 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior Function Arguments 1. Default 2. Positional 3. Keyword 4. Arbitrary(Variable no.) 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Variable no. of arguments 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Global and Local Variables in Python a=50 # Global Variable def MyFunc(): b=100 # Local Variable print("Function Called :",a) print("Function Called :",b) >>>MyFunc() # Function Call Output >>> Function Called 50 >>> Function Called 100 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Local and Global Variable…….. 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior The globals() method returns the dictionary of the current global symbol table. A symbol table is a data structure maintained by a compiler which contains all necessary information about the program 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior MOST USEFUL BUILT-IN FUNCTIONS 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Syntax :Filter(function, sequence) Returns an Object containing sequence of the items from the sequence for which function(item) is true Syntax :Filter(function, sequence) 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior By: Sangeeta M Chauhan

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior Example Computes primes up to 20 output 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Ex: map (factorial, list) Calls function(item) for each of the sequence’s items Syntax: map(function, sequence) Ex: map (factorial, list) 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Example of Map function Computes the factorial of given values output 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

(Anonymous Functions) Python lambda (Anonymous Functions) A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression. Syntax lambda <arguments> : <expression> 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Example 1 of lambda function output 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Example 2 : lambda Function output 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Example 3: lambda Function output 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

Calling of functions with ternary operator. OUTPUT 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

# Function argument unpacking output 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior Output 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior

www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior Thanks 10/23/2019 www.pythonclassroomdiary.wordpress.com © Sangeeta M Chauhan, Gwalior