Skip to document
This is a Premium Document. Some documents on Studocu are Premium. Upgrade to Premium to unlock it.

Accenture 3 - Practice notes

Practice notes
Course

python programming (cs8151)

86 Documents
Students shared 86 documents in this course
University

Anna University

Academic year: 2014/2015
Uploaded by:
0followers
4Uploads
0upvotes

Comments

Please sign in or register to post comments.

Preview text

Python Programming

Fundamentals

Function & Packages in Python

Hands-on Exercise

Index

  • 1 Objectives
  • 2 Overview
  • 3 Prerequisites
  • 4 Preliminary Concepts.............................................................................
    • 4 Functions
    • 4 Built-in Functions
    • 4 Defining a Function
    • 4 Examples of Function
      • 4.4 Defining Function
      • 4.4 Calling a Function
      • 4.4 Return Statement
      • 4.4 Parameters
      • 4.4 Docstrings.......................................................................................
      • 4.4 Default Parameter Value
      • 4.4 Python Arbitrary Arguments
    • 4 Local & Global Variables............................................................................
    • 4 Modules and Packages in Python
      • 4.6 name and main
  • 5 Practice Exercise

2 Overview

This document provides information on the functions, parameters, variables, and modules in Python, along with guided and practice exercises to help you understand the concepts. Estimated time needed: 45 minutes Note: The links provided in the document navigate to the respective web sources and open in the same window. The users can use the back arrow in the browser, which navigates back to the beginning of the document.

3 Prerequisites

  1. Visit edX website and check the reference videos on Function
Write the code below

my_name = "I am Tech Robot" len(my_name) Output 15 iii. round

Write the code below

round(234) Output 235 We can explore more built-in functions through the link provided below. The functions are listed in alphabetical order. Built-in Functions — Python 3.8 documentation 4 Defining a Function Def is a keyword that is used to mark the start of the header function. The optional parameters are:

  • Colon is used to mark the end of header functions
  • String documentation, docstring are optional strings
  • One or more Python statements make up the body of the function. Both declarations must have the same degree of indentation (1 tab or 4 spaces)

4 Examples of Function 4.4 Defining Function Guided Exercise # Write the code below def welcomeMessage(name): print("Hi " + name + ", Welcome to Tournamnet") print("End of Tournamnet") welcomeMessage("Mark") Output Hi Mark, Welcome to Tournamnet End of Tournamnet 4.4 Calling a Function Guided Exercise # Write the code below def welcomeMessage(name): print("Hi " + name + ", Welcome to Tournament") print("End of Tournament") welcomeMessage("Mark") print("End of the function") Output Hi Mark, Welcome to Tournament End of Tournament End of the function 4.4 Return Statement The return statement returns a value after evaluating the expression. It will return None if there is no expression or return statement.

Output Hello I am a Wicket keeper Smith Hello I am a Wicket keeper Adam Hello I am a Wicket keeper Mark # Write the code below def my_function(x,y,z): """This function tells the person with the fast pace bowler""" if x > y and x > z: return ("Luis is a fast pace bowler") elif y > x and y > z: return ("Samuel is fast pace bowler") else: return ("John is fast pace bowler") print(my_function(133,110,113)) Output Kapil is a fast pace bowler 4.4 Docstrings

Write the code below

def my_function(x,y,z): """This function tells the person with the fast pace bowler""" if x > y and x > z: return ("Luis is a fast pace bowler") elif y > x and y > z: return ("Samuel is fast pace bowler") else: return ("John is fast pace bowler") print(my_function.doc) Output This function tells the person with the fast pace bowler

4.4 Default Parameter Value Now, let us see how to use a default parameter value. If the function is called without specifying any parameters, it will use the defualt value. Guided Exercise # Write the code below def printbowlerinfo(name,bowling="fast pace"): print ("Name: ",name) print ("bowling: ",bowling) return printbowlerinfo("Peter") Output Name: Peter bowling: fast pace 4.4 Python Arbitrary Arguments The number of parameters sent into a function is not always defined ahead of time. By using function calls, Python allows you to handle this type of circumstance with an arbitrary argument of inputs. Asterisk (*) is used to indicate the arbitrary arguments function description before the name of the parameter. Guided Exercise # Write the code below def greet(*names): """All the persons included in the tuple names will be greeted using this function.""" for name in names: print("Hello",name) greet("Monica","Luke","Steve","John","lynda") Output Hello Monica Hello Luke Hello Steve Hello John Hello lynda

4 Local & Global Variables It is recommended to initialize a variable as global if the same variable used at multiple places in the program or module. For usage only in particular function or method, a local variable should be chosen. Guided Exercise # Type and execute the following code block # Declare and initialize a variable f = 101 print(f) # Using a global variable def someFunction(): #This is the same variable f initialized earlier, and hence is a global variable f= print(f) someFunction() print(f) Output 101 9 101 4 Modules and Packages in Python A module in Python is a file that contains definitions of Python and its statements. The name of a file is a module name with an extension .py. With the support of a global variable, we may refer to the module name. Python modules are used to provide a Python script with a modular approach. 4.6 name and main - 'main' is the name of the scope in which top-level code executes - A module’s name is set equal to 'main' when read from standard input, a script, or from an interactive prompt - A module can discover whether it is running in the main scope by checking its own name

Guided Exercise import math from math import * math(3,4) math([22,333]) Output 81. 355. import matplotlib as plt plt([5,7,8,4]) plt("GRID REPRESENTATION") plt("X-axis") plt("Y-axis") Output

d. Write a function for the palindrome. Create a module mypalindrome and execute in a Python prompt using import # Write your code below Output You have completed Function in Python hands-on exercises in Python Programming Fundamentals.

Copyright © 2021 Accenture All rights reserved. Accenture and its logo are trademarks of Accenture.

Was this document helpful?
This is a Premium Document. Some documents on Studocu are Premium. Upgrade to Premium to unlock it.

Accenture 3 - Practice notes

Course: python programming (cs8151)

86 Documents
Students shared 86 documents in this course

University: Anna University

Was this document helpful?

This is a preview

Do you want full access? Go Premium and unlock all 17 pages
  • Access to all documents

  • Get Unlimited Downloads

  • Improve your grades

Upload

Share your documents to unlock

Already Premium?
Python Programming
Fundamentals
Function & Packages in Python
Hands-on Exercise

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.