Wednesday, April 11, 2018

Python introduction

types in python:
in python we dont need to explicitly define the type. we can simply say -

answer = 5
pi = 3.14159

and then we can simply type cast like -
int(pi) == 3
float(answer) = 5.0


strings are defined with single quote (''), double quote ("") or even triple quote ("""). I dont know why so much of flexibility, but triple quote I guess used for comment.
name = "Amit"

Boolean(True, False) and None are useful


python uses indentation instead of curly braces for any code bock. example
number = 5
if number == 5:
do something
else:
do something

: - is mandatory for both if-else

any number (except 0) and string has truthy value. so if you use in a condition check it will be true. None will be treated as false value.

you can use multiple consition using and, or , not in if as -
if number == 5 and name not "amit" or pi == 3 :

ternary if statement:
a = 1
b = 2
"bigger" if a>b else "smaller" :


list:

student_names = [] -- empty list
student_names = ["deb", "arun", "Sunil"] -- list with elements

to access list elements python use index starts with ZERO. But it also support index in reverse order starts with -1. like
0 1 2
student_names = ["deb", "arun", "Sunil"]
-3 -2 -1

student_names [1:] -- will skip the 1st element
student_names [1:-1] -- will ignore 1st and last element

you can have multiple types in a single list, but its a good practice to have a list of single type. few common functionalty in list -
student_names.ppend("Mike")
len(student_names)
del student_names[2]

For Loop:

for name in student_names:
print ("student name is {0}".format(name))

for name in student_names:
if name = "deb" :
break

for name in student_names:
if name = "deb" :
continue


while loop:
x = 0

while x < 10
 print ("value of x is {0}".format(x)) x += 1

 Dictionaries:
 student = {
     "name" : "mark"
     "Id" : 12345
     "feedback: none
}

 as noticed, value can be any type.

 student["name"] == "mark"
student["last_name"] = KeyError     -- anything that does not exist will raise exception

 exception handling:
 try:
     student["last_name"]
except KeyError as error:
     print ("KeyError handled")
except Exception:
    print ("got a generic exception")

few other types:
complex long -- only in python 2
bytes and bytearray
tuple = (3,1,4,"Mark") -- tuple is immunable
set -- set has unique elements compare to list frozenset

 function:
 def functionname(param1, param2=defaultvalue):
    return value

we can have variable argument function as -
def var_arg(param1, *args):
    print(args)
     
def var_arg(param1, **kwargs):
    print(kwargs["argument name"],...)

nested functions are allowed in python. like -

def outerfuc():
    def innerfunc():

Lambda functions and generators (yield):

classes are defined in python using class keyword as -
class student:
    def member_fuction(self, parm1) -- self is like this in c++

 -- constructors are defined as initialization method. like -
def __init__(self, other params):     -- __init__ is the keyword for constructor

def __str__(self):     -- string method, another special method. this is an override.

In python there is no special keyword for override.

inheritance:
class HighSchoolStudent (student):     
# here HighSchoolStudent derived from student class. Python support multiple inheritance.

modules:
you can import a module or file using import keyword as -
 import student     -- imported student.py file. but in this case we have use student. to access any data\function from student.py

from student import classname     -- this will import the classname class from student.py python 
packages:
 pip command is used for python package management. To install virtual environment you can run pip as -
pip install virtualenv

to create a virtual env run below command from terminal or powershell :
virtualenv --python=

after creating the virtual env, you need to activate that as -
$source /bin/activate

if you install any package in virtual env, it will be only available for that env. to deactivate the activated virtual env -
deactivate



No comments:

Post a Comment