0% found this document useful (0 votes)
320 views29 pages

MA1008 Week 2 (Introduction Tutorial)

The document is an introduction to computational thinking and programming concepts, specifically focusing on Python basics. It outlines key tips for success in the course, explains the differences between machine and high-level languages, and provides various tutorial questions and hands-on programming exercises. Topics covered include data representation, programming structures, and practical coding tasks related to mathematical computations and algorithms.

Uploaded by

Ananya Jayanty
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)
320 views29 pages

MA1008 Week 2 (Introduction Tutorial)

The document is an introduction to computational thinking and programming concepts, specifically focusing on Python basics. It outlines key tips for success in the course, explains the differences between machine and high-level languages, and provides various tutorial questions and hands-on programming exercises. Topics covered include data representation, programming structures, and practical coding tasks related to mathematical computations and algorithms.

Uploaded by

Ananya Jayanty
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

Introduction to Computational Thinking

MA1008
Week 2
Dr WONG Choon Yue
wongcy@[Link]
[Link]
2 Tips
1. Keep up with the course every week.
• Each week, the concept or technique covered will be more challenging than the
one before.
• Missing a tutorial = missing a big portion of the content.
• Try ALL the tutorial and programming questions.
• If you feel lost, seek help.
• Discuss with your classmates
• Email me (Dr Wong Choon Yue): wongcy@[Link]
• Email Prof Lee Yong Tsui: mytlee@[Link]

2. Be honest in your quizzes and final assignment


• You must have the ability to explain every single line of assignment submission.
• For this reason, students have been previously made to retake the quiz or assignment.
Python Basics
Week 2_Tutorial_Question 1
i) Name the two levels of languages that are normally involved in the programming process.
• Machine Language and High-Level Language
ii) What are they for?
• Machine language is the language that is designed for the computer hardware, and it is
not human-readable.
• A high-level language is designed to be human-readable, so that we can use it to write
programs.
iii) What is the relationship between them?
• A program written in a high-level language needs to be translated into the machine
language before it can be executed on a computer.
High level language
Machine Language
Week 2_Tutorial_Question 2
i) What is a compiled language?
• A compiled language is to be put through a program called the compiler to translate into
the machine language. This machine language program is the one to be executed. There
is also the process of linking to combine different compiled programs and external
libraries into the executable program but Python programmers do not see this.
ii) What is an interpreter?
• An interpreter is a high-level language that is directly translated into machine language
and executed without going through a compiler. Therefore, we can write statements in
the language and have them executed immediately.
iii) Give two examples of each
• Compiled: C, C++, Fortran, Java
• Intepreters: Python, MATLAB
Week 2_Tutorial_Question 3
i) How does a computer represent numbers?
• A computer represents numbers using binary digits 0 and 1. These are called bits. Bits are
held in groups of 8, called bytes.
• A bit is 0 or 1
• A byte is 8 bits, such as 0000010 (which means 2) and 00000001 (which means 1)
• Video introduction to binary numbers: [Link]
ii) How does it represent non-numberic data such as letters and colours?
• Non-numeric data are represented as numeric values.
• Colours are also stored numerically.
• Each colour has 3 primary colour component of Red, Green and Blue.
• Each R, G and B component has intensity (brightness) value between 0-255.

R: 255 R: 0 R: 255 R: 126


G: 255 G: 0 G: 0 G: 126
B: 255 B: 0 B: 0 B: 126
Letters have numeric values as specified by ASCII (American Standard Code for Information Interchange)
Week 2_Tutorial_Question 4
• Construct the steps for making a cup of coffee. You have the choice of:
i) Instant or powdered coffee
ii) With or without sugar
iii) With or without milk
• Assume you use a percolater for the powdered coffee.

If instant coffee
Put coffee power in cup
Add hot water to cup
Else #Assume percolator for powdered coffee
Put coffee powder in percolator
Add adequate water to percolator
Turn on percolator
Pour into cup once percolator done

