Introduction to Python Programming Course - Part 11

 Time to finish control flow! Once I have, I will spend some time consolidating this whole section. Lots of syntax and logic just to make proper sense of - not like it needs to be memorised, but I need to get how to adapt the right code to the right purpose!

Start Time - 11:38


List Comprehensions

So this is using a for loop in one step to create a list. The syntax is v similar to first line of for loop without colon.

Don't need a new list beforehand. Like a for loop would do. 

List comprehension is more concise than the actual for loop.

Conditionals can be added - an if statement to refine what goes in even more. NO ELSE can be added. You have to move the conditionals to the start of the list comprehension. Also LC are not in other languages - just python.

OK so far!

The quiz was tough - 

names = ["Rick Sanchez", "Morty Smith", "Summer Smith", "Jerry Smith", "Beth Smith"]


first_names = [name.split()[0].lower() for name in names] # write your list comprehension here

print(first_names)


Had no idea! Forgot about split. Need to remember that.

Wasn't sure on this one either - 


multiples_3 = [x * 3 for x in range(1, 21)]# write your list comprehension here


print(multiples_3)


It seems that I'm either not remembering stuff from before, or these are too hard. Either way I need to go over all of control flow! Possibly stuff before that too as some overall consolidation. Yes, a bit of both I think now.

And this one - again, didn't remember the item part or how to do this...


scores = {

             "Rick Sanchez": 70,

             "Morty Smith": 35,

             "Summer Smith": 82,

             "Jerry Smith": 23,

             "Beth Smith": 98

          }


passed = [name for name, score in scores.items() if score >= 65]# write your list comprehension here

print(passed)


OK. I've got practice questions. If they're too hard, then I'm going back to do consolidation as part of this entry. No point just checking the answers and accepting they are too hard!

Yep, they're too hard - not crazy but I need some consolidation time!


CONSOLIDATION

I'm going back to the start for this - just doing a summary of previous sections, then drilling in more so on the Control flow part...


Data Types and Operations

Arithmetic operators - these are +, -, *, / but also % ** //. These latter three are modulo, exponentiation and rounding to nearest integer after dividing. 

Use of brackets  and order of operations - usual BODMAS stuff

Variables and assignment operators - certain rules and syntax for naming, assigning with =, assignment multiple at a time in a line... all good. Assign op- specifically, the += or *= etc.

Integers and floats - whole numbers vs decimal numbers. You can use the type before e.g. Int(4.5) to make '4' and vice versa.

Booleans and comparisons - booleans return true or false. Symbols etc. Also 'and', 'or' and 'not'. 

Strings - to avoid confusion using apostrophes, \ is used. Printing strings - use of + and multiplying number of times. Got it. Len function. This is to show no. of char in a string. 

Types and type conversion - you can use 'type' to see specifically what this is for variables or values. String methods. There are LOADS! Some need further arguments put in. Others need just empty brackets. Important one we'll use is 'format'. This is where there is the {} with a missing value (or variable) which you then put in after the format. in the right order. 

String method: split - yes I knew I'd seen this before, this is why I'm reviewing ALL! This separates a string into separate words. You can also put in arguments - of what to separate the words by (space is default) and the max length of the word. 

Debugging - division by zero; syntax error (unexpected eof) - usually leaving out a bracket; type error - argument not included that should've been. Another tip is to google search the error and use print statements to properly test the code.

That's all this section. On an aside, I've changed my settings on codewars to python - will go back into that. But will only do so once I've finished this course - I need certain aspects like functions etc.

I've also realised that I need this 'summary' as a 'sticky' - something I did before so after each session, I would summarise the key points on there. 

I'll finish this one here, then get the summary of entries set up. 

Finish time - 12:20 (approx. 40 minutes)

Comments