Like many languages, object properties in python are accessed by the '.' (dot) operator.

Try playing with the 'math' object

Various types of imports

`import math`

`from math import factorial`

`from math import factorial as fac`

Other standard libraries

OS: `import os`

SYS: `import sys`

TIME: `import time`

Very Large Integers

Most languages have a limit on how big an integer you can use.

For very large integers you have to use specialised classes like 'BigInteger' in java

Python has no such problem

The size of the integer is only limited by the amount of RAM your system has

try: `fac(100)`

try: `fac(1000)`

try: `fac(10000)`

clear your screen:`CTRL + L`

Scalar types: int

We have already seen that integers in python can have infinite precision

Integers can be specified as:

Decimal: `10`

Binary: `0b10`

Octal: `0o10`

Hexadecimal: `0x10`

Converting to integers

from float:`int(3.5)`

from strings: `int('3')`

from one base to base 10: `int('<num>', <base>)` -> `int('100', 2)`

Scalar types: float

If the number has a '.' or a 'e', it is interpreted as float by python.

Try:

`3.5`

`1e8`

`1e-15`

Converting to float

`float(3)`

`float('3.5')`

`float('nan')`

`float('inf')`

`float('-inf')`

`3.0 + 1`

Scalar types: None

None is literally none.

None means it is not there (different from not defined)

`print (x)`

This will give error:

This will not give error:

`x = None`

`print (x)`

Can be checked by:

`a = None`

`a is None` (will return True)

Scalar types: Bool

True and False (inital capitals)

bool constructor

0 is considered 'falsy' and all other values (+ve/-ve) are considered 'true'

empty collections (like strings, lists) are falsy

Conditional Statements

==

>=

<=

>

<

!=

Return True or False based on the validity of the statement

Like most other languages, '=' is for assignment and not for comparison

Example: `3==2` gives False

Conditional statements: if

if expr:
print('expr is True')

`if` statement converts the expr to bool as if the bool constructor was doing so and checks whether it is True, if yes, then it executes the relevant code defined by the next indentation level in the consecutive lines

if -elif-else

if True:
print('#yolo')
else:
print('#swag')

a = 20
if a > 20:
print('above 20')
elif a < 20:
print('less than 20')
else:
print('it is twunty')

If the code to be executed is small enough, we can write it in the same line as if statement

>>> if True: print('bwahaha')

Conditional Statements: while

a = 5
while a != 0:
print(a)
a -= 1

while loop runs while the expr is True

but it can break out of the loop if we explicitly issue a break statement

Each method that is to be called on a class object or has to be called from a class instance has to accept 'self' as an argument.

'self' is similar to 'this' in java

Creating the initialisation method

The __init__ method is called by the python runtime when an instance of the class is created. It should not return any value. This is a constructor so can be used to initialise variables to some values.

Getters and Setters can also be used by declaring variables as properties (out of scope for now) but do read about it.

Documenting the code

Recall how we use help(module_name) to get help about it.

But how do we get provide usage help about the modules we create?

We create docstrings

"""This is a module docstring"""
from math import factorial as fac
def mahfunc():
"""This is the help we want to give.
Even for single line docstrings, use triple quotes (must)
Args:
None
Returns:
The number of ways to select 2 things out of 5 things.
"""
n = 5
r = 2
output = int(fac(n)/(fac(r)*fac(n-r)))
return('There are {} ways to select {} from {}'.format(output, r, n))
def main():
print(mahfunc())
if __name__ == "__main__": main()

Testing docstrings

>>> import test
>>> help(test)
>>> help(test.mahfunc)

Code comments in python

Explains the code implementation

Comments in python are written by pre-pending them with a '#'

>>> # this is mah comment

Adding Shebang

Shebang is a common string added to the top of a Unix program that tells the system what interpreter to use with the file