Python
Python
Feature of python
Python is object oriented language.
Python is interpreted language.
Python is open sources language.
Cross platform language.
Basic syntax in python
print(“hello to python”)
num=10
print(num)
print(“num is : ”,c, end=‘’)
Tuple (it will store values like list but we can not change after declare the
tuple)
Comparison : ==
-- for loop
for i in range(1,11):
print(i, end=‘’)
Loops
while loop
i=1
while i<=10:
print(i, end=‘’)
i=i+1
String’s
str=“python”
str[:] -- python
str[0:] -- python
str[:5] -- python
str[:2] -- py
str[1:3] -- yt
note: python always consider last range as n-1.
List
lst=[101,”ankit”,”account”]
lst=[10,20,30,40,50]
10 20 30 40 50
-5 -4 -3 -2 -1
Tuple
tpl=(101,”ankit”,”account”)
tpl=(10,20,30,40,50)
10 20 30 40 50
-5 -4 -3 -2 -1
List v/s Tuple
dict={“id”:101, “name”:”ankit”}
print(type(dict))
print(“----- output -----”)
print(dict)
Date time
In
di
Function
def myfun():
print(“hello to function”)
Calling:
myfun()
Function
Call by reference:
In python if we pass simply variable then it behaves like
call by value but when we pass object then python behave
like call by references.
def change(x): def mylst(lst):
x=x+30 lst.append(“hello”)
num=100
change(num) //100 // call by references
Function
Types of arguments
Required
Keyword
Default
Variable-length argument
Keyword argument
sum(x=10, y=20)
sum(y=20, x=10)
sum(10)
sum(10,20)
Variable length argument: in this when we don’t know the
number of argument in advance then python provide the
flexibility to provide the comma separated values which is
internally treated as tuple.
Function
def display(*employees):
for name in employees:
print(name)
display(“ankit”,”neha”,”sonu”)
Modules
import statement
import mycode
mycode.mymsg(“ankit”)
import mycode as m
m.mymsg(“ankit”)
--reload(mycode)
Packages
CS_student.py _init_.py
no code here
def CS_Name():
lst=[“ankit”,”sonu”,”amit”] Calling package:
return lst test.py
from student import
IT_student.py CS_student
File handling we can write the data in text file and also
read the data text file in python program.
try
except
else
finally
raise
Object Oriented Programming
class
Object
Abstraction
Encapsulation
Inheritance
Polymorphism
Object Oriented Programming
Types of constructor
Types of inheritance
single inheritance
multi-level inheritance
multiple inheritance
Regular Expressions
Advantage of multithreading:
Multiple thread share same data space with main thread.
Thread also called light-weighted process because they
are not required much more memory.
We can also put the thread on hold(sleep) while other
thread are still running and that called yielding.
It can be pre-empted.
Multithreading
import threading
import time
Create thread
thread.start_new_thread(“method name”)
time.sleep(5) //seconds
threading modules: