2. PYTHON
• It is the programming high level language that is applied for
scripting roles
• Python is a widely used high-level programming
language for general-purpose programming, created by Guido van
Rossum and first released in 1991.
• Python interpreters are available for many operating systems,
allowing Python code to run on a wide variety of systems.
3. INDEX
• Data Types
• Numbers
• Strings
• Tuples
• Lists
• Dictionaries
• Methods
• String methods
• List methods
• Dictionary methods
• Files
• Operators
• Arithmetic
• Logical
• Control Flow
• If / Elif /else
• For
• While
• Break
• Functions
• Arguments
• Lambda
• Scope
• Recursive
• Modules and
Packages
• Class
• Inheritance
• Polymorphism
4. DATA TYPES:
NUMBERS
Numeric type is used to store the numeric value.
var =2
Types of numeric types:
1)Integers/Floating- 1, 1/2=0, 2.4, 1.0/2=0.5
var=3.25421
print var=3.25421
2)Complex - a+bi
5. STRINGS
It is a sequence of characters which to be represented in
“ ”(or)’ ‘.
example :“digital”
Indexing:
The process of assigning a sequence of numbers to the element.
Example:
var =“digital”
var[0]=“d”
len(var)=will give length of string =7
6. TUPLES
These are immutable objects so that we can’t change values in tuple.
These are same as that of the lists.
var =(1,2,3,4)
var1=(4,5,6,7)
var + var1=(1,2,3,4,4,5,6,7)
var*3= (1,2,3,4,1,2,3,4,1,2,3,4)
7. LISTS
• It is the most flexible ordered collection object type. List contain any sort of object i.e;
numbers, string, other lists.
• These are mutable objects.
Example:
var =[“a”,1,2,”b”]
var1 =[3,4,5,6]
var+var1=[“a”,1,2,”b”,3,4,5,6]-concatenation
var*3=[“a”,1,2,”b”, “a”,1,2,”b”, “a”,1,2,”b”]-repetition
8. DICTIONARIES
• Dictionaries are one of the most flexible built in data types in python. These are
unordered collection, here the values (or) items are stored and fetched by key
instead of position offset.
• These are immutable objects.
Example: d1={“a”:1,”b”:2,”e”:3}
d2={“a”:[1,2,3],”b”:[3,4,5]}
9. METHODS:
STRING METHODS
i)endswith(): It will check whether the condition is true(or)false.
Example:
var=“digital”
var.endswith(“l”)=true
ii)startswith(): It also checks whether condition is true (or)false.
Example:
var.startswith(“d”)=true
Var.startswith(“i”)=false
10. iii)split(): It split the string with the condition.
Example:
var=“digital lync”
var.split()=[“digital”,”lync”]
var=“digital_lync”
Var.split(‘_’)=[“digital”,”_lync”]
iv)strip():It will remove escape sequences.
Example:
var=“ndigitalnlynct”
var.strip()=digital
lync
v)replace():It will replace the text with the new text.
Example: var=“digital lync”
var.replace(“c”, ”k”)=“digital link”
vi)join(): It will add symbols according to the
condition.
Example: var=“digital”
‘ ’.join(var)=“d I g I t a l”
vii)zfill(): It will provide 0’s before the number with
condition.
Example:
`num=“2435”
num.zfill(5)=“02435”
viii)lower(): It will convert upper to lower case.
Example:
new=“DIGITAL”
new.lower()=“digital”
ix)upper():It will convert lower to upper case.
Example:
new =“digital”
new.upper() =“DIGITAL”
11. LIST METHODS
1)append(): It is used for placing a values in list.
Example: var=[1,2,”a”,”b”]
var.append(6)=[1,2,”a”,”b”,6]
2)extend(): It is used for extending values in list.
Example: var=[1,2,”c”] var1=[3,4,5,6]
var.extend(var1)=[1,2,”c”,3,4,5,6]
3)insert(): It will insert the value with the help of index value.
Example: var.insert(0,”z”)=[“z”,1,2,”c”]
12. 4)index():
It will return the index value of the element we specify.
Example:
var=[1,2,”a”,”b”]
var.index(“a”)=2
5)count():
It will count no.of time the element is repeated.
Example:
var=[1,2,”a”,”c”,”k”,2,”y”,”c”]
var.count(“c”)=2
6)remove():
It will remove specified element from the list.
Example:
var.remove(“a”)
7)pop():
It will remove the element and return the index value.
Example:
var=[1,2,3,”a”]
var.pop(“a”)=3
13. DICTIONARY METHODS
d1={“a”:1,”b”:2,”c”:3}
d2={“e”:6,”f”:7,”g”:8}
1)d1.keys():It will give the keys in d1 dictionary as [“a”, ”b”, ”c”]
2)d1.values():It will give only values in d1 dictionary as [1,2,3]
3)d1.items():It will give the items in d1 dictionary as [(“a”,1),(“b”,2),(“c”,3)]
4)update():It will update the items in d1 with the items in d2.
Example: d1.update(d2)={“a”:1,”b”:2,”c”:3,”d”:4,”e”:5,”f”:6}
5)pop():It will remove the key item from dictionary and returns key value associated with the key item
Example: d2={“d”:4,”e”:5,”f”:6}
d2.pop(“f”)= 6
14. FILES
A FILE IS A COLLECTION OF DATA STORED IN ONE UNIT.
Modes:
read-”r” write-”w” append-”a” read and write-”r+”
Read: Its for reading the content in a file
var=open(“F:test.txt”,”r”)
print var.readline()
var.close()
print var.readlines()
Write: Its for writing into the file.
var = open(“F://test.txt”,”w”)
print var.write(“This is india”)
var.close()
append: It will add content to the previous lines in the file.
var =open(“F:test.txt”,”a”)
print var.write(“nhelloworld”)
var.close()
Read and write: It will perform both read and write operation.
var =open(“F:test.txt”,”r+”)
print var.readline()
print var.write(“helloworld”)
var.close()
15. OPERATORS
ARITHMETIC OPERATIONS
• Addition (a+b)
• Subtraction (a-b)
• Multiplication (a*b)
• Division (a/b)
• Modulus a%b
• Power (a**b)
LOGICAL OPERATIONS
• Greater then >
• Less than <
• Greater than or Equal to >=
• Less than or Equal to <=
• Equals to ==
• Not Equals to !=
16. CONTROL FLOW:
If-elif-else:
It is a conditional statement which is
use for selecting action.
Syntax:
if expression1:
statement()
elif expression2:
statement()
else:
statement()
17. While:
It is the general iteration construct
because it repeatedly execute blocks of
statements as long as the condition is “true”.
Syntax:
while condition:
statement
Break:
Jump out of the closest enclosing loop.
Continue:
Jump to the top the closest enclosing
loop.
Pass:
Pass is a no operation place holder which
is used to code a body of empty statement
Example:
i=1
j=int(raw_input('Enter a number:'))
while i<=j:
print ' *'*i
i+=1
Enter a number:5
*
* *
* * *
* * * *
* * * * *
18. FOR LOOP
For is the looping statement which can step through the items in order sequence from
list having strings, tuples and other built in objects
Example:
for x in range(0, 5)
print x
1
2
3
4
5
19. FUNCTIONS
Functions are defined using the def keyword. After this keyword comes an identifier name for the function,
followed by a pair of parentheses which may enclose some names of variables, and by the final colon that ends the
line. Next follows the block of statements that are part of this function. An example will show that this is actually very
simple
Example:
def operations_union(A,B):
for item in B:
if item in A:
continue
else:
A.append(item)
return A
A= [1,2,3,4]
B= [3,4,5,6]
print operations_union(A,B)
20. Lambda:
• var=lambda x,y: x+y
print var(2,3)
SCOPE:
x='digital'
def scope_1():
global x
x= 'lync'
def scope_2():
x='school'
print x
scope_2()
scope_1()
print x
Recursive:
def factorial(n):
if n==0:
return 1
else:
return n * factorial(n-1)
a = int(raw_input('Enter a number:'))
print factorial(a)
Enter a number:5
120
21. MODULES AND PACKAGES
Module can be imported by another program to make use of its
functionality. This is how we can use the Python standard library as well. First, we
will see how to use the standard library modules.
Builtin: import file_name
from
Example: file_a.py file_b.py
def stmnt(): import file_a
Return
22. OBJECT ORIENTED PROGRAMMING
CLASS
Classes and objects are the two main aspects of object oriented
programming. A class creates a new type where objects are instances of the
class. An analogy is that you can have variables of type int which translates to
saying that variables that store integers are variables which are instances
(objects) of the int class.
SELF:
Self is the name commonly giving to the first left most argument in a class
method.
Python can automatically fills it in with the instance object