CMPG171: Study Unit 4
CMPG171: Study Unit 4
CMPG171: Study Unit 4
Study Unit 4
Faculty of Natural- and Agricultural Sciences
• Output
• Resulting information that is sent to a printer,
a monitor, or storage devices after processing
• A cloud based device is accessed through the Internet
• Programming language
• Used to write computer instructions called program code
• Writing instructions is called coding the program
• Examples
• Visual Basic, C#, C++, or Java
• Syntax
• Rules governing word usage and punctuation
• Mistakes in a language’s usage are syntax errors
5
Understanding Computer Systems
-3)
(continued
• Computer memory
• Computer’s temporary, internal storage – random access memory (RAM)
• Volatile memory – lost when the power is off
• Permanent storage devices
• Nonvolatile memory
• Compiler or interpreter
• Translates source code into machine language (binary language) statements called
object code
• Checks for syntax errors
• Program executes or runs
• Input will be accepted, some processing will occur, and results will be output
• Why programming?
• Writing programs (or programming) is a very creative and rewarding activity. You can write
programs for many reasons ranging from making your living to solving a difficult data analysis
problem to having fun to helping someone else solve a problem.
6
Evolution of Programming language
• Features of an algorithm:
• Consists of a finite number of instructions
• Each individual instruction is well defined
• Action described by the instruction can be performed effectively or be executed by a
computing agent
• Describes a process that eventually halts after arriving at a solution to a
problem
• Solves a general class of problems
• Computers can be designed to run a small set of algorithms for
performing specialized tasks
Basic features of an Algorithm
• Pseudocode
• English-like representation of the logical steps it takes to solve a problem
• Flowchart
• Pictorial representation of the logical steps it takes to solve a problem
Writing Pseudocode
• Pseudocode representation of a number-doubling problem
start
input myNumber
set myAnswer = myNumber * 2
output myAnswer
stop
• Program statements are indented a few spaces more than the word
start or the module name
• Each program statement appears on a single line if possible. When
this is not possible, continuation lines are indented
• Program statements begin with lowercase letters
• No punctuation is used to end statements
• Output symbol
• Represents output statements
• Parallelogram
• Flowlines
• Arrows that connect steps
• Terminal symbols
• Start/stop symbols
• Shaped like a racetrack
• Also called lozenges
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Computer Hardware (1 of 3)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Computer Hardware (2 of 3)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Computer Hardware (3 of 3)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Computer Software (1 of 3)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Computer Software (2 of 3)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Computer Software (3 of 3)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Three fundamentals of Structured
Programming.
• Structured programming (sometimes known as modular programming) is a
type of procedural programming that enforces a logical structure on the
program being written to make it more efficient and easier to understand and
modify. Structured programming have three basic logic structures:
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Running Code in the Interactive Shell (2 of 2)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Input, Processing, and Output (1 of 5)
• Programs usually accept inputs from a source, process them, and output
results to a destination
• In terminal-based interactive programs, these are the keyboard and terminal display
• In Python, inputs are Python expressions or statements
• Outputs are the results displayed in the shell
• Programmers can also force output of a value by using the print function
• print (<expression>)
• Example:
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Input, Processing, and Output (2 of 5)
• The following example receives an input string from the user and saves it for further
processing:
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Input, Processing, and Output (3 of 5)
• The input function always builds a string from the user’s keystrokes and
returns it to the program
• Strings that represent numbers must be converted from strings to
appropriate number types
• Two type conversion functions: int (for integers) and float (for floating-point numbers)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Input, Processing, and Output (4 of 5)
• The next session inputs two integers and displays their sum:
input(<a string prompt>) Displays the string prompt and waits for
keyboard input. Returns the string of characters
entered by the user.
print(<expression>, Evaluates the expressions and displays them,
...,<expression>) separated by one space, in the console window.
<string 1> + <string 2> Glues the two strings together and returns the
result.
• We can then run Python program files or scripts within IDLE or from the OS’s
command prompt
• Run within IDLE using menu option, F5 (Windows), or Control+F5 (Mac or Linux)
• Python program files use .py extension
• Running a script from IDLE allows you to construct some complex programs,
test them, and save them in program libraries to reuse or share with others
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Editing, Saving, and Running a Script (2 of 3)
© 2018 Cengage. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed
with a certain product or service or otherwise on a password-protected website for classroom use.
Editing, Saving, and Running a Script (3 of 3)
• The next statement attempts to print the value of the correctly spelled
variable:
>>> print(length)
SyntaxError: unexpected indent
• Final example, programmer attempts to add two numbers, but forgets to
include the second one:
>>> 3 +
SyntaxError: invalid syntax