Getting Started with Python: Control Structures and Functions
I am studying Python with @skillcrush, in this article we cover control structures and functions
I am learning with Skillcrush. Please check them out and sign up if you're looking to break into tech!
Receive $250 off your course by using the link above. Use the coupon code: TWIXMIXY
NOTE: this discount is for Break Into Tech + Get Hired package exclusively.
It's the same program I am participating in!
🐍 Greetings travelers! We are studying Python with Skillcrush. In this article we cover control structures and functions.
🟩🟩⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜ 14% DONE
I’ve wrapped up my Front End Developer portfolio at Skillcrush, read here:
I am now enrolled at UC Berkeley! 🏫 I will be taking their Full Stack MERN Coding Bootcamp through the remainder of the year.
You can subscribe without signing up for a substack account! Just select “None” when subscribing. All the content from my learning journey is free.
Control Structures
→ CONTROL STRUCTURES
Let your code keep running if a condition is true, and stop running if a condition is false.
Tools to build control structures:
Comparison Operators
The IF statement control structure
The boolean data type
The try and except control structure
→ COMPARISON OPERATORS
Symbols you can use to compare two or more values.
→ IF STATEMENTS
Similar to comparison operators in that they return true or false, but they use an “if” keyword instead of math symbols.
→ BOOLEANS
A data type with only two possible values: true or false.
→ BOOLEAN OPERATORS
“and”, “or”, and “not”.
→ TRY AND EXCEPT
A control structure for when a user might input an incorrect or unexpected value.
→ USER INPUT
When someone uses an input device, like a keyboard or mouse, to interact with a program.
Most common comparison operators:
< less than: 4 < 5
> greater than: 5 > 4
== equal to: 5 == 5
<= less than or equal to: 4 <= 4
>= greater than or equal to: 4 >= 4
!= not equal to: 4 != 5
Boolean operators include the keywords and
, or
, and not
.
The
and
Boolean operator will check if all conditions are true or false.The
or
Boolean operator tests if just one condition is true.The
not
Boolean operator checks if one condition is false.
len()
The len function will count the number of characters, or length of a string.
Challenge - Birthday Reminder App
Definitely feeling more confident in what I’m learning with Python thanks to my background now in JavaScript. It makes me feel more comfortable playing around in the code. I went ahead and added in the print() section the person’s name, which is something we haven’t covered yet.
Check out the app here:
Use names like “Janet”, “Rowan”, or “Winona” to see results. Otherwise you will receive a message letting you know the birthday is not in the system.
Feedback Challenge - Error Handling with Try and Except
In the courses with Skillcrush there are feedback challenges. Meaning we can submit our code to the teachers and they will give us feedback on our work.
I was trying to combined a string with the sum total amount, but I had to create a separate print statement to do so.
That wraps our Control Structures. On to Functions now!
🟩🟩🟩⬜⬜⬜⬜⬜⬜⬜⬜⬜ 24% DONE
Functions
Writing Functions
Using different variable types in your functions
→ DEFINING A FUNCTION
Telling the computer what the function is called and what it does.
→DEF
To write a function
→ VARIABLE SCOPE
How accessible a variable is, AKA if it can be used throughout your code or just in a single function.
→ GLOBAL VARIABLE
Can be used in any function in the code, but must be initially defined outside of a function.
→ LOCAL VARIABLE
A variable that is only recognized inside of the function it is defined in.
BUILDING FUNCTIONS
def log_out(username):
goodbye = "Thanks for visiting," + username + "!"
return goodbye
I see some similarities to JavaScript here, but I feel like Functions I’m still trying to grasp in JS as well.
DEF
- to define a function
Followed by the function name: log_out
Padding an argument in the parenthesis: (username)
:
(colon) is like the semi-colon in JS
Inside the function variables can be written or other types of actions.
return
statement is used to end the function.
RUNNING FUNCTIONS
Next we need to run it, in the example given it’s through a print function.
print(log_out("Matthew"))
This combined the log_out
message with “Matthew” so it says “Thanks for visiting, Matthew!”
VARIABLE SCOPE
Global Example:
username = "Matthew"
email = "HappyMatthew@gmail.com"
about_me = "I love a good sandwich! PB and J, plz."
Local Example:
def log_in(username):
welcome = "Hi there," + username + "!"
reminder = "Don't forget to change your password in the next 10 days.!"
Hours Worked Challenge
I struggled a bit on this one. I didn’t realize in the return
that we could calculate the arguments being passed through the function.
Overtime Pay Challenge
I followed the instructions and it was pretty straight forward. Next thing I would want to do is explain why the 50hours statement earned so much more. Or ideally change this to be an input based calculator and then display a statement if folks work overtime.
Monthly Income Challenge
I definitely felt tempted to use the solution code at first. Then I told myself to just write it best as I could. To write SOMETHING! Try SOMETHING! And turns out… I actually got it right!
The big thing here is to not be afraid to fail. To write something, even if it’s wrong. Because then I at least tried.
🟩🟩🟩🟩⬜⬜⬜⬜⬜⬜⬜⬜ 33% DONE
I’ve loved beginning to learn Python! Now that I have done work in both JavaScript and PHP I could see how once you learn one language it can be easier to adapt it to others. In the next article about Python we will cover modules and lists.
What did you think? Leave me a comment and share!