Fundamentals of Python: Function and Variable Types (Week 3)

Dara Monasch / Thursday, September 26, 2013

This week’s worth of lessons is super intuitive and really sets the tone for the rest of the class. As you’ll see, the speed of new information as well as the course structure meld together in a pretty harmonious way and let the student actually grasp concepts before moving on. While some of these are review if you’ve been following me since my last class, as always I enjoy a good subject matter comparison between languages. :)

If you need access to Week 1 or Week 2, here they are!

Lesson 1: Functions, Variables & The Call Stack

The call stack holds what are known as “stack frames” which basically represent the order in which different actions will be executed by the computer.

This is important to note when answering the question of what variable to use when there is a program with multiple instances of the same variable. The correct answer is the variable that’s in the stack frame, or as I like to call it, the active variable. Regarding assignment states and variables, if a variable does not exist in the current stack frame, Python will create the variable so it can be used. :)

HOWEVER!

When a function exists, the frame for the function is erased and control passes back to the statement containing the function call. The return of the function call is whatever was returned by the function itself.

Lesson 2: type bool

Boolean values represent the binary concept of true/false. This lesson goes over, in a pretty comprehensive way, what they are and how to use them…

Boolean Operation

Symbol

Less Than

Greater Than

Equal To

==

Greater Than or Equal To

>=

Less Than or Equal To

<=

Not Equal To

!=

 

Additionally, there are logical operators, which you can use to create longer, more specific selections using Boolean expressions as their operands.

Logical Operation

Symbol

Not:

not

And:

and

Or:

or

 

If you’re using the logical operator “and,” both booleans must evaluate to the same result in order to be true.

If you’re using the logical operator “or,” at least one of the booleans must evaluate to true in order to be true.

Additionally, when using the logical operators that are evaluating naturally, the order of precedence is simply left to right.

Lesson 3: Converting Between int, str and float

Let’s just cover this visually, because I think it actually is way more intuitive that way than through text.

Convert an into into a str:

str(3) -> ‘3’

Convert a str into an int:

int(‘3’ * 5) -> 33333

Convert a str into an int into a str:

str(int(‘3’ * 5)) -> ‘33333’

Convert a float into a str:

str(4.6) -> ‘4.6’

Convert a str into a float:

float(‘456’) -> 456.0

Convert a str into an int:

int(‘hello’) -> ValueError (AKA Juuuuuuuuuuuust kidding you can’t do that! The conversion of str to into or float only works when numbers are the only items in the string!)

Lesson 4: Import: Using Non Built-In Functions

Python has hundreds of functions that are pre-built and at the disposal of the developer; however, you have to import them to be able to use them!

In Python, the items that you import are called modules. A module is a file containing function definitions and other statements. A Python file defines a module, for example, triangle.py defines the module ‘triangle.’

In order to import a module, you would use the following statement:

import module_name

In order to access a function that is housed within a module, you would use the following statement:

module_name.function_name

Lesson 5: if Statements

if expression:
    statement

The above is the simplest form of an if statement, which is basically a way to conditionally break up your program. Here’s an example using this simile form:

if time1 == time2:
   return “same time”

If a function ends without a return statement being executed, it will automatically return None.

If your conditional if statement has multiple conditions or items you want to account for, you have several options!

Option number 1 is “elif.” Elif works the same way as “if,” but it cannot exist without an if statement before it. You can have any amount of elifs to accompany your if, including 0.

Option number 2 is “else.” Like with elif, else must come after an if, but it can also come after all of your elifs as well. Clearly, this means it has to be the last item in any sequence of an if statement, but this makes sense because else simply encompasses any cases you haven’t specifically accounted for in your ifs or elifs.

Lesson 6: No if Required!

This lesson addresses the concept of… when you might not need to use an if statement! When is that? If you are to write an if with a Boolean, of course the else will be false! It’s the only other option.

For example… you could write:

if num % 2 == 0:
  return true
else:
   return false

OR!

You could write:

return num % 2==0

These two statements represent the same process; however, the second one is cleaner, and one could even say, optimized. J

Lesson 7: Structuring if Statements

It’s super easy to nest if statements, so you can set up a conditional tree and then have sub-conditions within any level. That’s all you need to know from here! A series of if, elif, else statements can be housed WITHIN a parent if, elif, or else.

Week 3 Summary

Week 3 was super intuitive and really nailed home the idea of if statements and how to transition between different types of data. There’s not much else to say really, except let’s get excited for Week 4!

Questions/Comments?

Feel free to comment here on my blog, or find me on Twitter @DokiDara.

By Dara Monasch