The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by taribahmed13, 2022-07-18 07:09:29

STEM Wonders GRADE 8

STEM Wonders GRADE 8

Introduction To Python STEM Wonders

7.5 Algorithm

An algorithm is a sequence of instructions that must be followed to solve a problem.
These instructions must be clear enough that each step may be completed without much
effort. Algorithms exist in the real world as well. Consider the instructions you follow in
everyday life. (Fig 7.17)
Let’s consider another example. A recipe is like an algorithm
telling you the ingredients and steps to follow.
1. Beat the eggs: Use two or three eggs per omelet,
depending on how hungry you are. Beat the eggs lightly with
a fork.
2. Melt the butter: Use an 8-inch non-stick skillet for
a 2-egg omelet, and a 9-inch skillet for 3 eggs. Melt the
butter over medium-low heat, and keep the temperature low
and slow when cooking the eggs so the bottom doesn’t get
too brown or overcooked.
3. Add the eggs: Let the eggs sit for a minute, then
use a heatproof silicone spatula to gently lift the cooked
eggs from the edges of the pan. Tilt the pan to allow the
uncooked eggs to flow to the edge of the pan.
4. Fill the omelet: Add the filling—but don’t overstuff Fig 7.17

the omelet—when the eggs begin to set. Cook for a few
more seconds
5. Fold and serve: Fold the omelet in half. Slide it
onto a plate with the help of a silicone spatula.

Do You
Know?

The algorithm comes from Algoritmi, the
Latin form of the name of the famous
Persian mathematician, Muhammad ibn Musa
al-Khwarizmi. His work in mathematics, geog-
raphy, and astronomy advanced the subject

of algebra and trigonometry.

147

Introduction To Python STEM Wonders

7.6 Flowchart

A flowchart is a diagram that describes the sequences of an
algorithm in the correct order. These diagrams show a step-by-
step way of solving a problem. Before you start developing the
programming, you can use flowcharts to express your ideas about
how to solve a problem with the computer.
Consider the example in Fig 7.18.
Different shapes in the flowchart have different meanings. To de-
scribe the steps of an algorithm you draw a flowchart using these
shapes. Let’s learn these shapes.

Represent starting or ending stage.

Represent the process stage.

Represent the conditional Fig 7.18
(decision) branch.
Represent the data input or output.

Represent process flow.

7.7 More Python!

7.7.1 If Statement

If statement is used to check whether a condition is true or false. if keyword is used to
write this statement. Let’s consider the following statement. (Fig 7.19)

sy nta x Code Output

if (condition): a = 10 b is greater than a
do this b = 20
do this if b > a:
print(“b is greater than
a”)

Fig 7.19

148

Introduction To Python STEM Wonders

7.7.2 If – Else Statement

If – else statement is an extension to the if statement. If the condition is true a block of
code will execute else another block of code will run.

sy nta x Code Output

if (condition): a = 20 a is greater than b
do this b = 10
if b > a:
else:
do this print(“b is greater than a”)
else:

print(“a is greater than b”)

Fig 7.20

7.7.3 For loop

To repeat tasks programmers use loops. For loop is a type of loop. Let’s say you need to
print all letters in a new line in a string we can easily achieve this task using a for loop.

sy nta x Code Output

for variable in sequence: for x in “for”: f
loop body print(x) o
r

See Fig x.21 to understand the flow of for loop.
Note: You can use a break statement to exit the loop on the desired condition.

Let’s do it!

Print all characters in a new line in the string
“TechTree” using for loop.

Fig 7.21

149

Introduction To Python STEM Wonders

7.7.4 While loop

The while loop allows us to run a series of statements as long as a condition is true. Let’s
keep printing a number as long as the number is less than 4. See Fig x.22 to understand
the flow of the while loop.

sy nta x Output Output

while condition: a=1 1
body of loop while a < 4: 2
3
print(a)
a += 1

Let’s do it! Fig 7.22

Print the table of number two using the while loop.

7.8 User input

Python allows users to input data. The input() method is used to accept the user’s input.
Let’s enter your name as an input and print it.
Note: You can use the + operator to join a string and number.

sy nta x Code Output

input(“Text to username = input(“Enter your name:”) Enter your name: TechTree
appear on ternimal”) print(“Your name is: “ + username) Your name is TechTree

7.9 How to design a program

> Understanding the problem. - You must first understand the problem you wish to solve
before beginning to design a program.
> Create the algorithm - Write down the small steps required to solve a problem in plain
English.
> Draw the flowchart - To create a flowchart you need to analyze the steps of the algo-
rithm.
> Start programming – Start programming the program with the help of a flowchart.

150

Introduction To Python STEM Wonders

Let’s print a table of a number entered by the user.

7.9.1 Algorithm

1. Accept a number as the user’s input. 7. Save the multiplication to a variable.
2. Save it to a variable. 8. Print the answer.
3. Change input to an integer. 9. Add 1 to the loop variable.
4. Create a variable to start a while loop. 10. Repeat the task 10 times.
5. Start a loop to run 10 times. 11. Exit the loop.
6. Multiply the number to the loop variable.

7.9.2 Flowchart

Code Output

number = input(“Enter Enter Number: 2
number: “) 2
number = int(number) 4
a=1 6
while a < 11: 8
10
answer = number * a 12
print(answer) 14
a=a+1 16
18
20

Fig 7.23

151

End of Section - 07 Introduction To Python

Choose the right one!

1. A person that writes codes in different programming languages?
a. A programmer.
b. A Technician.
c. A Writer.
d. A polyglot.
2. Which one of the following is not a programming language?
a. C++.
b. JavaScript.
c. Python.
d. B++.
3. Which version of Python did you learn in this chapter?
a. Python1.
b. Python2.
c. Python3.
d. Python4.
4. Which of the following is an assignment operator?
a. +
b. -
c. %
d. =
5. Which of the following is a loop in Python3?
a. While Loop.
b. For Loop.
c. None of the above.
d. All of the above.

152

Answer the following!

1. Define what is programming?
2. Define what is a programming language?
3. What are arithmetic operators? Give examples.

153

End of Section - 07 Introduction To Python

4. Define the algorithm?
5. Define a flowchart and explain the shapes used in drawing a flowchart?

154

Project!

Take temperature as user input. Give user conversion options, if the user selects the
conversion unit, then change the current temperature to the selected unit and print the
output.
Hint: Give the user options, if the user press 1 temperature will be converted into Kelvin,
if the user press 2 temperature will be converted to Fahrenheit. Take option selection as
user input.

155


Click to View FlipBook Version