You are on page 1of 14

IT 2108 – Object Oriented Programming 1 - Python

Deadline: 1 week before Preliminary Exam (October 1)

Submission: You can submit the assessment tasks to our Google Classroom, I’ll be posting later where
you can upload the file.

Filename: BSIT-2D_Surname. I will not accept incorrect filenames so double check before you submit
the assessment tasks.

John Carlo V. Espinase

BSIT – 2D Sir. John Ren G. Santos

Module No. 1
Activity #1.

1.It is a cross platform programming language,which means that it can be run on multiple platform like
window and etc.It is also an object oriented scripting language and designed to he highly readable.
Python is processed at runtime by interpreter.

2. Anaconda is a package manager, an environment manager and Python distribution that contains a
collection of many open source package.

To install Anaconda this is the step:

A. Go to anaconda website and choose a python 3.x graphical installer.

B. Locate your download and double click it. When the screen below appears, Click next.

C. Read the license agreement and click on agree.

D. Click on next.

E. Note your installation location and then click next.

F. Use the alternative approach and check the box.

G. Click on next.

H. You can install M.S Viscode if you wish, but it is options.

I. Click on finish.

3.
print("Benjie B. Jayona\n")

print("Laguna University\n")

print("Santa Cruz,Laguna\n\n")

print("Bachelor of Science in Information Technology")

Module 2

Activity 1

1. Numbers

Python allows programmer to use several different types of numbers. Integers, floating point numbers
and complex number fall under number.

2. Strings

It is used in Python to record text information such as names. String in python are actually a
sequence,which basically means python keeps track of every elements in the string as in sequence.

3. List

List can be thought of the most general version of a sequence in python. Unlike strings, they are
mutable- meaning the elements inside a list can be change.

4. Tuples

It is similar to list,however, unlikr list they are immutable meaning they cannot be change. You would
use tuples to present things that shouldnt be changes, such as day of the week or dates on a calendar.

5. Dictionaries

It is the mapping of sequence in python. Mapping are a collection of objects that stored by a key.

6. Boolean

Python cames with boolean ( with predefined True or false display that are basically just the integers 1
and 0). It is also has a placeholder object called None.

Activity 2

1.

x=1.25+(5**2*8/2)-1

print(x)
2.

4*(6+5)=44

a=4*(6+5)

print(a)

4*6+5=29

a=4*6+5

print(a)

4+6*5

a=4+6*5

print(a)

3. 8.5 = floatint point number

4. By **(0.5)

5**(0.5)

5. By **

5**2

6.

a='Hello'

print(a[1])

7.

a='Hello'

print(a[::-1])
8.

s='Hello'

print(s[4])

s='Hello'

print(s[-1])

9.

A.

mylist=[0,0,0]

B.

mylist=[]

10.

list3=[1,2,[3,4,'hello']]

list3[2].pop(2)

print(list3)

list3[2].append('goodbye')

print(list3)

11.

list4={5,3,4,6,1}

print(list4.reverse())

12.

A.

d={'simple_key':'Hello'}
print(d['simple_key'][0:5])

B.

d={'k1':{'k2':'hello'}}

print(d['k1']['k2'][0:5])

C.

d={'k1':[{'nested_key':['this is deep',['hello']]}]}

