Introduction to Python Programming Course - Part 6
So last time it was all about data structures - the key parts being mutability and ordered/not. From that, specific methods with each the structures - lists (formerly arrays in swifts), dictionaries, sets and tuples. All of it made sense and nothing too conceptually tricky. Now I'm on the 3rd section of Python - Control Flow!
Start Time - 22:32 (late night quick session!)
So this is all to do with loops, conditionals etc.
If Statement
Key difference to Swift: colon is used rather than curly brackets. Boolean used to return true/false and then an action happening.
If - runs or skips code depending on if a condition is true or false. If false then those lines not executed.
Use of == as equal to, != or use of < etc.
'Elif' - new keyword! So this would be else if but combined to Elif.
Else is best for if there is only a second option. If there are multiple, then Elif. Else can also be seen as all other options.
Indentation convention - 4 spaces to indent. So the second and third etc. lines etc. Rather than the tab. That's so other programmers have the same expectations and rules.
OK I did the challenge and it worked but it's telling me formatting is not right - can't see where!
*One thing I noticed is that I didn't have to create the variable of 'result' separately. It was created within the if statement, then accessed outside of it. Interesting!
One thing they seem to prefer is == no gap before or after e.g. answer==guess. But that's not consistent - so just an error I think.
Boolean Expression
If you are testing conditions e.g. using and for multiple ones. And, not, or are keywords. NOT instead of ! before the variable.
Don't use 'True' or 'false' as conditions - will only give that one answer. Basically if something is ALWAYS true or false, not worth doing.
Don't use == true. As you would just put the condition. Instead of == false, you would use the 'not' before the condition.
Truth value testing - to then see if something is equal or not to a value.
In the challenge, it is 'None' as a keyword for an empty value. The syntax needs some work but I got it to all actually work! To make it more succinct, I just need to decide on the stored value and making the code neater etc.
Another thing is comparing numbers e.g. elif 151 <= prize <= 180
that means that the prize is bigger than or equal to 151 and less than or equal to 180. Between 151 to 180 basically. Putting the number before the <= is unusual!
Still an error apparently when my answer is DEF right! Not worth wasting time!
One key thing that came up - rather than use if prize != None: - that is a waste! Use if prize:
Makes sense!
Finish Time 22:24 (approx 50 minutes).
A good session! Spent a long time on the actual codes, which did waste a fair amount of time, as what I had was spot on, and it was an error on the course that claimed I wasn't right. That was annoying and net time, I will move on sooner. Overall, I've got the basics of the if statements with the use of bool.
Comments
Post a Comment