Module 1
Module 1
Output:
Contd..
• The Starting, Stopping, and Stepping
Arguments to range()
Contd..
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.
• Example, the math module, the random module.
• Before you can use the functions in a module,
you must import the module with an import
statement.
Contd..
examples
• Python program to find the sum of natural
numbers up to n where n is provided by user.
• Python Program to find the factors of a
number.
• Python program to check if the input number
is prime or not.
• Python program to find the factorial of a
number provided by the user.
examples
• Python program to find a given year is leap
year.
• Python Program to find the roots of quadratic
equation.
• Python program to swap to numbers.
• Python program to print stars shown:
*
***
*****
*******
*********
***********
Functions
• A function is like a mini-program within a
program.
• ➊ def hello():
• ➋ print('Howdy!')
• print('Howdy!!!')
• print('Hello there.')
➌ hello()
• hello()
• hello()
def Statements with Parameters
• ➊ def hello(name):
• ➋ print('Hello ' + name)
• ➌ hello('Alice')
• hello('Bob')
Return Values and return Statements
• 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
example
print(getAnswer(random.randint(1,9)))
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.
Keyword Arguments and print()
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.
Local and Global Scope
• 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.
Local and Global Scope