100% found this document useful (1 vote)
700 views4 pages

Name - Grade - / 32: National 5 Computing Homework

This document provides examples of built-in functions in the Python programming language for text manipulation, mathematics, and other tasks. It includes sample code demonstrating the use of functions like len(), count(), replace(), round(), int(), ceil(), modulus(%), and pow(). The document then asks the reader to predict the output of additional short programs using these functions.

Uploaded by

Jayne Aruna-Noah
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
100% found this document useful (1 vote)
700 views4 pages

Name - Grade - / 32: National 5 Computing Homework

This document provides examples of built-in functions in the Python programming language for text manipulation, mathematics, and other tasks. It includes sample code demonstrating the use of functions like len(), count(), replace(), round(), int(), ceil(), modulus(%), and pow(). The document then asks the reader to predict the output of additional short programs using these functions.

Uploaded by

Jayne Aruna-Noah
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
You are on page 1/ 4

National 5 Computing Homework Name -

CPU

Computational Thinking Grade - / 32


Feedback
Topic 5 - Built-In Functions

All programming languages have built-in functions that allow basic tasks to be carried out without the need
for additional code.
A few examples of text functions (in the language Python 3.3) are shown below:
len( ) The Length function counts the number of characters in a string
word = “Century” Output from Program
print(len(word)) 7
count( ) The Count function returns the number of times one string occurs in another string
Note - The count function is case sensitive so in the example below only 2
occurrences of “s” are found. The capital S is not counted.
sentence = “Six rain ridden summers” Output from Program
print(sentence.count(“s”)) 2
replace( ) The Replace function finds some given text in a string and replaces it with alternative text.
motto = “Work Hard, Study Hard” Output from Program
newMotto = motto.replace(“Hard”,”Well”) Work Well, Study Well
print(newMotto)

1. For each of the small programs below write down the expected output.
a) word = “Processor” Output from Program (1)
print(len(word)) 9

b) usersGuess = “Rio Grande” Output from Program (1)


print(len(word)) 10

c) wordOne = “Tailor”
wordTwo = “Made” Output from Program (1)
wordsTogether = wordOne + wordTwo 10
print(len(wordsTogether))
d) lyrics = “Don't criticize what you can't understand” Output from Program (1)
print(lyrics.count(“c”)) 3

e) quotation = “There are only two kinds of people who are really Output from Program (1)
fascinating: people who know absolutely everything, and people 4
who know absolutely nothing. - Oscar Wilde”
print(quotation.count(“re”))
Output from Program
f) quotation = “One day I will find the right words.” One day I will discover the right (1)
quotationTwo = quotation.replace(“find”,”discover”)
words.
print(quotationTwo)
g) quotation = “I met an old lady once, almost a hundred years old, and she told me…”
quotation = quotation.replace(“lady”,”gentleman”) Output from Program
quotation = quotation.replace(“she”,”he”) I met an old gentleman once, almost (1)
print(quotation) a hundred years old, and he told
me…
Created by Mr G Reid, PTC Technologies, Buckhaven High School, September 2013
C

N5 Homework Computational Thinking Topic 5 - Built-In Functions

2. The following examples combine two or more of the text functions. Like before, try and predict the
output from the following. You may wish to use a bit of scrap paper to scribble your working on.
a) word = “Code & Coding” Output from Program
word = word.replace(“Code”,”Programs”) Programs & Coding (1)
print(word) 17
print(len(word))
b) quotationPart1 = “Nobody” Output from Program
quotationPart2 = “can make you feel inferior without your” 17 (1)
quotationPart3 = “consent”
total = len(quotationPart1) + len(quotationPart3) + quotationPart2.count(“e”)
print(total)
c) password = “hheeh_shh01” Output from Program
password = password.replace(“hh”,“fff”) Number of characters in password = 12 (1)
password = password.replace(“ee”,“e”)
print(“Number of characters in password =”,len(password))
d) sentenceOne = “The to boys learned to new skills” Output from Program
sentenceTwo = sentenceOne.replace(“to”,“two”) 9 (1)
sentenceThree = sentenceTwo.replace(“boy”,“girls”)
totalLetterW = sentenceOne.count(“w”) + sentenceTwo.count(“w”) + sentenceThree.count(“w”)
totalLetterB = sentenceOne.count(“b”) + sentenceTwo.count(“b”) + sentenceThree.count(“b”)
print(totalLetterW + totalLetterB)
e) messageScrambled = “t s d 20:30”
if len(messageScrambled) >= 15:
messageScrambled = messageScrambled.replace(“t”,“Don’t”)
messageScrambled = messageScrambled.replace(“s”,“Arrive”)
messageScrambled = messageScrambled.replace(“d”,“at”)
else:
messageScrambled = messageScrambled.replace(“t”,“Meet”)
messageScrambled = messageScrambled.replace(“s”,“me”)
messageScrambled = messageScrambled.replace(“d”,“at”)
if (len(messageScrambled) >= 15: Output from Program
message = messageScrambled.replace(“20”,“18”) Meet me at 18:30 (1)
else:
message = messageScrambled.replace(“30”,“15”)
print(message)

3. Using the inputs given, predict what the output will be from the following small program.
usersWord = str(input(“Please enter a word of your choice”))
a = usersWord.count(“a”) Input Entered Output from Program

e = usersWord.count(“e”) a) software Vowels = 3 , Consonants = 5 (1)


