0% found this document useful (0 votes)
31 views38 pages

Python Module 1

The document discusses Python modules and functions. It explains that modules contain related functions and must be imported before use. Functions group code to be reused, can take parameters and return values. Local variables inside functions are not accessible globally while global variables can be accessed locally. Keywords like global and exceptions are used to modify global variables from functions and handle errors.

Uploaded by

Phoenix Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views38 pages

Python Module 1

The document discusses Python modules and functions. It explains that modules contain related functions and must be imported before use. Functions group code to be reused, can take parameters and return values. Local variables inside functions are not accessible globally while global variables can be accessed locally. Keywords like global and exceptions are used to modify global variables from functions and handle errors.

Uploaded by

Phoenix Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Module 1

Python Basics
APPLICATION
DEVELOPMENT USING
PYTHON-18CS55

Sneha N P
Assistant Professor
Dept. of Computer Science & Engineering
AIT, Bangalore

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Importing modules
• Python also comes with a set of modules called the standard library.
• Each module is a Python program that contains a related group of functions
that can be embedded in your programs.
• For example, the math module has mathematics related functions,
• the random module has random number–related functions, and so on.
• Before you can use the functions in a module, you must import the module
with an import statement.

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Importing modules
In code, an import statement consists of the following:

•The import keyword

•The name of the module

•Optionally, more module names, as long as they are separated by commas

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Importing modules

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Importing modules

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Importing modules
from import Statements
• An alternative form of the import statement is composed of the from
keyword, followed by the module name, the import keyword, and a
star;
• for example, from random import *.
• With this form of import statement, calls to functions in random will
not need the random. prefix.

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


ending a Program early with sys.exit()

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Functions
• A function is a block of code which only runs when it is called.
• You can pass data, known as parameters, into a function.
• A function can return data as a result.

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Creating a Function
In Python a function is defined using the def keyword:
def hello():
print('Howdy!’)
print('Howdy!!!’)
print('Hello there.')
Calling a Function
• To call a function, use the function name followed by parenthesis:
hello()
hello()
hello()

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Output:

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Function
• A major purpose of functions is to group code that gets executed multiple
times.
• makes your programs shorter,
• easier to read, and
• easier to update.

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Arguments
• Information can be passed into functions as arguments.
• Arguments are specified after the function name, inside the parentheses.
• You can add as many arguments as you want, just separate them with a comma.
Function When you run this program, the output
looks like this:
Parameters

Arguments

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Arguments
• One special thing to note about parameters is that the value stored in a parameter is
forgotten when the function returns.

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


return Values and return Statements
• The value that a function call evaluates to is called the return value of the function.

• When creating a function using the def statement, you can specify what the return value should be
with a return statement.

• A return statement consists of the following:


• The return keyword
• The value or expression that the function should return

• When an expression is used with a return statement, the return value is what this expression evaluates
to.

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Example:

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


return Values and return Statements

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


the none Value
• In Python there is a value called None, which represents the absence of a value.

• None is the only value of the NoneType data type.

• None must be typed with a capital N.

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


keyword Arguments and print()
• keyword arguments are identified by the keyword put before them in the
function call.

• Keyword arguments are often used for optional parameters.

• For example, the print() function has the optional parameters end and sep
to specify what should be printed at the end of its arguments and between its
arguments (separating them), respectively.

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


keyword Arguments and print()

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


keyword Arguments and print()
• For example, if the program were this:

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


keyword Arguments and print()
• when you pass multiple string values to print(), the function will
automatically separate them with a single space.

• Enter the following into the interactive shell:

• replace the default separating string by passing the sep keyword argument.
Enter the following into the interactive shell:

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Local and global Scope
• Parameters and variables that are assigned in a called function are said to exist in
that function’s local scope.

• Variables that are assigned outside all functions are said to exist in the global
scope.

• A variable that exists in a local scope is called a local variable, while a variable
that exists in the global scope is called a global variable.

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Local and global Scope contd..
Scopes matter for several reasons:

• Code in the global scope cannot use any local variables.

• However, a local scope can access global variables.

• Code in a function’s local scope cannot use variables in any other local scope.

• You can use the same name for different variables if they are in different scopes. That
is, there can be a local variable named spam and a global variable also named spam.

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Local Variables Cannot Be Used in the Global Scope

• Consider this program, which will cause an error when you run it:

def spam():
eggs = 31337
spam()
print(eggs)

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Local Scopes Cannot Use Variables in Other Local Scopes

• A new local scope is created whenever a function is called, including when a


function is called from another function.

• Consider this program:

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Global Variables Can Be Read from a Local Scope

• Consider the following program:

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Local and Global Variables with the Same Name

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


the global Statement
• If you need to modify a global variable from within a function, use the global
statement.

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


the global Statement contd..
There are four rules to tell whether a variable is in a local scope or global scope:

1. If a variable is being used in the global scope (that is, outside of all functions), then it is
always a global variable.

2. If there is a global statement for that variable in a function, it is a global variable.

3. Otherwise, if the variable is used in an assignment statement in the function, it is a local


variable.

4. But if the variable is not used in an assignment statement, it is a global variable.

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


the global Statement contd..
• To get a better feel for these rules, here’s an example program.

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


the global Statement contd..
• If you try to use a local variable in a function before you assign a value to it, as
in the following program, Python will give you an error.

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Exception handling
• Getting an error, or exception, in your Python program means the entire program
will crash.
• Instead, you want the program to detect errors, handle them, and then continue to
run.
• For example, consider the following program

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Exception handling
• Getting an error, or exception, in your Python program means the entire program
will crash.
• Instead, you want the program to detect errors, handle them, and then continue to
run.
• For example, consider the following program Output:

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Exception handling
• Errors can be handled with try and except statements.
• You can put the previous divide-by-zero code in a try clause and have an except
clause contain code to handle what happens when this error occurs.
Output:

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Exception handling
• Consider the following program, which instead has the spam() calls in the try block:
The output looks like this:

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


A Short Program: guess the number

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore


Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore
Output:

Department of Computer Science & Engineering, Acharya Institute of Technology, Bangalore

You might also like