Program Design: Algorithms, Pseudocode and Flowcharts

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

What should be done before writing code in the program development cycle?

  • Testing the program
  • Correcting syntax errors
  • Correcting logic errors
  • Designing the program (correct)

Which of the following is the most important part of the program development cycle?

  • Correcting syntax errors
  • Writing the code
  • Testing the program
  • Designing the program (correct)

What is the definition of an Algorithm?

  • A diagram that graphically depicts the steps in a program.
  • Set of well-defined logical steps that must be taken to perform a task. (correct)
  • Informal language that has no syntax rule.
  • A variable that can only be assigned integer values.

What is pseudocode primarily used for?

<p>To create a model program (B)</p> Signup and view all the answers

Which symbol represents processing in a flowchart?

<p>Rectangle (C)</p> Signup and view all the answers

In the typical three-step process performed by computers, what follows after receiving input?

<p>Performing some process on the input (C)</p> Signup and view all the answers

What is the purpose of the print function in programming?

<p>To display output on the screen (A)</p> Signup and view all the answers

How are string literals defined in Python?

<p>Enclosed in single, double, or triple quotes. (A)</p> Signup and view all the answers

Which character is used to begin a comment in Python?

<h1>(D)</h1> Signup and view all the answers

What is a variable in programming?

<p>A name that represents a value stored in the computer's memory (D)</p> Signup and view all the answers

Which of the following is a valid variable name in Python?

<p>_variable (B)</p> Signup and view all the answers

What happens when multiple items are passed to the print function in Python?

<p>They are automatically separated by a space. (D)</p> Signup and view all the answers

What is 'garbage collection' in Python?

<p>The removal of values no longer referenced by variables (C)</p> Signup and view all the answers

What does it mean when a variable is reassigned in Python?

<p>The variable references a different value. (C)</p> Signup and view all the answers

Which data type is used for storing strings in memory?

<p>str (C)</p> Signup and view all the answers

What happens if you try to convert a non-numeric string to an integer using the int() function?

<p>An exception is raised. (A)</p> Signup and view all the answers

What is the result of the expression 10 / 3 in Python?

<p>3.333... (A)</p> Signup and view all the answers

Which operator is used to return the remainder of a division?

<p>% (D)</p> Signup and view all the answers

What is the order of operations in Python?

<p>All of the above (D)</p> Signup and view all the answers

What is the purpose of the exponent operator **?

<p>To raise a number to a power (B)</p> Signup and view all the answers

If you multiply two int values, what will the resulting data type be?

<p>int (A)</p> Signup and view all the answers

If you add an int and a float, what will the resulting data type be?

<p>float (C)</p> Signup and view all the answers

What happens when you convert a float to an int?

<p>The decimal part is truncated. (A)</p> Signup and view all the answers

How can a long statement be broken into multiple lines in Python?

<p>By using the multi-line continuation character <code>\</code> (A)</p> Signup and view all the answers

How does print function display the output by default?

<p>Newline character at the end of the line (C)</p> Signup and view all the answers

What will be the output of the code, print('line1 \n line2')?

<p>line1 line2 (C), line1 line2 (D)</p> Signup and view all the answers

Which type of data will input() function return?

<p>string (C)</p> Signup and view all the answers

Which of the following is the proper way to concatenate strings in Python?

<p>string1 + string2 (B)</p> Signup and view all the answers

What are “magic numbers” in programming?

<p>Unexplained numeric values in a program's code. (D)</p> Signup and view all the answers

What is the main problem associated with using magic numbers in code?

<p>They make the code difficult to understand and maintain. (D)</p> Signup and view all the answers

Why should named constants be used instead of magic numbers?

<p>To make code self-explanatory and easier to maintain (D)</p> Signup and view all the answers

What happens if you enclose any part of a statement in parentheses?

<p>Statement can be broken without using line continutation character (A)</p> Signup and view all the answers

If the variable number has the value 123.4567, what would format(number, '.2f') return?

<p>123.46 (C)</p> Signup and view all the answers

Consider this line of code: print('Total: $' + format(1000, ',.2f')) What exactly is happening when + is used in this context?

<p>String concatenation (D)</p> Signup and view all the answers

What will be printed when the below code is executed?

num1 = 10
num2 = 3
print(num1 % num2)

<p>1 (A)</p> Signup and view all the answers

What is the result of the following expression in Python? python 2 + 3 * 4 ** 2 / 2 - 10

<p>26.0 (A)</p> Signup and view all the answers

Given:

x = 5
y = '7'
z = x + int(y)
print(z)

What value will then be printed for z?

<p>12 (B)</p> Signup and view all the answers

Flashcards

Program design

