PYTHON
• Python:
1. Interpreted
2. High level language
3. Object Oriented
IDE & Notebooks:
• Notebook : jupyter, google colab
• spyder, pycharm
Extensions:
1. (.ipynb)
2. (.py)
CONTD..
• DATA TYPES:
• #variables names can
1. Numeric
only have alphabet,
2. String/Object -- sequence of characters numbers and underscore.
3. Boolean (True/False) • #variables names can
only start with alphabet
4. DateTime and underscore.
• VARIABLES: • #variables can not start
#defining my first variable with any number.
• #variables names are
my_first_variable = 10 case sensitive.
#defining my second variable
my_second_variable = 'this is my second variable'
Contd..
bool1 = True from datetime import date
bool2 = False today_date = date(2023,7,31)
type(bool1) bool today_date [Link](2023, 7, 31)
Operators : (+, -, *, /, //, =, ==, %)
• Concatenation:
str1 = 'day 1' • 4/2 2.0
str2 = 'Python'
• floor division 5 // 2 2
conc = str1 +' '+ str2 • 5 % 2 (remainder) 1
Defining a function: • #exponential (2 ** 3)
#2 raised to the power 3
def sum(var1, var2): 2 ** 3 8
return var1 + var2
Assignment Operators..
a += 3 a=a+3
b-=1 b = b-1
a /= 2 a = a/2
a == b a is equal to b
a != b a is not equal to b
a >= b; a<=b a is less than or greater than or
equal to b
DATA TYPES in PYTHON
STRINGS
[Link]
[Link] of characters
[Link]
[Link] can be as long as whole encyclopedia
[Link]
[Link] can have 1 or 0 character
[Link]
[Link] can have words, sentences, and
paragraphs.
[Link] is ordered and hence there is index
attached to each character of the string.
[Link] is useful to grab characters from
your string.
STRINGS
a = "This is day2 of python"
print(type(a))<class 'str’>
• #ctrl+/ --- comment
• #shift+enter --- execute you
code
#length of string ---
print(a, end = ' ') number of characters in
the string
print(par)
len(a)
print(a) print((len(a))
print(end = '\n')
print(par)
STRING SLICING
#var_name[start(inclusive) : end(exluded) : step(default = 1)]
‘This is Day2 of Python’
a[0] ‘T’
a[(len(a)-1)]=a[-1] ‘n’
name=‘Chandan’ what is a palindrome
name[4:6] ‘da’ Ex:madam, pop
name[4:] ‘dan’ a = 'madam’
name[:4] ‘Chan’ • if a == a[::-1]:
name[::2] ‘Cadn’ print('It is a
#reverse the given string palindrome')
name[::-1] ‘nadnahC’
STRINGS
• #lower()
[Link]() ‘this is day2 of python’
name = [Link]()
• #upper()
[Link]() ‘THIS IS DAY2 OF PYTHON’
[Link]() ‘CHANDAN’
• #count
[Link]('a’) 2
• #find(provides the index no)
[Link]('p’) 16
If [Link]('yusuf’) -1 #”Character or word is not
in string”#
STRINGS
• if [Link]('chandan') == -1:
print("chandan not in a")
‘chandan not in a’)
#replace
a = [Link]('python', 'java’)
‘This is day2 of java’
'python' in a False
• #split
[Link](‘ ‘) ['This', 'is', 'day2', 'of', 'java']
• #join
" ".join(a) ‘This is day2 of java’
LIST
• l1 = []
print(type(l1)) <class 'list'>
• l2 = [1, 2, 3, 4]
#number of element inside the list
len(l2) 4
l2[0] 1
• #list can contain heterogeneous data
l3 = ['a', 1, '2.56', [2,3,4]]
print(l3) ['a', 1, '2.56', [2, 3, 4]]
l3[3][2] 4
len(l3) 4
LIST
#mutability & Immutability
l3 = ['a', 1, '2.56', [2,3,4]]
print(l3) ['a', 1, '2.56', [2, 3, 4]]
l3 = l3 + ['Vibhuti']
print(l3) ['a', 1, '2.56', [2, 3, 4], 'Vibhuti']
l2 = [1, 2, 3, 4]
max(l2) 4
min(l2) 1
sum(l2) 10
l4 = ['a', 'b', 'c', 'z', 'e’]
max(l4) ‘z’
LIST
l2 = [1, 2, 3, 4]
sorted(l2, reverse = True) [4, 3, 2, 1]
sorted(l3) “Error”—not supported
• #append
[Link]('Drishti’)
[1, 2, 3, 4, 'Drishti', 365, [1, 2, 3]]
• #extend
• l3 = ['a', 1, '2.56', [2,3,4]]
[Link](l3)
[1, 2, 3, 4, 365, [1, 2, 3], 'a', 1, '2.56', [2, 3,
4]]
LIST
l2 = [1, 2, 3, 4]
• #extend
• l3 = ['a', 1, '2.56', [2,3,4]]
[Link](l3)
[1, 2, 3, 4, 365, [1, 2, 3], 'a', 1, '2.56', [2, 3,
4]]
• #pop
popped_ele = [Link]()
popped_ele 4
• #remove
[Link](365)
TUPLE
tuple1 = ('a', 1)
print(type(tuple1)) <class 'tuple’>
len(tuple1) 2
• #empty tuple
tuple2 = ()
type(tuple2) tuple
NOTE:#tuples are immutable and lists are mutable.
tup = ('mon', 1, 'tue', 2.56)
tup[0], tup[-1], tup[:2] ('mon', 2.56, ('mon', 1))
[Link]('tue’) 2
[Link]('tue’) 1
TUPLE
city = ['patna', 'delhi', 'MP', 'Karnataka','Darbhanga']
river = ['ganga', 'yamuna', 'Narmada', 'Kaveri’]
zip(city, river) <zip at 0x7a38bf218600>
city_riv = list(zip(city, river))
city_riv [('patna', 'ganga'), ('delhi', 'yamuna’),
('MP', 'Narmada'), ('Karnataka', 'Kaveri’)]
t1 = ('a', 1, [2,3,4])
t1 ('a', 1, [2,3,4])
SETS
#sets
#unordered
#it cannot have duplicates
#empty_set === ()
empty_set = set()
print(type(empty_set)) <class 'set’>
set1 = {'a', 2, 4, 8.56}
type(set1) set
l1 = [1,1,2,3,3,3,4]
set(l1) {1, 2, 3, 4}
l2 = list(set(l1)) [1, 2, 3, 4]
SETS
#sets are muttable but the elemnts inside the sets are
immutable.
set1 {2, 4, 8.56, 'a’}
[Link](5)
set1 {2, 4, 5, 8.56, 'a’}
#update
[Link](['b', 'c', 'z’])
set1 {2, 4, 5, 8.56, 'a', 'b', 'c', 'z'}
[Link]('a’)
set1 {2, 4, 5, 8.56, 'b', 'c', 'z'}
SETS Contd..
• #union
A = {1, 2, 3, 4, 5, 6, 7}
B = {1, 2, 3, 8, 9, 10, 11, 23}
[Link](B) {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 23}
A | B {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 23}
• #intersection
[Link](B) {1, 2, 3}
A & B {1, 2, 3}
• #difference
A – B {4, 5, 6, 7}
[Link](B) {4, 5, 6, 7}
B – A {8, 9, 10, 11, 23}
[Link](A) {8, 9, 10, 11, 23}
SETS
#symmetric_difference --- all the elements from A and B
but not the intersection
A.symmetric_difference(B)
{4, 5, 6, 7, 8, 9, 10, 11, 23}
A ^ B {4, 5, 6, 7, 8, 9, 10, 11, 23}
Dictionary
• empty_dict = {}
print(type(empty_dict)) <class 'dict’>
#{key1:value1, key2:val2, .......................}
marvel_dict = {'Name':'Thor','Place':'Asgard','Weapon’ :
'Hammer', 1:2, 3 : 'power', 'alibies’ :
['Ironman','Captain America’],
'abc' : {1:2, 4:5}}
• #number of key value pair in the dictionary
len(marvel_dict) 7
marvel_dict['Place’] 'Asgard’
marvel_dict.keys()
dict_values(['Thor', 'Asgard', 'Hammer', 2, 'power',
['Ironman', 'Captain America'], {1: 2, 4: 5}])
list(marvel_dict.items())
Dictionary
• #keys, #values, #items
• #marvel_dict[3]
marvel_dict.get(3) ‘power’
• #marvel_dict['Thor'] -- key error
marvel_dict.get('Thor', 'Thor Not Found’)
'Thor Not Found’
• #update
marvel_dict.update({'Name': 'Thor', 1: 100, "age":2000})
• #pop --- it will remove/pop the key value pair
marvel_dict.pop(1) 100
marvel_dict.pop('SKS', 'NotFound’) 'NotFound'
Dictionary
dict1 = {'India': 'New Delhi', 'Australia': 'Canberra',
'United States': 'Washington DC', 'England': 'London'}
India
for i in dict1: Australia
print(i) United States
England
dict_items([('India', 'New
[Link]() Delhi'), ('Australia',
'Canberra'), ('United States',
'Washington DC'), ('England',
'London')])
Dictionary
for key,value in [Link]():
print(f'The capital of {key} is {value}')
The capital of India is New Delhi The capital
of Australia is Canberra
The capital of United States is Washington DC
The capital of England is London
Conditionals:
#EVEN or ODD No. if cond:
if num%2 == 0: if cond is true
print('even number') execute this part
else:
else: if cond is false
print('odd number’) execute this part
num = int(input())
if num < 30 and num%2 ==0:
print(f'Number {num} is less than 30 and it is even')
else:
print(f'Number {num} is greater that 30 or it is odd')
Conditionals:
#EVEN or ODD No. with ‘elif’
if num > 30 and num%2 ==0:
print(f'Number {num} is greater than 30 and it is even')
elif num < 30 and num%2 ==0:
print(f'Number {num} is less than 30 and it is even’)
elif num == 30 and num%2 ==0:
print(f'Number {num} is equal than 30 and it is even')
else:
print(f'Number {num} is less that 30 or it is odd')
Conditionals: calculator
num1 = int(input("first number"))
Take three inputs from the
num2 = int(input("second number"))
user
operator = input("Enter the operator : +, -, /, *")
# num1 -- int, num2 -- int,
if operator == '+’: operator -- str(+, -, /, *)
result = num1 + num2 # create a calculator using
elif operator == '-’: if, elif, else conditioning
result = num1 - num2 # inside else --
elif operator == '/': print('choose proper
if num2 == 0: operator')
print("Div by zero is not defined")
result=(“Error”)
else:
result = num1 / num2
elif operator == '*’:
result = num1 * num2
else:
print("Choose a proper operator +, -, /, *")
print (result)
LOOPS: #for loop
• #while --- conditions for i in range(10):
• #for --- iterators # print('IN A FOR LOOP')
print(f'Number is {i} from
##While range(10)’)
counter = 5
while counter >= 0:
print(counter)
counter -= 1 Number is 0 from range(10)
Number is 1 from range(10)
Number is 2 from range(10)
Number is 3 from range(10)
5 Number is 4 from range(10)
4 Number is 5 from range(10)
3 Number is 6 from range(10)
2 Number is 7 from range(10)
1 Number is 8 from range(10)
0 Number is 9 from range(10)
Range
list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list(range(1,11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list(range(11,1,-1))
[11, 10, 9, 8, 7, 6, 5, 4, 3, 2]
l1 = ['apple', 'banana', 'guava', 'strawberry’]
s = ''
for fruit in l1: apple is a fruit
print(f'{fruit} is a fruit') banana is a fruit
guava is a fruit
s = s + ' ' + fruit strawberry is a fruit
for: #take input from user
t1 = (1, 2, 2, 3, 3, 4, 4)
--- x
sum = 0 #create two lists one
for num in t1: is even and another
is odd
sum += num 19
#separate the odd &
number = int(input("Enter the number?")) even numbers in the
even = [] range of the input
taken
odd = []
for num in range(number+1): [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24,
if num % 2 == 0: 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48,
[Link](num) 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72,
74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96,
else: 98, 100]
[Link](num) [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25,
print(even) 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49,
51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73,
print(odd) 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97,
99]
for: (USING PRINT)
got_houses = ['Stark', 'Arryn', 'Baratheon', 'Tully', 'Greyjoy', 'Lannister',
'Tyrell', 'Martell', 'Targaryen']
• for house in got_houses: for index,house in enumerate(got_houses):
print(f'House {house}’) print(f'The index of House {house} is {index}')
House Stark The index of House Stark is 0
House Arryn The index of House Arryn is 1
House Baratheon The index of House Baratheon is 2
House Tully The index of House Tully is 3
House Greyjoy The index of House Greyjoy is 4
House Lannister The index of House Lannister is 5
House Tyrell The index of House Tyrell is 6
House Martell The index of House Martell is 7
House Targaryen The index of House Targaryen is 8
for: (USING PRINT)
s1 = 'Intellipaat'
for ele in s1:
print(ele)
I The index of House Stark is 0
n The index of House Arryn is 1
t The index of House Baratheon is 2
e The index of House Tully is 3
l The index of House Greyjoy is 4
l The index of House Lannister is 5
i The index of House Tyrell is 6
p The index of House Martell is 7
a The index of House Targaryen is 8
a
t
LIST OF SQUARE #take input from the user
num = int(input("Enter the number")) num --- int
list_square_of_numbers = [] #create a list that
contain the square of the
for i in range(1,num+1): elements in the given
list_square_of_numbers.append(i**2) range
# ex:
# num = 10
# output --- [1, 4, 9,
16, 25, 36, 49, .... 100]
square_list = []
number_range = int(input("enter the range of numbers to print squares"))
for i in range(1,number_range+1):
square_list.append(i*i)
print(f'your list of squared numbers is as follows {square_list}')
LIST COMPREHENSION:
num = int(input("Enter the number"))
square_of_numbers = [i**2 for i in range(1,num+1)]
square_of_numbers [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
number = int(input("Enter the number?"))
even = []
odd = [] number = int(input("Enter
for num in range(number+1): the number?"))
if num % 2 == 0: e = [i for i in
[Link](num) range(number+1) if i%2 ==
else:
0]
[Link](num)
print(even)
print(odd)
BREAK:
#break --- break the
num = 1
e = []
o = []
while num < 21:
if num % 2==0:
[Link](num)
if num%10 == 0:
break
else:
[Link](num)
num += 1
DEFINING FUNCTION:
def function():
pass
def even_odd(number,e_o):
'''
this function is used to check get even odd number is used for range e_o is used
to make sure
of the return
''' #even_odd()
even = [] #even_odd(20,'odd')
odd = []
for num in range(number+1):
if num % 2 == 0:
[Link](num)
else:
[Link](num)
if e_o == 'even':
return even
elif e_o == 'odd':
return odd
else:
print('choose even or odd')