More on this book
Community
Kindle Notes & Highlights
by
Jamie Chan
Read between
December 18, 2017 - February 9, 2018
"string to be formatted" %(values or variables to be inserted into string, separated by commas)
brand = 'Apple' exchangeRate = 1.235235245 message = 'The price of this %s laptop is %d USD and the exchange rate is %4.2f USD to 1 EUR' %(brand, 1299, exchangeRate) print (message)
The price of this Apple laptop is 1299 USD and the exchange rate is 1.24 USD to 1 EUR
"string to be formatted".format(values or variables to be inserted into string, separated by commas)
When we use the format() method, we do not use %s, %f or %d as placeholders. Instead we use braces { }, like this: message = 'The price of this {0:s} laptop is {1:d} USD and the exchange rate is {2:4.2f} USD to 1 EUR'.format('Apple', 1299, 1.235235245)
if condition 1 is met: do A elif condition 2 is met: do B elif condition 3 is met: do C elif condition 4 is met: do D else: do E
do Task A if condition is True else do Task B
for a in iterable: print(a)
The range() function generates a list of numbers and has the syntax range(start, end, step).
When using the format() method for strings, the positions of arguments start from zero. When using the range() function, if start is not given, the numbers generated start from zero.
while condition is true: do A
When working with loops, sometimes you may want to exit the loop when a certain condition is met. To do that, we use the break keyword.
Another useful keyword for loops is the continue keyword. When we use continue, the rest of the loop after the keyword is skipped for that iteration.
try: do something except: do something else when an error occurs
def functionName(list of parameters): code detailing what the function should do return [expression]
Firstly, any variable declared inside a function is only accessible within the function. These are known as local variables. Any variable declared outside a function is known as a global variable and is accessible anywhere in the program.
Python allows us to define default values for the parameters of a function.
def someFunction(a, b, c=1, d=2, e=3): print(a, b, c, d, e)
In addition to having default values for parameters, Python also allows us to pass a variable number of arguments to a function. This is very useful if we do not know the number of arguments a function has in advance.
In cases like this, we can use the * symbol.
As we can see in the examples above, when we add an asterisk in front of a parameter name, we can pass in a variable number of arguments to the function. This is known as a non-keyworded variable length argument list.
If we want to pass in a keyworded variable length argument list to the function, we can use double asterisks.
This function has a parameter called age. The double asterisks denote that this parameter stores a keyworded variable length argument list, which is essentially a dictionary.
If our function uses a normal argument (also known as a formal argument), a non-keyworded variable length argument list and a keyworded variable length argument list, we must define the function using the following order: def someFunction2(farg, *args, **kwargs): That is, the formal argument must come first, followed by the non-keyworded argument and the keyworded argument in that order.
Python comes with a large number of built-in functions. These functions are saved in files known as modules. To use the built-in codes in Python modules, we have to import them into our programs first. We do that by using the import keyword. There are three ways to do it. The first way is to import the entire module by writing import moduleName.
class Staff: #contents of the class
PascalCasing