If require sugar
Add sugar to cup
If require milk
Add milk to cup
Stir the coffee
Week 2_Tutorial_Question 4
• Construct the steps for making a cup of coffee. You have the choice of:
i) Instant or powdered coffee
ii) With or without sugar
iii) With or without milk
• Assume you use a percolater for the powdered coffee.
Select one of the choices
If instant coffee
Put coffee power in cup Steps performed for choice 1
Add hot water to cup
If choice 1 not selected, we know
Else #Assume percolator for powdered coffee automatically it is choice 2.
Put coffee powder in percolator
Add adequate water to percolator
Steps performed for choice 2
Turn on percolator
Pour into cup once percolator done

If require sugar
Add sugar to cup
If require milk
Add milk to cup
Stir the coffee
Week 2_Tutorial_Question 5
Construct the steps for solving a quadratic equation, starting from getting the inputs for the
coefficients through to delivering the solutions.
Ax2 + Bx + C = 0

1. Read in the three coefficients A, B and C for the quadratic equation.


2. Find the discriminant D = B2 - 4AC
3. If D < 0
• Return no real roots.
4. Else if D = 0
• Return one repeated root.
5. Else #After not being able to satisfy the previous two evaluations, D is obviously > 0
• Return two real roots.

Note: “Return” is computer lingo that means to output.


Week 2_Tutorial_Question 6
Construct the steps for swapping two numbers stored in two variables.

1. Let the two variables with values be A and B


2. C = A #C is a temporary variable
3. A=B
4. B=C
Week 2_Tutorial_Question 7
Construct the steps for finding the maximum (or minimum) in a list of 2 variables. Then extend
to 3 variables. Then extend it to many variables, for e.g 100.

• For two variables A and B


Bigger = A
If B > A
Bigger = B

• For three variables, let them be A, B and C


Biggest = A
If B > Biggest
Biggest = B
If C > Biggest
Biggest = C
Hands On Session
The purpose of this lab is for you to practise using the Python programming environment
and do some basic programming with Python. Before you start, you are advised to review
the material in the lectures to pick up the related basic elements in Python such as
comments, keywords, variables, operators, etc.

In this exercise, you are to devise Python programs to solve simple problems. But before you
start the coding on each problem, you are strongly advised to think about the followings:

1) What is the problem here? Understand and analyze it first.


2) What are the input(s) and output(s)?
3) Do I need to use any formulae?
4) What are the steps in the program?

After that, you can first build a simple skeleton for each major steps using simple English. That
is, write the pseudo-code.
Week 2_Hands-On_Question 1
Write a Python program to compute the volume and surface area of a sphere in three
dimensional space. The program should read the user input on the radius of the sphere, and
then compute and display the sphere volume and surface area. (Note: Area = 4πr2 , Volume =
4πr 3 /3.)
Week 2_Hands-On_Question 1
import math

# Get radius
radius = float( input( "input radius: " ) )

# Compute volume
volume = 4.0 / 3.0 * [Link] * radius * radius * radius

# Compute surface area


surface = 4.0 * [Link] * radius * radius

# Report results
print( "sphere volume = " , volume )
print( "sphere surface area = " , surface )
Math Library
import math
Comment Data type

# Get radius
radius = float( input( "input radius: " ) ) Input function
Variable
called
“volume” # Compute volume
volume = 4.0 / 3.0 * [Link] * radius * radius * radius

# Compute surface area Multiply


Assignment operator
operator surface = 4.0 * [Link] * radius * radius

# Report results
Print function print( "sphere volume = " , volume )
print( "sphere surface area = " , surface )
Week 2_Hands-On_Question 2
Write a Python program to compute the solutions for x and y in the linear system of equations:

𝑎1𝑥 + 𝑏1𝑦 = 𝑐1
𝑎2𝑥 + 𝑏2𝑦 = 𝑐2

The solutions for x and y are given by:

