Learn Python in One Day and Learn It Well: Python for Beginners with Hands-on Project
Rate it:
Kindle Notes & Highlights
10%
Flag icon
"string to be formatted" %(values or variables to be inserted into string, separated by commas)
10%
Flag icon
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)
11%
Flag icon
The price of this Apple laptop is 1299 USD and the exchange rate is 1.24 USD to 1 EUR
11%
Flag icon
"string to be formatted".format(values or variables to be inserted into string, separated by commas)
11%
Flag icon
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)
22%
Flag icon
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
23%
Flag icon
do Task A if condition is True else do Task B
24%
Flag icon
for a in iterable:     print(a)
25%
Flag icon
The range() function generates a list of numbers and has the syntax range(start, end, step).
26%
Flag icon
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.
26%
Flag icon
while condition is true:     do A
27%
Flag icon
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.
28%
Flag icon
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.
28%
Flag icon
try:     do something except:     do something else when an error occurs
31%
Flag icon
def functionName(list of parameters):     code detailing what the function should do     return [expression]
33%
Flag icon
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.
34%
Flag icon
Python allows us to define default values for the parameters of a function.
34%
Flag icon
def someFunction(a, b, c=1, d=2, e=3):     print(a, b, c, d, e)
35%
Flag icon
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.
35%
Flag icon
In cases like this, we can use the * symbol.
35%
Flag icon
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.
35%
Flag icon
If we want to pass in a keyworded variable length argument list to the function, we can use double asterisks.
36%
Flag icon
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.
36%
Flag icon
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.
36%
Flag icon
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.
42%
Flag icon
class Staff:     #contents of the class
43%
Flag icon
PascalCasing