Professional Documents
Culture Documents
ITL313 18 - 3 BSIT .2 - A 6 - Function
ITL313 18 - 3 BSIT .2 - A 6 - Function
BS Information Technology
Python Programming Language
Assignments |1
Assignment No. 6
Functions and Dictionaries
Learning Outcome
At the end of this activity, the student should be able to code the functions and dictionaries statements
using Python PL.
Discussion
Functions
- block / bundle of code that runs when called to carry out a specified tasks
- bundle a set of codes for repeated use
- handle complex codes (better to be self-contained as a subprogram then called when needed)
- accept data for processing (parameters)
- can return data as a result of execution (return value)
When calling a function, the correct number of arguments is important. By default, the number of
arguments in the invocation must match the number of function parameters in the definition.
Best practices for functions:
- a function should only handle a single aspect of the program (single responsibility principle)
- a function name should indicate its purpose when used
- use docstrings (comments to document your functions)
Dictionary
- store data values in key:value pairs
- ordered, changeable, no duplicates (As of Python 3.7, dictionaries are ordered. In prior
Python versions, dictionaries are unordered)
When to use dictionaries?
- mapping of keys with values
- when data has a unique reference that can be associated with a value
Requirement
1. Create a new Python file and name it as “FIMISurname-A6.py” (ex: TCAlcantara-A6.py)
2. Copy and paste the code from each box in the output, debug to eliminate the code errors and
ITL313-18 |Systems Integration and Architecture Laboratory Prepared by Prof. TCAlcantara, MSIT
College of Computer Studies
BS Information Technology
Python Programming Language
Assignments |2
supply values if necessary.
3. Compile and then execute.
4. Screenshot your output then paste on the space provided. Make sure to capture the filename and
the exit code. Please refer to the sample output below.
Sample Output
a = 40
b = 510 #added code for value
if a > b:
print(f"{a} is greater than {b}")
print("Testing control structures")
Output
Code Screenshot
#Calling/Invoking a function
message()
'''
Function arguments (args)
- data can be passed into
functions as
arguments/parameters
- are placed inside the
parentheses
- for multiple args, separate
with comma
'''
def message(name):
print("Hi "+name)
message("John")
ITL313-18 |Systems Integration and Architecture Laboratory Prepared by Prof. TCAlcantara, MSIT
College of Computer Studies
BS Information Technology
Python Programming Language
Assignments |3
message("John", "Peter")
'''
Default Parameter Value
- calling a function with no
argument may use a default
value
'''
message()
'''
Passing a list as an argument
-any object (data type) can
be sent as an
argument and will be treated
as the same data
type inside the function
'''
def my_supplies(food):
for x in food:
print(x)
snacks =
["fries","burger","icecream"]
my_supplies(snacks)
def greet():
return "Hello"
x = greet()
print(x)
ITL313-18 |Systems Integration and Architecture Laboratory Prepared by Prof. TCAlcantara, MSIT
College of Computer Studies
BS Information Technology
Python Programming Language
Assignments |4
print(dict["Name"])
dict["Age"] = 10
dict = {
'Name': 'Zara',
'Age': 7,
'Class': 'First'
}
dict["Address"] = "Zamboanga
City"
print(dict)
print(type(dict))
dict = {
'Name': 'Zara',
'Age': 7,
'Class': 'First',
'Address': 'Zamboanga City'
}
dict = {
'Name': 'Zara',
'Age': 7,
'Class': 'First',
'Address': 'Zamboanga City'
}
ITL313-18 |Systems Integration and Architecture Laboratory Prepared by Prof. TCAlcantara, MSIT
College of Computer Studies
BS Information Technology
Python Programming Language
Assignments |5
dict = {
'Name': 'Zara',
'Age': 7,
'Class': 'First',
'Address': 'Zamboanga City'
}
Reporting
1. Save this file as PDF following the filename “ITL313-18_section_A-6_Functions_Surname.pdf”
(ex: ITL313-18_3-BSIT-.1_A-6_Functions_Alcantara.pdf).
2. Upload your assignment to the folder you created in the shared Google Drive under ITL313-
18_Lab_Surname folder.
ITL313-18 |Systems Integration and Architecture Laboratory Prepared by Prof. TCAlcantara, MSIT