𝑏2𝑐1 − 𝑏1𝑐2
𝑥=
𝑎1𝑏2 − 𝑎2𝑏1
𝑎1𝑐2 − 𝑎2𝑐1
𝑦=
𝑎1𝑏2 − 𝑎2𝑏1

Assume that the denominator is always non-zero. Your program reads in 𝑎1, 𝑏1, 𝑐1, 𝑎2, 𝑏2 and
𝑐2, and then computes and prints out the solutions.
Week 2_Hands-On_Question 2
Week 2_Hands-On_Question 2
# Get inputs
print( "please input the values" )
a1 = float( input( " a1: " ) )
b1 = float( input( " b1: " ) )
c1 = float( input( " c1: " ) )
a2 = float( input( " a2: " ) )
b2 = float( input( " b2: " ) )
c2 = float( input( " c2: " ) )

# Compute x and y
x = ( b2*c1 - b1*c2 ) / ( a1*b2 - a2*b1 )
y = ( a1*c2 - a2*c1 ) / ( a1*b2 - a2*b1 )

# Report results
print( "x = " , x )
print( "y = " , y )
Week 2_Hands-On_Question 3
Write a program that reads in an integer and prints “Even” if it is an even number and “Odd” if
it is an odd number.

num = int(input("Enter an integer: "))


if num%2 == 0:
print("Even")
else:
print("Odd")

The Python modulus operator:


% If….. else….

A good online resource for Python operators: [Link]


Week 2_Hands-On_Question 4
A person’s body-mass index is given by the formular BMI = (weight in kg)/(height in m)2. Write
a program that reads in the weight and height of a person and prints the BMI.

weight = float(input("Body weight in kg: "))


height = float(input("Height in meters: "))

BMI = weight/(height*height)

print("Your body-mass index is ", BMI)


Week 2_Hands-On_Question 5
The distance of a marathon is 42.2km. A runner paces himself such that (i) his average speed
for the first 12km is 15 km/hr, (ii) he reduces the exertion for the next 15km with an average
speed of 10 km/hr (iii) he increases the speed to 14 km/hr for the next 10 km and (iv) for the
remaining distance, he makes a dash for it by averaging 18km/hr. Write a program to calculate
and print the time he took to complete the marathon.

TotalDist = 42.2
TimeFirst12km = 12.0/15
TimeNext15km = 15.0/10
TimeNext10km = 10.0/14
Remainingkm = 42.2-12-15-10
TimeRemainingkm = Remainingkm/18

TotalTime = TimeFirst12km + TimeNext15km + TimeNext10km + TimeRemainingkm

print("The total time taken is ", TotalTime, "hours.")

*notice that the names of each variable (such as TimeNext15km) communicates the meaning of that variable.
The use of meaningful variables is good computing practice.
Week 2_Hands-On_Question 5
Converting a decimal hour to hours, mins and seconds
This portion requires knowledge about float and integer data types, as well as the concept of “casting”

# Express the time in hours, minutes, seconds


Hours = int(TotalTime)
HourFraction = TotalTime - Hours
Minutes = int(HourFraction*60)
MinuteFraction = HourFraction*60 - Minutes
Seconds = MinuteFraction*60

print("which is ", Hours, "hours", Minutes, "minutes and", Seconds, "seconds")


Week 2_Hands-On_Question 5
Converting a decimal hour to hours, mins and seconds
Let us assume as an example that TotalTime had a value of 2.52

Hours is assigned only to the integer HourFraction is the portion of the


value of TotalTime. In our example, hour that remains. In our example, it
Hours has value of 2. has the value of 0.52

# Express the time in hours, minutes, seconds


Find out how many whole minutes 0.52
Hours = int(TotalTime) hours is, then cast it into an integer.
HourFraction = TotalTime - Hours
Minutes = int(HourFraction*60)
MinuteFraction = HourFraction*60 - Minutes
Seconds = MinuteFraction*60
Find out the size of the
remaining fractional minute.
Find out how many seconds
that fractional minute is.

print("which is ", Hours, "hours", Minutes, "minutes and", Seconds, "seconds")

You might also like