Here we are at Week 2 of Fundamentals of Python! This week covered Functions, Formatting & Assignment statements, which don't need much explanation really, so let's get right into it!
If you need access to Week 1, here it is!
Lesson 1: Visualizing Assignment Statements
The most important note in this lesson is understanding that assignment of variables does not mean that a variable is *equal* to the value or function it is assigned to represent.
Here are the rules for Executing an Assignment Statement:
- Evaluate the expression on the right side of the equals sign to produce a value. This value also has a memory address. This value also has a memory address.
- This memory address will be stored in the right side of the = sign, with the variable who is referencing it remaining on the left side.
REMEMBER! Assignment changes the value of a VARIABLE, not what is stored in the memory address!
Additional Notes from this Lesson:
- In IDLE, you can access the visualizer tool by navigating through Explore > Visualize to see the state of computer memory used during the execution of your code.
Lesson 2: typ str
typ str is Python’s representation of the String type. In order to define a string literal, which is a sequence of characters, it must begin and end with either ‘ or “. Additionally, string literals can be assigned to variables. It is important to note that if your string literal needs to include a contraction or possessive, you can use “s to define them. Additionally, you have the option of using \’ to refer to your ‘ in the string, and using ‘s to define it completely.
Strings also are able to be concatenated using the + operator, but you have to remember to include spaces where appropriate, as the + operator simply mashes the strings together and does not take any spacing into account. Additionally, you can use the * operator on strings to replicate a string any number of times. However, it’s important to note that order of operations still applies when using these “mathematical operators” on your strings. And before you go running off to try / and -, no, those operators don’t work and will only result in you achieving a TypeError.
Lesson 3: Input/Output & str Formatting
In order to print by passing a single function to argument call, you’ll want to produce a statement something to the effect of this:
Print (“text”) => text
Print(3+7-3) => 7
Print(“hello”, “there”) => hello there
Here’s an example of how you would write functions to produce the square of a number, and print it!
NOTE! If the end of a function body is reached without executing a return statement, that function call produces value “None”.
Additional Notes from this Lesson:
Triple quoted strings can span several lines. Example:
In memory, this is stored as ‘How\nAre\nYou?’ (\n is the newline designation)
\t is an escape sequence for tabs
\\ is to print a single backslash
\’ is for ‘ in a string
\” is for “ in a string
Lesson 4: Docstring & Function Help
Docstring is documentation for your own functions! Make sure that these are triple quoted so they are formatted correctly. Something I thought was really neat here was that the first two lines of your docstring are what show up as the hint when you begin to type the function name.
Lesson 5: Function Design Recipe
- Header – includes your function name & parameters
- Type contract – types for values of parameters & expected return type
- Description – C’mon kids! J
- Examples – Function use examples.
- Function Body – your code to make things happen!
Next comes the Design Recipe, which is crafted, of course, to make things easier for you to execute as a developer.
1. Write examples
2. Write type contract
a. What times for the parameters?
b. What type needs to be returned?
3. Write header
a. Pick meaningful parameter names
4. Write description
a. Mention every parameter and describe your return value
5. Write body!
Lesson 6: Function Reuse
Once defined, you can use your functions over and over again! You can even pass your function calls as arguments. Get. On. It.
Lesson 7: Visualizing Function Calls
Formatting note: All code belonging to a function should be indented 4 spaces.
Stack Frame: a region of computer memory for keeping track of information about a function being executed
Locak Variable: variable created inside a function body that can only be accessed within that function.
Week 2 Summary:
This week was awesome because it took concepts that I’ve worked with before and brought them into the Python context. I’m excited to see what’s on tap for next week, especially since this course is also incorporating a Design Recipe, similar to Systematic Programming.
Feel free to comment here on my blog, or find me on Twitter @DokiDara.
By Dara Monasch