Programs must be designed before writing code.

Program development cycle

Design, write, correct syntax and logic, test.

Importance of design

Most important part of program development, understand the task, software requirements.

Algorithm

A set of well-defined logical steps to perform a task.

Signup and view all the flashcards

Pseudocode

Informal language with no syntax rules, used to create a program model.

Signup and view all the flashcards

Flowchart

A diagram that graphically depicts the steps in a program.

Signup and view all the flashcards

Input, Processing, Output

Computer process of receiving input, processing, and producing output.

Signup and view all the flashcards

Print function

Displays output on the screen.

Signup and view all the flashcards

Argument

Data given to a function.

Signup and view all the flashcards

String

Sequence of characters used as data.

Signup and view all the flashcards

String literal

String that appears directly in the code.

Signup and view all the flashcards

Comments

Notes of explanation within a program, ignored by Python.

Signup and view all the flashcards

End-line comment

Appears at the end of a line of code, explains the purpose.

Signup and view all the flashcards

Variable

Name that represents a value stored in computer memory.

Signup and view all the flashcards

Assignment statement

Used to create a variable and make it reference data.

Signup and view all the flashcards

Variable Naming Rules

Rules for naming variables in Python.

Signup and view all the flashcards

Displaying Multiple Items with the print Function

Python allows one to display multiple items with a single call to print.

Signup and view all the flashcards

Variable Reassignment

Variables can reference different values during program execution.

Signup and view all the flashcards

Garbage collection

Removal of values from memory that are no longer referenced.

Signup and view all the flashcards

Data types

Defines the type of value a variable can hold (e.g., int, float, str).

Signup and view all the flashcards

Numeric literal

Actual number written in a program's code.

Signup and view all the flashcards

input function

Reads input from the keyboard.

Signup and view all the flashcards

int() function

Converts item to an integer.

Signup and view all the flashcards

float() function

Converts item to a floating-point number.

Signup and view all the flashcards

Math Operator

Math operator: tool for performing calculation

Signup and view all the flashcards

Operands

Values surrounding an operator.

Signup and view all the flashcards

/ operator

Operator that performs floating point division

Signup and view all the flashcards

// operator

Operator that performs integer division

Signup and view all the flashcards

Python operator precedence

Python operator precedence.

Signup and view all the flashcards

Exponent operator (**)

Raises a number to a power.

Signup and view all the flashcards

Remainder operator (%)

Performs division and returns the remainder.

Signup and view all the flashcards

Breaking Long Statements into Multiple Lines

Breaking Long Statements into Multiple Lines

Signup and view all the flashcards

Print Function

Displays line of output

Signup and view all the flashcards

string literal

Special characters appearing in string literal.

Signup and view all the flashcards

Built in format function

Can format display of numbers on screen using built-in format function

Signup and view all the flashcards

Magic Number

Magic number is an unexplained numeric value that appears in a program's code.

Signup and view all the flashcards

Named Constant

You should use named constants instead of magic numbers.

Signup and view all the flashcards

Study Notes

Designing a Program

  • Programs must be designed before they are written
  • The program development cycle consists of; design, writing code, correcting syntax errors, testing the program, and correcting logic errors
  • Design is the most important part of the program development cycle
  • Understand the task of the program, working with the customer and creating software requirements

Algorithm

  • Determine the steps to perform the task by breaking it down into a series of steps
  • An algorithm is a set of well-defined logical steps to perform a task

Pseudocode

  • Pseudocode is fake code written in an informal language with no syntax rules
  • Informally models programs without compilation or execution, and is translated into actual code in any programming language
  • Focus should be on program design without worrying about syntax errors

Flowcharts

  • Flowcharts are diagrams that graphically depict the steps in a program
  • Ovals are terminal symbols
  • Parallelograms are input and output symbols
  • Rectangles are processing symbols
  • Symbols are connected by arrows represent the flow of the program

Input, Processing, and Output

  • A computer typically performs the three step process of input, processing, and output
  • Input is any data that the program receives while it is running, such as mathematical calculations
  • Output is a result of the process of the input
  • A function is a piece of prewritten code that performs an operation
  • A print function displays output on the screen
  • An argument is data given to a function, used like examples for what is printed
  • Statements in a program will execute in the same order that they appear

