Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

College of Computer Studies

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

# Python supports the common logical


mathematical conditions
# a == b (equals), a!=b (not equals), <,
<=, >, >=
# if

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

#Creating a function using


def
def message():
print("Hello Python")

#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

def message(fname, lname):


print(f"Hi {fname} {lname}")

message("John", "Peter")

'''
Default Parameter Value
- calling a function with no
argument may use a default
value
'''

def message(name = "Mister"):


print(f"Hi {name}")

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)

#Functions with return values

def greet():
return "Hello"
x = greet()
print(x)

def compute(x, y):


return x*y
a = compute(3,5)
print(a)

ITL313-18 |Systems Integration and Architecture Laboratory Prepared by Prof. TCAlcantara, MSIT
College of Computer Studies
BS Information Technology
Python Programming Language
Assignments |4

dict = {'Name': 'Zara',


'Age': 7, 'Class':
'First'}

#The values can be referred


to by using the key name
#duplicate values will
overwrite existing values

print(dict["Name"])
dict["Age"] = 10

#adding new entries

dict = {
'Name': 'Zara',
'Age': 7,
'Class': 'First'
}

dict["Address"] = "Zamboanga
City"
print(dict)
print(type(dict))

#removing dictionary elements

dict = {
'Name': 'Zara',
'Age': 7,
'Class': 'First',
'Address': 'Zamboanga City'
}

del dict['Name'] #removes


the Name entry

dict = {
'Name': 'Zara',
'Age': 7,
'Class': 'First',
'Address': 'Zamboanga City'
}

#removes all entries


dict.clear()

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'
}

#removes dict completely from


memory
del dict

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

You might also like