Learning Python, Day 12 — Functions
First of all, if you were planning on doing anything today, don't fall victim to Thomas Colthurst's 45 by 45 Connections. You will glance at it, be like "boy, that looks neat" and the next thing you know, you're blinking away from the screen going "What year is this?"
Yeah, I'm trying to avoid it now. I have Python to learn. And yet my brain is going "...just one more connection, come on, you can figure it out..."
NO. Python! PYTHON!
To go back to Loops, Mark Norman Francis helped me figure out how to get it so that this:
original_treks = range(1,7)
for i in original_treks:
print(i)
Can be written as this:
original_treks = range(1,7)
for i in original_treks:
print("Star Trek", i)
And return what I want:
>> Star Trek 1
>> Star Trek 2
>> Star Trek 3
>> Star Trek 4
>> Star Trek 5
>> Star Trek 6
I don't need to have the + or the and print(). If I just have both in the same () with a comma, it'll print both.
So, let's see how that works with other items.
fish = ["Coelacanth", "Sea Bass", "Dab", "Oarfish","Squid"]
for item in fish:
print("I caught a", item)
>> I caught a Coelacanth
>> I caught a Sea Bass
>> I caught a Dab
>> I caught a Oarfish
>> I caught a Squid
The grammar may not be right, but the code is perfect.
Okay, now that we have that down, time to ignore the 45x45 connections and focus on Functions.
18 — Functions, 19 — Functions Examples, & 21 — Parameters and Return Values Examples
I wish the instructor would stop talking about what other languages do. This just keeps on making it confusing. I'm sure other people, who have been programming, will be like "But my favourite language does this", but are they really going to be watching an Introduction to Python course?
Also, I know Python uses break to talk about when a loop or function stops running, but I can't write "break" in my notes, because my brain defines "break" as "you messed it up and it is broken" rather than "it's perfectly written and it's done". The function doesn't break, it stops. If it breaks, then you need to review the code.
Also, I really do learn better by seeing examples and trying it out myself. I don't know why I'm watching the videos that just talk about what things are, because they just fly over my head and I go "...the what now?"
I also don't think trying to make things into a game really helps either. Like, for example, the first example of a function is this:
x_pos = 0
def move():
global x_pos
x_pos += 1
move()
move()
move()
print(x_pos)
>> 3
Which, yeah, works, but doesn't really show off what can be done. And is pretty boring. Plus, calling the Function multiple times seems awkward. Plus, it doesn't really show what you can do with local variables and global variables.
Like...
c_temp = 11
def tempconvert():
global c_temp
f_temp = (c_temp * 1.8) + 32
print(f_temp, "or", c_temp)
tempconvert()
>> 51.8 or 11
See, I have an impossible time reading Celsius. Still. I know, I know, I've been in the UK for 25 years, but I still can't actually know what the hell kind of weather 11° is. Is it cold? Is it hot? What is it?
So if I know that the formula for converting Celsius to Fahrenheit is (C x 1.8) + 32, I can have the c_temp be global, keep the f_temp local, and do a bit of programming to give me the temperature in Fahrenheit.
No, it's not perfect, but it's definitely getting the job done.
But then they bring up parameters, and that makes it even cleaner.
def tempconvert(c_temp):
f_temp = (c_temp * 1.8) + 32
print(f_temp, "or", c_temp)
tempconvert(11)
>> 51.8 or 11
It's still 11° in Nottingham at the moment. Let me try it with a different location. Disneyland Paris is 16°.
def tempconvert(c_temp):
f_temp = (c_temp * 1.8) + 32
print(f_temp, "or", c_temp)
tempconvert(16)
>> 60.8 or 16
Or Disneyland in Anaheim is going to be 23° today. What does that mean?
def tempconvert(c_temp):
f_temp = (c_temp * 1.8) + 32
print(f_temp, "or", c_temp)
tempconvert(23)
>> 73.4 or 23
Dangit, America, y'all need to get your political act together. I could definitely stand to be in Disneyland right now.
Day 12 — Results
- Functions are blocks of code that isn't executed until we run them by calling them.
- You bring in a function by using
def function_name():and then putting what the function needs to do underneath (and indented) - Parameters are what's inputted into a function.
- You add a parameter to the function by putting a variable name into
def function_name(variable_name)and then adding in the variable when you call the function, likedef function_name(1). - A function doesn't need to have parameters to run.
- You can have a global parameter built in by defining it when you define the function, like
def function_name(variable_name=1). - Return Values are what comes out of the function.
- You specify a return statement by using
returnin the function. You can includeifandwhilein the function, and then as many returns as needed. - If you don't have a Return Statement, the Function will stop when it's done.
- Functions can't refer to variables that are outside the function unless you add
global variablename. - A local variable is a variable in the function.
- If you name the local variable the same as a global variable, anything you do to the local variable will not affect the global variable.
I think I get functions and variables and all that, but I'm also struggling to actually see how it can be used, just because I need better examples. I mean, I can come up with random things, but I'm still not fully seeing the potential. But I'm hoping the next one, or even the next course, will do that for me.
I mean, though, 12 days in and I'm converting the weather. That's not bad.
Today's Sticker

A sad little Pikachu I got from a Pokémon sticker bundle either from Wish or Kawaii Pen Shop, I can't remember which.
(He's sad because I won't buy a Switch 2 just to play Pokopia. No, Pikachu, we have Animal Crossing at home.)