Clarifying Function Calls; Notes

Resource: Practical Programming – An Introduction to Computer Science Using Python 3, 2nd Edition by Paul Gries, Jennifer Campbell, and Jason Montojo

Chapter 3: Designing and Using Functions
ep44/ 3.1 Functions that Python Provides

An example of a built-in function is abs , which produces the absolute value of a number:

Example 1:
>>> abs(-9)                                               [function call]
9

Example 2:
>>> abs(3.3)                                             [function call]
3.3

General form of a function call

<<function_name>> (<<arguments>>)

argument: an expression that appears between the parentheses of a function call

In Example 1, the argument is -9.
In Example 2, the argument is 3.3.

Example 3: Calculating the difference between a day and night temperature

>>> day_temperature = 3
>>> night_temperature = 10
>>> abs(day_temperature – night_temperature)
7

  • The argument for function call abs(day_temperature – night_temperature) is day_temperature – night_temperature.

 

  • day_temperature refers to 3 and night_temperature refers to 10.

 

  • Python evaluates the expression day_temperature – night_temperature (3 – 10) to -7.

 

  • This value of -7 is passed to function abs , which returns the value 7.

 

Rules for executing a function call 

  1. Evaluate each argument one at a time, working from left to right.

  2. Pass the resulting values into the function.

  3. Execute the function. When the function call finishes, it produces a value.

Invent with Python — Chapter 3: Writing Programs; Notes

Topics covered:

  • Flow of execution
  • Strings
  • String concatenation
  • Data types (strings, integers)
  • Using the file editor to write programs
  • Saving and running programs in IDLE
  • The print() function
  • The input() function
  • Comments
  • Case-sensitivity

Learn to: store text in variables, combine text, and display text on the screen

Strings

string: a text value

  • string values can be used just like integer and float values
  • can store strings in variables
  • string values start and end with a single quote (‘)
  • or a double quote (“)
  • Ex:
    >>> spam = ‘hello’
    >>> spam
    ‘hello’
    >>> spam = “hello”
    >>> spam
    ‘hello’

NB: Single or double quotes tell Python where the string begins and ends; they are not part of the string value{?}

  • strings can comprise of any keyboard character with no character count limit;
  • Examples:
    -‘Hi there!’
    -‘KITTENS’
    -‘7 apples, 14 oranges, 3 lemons!!’
    -‘0*&#wY%***05FDLKJ okay therkjlajksf568415224599@#$#$!’

 

String Concatentation 

string concatentation: the combination of two strings with the + operator

  • Just like integer and float values do, string values can combine with operators to make expressionsExample:
    >>> ‘Hello’ + ‘World!’             [expression]
    ‘HelloWorld!’                           [expression evaluates to a single string value]
    >>> ‘Hello’ + ‘ World!’            [notice the single space after the first single quote]
    ‘Hello World!’

 

  • + operator works differently on string values vs. integer and float values because they are different data types
  • All values have a data type
    Example:The data type of the value ‘Hello’ is a string.
    The data type of the value 5 is a an integer. 

data type:  tells Python what operators should do when evaluating expressions

  • + operator will concatenate string values but add integer and float values

concatenation errors

NB: Different error types depending on the first value

 

Hello World! 

hello world july 6

How it works

  • Each line of code is an instruction interpreted by the Python interpreter software (will be referred to as Python from now on)
  • These instructions make up the program

Execution: the step Python is at in the program;
ex: when the program starts, the execution is at the first instruction; after executing     the instruction, the execution moves down to the second instruction, and so on etc.

Comments

#This is a comment

This instruction is a comment : any text following a sign (pound sign)

  • Comments are for the programmers — notes of what their code does, usually at the top of the code
  • Comments are ignored by Python

 

Functions 

Function:
-mini-program inside the ‘main’ program
-contains instructions to execute when the function is called

Built-in function: Python-provided function; ex: print(), input()

Function call: an instruction that tells Python to run the code inside the function*
Example:
>>> print(‘Hello World!’)            [function call – a call to the print() function]

*The code inside built-in functions such as print() are not visible to us

A program calls the print() function – Python takes the string between the parentheses as input and displays the text on the screen

QUESTION: what is the visual difference between a function and a function call?

argument: the value between the parentheses in a function call
Example: ‘Hello World!’ is the argument in print(‘Hello World!’)

passing the argument to the print() function – argument(s) inputted between the parentheses {?}

input() function

>>> myName = input()        [an assignment statement]

Variable: myName
Function call: input()

When the function input() is called, the program waits for the user to enter text.
The text string the user enters becomes the value that the function call evaluates to.

NB: Function calls can be used in expressions that values can be used in.

return value: the value that the function call evaluates to/returns

In the case of the input() function, the return value is the string that the user typed in.

Example:
myName input()

The user typed in May.
The input() function call evaluates to the string ‘May’.
The string ‘May’ gets stored in the myName variable.

 

Using Expressions in Function Calls 

