Introduction to Python Programming Course - Part 2
Here we go! As promised, approximately one hour during the week and one at the weekend. I'm going to do about that long today - hopefully getting to a natural point.
I think it's useful to begin with a recap from before...
Last time it was about data types (the four operations plus others such as modulo, increments etc.), creating variables (no var/let keyword needed and naming conventions with _). Next!
Start Time - 13:26
8. Integers and Floats
Whole numbers (integers) we've been looking at. Decimal numbers - used as floats (floating point number).
You need to include a decimal point e.g. .0 do declare a float. An int and float together will always make a float.
A float to int just cuts off the decimal values, no rounding.
Floating point numbers are approximations.
About best practices - Conventions - not using extra spaces in parentheses etc. A space around the lower priority e.g. where you have x and -. Space around the -.
79-99 characters per line are typical
9. Quiz
Dividing by 0 - will always lead to an error
10. Booleans, comparisons, logical
Bool - George Bool. True or false. Less than/greater than. This is all familiar so far.
Note that == is equal to (not = as this is the assignment operator)
Logical - and/or (&& || on Swift)
Using the keyword and is nicer!
The other one is 'not' e.g. not 5 < 3 would be 'true'
11. Quiz
Smashed it! Just an easy use of the >. Another option was the if statement, but that's just flashing out the basic bool.
13. Strings
Immutable, ordered series of characters. Single or double quotes can be used.
I think I prefer double - I always use that for speech.
Strings can use any characters including spaces and numbers
Quotation marks in strings...this is tricky but basically but you need '" ad the start and '" at the end. And apostrophes need \.
Combining and repeating strings. + sign to combine. * to repeat.
Combine is concatenate. Remember a space for words - Python is literal! Spaces do matter in strings.
* will repeat that number of times being multiplied. Not for divide.
Len - diff from print as it returns a value that can be stored in a variable.
14. Quiz
I sorted out the quote one, but still not allowing it despite no errors. Put in the \ for apostrophes. Got the concatenating one - just had to put timestamp as its own value and separate full stop in a string. Interesting how no need for the \() variable bit within a string on Python compared to Swift (that's actually when putting within a string - not come up yet).
Yes, as suspected - my solution for the quote did work when checking the solution. Error on the site. Got the len() function working too. Can't put integers in that.
Paused for approx. 7 minutes
16. Types and type conversion
The four types so far: int, float, string and bool
You can use built in function 'type' to check the type of any value
You need to choose the type for your data based on what you need them for
Special functions designed for each data type
New objects can be created and type changed. e.g. int to string put in str(var). Or use float(var). That will change the type basically.
17. Quiz
Questions about types - all good!
And the converting to int for each string, then back to a string with the concatenating - all good!
19. String Methods
Functions and operators are very similar - functions are in parentheses and have names e.g. print or len.
Operators use the symbols.
Methods - associated with specific types. E.g. different methods depending on the type of data. So with strings there will be a specific method e.g. changing to title case.
Methods are functions that belong to an object. That makes MUCH more sense about the functions vs methods debate on Swift!
Methods have arguments. And use dot notation. There a lot with strings!
Some don't accept arguments e.g. islower does not. format() is an important method - ok so using the {}, you then put in the values later. That is tricky but I think get it. Last bit - quiz!
Paused for 4 minutes
20. Quiz
This was the bit I was practising -
first_name = "Josh"
surname = "Gonet"
# Now write a print statement using .format() to print out a sentence and the
# values of both of the variables
full_name_string = "My full name is {} {}"
print(full_name_string.format(first_name, surname))
I could have done it with lots of examples - the above is a simple one of using the {} for empty values, that then get filled. Those number of arguments must be met - in the case above, there are two. They also have to be strings, as format is a STRING METHOD.
Finish Time - 14:41: 1 hour 1 minute total
Comments
Post a Comment