print(d['k1'][0]['nested_key'][1]

D.

d={'k1':[1,2,{'k2':['this is tricky',['tough':[1,2,['hello']]}]}]}

print(d['k1'][2]['k2'][1]['tough'][2])

13. No, because dictionary can have both string and integer so that it is hard to be sort. Also it is
mutable. Also, dictionary are mapping and not sequence.

14. Tuples is immutable and cannot be changed while list is mutable and can be change.

15.

Create a tuple it is just like making a list but is immutable

For example

s=(1,2,3)

Show index in tuple by

print(s[0])
Output: 1

It just like a list but its elements cannot be changed.

16. Because set is only concerned with unique elements.

17.

list5=[1,2,2,33,4,4,11,22,3,3,2]

print(set(list5))

18.

A. True

B. Not true

C. True

D. Not true

19.False

Module 3

1.

b="Print only the word that start with s"

for a in b.split():

if a.startswith("s")

print(a)

2.

start = 0

end = 10

for num in range(start, end + 1):

if num % 2 == 0:

print(num, end = " ")


3.

def even_words(str):

value = str.split()

for word in value:

if len(word)%2==0:

print(word)

str = "Print every word in this sentence that has an even number of letters"

even_words(str)

4.

first = 1

second = 100

for num in range(first,second+1):

if ((num%3==0) & (num%5==0)):

print("Fizzbuzz")

elif num%3==0:

print("Fizz")

elif num%5==0:

print("Buzz")

else:

print(num)

Module 4

1.Write a function that computes the volume of a sphere given its radius.
def vol(radius):

pi=3.14

vol=(4.0/3)*pi*(radius**3)

return vol

vol(3)

2.Write a function that checks whether a number is in a given range (Inclusive of high and low)

def ran_check(number,low,high):

for i in range(low,high+1):

if number==i:

print 'Number is within the range'

break

else :

print 'Number is out of range'

ran_check(3,5,10)

# If you only wanted to return a boolean:

def ran_bool(number,low,high):

for i in range(low,high+1):

if number==i:
print True

break

else :

print False

ran_bool(3,1,10)

3.Write a Python function that accepts a string and calculate the number of upper case letters and lower
case letters.**

Sample String : 'Hello Mr. Rogers, how are you this fine Tuesday?'

Expected Output :

No. of Upper case characters : 4

No. of Lower case Characters : 33

If you feel ambitious, explore the Collections module to solve this problem!

def up_low(s):

ucount=0

lcount=0

for letter in s:

if str.isupper(let):

ucount+=1

elif str.islower(let):

lcount+=1

print 'count of upper case characters in string is '+str(ucount)

print 'count of lower case characters in string is '+str(lcount)


up_low('Hello Mr. Rogers, how are you this fine Tuesday?')

4.Write a Python function that takes a list and returns a new list with unique elements of the first list.**

Sample List : [1,1,1,1,2,2,3,3,3,3,4,5]

Unique List : [1, 2, 3, 4, 5]

def unique_list(l):

x=set(l)

print list(x)

unique_list([1,1,1,1,2,2,3,3,3,3,4,5])

5.Write a Python function to multiply all the numbers in a list.**

Sample List : [1, 2, 3, -4]

Expected Output : -24


def multiply(number):

product=1

for num in number:

product=product*num

return product

multiply([1,2,3,-4])

6.Write a Python function that checks whether a passed string is palindrome or not.**

Note: A palindrome is word, phrase, or sequence that reads the same backward as forward, e.g.,
madam or nurses run.

def palindrome(s):

reverse_s=s[::-1]

if s==reverse_s:

print('string is palindrome')

else:

print('not a palidrome')

palindrome('tapat')

____
Hard

Write a Python function to check whether a string is pangram or not.

Note : Pangrams are words or sentences containing every letter of the alphabet at least once.

For example : "The quick brown fox jumps over the lazy dog"

import string

def ispangram(str1, alphabets=string.ascii_lowercase):

s=str.lower(str1)

string=str.replace(s,' ','')

for alpha in alphabets:

if str.find(string,alpha)==-1:

return False

break

else:

return True

ispangram("The boy is buying a food for his mother")

Laboratory activity
Write a Python function to check whether a string is pangram or not.

Note : Pangrams are words or sentences containing every letter of the alphabet at least once.

For example : "The quick brown fox jumps over the lazy dog"

import string

def ispangram(str1, alphabets=string.ascii_lowercase):

s=str.lower(str1)

string=str.replace(s,' ','')

for alpha in alphabets:

if str.find(string,alpha)==-1:

return False

break

else:

return True

ispangram("The boy is buying a food for his mother")

You might also like