Strings and String Literals

  • Strings are sequences of characters used as data
  • String literals are strings represented in a program's code
  • String literals must be enclosed in single (') or double (") quote marks
  • String literals can be enclosed in triple quotes (''' or """)
  • Enclosed strings can contain both single and double quotes and can have multiple lines

Comments

  • Comments are notes of explanation within a program, which are ignored by Python and intended for a person reading the program's code
  • Comments begin with a # character
  • An end-line comment appears at the end of a line of code, typically explaining the purpose of that line

Variables

  • Variables are names that represents a value stored in the computer memory, used to access and manipulate data stored in memory
  • A variable references the value it represents
  • An assignment statement is used to create a variable and make it reference data using a general format
  • variable = expression, for example age = 29, where (=) is the assignment operator

Variable Assignments

  • In an assignment statement, a variable receiving a value must be on the left side
  • A variable can be passed as an argument to a function, with the variable name not enclosed in quote marks
  • A variable can only be used if a value is assigned to it

Variable Naming Rules

  • The rules for naming variables in Python are that; variable names cannot be Python key words, cannot contain spaces, the first character must be a letter or underscore, after the first character letters/digits/underscores are allowed, and variable names are case sensitive
  • Variable names should reflect their use
  • Python permits displaying multiple items with a single call to print
  • Items are separated by commas when passed as arguments
  • The arguments are displayed in the order they are passed to the function
  • Items are automatically separated by a space when displayed on a screen

Variable Reassignment

  • Variables can reference different values while the program is running
  • Garbage collection removes used values that are no longer referenced by variables via the Python interpreter
  • Variables can refer to items of any type, even if they have been assigned to a specific one

Data Types

  • Data types categorize values in memory
  • Int is for integer, float is for real numbers, and str is used for storing strings
  • A numeric literal is a number written in a program
  • Numbers with no decimal point are considered int, otherwise they are considered float
  • Some operations behave differently depending on data type

Reassigning Types

  • Variables in Python can refer to items of any type

Keyboard input

  • Most programs require reading input from the user
  • The built-in input function is used when reading input from a keyboard
  • All input is returned as string data
  • Variable = Input (Prompt) format
  • Value is typically a string prompting the user to value

Input Function

  • The input function always returns a string
  • Built in functions convert between data types
  • int(item) converts an item to an int
  • float(item) converts an item to a float
  • Nested function calls follow the Function1 (Function2 (Argument)) format
  • Value returned by Function 2 is passed to Function 1
  • Type conversion only works if the item is a valid numeric value, otherwise an exception is thrown

Calculations

  • The Math expression performs calculations and gives a value
  • Math operators is the tool used, operating on the surrounding variables
  • The resulting value is typically assigned to a variable
  • Regular division "/" is a floating point
  • Integer division "//" truncates

Operator Precedence

  • Python operator precedence is as follows
  • Operations enclosed in parentheses
  • Exponentiation (**)
  • Multiplication (*), division (/ and //), and remainder (%)
  • Addition (+) and subtraction (-)
  • Higher precedence is performed first, with same level proceeding from left to right

Exponents and Remainders

  • Exponent operator (**) raises a number to a power
  • Remainder operator (%) performs division operations and returns the remainder
  • Also known as a modulus operator
  • They can convert times and distances, or to detect odd or even numbers

Long Statements

  • Long statements cannot be viewed on screen without scrolling and cannot be printed without cutting them off
  • Multiline continuation character () allows you to break a statement into multiple lines
  • Any part of a statement enclosed in parentheses can be broken without the line continuation character

Data Output

  • The print function displays lines of output end= ' delimiter' replaces new lines in output data
  • sep='delimiter' replaces item separation in output data

String Operations

  • Special characters appearing in a string literal are preceded by a backslash ()
  • Examples are newline (\n), horizontal tab (\t)
  • The + operator performs string concatenation

Number Formats

  • You can format display of numbers on the screen using the built in format function
  • It requires two items including; Numeric value to be formatted and a Format specifier
  • Returns string containing formatted number
  • Format specifier typically includes precision and data type
  • Scientific notation, comma separators, and the minimum field used to display the value

Integers as Formats

  • The % symbol can be used in the format function to format a number as a percentage
  • To format an integer using the format function do not specify precision
  • Use "d" as the type designator, and still use format function to set field width or comma separator

Magic Numbers

  • A magic number is an unexplained numeric value that appears in a program's code
  • It is difficult to determine the purpose of the number
  • If this type of value needs to be changed it must be modified in every place it is used
  • There is a risk of making a mistake each time you type in the value

Named Constants

  • Named constants should be used instead of magic numbers
  • Named constant is a name representing a value that does not change during the program's execution

Constant Advantages

  • They make code self-explanatory
  • Help to make code easier to maintain
  • Help prevent typographical errors associated with manual entry

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Programming Fundamentals
25 questions

Programming Fundamentals

TriumphalBinomial2021 avatar
TriumphalBinomial2021
Program Design and Development
42 questions
Use Quizgecko on...
Browser
Browser