Input Entered Output from Program
i = usersWord.count(“i”)
b) assignment Vowels = 3 , Consonants = 7 (1)
o = usersWord.count(“o”)
Input Entered Output from Program
u = usersWord.count(“u”) c) conditional Vowels = 5 , Consonants = 6 (1)
vow = a + e + i + o + u Input Entered Output from Program
cons = len(usersWord) - vow d) python Vowels = 1 , Consonants = 5 (1)
print(“Vowels =”,vow,“, Consonants =”,cons)
C

N5 Homework Computational Thinking Topic 5 - Built-In Functions

The most common type of built-in functions found in programming languages are mathematical functions.
A few examples of mathematical functions (in the language Python 3.3) are shown below:
round( ) The Round function reduces the number of decimal places in a number
rounding up or down as required. In the example below 4.6783 is rounded to 1
decimal place.
Output from Program
number = round(4.6783,1)
print(number) 4.7

int( ) The Integer function changes a real number into an integer by removing all the
decimal places, without rounding up or down.
number = 4.6783 Output from Program
print(int(number)) 4

ceil( ) The Ceiling function round a real number up to the nearest integer. Note that this
function requires “import math” at the top of your program.
import math
Output from Program
number = math.ceil(4.6783)
5
print(number)
% The Modulus function calculates the remainder when one number is divided by another.
leftOver = 13%5 Output from Program
print(number) 3
pow( ) The Power multiplies one number to the power of another number.
The example below would calculate 4 to the power of 2 (or 4 squared).
answer = pow(4,2) Output from Program
16

4. For each of the small programs below write down the expected output.
a) height = 193.734 Output from Program (1)
print(round(height,2) 193.73

b) shoeSize = 10.3 Output from Program (1)


print(round(shoeSize,0)) 10

c) weight = 78.65 Output from Program (1)


print(math.ceil(weight)) 79

d) offcut = 25%7 Output from Program (1)


print(offcut) 4
e) value = 12.5%3 Output from Program (1)
print(math.ceil(value)) 1
f) measurement = 3.48 Output from Program (1)
woodNeeded = math.ceil(measurement) 4
print(woodNeeded)
g) value = 57.884 Output from Program (1)
value = round(value,1) 58
print(ceil(value))
C

N5 Homework Computational Thinking Topic 5 - Built-In Functions

h) cat = 5.91 Output from Program (1)


dog = int(cat) 25
print(pow(dog,2))
i) cake = 99.9999 Output from Program (1)
cake = 100 - int(cake) 27
cake = cake + 2
print(pow(cake,cake))
j) guess = 12 Output from Program (1)
guess2 = pow(guess,2) 33
print(int((guess2-44)/3))

5. Using the inputs given, predict what the output will be from the following program.
import math
usersOption = str(input(“Please enter a mathematical function of your choice”))
numberOne = float(input(“Please enter the first number”))
numberTwo = float(input(“Please enter the second number”))
if usersOption == “integer”:
answer = int(numberOne + numberTwo)
elif usersOption == “modulus”:
answer = numberOne%numberTwo
answer = numberOne%answer
elif usersOption == “ceiling”:
answer = math.ceil(numberOne + numberTwo)
answer = math.ceil(answer/2)
elif usersOption == “square”:
answer = pow((int(numberOne) + int(numberTwo)),2)
answer = pow(int(numberOne),2) + answer
elif usersOption == “round”:
answer = round((numberOne + numberTwo),1)
answer = round(answer + 0.7,0)
else:
answer = int(numberOne)+pow(numberTwo,2)+math.ceil(numberOne)+round(numberTwo,2)
print(answer)

a) usersOption numberOne numberTwo Output from Program (1)


square 5.2 3.9 89
b) usersOption numberOne numberTwo Output from Program (1)
modulus 39 5 3
c) usersOption numberOne numberTwo Output from Program (1)
ceiling 23.79 14.38 20
d) usersOption numberOne numberTwo Output from Program (1)
integer 7.845 13.1 20
e) usersOption numberOne numberTwo Output from Program (1)
round 34.55 8.13 43
f) usersOption numberOne numberTwo Output from Program (1)
percentage 6.7 7 69

You might also like