Skip to main content

Design and Coding Basics

Everything in this class that we will learn is keyed to the Gaddis text. This page is not set up a a flowing narrative. It supports the book and is external to the book. These abbreviated concepts are my personal notes from the book; highlights I want to be sure to cover in class. If you come back to this page later, and you can't grasp these abbreviated notes, log into the online book where you can find a flowing narrative. Or scan over them before you do the reading.

gaddis 2.1

Chapter 2 in Gaddis Text If you are renting the online book, you can access it here: https://www.pearson.com/en-us/pearsonplus.html

Chapter 2 Slides

2.1 Program Development cycle

Link to Section 2.1 in the Book

2.2 Input, Processing, and Output

Supplemental: Nice overview at Runestone Academy with some interactive learning

2.3 Print Function

Tony Gaddis demos the print function in a 6:15 minute video in the online book. This is a Python pre-built function which introduces the concept of calling the function and using an argument. String literals are also introduced in this section. And different quotation mark options.

Every single code sample in this session will use the print() function in some form or fashion. link to first code sample

2.4 Comments

Comments at Python4erBody

# This is a python comment on one line which is the most common way to comment. 

# You can comment on multiple lines
# By using a series
# of one line comments.

"""
This is how to write a comment on
multiple lines so you don't have to
add a hashtag before each line.
"""

print('Use in-line comments sparingly.') # This is an in-line comment.

# PEP 8 recommends using inline comments sparingly.

There is a lot more to learn about coding conventions via the Python Enhancement Proposals Style Guide (PEP 8)

2.5 Variables

MUST KNOW Terms/ConceptsExample
Assignment operator is equal symbol=
Equal as a math function is two equal symbols (will practice this later)==
Assignment statementage = 18
multiple assignment statement (useful for math or other short variables)x,y,z = 0,1,2
multiple assignment statement example 2age, name = 22, 'Chung'
variables are stored in memory
passing variables
variable must precede any code that needs to use the variable
rules for naming variablesPython4Erbody

Displaying multiple items with the print function:

name = 'Sally'
car = 'Honda Civic'
pet = 'cat'
print('My name is', name, ', and I drive a', car, ', and I have a', pet, '.')

Output with default spacing needs some "debugging" (we will deal with this in Section 2.9):

My name is Sally , and I drive a Honda Civic , and I have a cat .

variable reassignment

Data types: numbers, numeric literal, float, using the type function to determine data type

2.6 User Input

height = input("Enter your height in inches")

print("You entered", height, "for your height")

if you enter 72 you get this output:

Enter your height in inches: 72
You entered 72 for your height.

if you enter Very TaLL, I yam!! ;) lol you get this output:

Enter your height in inches: Very TaLL, I yam!! ;) lol
You entered Very TaLL, I yam!! ;) lol for your height.

Your program accepted variable number or crazy text both at strings. So the default type for input is a string, str. That is important to know because if you try to do math with string input, you will get a not-int TypeError:

height = input("Enter height to closest inch: ")
width = input("Enter width to closest inch: ")

print("Area is multiplication of height and width:",height * width)

output:

Enter height to closest inch: 6
Enter width to closest inch: 4
Traceback (most recent call last):
File "/Users/lawrencejones/Desktop/python.py", line 4, in <module>
print("Area is multiplication of height and width:",height * width)
TypeError: can't multiply sequence by non-int of type 'str'

2.7 Calculating Math in Python

salary = 2500.0
bonus = 1200.0
pay = salary + bonus
print('Your pay is', pay)

2.8 String Concatenation Formatting Data Output


first_name = input('Enter your first name: ')
last_name = input('Enter your last name: ')

full_name = first_name + ' ' + last_name

print('Your full name is ' + full_name)

2.9 More About the print Function

print('do','re','me')
print('do','re','me', sep='')
print('do','re','me', sep='*')
print('do','re','me', sep=' da-da ', end=' doobie-da') # Jazzing up do, re, mi. Notice the extra spaces.

2.10 Displaying Formatted Output with F-strings

amount_due = 5000.0
monthly_payment = amount_due / 12.0
print(f'The monthly payment is {monthly_payment}.')

2.11 Named Constants

A = π r²

PI = 3.1415926 # Pi is a constant; it should never be changed as variables are changed.
radius = float(input("input the Radius: "))
area = PI * (radius * radius)
print(f'The area of the circle with the entered radius of {radius} is {area}.')