Sunday, October 3, 2021

Python Control Flow lesson today (7% of Data Science path completed)

Python scripts execute from the top down, until there is nothing left to run. 

My job is to provide gateways (conditional statements) that tell the computer whether it should run certain blocks of code. 

Boolean expression = true or false

Relational operator = compare two items and return true or false (aka comparators)

    Equals ==

    Not equals !=

    (str != int)    

True and False without quotes around them! <cue facepalm emoji> Punctuation will be the death of me in programming.

True and False are the only type of bool, assigning variables to True or False makes them boolean variables

This is the type of thing that takes many iterations to realize at the moment:

This code wouldn't run

This code did run

This is what is taking sooooooooooo long right now. It's ok, I'm just now starting to learn Python. Maybe in the next few weeks I won't be making mistakes like this anymore. 

Success, finally:


This is my first if statement in Python:
If the program detected either "Dave" or "angela_catlady_87" then it was supposed to print that statement to Dave. (Don't ask, it's part of the lesson)
More relational operators:

    > greater than
    >= greater than or equal to
    < less than
    <= less than or equal to


Boolean operators:
    and = all items have to be true or false
    or = one item has to be true or false
    not = reverses boolean value (e.g. not True == False) added at the beginning of the statement

My first and statement:


My first or statement:


Not statement issues:

This does not execute


This does execute


I'm abstaining from getting angry since these are my first and, or, not statements...

My first else statement:



elif statements: checks another condition after the previous if statement condition isn't met.
  1. the if statement is checked, then
  2. each elif statement is checked from top to bottom, then
  3. else code is executed if the above items have not been executed

Got my first elif on the first try!

I did the optional assignment which was to calculate weights on different planets. Lines 1-7 were already written for me. I am still forgetting to str() numbers in print statements, but I will learn. 


Additional challenges in this lesson:

Several iterations to get this one, but I did get it!


Note to self: if and else need to be at the same indentation level! 

Additional practice:

Got it on the first try:

Note to self: apparently I don't need the else statement since there are only two options. It could've looled like this and executed properly:


Learned from above and wrote this for success the first time!


FFS, why did they use an else statement in the solution they provided? 
<facepalm emoji here>
I guess if both ways execute properly, you can use the one you prefer.

This one was fun! It was creating a contradiction that will always return False.



Originally I had written print statements to print the string, but it wanted return instead, so I learned that you can return a print statement.


Another way (their solution):


This one took a while, and I had to use the hint:


Their solution does not include parentheses:


Longest code I've written to date. Seasoned programmers, no laughing please.


This is the result it printed, which executed correctly. 


So, what did I learn today?
  • There are many ways to write code
    • One day my code will be clean and minimal
    • today is not that day
  • Parentheses and quotes are a killer
  • Indentations matter
  • Not bad for a noob

Strings

A string consists of characters inside 'single quotes' or "double quotes". It can be any length and can contain letters, ...