Activity 1.1.2: Planning a Picture - Exhaustive Study Guide
Learning Goals and Core Objectives
Utilize variables, user inputs, and program outputs within executable Python programs.
Construct geometric arcs, regular polygons, and custom shapes using fill color capabilities in Python Turtle.
Analyze and explain the fundamental architectural differences between a class and an object.
Identify and classify code commands as object properties or object methods.
Interactive Algorithms, Input, and Output
Interactive Algorithms: Algorithms can be designed to accept external information, known as input, during execution. Allowing user choices renders programs interactive and enables customized experiences.
Sandwich-Making Conceptual Example:
Input Statements: Asking the user to select choices for bread type, meat type, and cheese type.
Processing: Algorithmic execution dependent on user selections.
Output: The resulting customized sandwich.
Program Input Methods and Devices:
Keystrokes entered on a keyboard.
Mouse clicks executed with a mouse.
Screen taps on a touch screen.
Voice commands captured through a microphone.
Sensor readings gathered from external components (e.g., temperature sensors, light sensors, or circuit board attachments).
Program Output Methods and Devices:
Displaying textual or visual information on a visual display screen.
Sending textual or graphical data to a printer.
Playing audio or sound clips through speakers, circuit board audio units, or robotic sound modules.
Activating hardware mechanisms, such as illuminating lights or driving motors on external circuit boards and robots.
Python Input and Output Mechanics
Python Terminal Interface: The display environment for text execution split into two functional areas: a graphical output canvas above and a text terminal below. The interface height can be adjusted using the horizontal resize bar.
Variables: Designated storage locations within a program that hold data. Each variable manages one distinct value at any given point during execution.
Assignment Operator (
=):Assigns the evaluated value of the expression on the right-hand side to the variable declared on the left-hand side (e.g.,
user_name = "student").Does not represent mathematical equality or equivalence.
College Board Pseudocode Representation: Uses a leftward arrow notation: Evaluates the expression and assigns its result to variable .
User Input Command (
input()):Prompts the user for entry via the keyboard and retrieves the entered value.
College Board Pseudocode Representation: Accepts a value provided by the user and returns that input value.
Display Output Command (
print()):Outputs text or evaluated data directly to the terminal interface.
College Board Pseudocode Representation: Displays the evaluated value of the expression, followed immediately by a space.
Data Types, Conversion, and Comments
Code Comments:
Lines beginning with the
#symbol in Python.Non-executable text annotations ignored by the interpreter during runtime.
Purpose: Enhances code readability, facilitates collaborative development, and aids program maintenance.
Note: Comment mechanisms vary across programming environments; alternative documentation strategies are required when comments are unsupported.
Data Types in Python:
Supported built-in data types include numbers (integers, floats), Booleans, lists, and strings.
String Data Type (
str): An ordered sequence of characters.Input Data Behavior: The
input()function exclusively returns data as a string data type, regardless of numerical entry (e.g., entering100outputs"100"as three text characters rather than numeric quantity100).Substring: A smaller, contiguous segment of an existing character string.
Type Restriction: String data types cannot be used in mathematical calculations.
Data Type Conversion (
int()):Converts string representations of numeric digits into true integer numbers suitable for arithmetic operations (e.g.,
num = int("100")).Program Logic Example (Birth Year Calculation):
Goal: Estimate birth year using
birth_year = curr_year - age.Error Source: Subtracting a string variable
agefrom an integercurr_yeargenerates a type error.Solution: Wrap the input statement with
int()to convert string input into an integer.
Parentheses and Evaluation Order:
Syntax requires strictly balanced open and closed parentheses pairs.
Processing occurs from the innermost parentheses outward:
age = int(input("How old are you?"))
Inner step:
input("How old are you?")displays the string prompt and captures input as a string.Outer step:
int(...)converts the returned string into an integer.Assignment step: Stores the integer value inside variable
age.
Advanced Turtle Graphics Methods
Turtle Object Variables:
Default variable names like
painter(representing the drawing instrument) andwn(representing the screen canvas window) are customizable identifiers selected by the programmer.
The Extended
circle()Method:Full Signature:
circle(r, e, s)First Parameter
r(Radius): Mandatory numeric parameter specifying the radius of the circle.Second Parameter
e(Extent): Optional parameter specifying the arc angle in degrees. If omitted, the full circle is drawn. An extent of produces a semicircle.Third Parameter
s(Steps): Optional parameter specifying the number of straight line segments used to approximate the curve.Geometric Applications:
Hexagon:
t.circle(100, 360, 6)ort.circle(100, 6)draws a 6-sided polygon.Three-quarters Circle:
t.circle(100, 270)draws an arc across .Half Square:
t.circle(100, 180, 2)draws 2 sides of a square across an extent of .One-quarter Octagon:
t.circle(100, 90, 2)draws 2 sides of an 8-sided polygon across an extent of .
Color and Pen Control Methods:
.pencolor(c): Sets the pen drawing color using a color string literal (e.g.,"red") or variable..fillcolor(c): Sets the shape fill color using a color string literal or variable (e.g.,fav_color = "green",t.fillcolor(fav_color))..begin_fill(): Marks the starting boundary of enclosed paths to be filled. Requires.fillcolor()to be defined beforehand..end_fill(): Closes the filled shape path and applies the fill color. Must be preceded by.fillcolor()and.begin_fill()..penup(): Lifts the pen off the canvas to allow coordinate repositioning without drawing lines..pendown(): Lowers the pen onto the canvas to resume drawing lines.
Object-Oriented Programming Fundamentals
Object-Oriented Programming (OOP) Paradigm: Focuses primarily on the data representations and state of problem components, contrasting with purely task-focused procedural paradigms.
Code Reusability: OOP abstraction removes repetitive details, reducing the frequency of code rewrite.
Class: A structural design blueprint or template recipe defining properties and behaviors.
Object: A specific operational instance generated from a class blueprint.
Properties: Characteristics, state variables, or static attributes belonging to an object.
Methods: Actions, callable functions, or operational behaviors that an object can perform.
Method Execution Analysis (
my_turtle.circle(5)):my_turtle: Identifier referencing the object instance.circle: Method action executed by the object.
Questions & Discussion
Data Type Evaluation Questions:
Question: Which of the following code statements will NOT assign variable
xa string data type?x = int("100")(Assigns an integer data type).x = 500(Assigns an integer data type).x = 50(Assigns an integer data type).Question: Which of the following statements assign variable
xan integer data type?x = 500x = 50x = int("100")Question: What is the output resulting from the following program?
python x = "My favorite color is" y = "blue" print(x, y) Output:
My favorite color is blue
Conceptual Review Questions:
Question: What is the difference between program input and output, and how does one affect the other? Give an example using Python code.
Response: Input is data supplied to a program from an external source (such as user keyboard entries or sensor data) during runtime. Output is the processed result generated and delivered back by the program to the user or external environment (such as terminal text display, drawn canvas shapes, or active motor movement). Input directly parameterizes execution; changing input variables dynamically alters processing operations and resultant outputs. For example, capturing user input via
user_color = input("Enter color: ")and passing it tot.fillcolor(user_color)directly alters the visual output rendered on the canvas.Question: Is there only one way to solve a programming problem? Explain and give an example using Python Turtle.
Response: No, computer science problems generally possess multiple valid solution paths. Programs can achieve identical final visual outputs using different structures or algorithmic approaches. For example, drawing a square with Python Turtle can be accomplished by explicitly calling individual
forward()andright()commands four times sequentially, by using aforloop that iterates four times over movement steps, or by executing a singlecircle(100, 360, 4)method call specifying four step segments.