>>> print(‘It is good to meeet you, ‘ +myName)      [print() function call]

NB: Arguments are always single values.

  1. Python evaluates this expression
  2. Pass that value as the argument

If Albert is the value stored in the myName variable, the evaluation would progress like this:

print albert

Ending the Program 

A program terminates/exits: stops running
when the program executes the last line

  • Python forgets all of the values stored in the variables, until the program runs again; ex:
    the string stored in myName; when the program runs again and you enter a different name, the program will use that as your name

 

Naming Variables

  • Variable names are case-sensitive; ex: SPAM, spam, Spam, sPAM — are four different variables in Python
  • A bad idea to use different case variables; use descriptive names instead
  • Variable names are usually lowercase
  • Short names are better than long names
  • If there is more than one word in a variable name, capitalise each word after the first word
  • Or add underscores; ex: first_country

Invent with Python — Chapter 2: The Interactive Shell; Notes

Topics Covered — Basic Programming Concepts:

  • Integers and Floating Point Numbers
  • Expressions
  • Values
  • Operators
  • Evaluating Expressions
  • Storing Values in Variables

Integers and Floating Point Numbers

integer (int): a whole number; ex: 5, 600, 76, 1, 0, … etc.

floating point number (float): a fraction or a number with a decimal point; ex: 3.5, 7.998, 23.0, 5.0, … etc.

value: any integer or floating point number

Expressions

python math operators

Expression: comprised of values connected together by operator(s); ex: math problems like 2+2+3/2*2, 1+1, … etc.

NB: There can be any number of spaces between values and operators;
ex:
>>> 2   +                 2
4

Evaluating Expressions

Evaluate an expression: reduce that expression to a single value; ex: getting 15 from 10+5

NB1: A single value is also an expression; ex: the expression 15 evaluates to the value 15

NB2: The / division operator evaluates to a float value; ex: 24/2 evaluates to 12.0

NB3: Math operations w/ float values also evaluate to float values; ex: getting 14.0 from 12.0 + 2

Syntax Errors 

syntax error: Python doesn’t understand the instruction because it is typed incorrectly

Example:
>>> 5 +
Syntax Error: invalid syntax

This error occurred because 5 + is not an expression.
Expressions have values connected by operators. The + operator expects a value after itself, but it is missing.

Storing Values in Variables 

variable:
-like a box that can store a value

statement: an instruction that doesn’t evaluate to any value

assignment statement: instruction to store a value inside a variable; ex: spam = 15
assignment operator: =

spam 15

SUMMARY

There are two types of instruction:

  1. Expression: evaluates to a single value
  2. Statement: doesn’t evaluate to any value

 

Other Notes

  • Python creates a variable the first time this variable is used in an assignment statement
  • A variable stores a value, not an expression; ex: spam = 10 + 5 v.s. spam = 10 + 7 -2 ; they both evaluate to 15; the end result is the same; both assignment statements store the value 15 in the variable spam, not the expressions 10 + 5 or 10 +7-2
  • If a value is stored in a variable, the variable can be used in an expression; ex:
    >>> spam = 1
    >>> spam
    1
    >>> spam + 5
    6
  • Can change the value stored in a variable by entering another assignment statement; ex:
    >>> spam = 15
    >>> spam + 5
    20
    >>> spam = 3
    >>> spam + 5
    8
  • Cannot use a variable before an assignment statement creates it; Python will return NameError b/c no such variable by that name exists yet

Starting with — Invent Your Own Computer Games with Python; Notes

Resource: Invent Your Own Computer Games with Python, 3rd Edition by Al Sweigart

Objective: Learn how to program by making simple video games.

Chapter 1 — Installing Python 

program: instructions in a language that a computer can understand

Python: one such language

Downloading and Installing Python 

Python interpreter software: a program that understands the instructions written in Python

.
.
.

Starting IDLE

IDLE:
Interactive DeveLopment Environment
-A development environment, like a word processing software for writing Python programs
-Starting IDLE is different on each OS
-On Windows, search for IDLE (Python GUI)

interactive shell:
-the window that appears when IDLE is run
-where Python instructions are entered (after the >>> prompt)
-the Python interpreter software will perform them

How to Use this Book

Each chapter begins with a sample run of a program, showing you what the program looks like when you run it.

Type the code for the program into IDLE’s file editor yourself to remember the programming better.

Play around with the code.

Finding Help Online

 

“When asking programming questions, do the following:

  • If you are typing out the programs in this book but getting an error, first check for typos with the online diff tool at http://invpy.com/diff. Copy and paste your code into the diff tool to find any differences from the book’s code in your program.
  • Explain what you are trying to do when you explain the error. This will let your helper know if you are on the wrong path entirely.
  • Copy and paste the entire error message and your code.
  • Search the Web to see whether someone else has already asked (and answered) your question.
  • Explain what you’ve already tried to do to solve your problem. This tells people you’ve already put in some work to try to figure things out on your own.
  • Be polite. Don’t demand help or pressure your helpers to respond quickly.”