0% found this document useful (0 votes)
2 views2 pages

Python Notes

The document explains how to pass parameters to functions in Python, covering positional, keyword, default, and variable-length arguments. It includes examples of a palindrome program and the use of a Boolean flag in searching for a number. Additionally, it discusses non-value returning functions, Python modules, and the benefits of modular design in programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Python Notes

The document explains how to pass parameters to functions in Python, covering positional, keyword, default, and variable-length arguments. It includes examples of a palindrome program and the use of a Boolean flag in searching for a number. Additionally, it discusses non-value returning functions, Python modules, and the benefits of modular design in programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

5 marks

[Link] to pass a parameter to a function


Functions in Python are created and denoted with the def keyword followed by a function
name. The parameters of the function are stored in the parentheses.

For example: greet_customer(name) is a function that has (name) as its parameter.

 Positional Arguments
Values are assigned to parameters in the order they are passed.

Eg

def greet(name, age):

print(“Hello”, name, “You are”,age,” years old.")

greet("Alice", 25)

 Keyword Arguments
You specify which value goes to which parameter using parameter names.

Eg

def greet(name, age):

print(“Hello”, name, “You are”,age,” years old.")

greet(name="Bob", age=30)

 Default Arguments
Parameters can have default values, used if no argument is passed.

Eg

def greet(name, age=18):

print(“Hello”, name, “You are”,age,” years old.")

greet("Charlie")

 Variable-Length Arguments

1.*args for multiple positional arguments (tuple)

2.**kwargs for multiple keyword arguments (dictionary)

def fun(*args, **kwargs):

print(args)

print(kwargs)

fun(1, 2, name="Alice", age=25)

[Link] Program

def is_palindrome(s):
return s == s[::-1]

word = input("Enter a word: ")

if is_palindrome(word):

print("It's a palindrome!")

else:

print("Not a palindrome.")

✅ Example:

Enter a word: madam

It's a palindrome!

2 marks

3. Show the example for Boolean flag

found = False
numbers = [10, 20, 30, 40, 50]
target = 30
for num in numbers:
if num == target:
found = True
break
if found:
print("Number found!")
else:
print("Number not found.")
[Link] of calling non value
Non-value returning functions are used to perform actions like displaying output or modifying data
without returning a result, helping to organize and simplify code.
5. python modules
1. Built-in Modules
Already available in Python.
Example: math, random, datetime
2. User-defined Modules
Created by users to organize their own functions and classes.
6. Python modular Design
Modular design improves code reusability, readability, debugging, and allows easier maintenance
and teamwork.

You might also like