Python Notes
Python Notes
Python Notes
Python: Python is a general purpose, highlevel, interpreted ,object oriented, dynamic typed programming language.
Guido Van Rossum developed Python Programming language in the year of 1989 at National Research Institute Netherland.
th
The First version was released in 20 feb 1991.
The Complete Monties Python Circus which was brodcasted in BBc from 1964 to 1974 was the famous an favorate show of guido
from which he took Python Word.
Features of Python:
I) Simple & Easy to Learn: Python is a simple programming language, when we read python program we can feel like reading
English statements.
II) Freeware and Open source: We can use Python Software without any liscense and it is freeware.
III) High Level Programming Language: It is a programmer friendly language. Being a programmer we are not required to
concentrate low level activities like memory management and security.
IV) Platform Independent: Once we write a python program it can run on any platform without rewriting once
again.PVM(Python Virtual Machine) is responsible to convert into machine understandable code.
V) ***Dynamically Typed:In Python there is no need to define type of data .
VI) Intrepreted Programming: The code which we type in this language will be executed line by line.
VII) Both Procedure(function) & Object Oriented Programming language: it support procedure oriented language like C,
Pascal,etc. It support Object Oriented P language like C++.
VIII) Extensive Library: Python has a reach inbuilt libraries.
IX) Embedded programming language: We can use Python program in any other language program.
2) Variables: Variables are the container or the memory location which is used to store values or data. E.g a=10, b=20, s=’hello’.
4) Keywords: Keywords are the reserved words which have special meaning in python. There are 35 keywords in python given
below:
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for',
'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
5) Datatypes: Datatype represent the type of data present inside a variable. In python we are not required to specify the type
of date based on value provided, the type will be assigned automatically. Python contain following inbuilt data type:
Syntax:
Import keyword
print(keyword.kwlist)
I) Int
II) Float
III) Complex
IV) Bool
V) Str
VI) Byte
VII) Range
VIII) List
IX) Tuple
X) Dictionary
XI) Set
XII) Frozenset
XIII) None
XIV) bytearray
II) Float: We can use float data type to represent floating point values or decimal values. E.g: f=10.5. We can also represent
floating ponit values by using exponential form. E.g. f=1.2e3.
III) Complex: Complex in the form of a+bj form where a= real number and bj= imaginary number.
IV) Bool: We can use this data type to represent boolean values. The only allowed values for this data type are true and false.
Internally python represent true as a one and false as a zero.
V) Byte: Byte data type are used to store integer numbers in the range of 0 to 255 character.
6) Comments: Comments are unexecutable part of program. There are two types of comments in python:
a) Single line comment(#)
b) Multiline comment(#)
i. Docstring(‘’’ ‘’’, “”” “””)
7) Type Casting:
a) int(): It is used to represent convert all datatypes into whole number.
Ex: a=10, print(int(a))
Note: We connot convert complex data type into int
If we want to convert string data type into int compulsory string should contain only intrgral value.
c) complex(): We can convert other data type into complex. We can use this function to convert x*complex number with
real part x and imigenary part ‘0’.
Ex: x=10, print(complex(X))
d) bool(): If x is int data type then zero is false and non zero is true.
Ex: X=10, print(bool(X) #true.
If x is float if float number value is zero then result is false otherwise true.
If x is complex data type if both imigenary and real number value is zero then result is false.
f) String: String is a sequence of character which enclosed in single quote(‘ ‘) or double quote(“ “) sequence of
character.
i. Accessing Element from string:
1. Indexing:
a) Positive Indexing:
s='itvedant'
print(type(s))
print(s[0])#i
print(s[1])#t
print(s[2])#v
print(s[3])#e
print(s[4])#d
print(s[5])#a
print(s[6])#n
print(s[7])#t
b) Negative Indexing:
#Negative Indexing
print(s[-1])#t
print(s[-2])#n
print(s[-3])#a
print(s[-4])#d
print(s[-5])#e
print(s[-6])#v
print(s[-7])#t
print(s[-8])#i
#negativ slicing
print(s[-8:])#itvedant
3. Length function: It is used to find length of string
s="python"
print(len(s))
for i in s:
print(i)
for i in range(len(s)):
print(i)
for i in range(len(s)):
print(s[i])
4. Mathematical operations on String:
1. Concatenation: Addition of strings.
Rule: To use + operator for string cumpulsory both argument
should be str type.
2. Multiplication/repetation : repetation of string.
To use * operator compulsory one aurgement should be in string
and other should be int.
5. Using for loop
g) Find Function: Find function returns index of first occurrence of the given sub string. If it is not available then we will
get -1.
Syntax:
s=”python”
s.find(substr)
h) Index Function: Index function is same as find function but the difference is that if the given substr is not available it
will give value error
i) Count Function: Count() function find the number of occurrence of substr present in given string.
s.count(substr,begin,end.
print(s.count("e"))
j) Replace Function: It is wsed to replace every occurrence of old string will be replaced with new string.
Syntax: S.replace(oldstr, newstr)
k) Split Function: Split function is used to split give string according to specified seperator by using split function, Thee
default seperator is space. The return type of split method is list.
Syntax: s.split( seperator)
l) Join Function: We can join a group of string with respect to given seperator.
Syntax: ‘seperator’.join(stringname)
In python if an new variable is required then pvm wont create object immediately first it will check is any object available
with the required content or not if available then existing object will be reused, if it is not available new objest will be
created.
The advantages of this approach is memory utilization and performance will increase.
Operators in Python:
Operators are special symbol which is used to perform specific operation operant.
Types of Operators:
I) Arithmatic Operator: It is the type of operators which is used to perform arithematic operations.
a) Addition(+)
b) Substraction(-)
c) Multiplication(*)
d) Division(/)
e) Floor division(//)
f) Modulo(%)
g) Exponential(**)
V) Assignment Operator: Assignment operator are used to assign value to the variable.
EX: +=, -=, /=, *=, //=, **=, %=
print(num1)
print(num2)
A) end=’ ‘ :
Ex:
print('hello',end=' ')
print('python',end=' ')
>>>hello python
3. IF ELIF ELSE:
Syntax:
If condition:
“code to be executed
Elif
‘code to be executed
Elif
“code to be executed
Else
“code to be executed
Transfer Statement:
I) Break:
II) Continue statement: Skip the current iteration and move to the next iteration.
Ex:
i=1
while i<=200:
i+=1
if i==100:
continue
print(i)
III) Pass: Pass is a null statement in python.Pass is a keyword. Pass is used to define empty block.
Ex:
i)for i in range(11):
Pass
ii)Def hello():
Pass
List Datatype: If you want to represent a group of individual object a s a single entity where insersetion order is reserved and
duplicates are allowed then we should go for list. Duplicate objects are allowed in a list. Heterogeneous objects are allowed. List
is a mutable datatype.
We can define list using square bracket.
Syntax:
List_name=[value1,value2,…..]
Ex:
L=[1,2,3,”a”,”b”,2.0]
Multiplication:
X=[1,2,3,4]
Z=x*3
Print(Z)
Z={1,2,3,1,2,3,1,2,3]
X2=[1,2,3,4,5,6,7,8,9,10]
Print(6 in X2)
Nested List: Some times we can take a list inside another list called as nested list.
EX:
Mylist= [10,20,30,[100,200,[1,2,3,]],50]
To access element inside nested list:
print(Mylist)
print(Mylist[3])
print(Mylist[3][2])
print(Mylist[3][2][1])
List Comprehensions: It is a very easy and compact way of creating list object from any iterable objects.
Syntax:
List=[expression for I in iterable_object if condition]
min() & max() function: min function returns minimum value from list & max function returns max value from list.
Tuples in Python: Tuple is a ordered collection of hetrogeneous data which are imutable. We can define tuple using round
bracket().
T=(1,2,3,4)
t
Using Slicing:
Tuple_name[starting Idx value: end idx value: steps]
Using loop:
for i in tuple_name:
print(i)
t1=(1,2,3,4)
t2=(5,6,7,8,9)
t3=t1 + t2
print(t3)
t4=t1*t2
print(t4)
Functions in Tuple:
***Dictionary Data type: If you want to represent a group of object as a key : value pair then we should go for dictionary.
Duplicate keys are not allowed in dictionary but value can be duplicate. Hetrogeneous objects are allowed for bath key and
values. Insersion order is not pre reserved. Indexing and slicing is not applicable for dictionary. We can define dictionary using {}
brackets.