Chapter 6: Programming Fundamentals

first, second, third replaced by la, lala, lalala

6.4 Iteration (Looping) Abstracts along Sequences

this is called an assignment | numbers in bracket assigned to variable named l

NOTE: iteration template / pattern for iteration is

for in :

___

NOTE: remove the indent to one level to enter the last line / represents one code block ending

total = 0 means initializing the variable total / assigns the value 0 to total

6.5 Polymorphisim Abstracts across Types

str means string / sequence of characters AKA "hello world"

Q: how to directly check the value of a string?

concatenation = the operation of putting two strings together by using +

Assign the strings you're using to variables and then concatenate the variables:

Using len() to provide the length of an argument:

works for both strings and lists

  • strings = returns how many characters are in the string
  • lists = returns how many elements the list has

Q: what does the in the bracket on the left mean?*

NOTE:

  • not everything has a length
  • not everything can be added together

NOTE:

  • don't take the length of an integer
  • don't add an integer to a string

Converting integers into strings

  • tells Python to treat integers as strings
  • tells us how many characters are in the string

^ tells Python unit '2' should be concatenated to 'hello'

  • casting = converting between types
  • polymorphism = when a single arithmetic operator/function/method is able to work on different types of data (integers/strings/lists)

Revisiting "Double, Double"

^ this code takes a list of volume levels and doubles it

What's happening in the line within the loop:

^ the variable was assigned the value of an empty list

^ the list is being built up AKA a list is being added/concatenated to the current result

what went wrong:

  • [] = empty list
  • 4.0 = float (number with a decimal point)

avoid DRY = use double() !

  • ✓ bundled
  • ✓ can be reused
  • ✓ does iteration (via using a for loop)
  • ✓ implements polymorphism | function can work on any element that can be multiplied by 2
  • ✓ can double strings
  • ✓ will work on sequences

Example of double() to double strings:

Q: ^ how do I define 'double' again??

Example of a list with different types of elements (that can be multipled) | double() can take the list as an argument:

double(countdown)

6.7 Testing Equality, True and False

Using = and == in Python:

  • = for assignment (giving a variable a value)
  • == to test if the left and right-hand sides are equal

NOTE: Boolean data type = value doesn't have quotation marks around it AKA True or False

NOTE: checking equality is most useful when there's at least one variable involved

Two-step equality test (to see what variables can be included):

(NOT A TEST): assigns a value to the variable "greeting" / nothing is returned

(A TEST): using the == operator

6.8 The Conditional, if

example 1:

example of test for equality using ==

a conversation with Python

  • "secret('please') returns the value Yes! to signal that the correct word was input
  • causes certain code to run ONLY if the condition is true
  • example of the conditional statemet AKA if statement

Q: if nothing returned, does this mean I just redefined secret with another word? or does it mean that the incorrect word was input?

example 2:

example 1.2:

new keyword else = indicates indented code that should run otherwise (when the if condition doesn't hold)

example 1.3 (secret() to return True or False instead of two strings:

this can be refactored

  • refactor = revise code to make it easier for people to read/understand/modify/further develop while keeping the function of the code the same
  • ex: True when word has the value 'please' and False otherwise -> new version of secret() =

return whether or not the value of word is the string please (so we can return the result of that equality test)

6.9 Division, a Special Error, and Types

don't ever allow your program to attempt to divide by zero

Strings can be multiplied by integers:

Strings cannot be divided by integers:

[Exercise 6-1] Half of Each: