Skip to content

Instantly share code, notes, and snippets.

@jendiamond
Last active September 15, 2023 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jendiamond/d05b8b0828c374aad1ca to your computer and use it in GitHub Desktop.
Save jendiamond/d05b8b0828c374aad1ca to your computer and use it in GitHub Desktop.
Starting Out with Python 3rd Edition - Tony Gaddis Chapters 2-6

Starting Out with Python 3rd Edition Chapters 2-6 - Tony Gaddis

Chapter 2 & 3

conditionally executed -
block - statements in a group
George Boole - 1800's mathematician
string - a sequence of characters used as data
string literal - a string in the code
numeric literal - a number in the code
variable - name that represents a value stored in the computer's memory that references the value
assignment statement - creates a variable and makes it refence a piece of data
garbage collection - when a value is no longer referenced it is removed

variable rules

  • no keywords
  • no spaces
  • 1st char a-z or underscore NO numbers
  • case sensitive
  • only use letters, numbers or underscores no characters
  • name it well

data types

  • int
  • float
  • str

input - variable = input("prompt")

convert to int - item = int(item)
convert to float - item = float(item)
convert to string - item = str(item)

nested function - pay_rate = float(input("What's your pay_rate""))

math operators

  • float division - 5/2 >> 2.5
  • integer division - 5//2 >> 2
  • exponent - **
  • modulus - %

multiline - print('We sold, units', \
'a lot of stuff')

print - has a newline at the end

separator - print("Dog", "Cat", "Tree", sep=" ") >> Dog Cat Tree

separator - print("Dog", "Cat", "Tree", sep="") >> DogCat*Tree

separator - print("Dog", "Cat", "Tree", sep="---") >> Dog---Cat--Tree


escape characters

  • \n
  • \t
  • '
  • "
  • \

concatenation - print('This' + 'and that')
formatting #s
format(3965.436930, '.2f') 39965.44
format(3965.436930, '.1f') 39965.4
format(3965.436930, 1 e) 39965.44
format(3965.436930, 2 e) 39965.44
format(3965.436930, ',.2f') 39965.44
format(3965.436930, '12,.2f') 39965.44
format(3965.436930, '%') 39965.44
format(3965.436930, '.0%') 39965.44
format(3965.436930, '.2f') 39965.44
format(3965.436930, '.2f') 39965.44

syntax error - returns an error message
logic error - does not prevent program from running
software requirement - a single task the program must perform to satisfy the customer
algorithm - set of steps to perform task
function - piece of prewritten code that performs an operation
argument - data you want displayed on screen

operand 2 + 2 the 2 is the operand
operator - 2 + 2 the + is the operator
logical operators - and or not
relational operator - < > == <= >= !=
not operator - unary operator that takes a Boolean expression as it's operand and reverses its logical value
the and and or operators perform
short circuit evaluation - if one expression is false the expression does not continue to evaluate

types of operators

  • logical
  • relational
  • precedence
  • assignment

and

  • true and false -- false
  • false and true -- false
  • false and false -- false
  • true and true --- true

or

  • true or false -- true
  • false or true -- true
  • false or false -- false
  • true or true --- true

types of structures

  • decision/selection structure - execute a set of statements only under certain circumstances, conditional p.82-97
  • single alternative decision structure - one possible path of execution; use an if statement
  • dual alternative decision structure - two possible paths of execution; one if true, one if false
  • control structure - a logical design which controls the order a set of statements execute
  • sequence structure - set of statements that are executed in the order that they appear

equality - ==
assignment - =

numeric ranges -

Boolean variables - True or False

flag - a variable that signals when some condition exists in the program, often Boolean p.112

1.c 2.b 3.d 4.a 5.c 6.b 7.c 8.b 9.a 10.b 11.c 12.a

True or False 1.F 2.F 3.F 4.T 5.T

  1. conditionally executed -
  2. dual alternative decision structure** - two possible paths of execution; one if true, one if false
  3. and operator - all condition must be true
  4. or operator - one condition must be true
  5. flag - a variable that signals when some condition exists in the program, often boolean

Chapter 4

repetition structure - a loop - write code once put it in a structure that repeats as many times as necessary

condition controlled loop - while loop - uses a true false condition

while loop (pre-test loop) condition controlled loop - uses a true false condition

  • condition tests for T or F
  • a statement that is repeated as long as the condition is true
  • condition is tested BEFORE performing an iteration
  • it will NEVER execute if condition is false

for loop count controlled loop - a loop that repeats a specific number of times - designed to work with a sequence of data items.

for variable in [val1, val2, val3]:
  print(variable)

iteration - each execution of the body of the loop

target variable - the target of an assignment at the beginning of each iteration

for num in range(5):
  print(num)

for x in range(5):
  print('Hello World')

range(1,5) - 1 is the start value, 5 is the ending limit - prints 1 2 3 4

step value - if you add a 3rd argument to a range function it will increment that number of times

for num in range(1, 10, 2):
  print num

returns >> 1,3,5,7,9

accumulator - the variable used to keep the running total

running total - sum of numbers that accumulates with each iteration of the loop

  • a loop that reads each number in the series
  • a variable that accumulates the total of the numbers as they are read

augmented assignment operator += , -=, *=, %= (total = total + number) == total += number

sentinel value - a special value that marks the end of a sequence of values(you choose it and make it unique enough not to be confusing)

validation loop (error trap) (error handler)

  • input is read
  • loop is executed
  • if input data is bad the loop executes its block of statements
  • loop displays an error message to user

input validation - if input is invalid the program discards it and prompts user for the correct data

priming read - get users input and send it to the validation loop as a condition to be t or f If true run the program. If false prompt user for the correct data

nested loop - loop inside another loop

  • an inner loop goes through all of its iterations for every iteration of an outer loop
  • inner loops complete their iterations faster than outer loops
  • to get the total number of iteration of a nested loop - multiply the number of iteration of all the loops

Chapter 5

keyword argument - specifies which parameter variable the argument should be passed to

parameter_name = value

pass by value - a function CAN NOT change the value of an argument that was passed to it

positional argument & keyword argument - position must come first or error can be mixed

global variable - accessible to all the functions in the program file - a variable written outside any function
to assign a global variable inside a function you must declare it

number = 0
def main90:
  global number
  number = int(input('Enter a number'))
  show_number()
def show_number():
  print('The number is', number)
main()

Why restrict use of Global Variables

  • global variables makes debugging hard
  • functions that use global variables are usually dependent on them
  • make the program hard to understand

Global Constant - a global name that references a value that cannot be changed UPPERCASE

Boolean function - tests for a condition True or False p. 215

  • simplify complex conditions that are tested in decision & repetition structures
  • simplify complex input validation

return expession1, expression2

math module - p.219 Python Standard Library import math sqrt

modularization

  • must end in .py
  • not be a keyword

menu driven programs - p. 224 displays a list of operations on screen and allows the user to select the operation they want to run from the menu

flow chart - graphically depicts flow of logic inside a function

hierarchy chart & structure charts - shows relationship between functions

Standard Library - library functions

2 type of functions
value returning & void functions

  • group of statements that perform a specific task
  • when you want to execute the function you call it

value returning functions - returns a value to the part of the program that calls it

  • return a value to the part of the program that calls it
  • can be assigned to a variable
  • displayed on screen
  • can be used in a mathematical expression if it's a number
  • it can be used like any other value

modules - store standard library must import them to load into your file

random -

number = random.random.random
@64git
Copy link

64git commented Jan 30, 2018

Thanks very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment