Podcast
Questions and Answers
Flashcards
Program development cycle
Program development cycle
Steps to design, write, and correct errors in a program.
Algorithm
Algorithm
A set of well-defined logical steps to perform a task.
Pseudocode
Pseudocode
An informal language that has no syntax rules, used to create a model program.
Flowchart
Flowchart
Signup and view all the flashcards
Input
Input
Signup and view all the flashcards
Processing
Processing
Signup and view all the flashcards
Output
Output
Signup and view all the flashcards
print function
print function
Signup and view all the flashcards
Argument
Argument
Signup and view all the flashcards
String
String
Signup and view all the flashcards
String literal
String literal
Signup and view all the flashcards
Comments
Comments
Signup and view all the flashcards
Variable
Variable
Signup and view all the flashcards
Assignment statement
Assignment statement
Signup and view all the flashcards
Garbage collection
Garbage collection
Signup and view all the flashcards
Data types
Data types
Signup and view all the flashcards
Numeric literal
Numeric literal
Signup and view all the flashcards
input() function
input() function
Signup and view all the flashcards
int(item)
int(item)
Signup and view all the flashcards
float(item)
float(item)
Signup and view all the flashcards
Nested function call
Nested function call
Signup and view all the flashcards
Math operator
Math operator
Signup and view all the flashcards
Operands
Operands
Signup and view all the flashcards
Types of division
Types of division
Signup and view all the flashcards
Exponent operator (**)
Exponent operator (**)
Signup and view all the flashcards
Remainder operator (%)
Remainder operator (%)
Signup and view all the flashcards
Mixed-Type Expressions
Mixed-Type Expressions
Signup and view all the flashcards
Multiline continuation character ()
Multiline continuation character ()
Signup and view all the flashcards
String Concatenation
String Concatenation
Signup and view all the flashcards
Implicit String Literal Concatenation
Implicit String Literal Concatenation
Signup and view all the flashcards
print() function arguments
print() function arguments
Signup and view all the flashcards
print() function separator
print() function separator
Signup and view all the flashcards
Special characters in string literal
Special characters in string literal
Signup and view all the flashcards
F-strings
F-strings
Signup and view all the flashcards
Format specifier designators
Format specifier designators
Signup and view all the flashcards
Magic Numbers
Magic Numbers
Signup and view all the flashcards
Named Constants
Named Constants
Signup and view all the flashcards
turtle
turtle
Signup and view all the flashcards
turtle.forward(n)
turtle.forward(n)
Signup and view all the flashcards
turtle.left(angle)
turtle.left(angle)
Signup and view all the flashcards
turtle.setheading(angle)
turtle.setheading(angle)
Signup and view all the flashcards
Study Notes
Designing a Program
- Programs must be designed before they are written
- Program development cycle involves designing a program, writing code, correcting syntax and logic errors, and testing
- Design constitutes the most important part of the program development cycle
- Understanding the program's task involves working with customers, asking questions, and outlining software requirements
- Steps to perform a task include breaking down the required task into a series of steps and creating an algorithm
Pseudocode
- Pseudocode translates to fake code
- Pseudocode is an informal language with no syntax rules
- It's not meant to be compiled or executed, but to create a model program
- Focus is on program design, as there are no concerns over syntax errors
- Pseudocode can be translated directly into actual code in a programming language
Flowcharts
- A flowchart is a diagram that depicts the steps in a program graphically
- Ovals are used as terminal symbols
- Parallelograms signify input and output
- Rectangles represent processing
- Arrows connect symbols, representing program flow
Input, Processing, and Output
- Computers generally perform a three-step process
- Input is received, or any data the program gets while running
- Processing occurs, such as a mathematical calculation
- Output is produced
Displaying Output with Print Function
- A function is a prewritten piece of code that performs an operation
- The
print
function displays output on the screen - An argument is data passed to a function, such as data printed to the screen
- Program statements execute in the order they appear, from top to bottom
Strings and String Literals
- A string constitutes a sequence of characters used as data
- A string literal appears in actual program code
- String literals must be in single (') or double (") quotes
- Triple quotes (""" or ''') may enclose string literals
- Enclosed strings can contain single and double quotes and span multiple lines
Comments
- Comments are notes of explanation within a program, which the Python interpreter ignores
- Comments are intended for people reading the program's code
- Begin comments with a
#
character - End-line comments appear at the end of a line of code
- End-line comments typically explains the line's purpose
Variables
- A variable represents a value stored in computer memory
- Variables are used to access and manipulate that data
- The variable references the value it represents
- An assignment statement creates a variable and makes a reference to data
- General format:
variable = expression
- Example:
age = 29
- The equal sign (=) is the assignment operator
Variables (continued)
- In an assignment statement, the variable receiving a value has to be on the left side
- A variable can be passed as an argument to a function
- Variable names should never be enclosed in quotes
- You can only use a variable if a value has been assigned to it
Variable Naming Rules
- Rules for naming variables in Python
- Variable name cannot be a Python key word
- Variable name cannot contain spaces
- First character must be a letter or an underscore
- After the first character, you can use letters, digits, or underscores
- Variable names are case sensitive
- Variable names should reflect its purpose
Displaying Multiple Items with the print
Function
- Python can display multiple items with a single call to
print
- Commas separate items when passed as arguments
- Arguments are displayed in the order they are passed to the function
- Items are automatically separated by a space when displayed
Variable Reassignment
- Variables can reference different values while a program runs
- Garbage collection removes variables that are no longer referenced
- Garbage collection is carried out by the Python interpreter
- A variable can refer to items of any type
- A variable can be assigned to one type and reassigned to another type
Numeric Data Types, Literals, and the str
Data Type
- Data types categorize values in memory
int
is for an integerfloat
is for a real numberstr
is for storing strings- A numeric literal is a number written in a program
- No decimal point means it’s considered
int
- A decimal point means it’s considered
float
- Some operations behave differently depending on data type
Reassigning a Variable to a Different Type
- A variable in Python can refer to items of any type
Reading Input from the Keyboard
- Most programs read from the user
- The built-in
input
function reads from the keyboard - The
input
function returns the data as a string - Format:
variable = input(prompt)
prompt
is a string that instructs users tp enter a value- A space is not automatically displayed after prompts
Reading Numbers with the input
Function
- The
input
function always returns a string - Built-in functions can convert between data types
int(item)
converts the item to anint
float(item)
converts the item to afloat
- A nested function call has the general format:
function1(function2(argument))
- the value returned by
function2
is passed tofunction1
- Type conversion works only if the item is a valid numeric value; otherwise, it throws an exception
Performing Calculations
- Math expressions perform calculations and give a value
- Math operators perform calculations
- Operands are values surrounding the operator
- variables can be used as operands
- Resulting values are typically assigned to a variable
- There are two types of division:
/
and//
/
performs floating point division//
performs integer division- positive results are truncated, negative results are rounded away from 0
Operator Precedence and Grouping with Parentheses
- Python operator precedence:
- Operations enclosed in parentheses
- enforces operations to be performed before others
- Exponentiation
(**)
- Multiplication
(*), division (/ and //), and remainder (%)
- Addition
(+)
and subtraction(-)
- Higher precedence is performed first
- Same precedence operators execute from left to right
The Exponent Operator and the Remainder Operator
- Exponent operator
(**)
raises a number to a power - Syntax:
x ** y = x^y
- Remainder operator
(%)
performs division and returns the remainder - also known as modulus operator
- Example:
4%2=0
,5%2=1
- Used to convert times and distances and to detect odd or even numbers
Converting Math Formulas to Programming Statements
- An operator is required for any mathematical operation
- When converting mathematical expressions to programming statements:
- Add multiplication operators where necessary
- Insert parentheses where necessary
Mixed-Type Expressions and Data Type Conversion
- The type of data resulting from a math operation depends on the data types of operands
- Two
int
values: result is anint
- Two
float
values: result is afloat
int
andfloat
:int
's temporarily converted tofloat
's, result is afloat
- mixed-type expression
- Conversion from
float
toint
can cause truncation of the fractional part
Breaking Long Statements into Multiple Lines (1 of 2)
- Lines of code that cannot be viewed on screens or printed due to length can be broken up
- The multiline continuation character
(\)
allows breaking a statement into multiple lines
Breaking Long Statements into Multiple Lines (2 of 2)
- Any part of a statement that is enclosed in parentheses can be broken without the line continuation character
String Concatenation
- String concatenation appends one string to the end of another
- The + operator does string concatenation
Implicit String Literal Concatenation
- String literals written adjacent to each other are implicitly concatenated into a single string
More About the print
Function
- The
print
function displays a line of output - A newline character is at the end of printed data
- The special argument
end='delimiter'
causesprint
to place the delimiter at the end of the data, instead of the newline character - The
print
function uses space as an item separator - The special argument
sep='delimiter'
causesprint
to use the delimiter as an item separator - Special characters appearing in string literals are preceded by a backslash (\)
- Examples: newline
(\n)
, horizontal tab(\t)
- Treated as commands embedded in strings
Displaying Formatted Output with F-strings
- F-strings are string literals prefixed with the letter
f
- F-strings support placeholders for variables
- Placeholders can also be expressions evaluated at runtime
- Format specifiers can be used with placeholders
.2f
means round the value to 2 decimal places and display the value as a floating-point number- The order of designators in a format specifier:
[alignment] [width] [,] [.precision] [type]
Magic Numbers
- Magic numbers are numeric values appearing in a program's code with no explanation
- It can be hard to determine its purpose
- Taking a lot of effort to change the number in each location
- Risk of making an error each time you use the magic number
Named Constants
- Name constants should be used instead of magic numbers
- A named constant is a name representing a value that will not change during the program
- Example:
INTEREST_RATE = 0.069
- This creates a named constant named
INTEREST_RATE
assigned the value 0.069 - Named constants can be used instead of magic numbers
Advantages of Using Named Constants
- Named constants make code self-explanatory (self-documenting)
- Make code easier to maintain
- Prevents typographical errors that are common when using magic numbers
Introduction to Turtle Graphics
- Python's turtle graphics system displays a small cursor known as a turtle
- Python statements can be used to move the turtle, drawing lines and shapes
Introduction to Turtle Graphics (2 of 2)
- Import the turtle module with the statement
import turtle
to use the turtle graphics system - This loads the turtle module into memory
Moving the Turtle Forward
- Use the
turtle.forward(n)
statement to move the turtle forwardn
pixels
Turning the Turtle
- The turtle's initial heading is 0 degrees (east)
- The
turtle.right(angle)
statement turns the turtle right by the given angle in degrees - The
turtle.left(angle)
statement turns the turtle left by the given angle in degrees
Setting the Turtle's Heading
- Use the
turtle.setheading(angle)
statement to set the turtle's heading to a specific angle
Setting the Pen Up or Down
- When the turtle's pen is down, the turtle draws a line as it moves
- By default, the pen is down
- When the turtle's pen is up, the turtle does not draw as it moves
- Use the
turtle.penup()
statement to raise the pen - Use the
turtle.pendown()
statement to lower the pen
Drawing Circles
- Use the
turtle.circle(radius)
statement to draw a circle with a specified radius
Drawing Dots
- Use the
turtle.dot()
statement to draw a simple dot at the turtle's current location
Changing the Pen Size and Drawing Color
- Use the
turtle.pensize(width)
statement to change the width of the turtle's pen in pixels - Use the
turtle.pencolor(color)
statement to change the turtle's drawing color - A complete list of colors is in Appendix D
Working with the Turtle's Window
- Use the
turtle.bgcolor(color)
statement to set the window's background color - A complete list of colors is in Appendix D
- Use the
turtle.setup(width, height)
statement to set the size of the turtle's window in pixels - Arguments
width
andheight
are width and height in pixels
Resetting the Turtle's Window
- The
turtle.reset()
statement does the following: - Erases all drawings in the graphics window
- Resets the drawing color to black
- Resets the turtle to it's original position (center)
- Does not reset the graphics background color
- The
turtle.clear()
statement does the following: - Erases all drawings that currently appear in the graphics window
- Does not change the turtle's position
- Does not change the drawing color
- Does not change the graphics window's background color
- The turtle.clearscreen() statement does the following:
- Erases all drawings that currently appear in the graphics window
- Resets the drawing color to black
- Resets the turtle to it's original position (center)
- Resets the graphics window's background color to white.
Working with Coordinates
- The turtle uses Cartesian Coordinates
Moving the Turtle to a Specific Location
- Use the
turtle.goto(x, y)
statement to move the turtle to a specific location - The
turtle.pos()
statement displays the turtle's current X, Y coordinates - The
turtle.xcor()
statement displays the turtle's current X coordinate - The
turtle.ycor()
statement displays the turtle's current Y coordinate
Animation Speed
- Use the
turtle.speed(speed)
command to change the turtle's speed of movement - The speed argument ranges from 0 to 10
- A speed of 0 makes all of the turtle's moves instantly and disables animation
Hiding and Displaying the Turtle
- Use the
turtle.hideturtle()
command to hide the turtle - This command does not change the way graphics are drawn. It simply hides the turtle icon
- Use the
turtle.showturtle()
command to display the turtle
Displaying Text
- Use the
turtle.write(text)
statement to display text in the turtle's graphics window - The
text
argument is a string you want to display - The lower-left corner of the first character will be positioned at the turtle's X and Y coordinates
Filling Shapes
- To fill a shape with color:
- Use the
turtle.begin_fill()
command before drawing the shape - Use the
turtle.end_fill()
command after the shape is drawn - When the
turtle.end_fill()
command executes, the shape will be filled with the current fill color
Getting Input With a Dialog Box
- Specify a default value, minimum value, and maximum value with turtle.numinput
- An error message will be displayed if the input value is less than
minval
or greater thanmaxval
Keeping the Graphics Window Open
- The graphics window closes immediately when the program finishes while running a turtle graphics program outside IDLE
- To prevent this, add the
turtle.done()
statement to the very end of your turtle graphics programs - This will cause the graphics window to remain open after the program finishes
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.