Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

11/18/21, 5:25 PM VT-ON-MLP-B44-21ST OCTOBER

In [1]: print(1)
print(2)
print(3)
print(4)
print(5)

In [21]: for i in range(1,9):


print(i, end=' ')

1 2 3 4 5 6 7 8

In [ ]: #v1) summ all the value from 1 to number(n),take number n as input from user.

In [23]: n=eval(input("Enter a number :: "))


s=0
for i in range(1,n+1):
s=s+i
print("My summation is :: ",s)

Enter a number :: 3

My summation is :: 6

In [ ]: # 1) print all the even number between 100 to 200


( use for loop and if else)

In [29]: # if else in python


x=eval(input("Enter a number :: "))
if x>0:
print("The number is positive")

elif x==0:
print("The number is nothing but zero")
else:
print('The number is negative')

Enter a number :: -9.8776

The number is negative

In [ ]: # 1) print all the even number between 100 to 200


# ( use for loop and if else)

localhost:8888/notebooks/VT-ON-MLP-B44-21ST OCTOBER.ipynb 1/18


11/18/21, 5:25 PM VT-ON-MLP-B44-21ST OCTOBER

In [39]: even=[]
odd=[]
for i in range(100,200+1):

if i%2==0:
even.append(i)

else:
odd.append(i)
print("The all even number are :: ",even)
print('--------------------------------------------------------------------------
print("The all odd number are ::: ",odd)

The all even number are :: [100, 102, 104, 106, 108, 110, 112, 114, 116, 118,
120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150,
152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182,
184, 186, 188, 190, 192, 194, 196, 198, 200]

----------------------------------------------------------------------------

The all odd number are ::: [101, 103, 105, 107, 109, 111, 113, 115, 117, 119,
121, 123, 125, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149, 151,
153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183,
185, 187, 189, 191, 193, 195, 197, 199]

In [41]: i=3
while i<10:
print(i)
i=i+1

In [ ]: #1) take 3 diff. value as i/p from user and print only the
# largest number.... ( if else)

localhost:8888/notebooks/VT-ON-MLP-B44-21ST OCTOBER.ipynb 2/18


11/18/21, 5:25 PM VT-ON-MLP-B44-21ST OCTOBER

In [54]: n1=eval(input("Enter 1st number :: "))


n2=eval(input("Enter 2nd number :: "))
n3=eval(input("Enter 3rd number :: "))

if n1>n2 and n1>n3:
print("The largest numebr is ",n1)
elif n2>n3:
print("The largest number is ",n2)
else:
print("the largest number is ",n3)

Enter 1st number :: 6

Enter 2nd number :: 3

Enter 3rd number :: 89


the largest number is 89

In [1]: L=[4,7,9,2,2,1,9] # ctrl+Enter

In [2]: print(L)

[4, 7, 9, 2, 2, 1, 9]

In [3]: print(type(L))

<class 'list'>

In [5]: h=['car','bus',8,4.5,6,6,True, False]

In [6]: print(h)

['car', 'bus', 8, 4.5, 6, 6, True, False]

In [9]: # 1) List is order.



L1=[4,5,7,7,7,'car',True,False,8,6,2.3,1]

In [10]: L1

Out[10]: [4, 5, 7, 7, 7, 'car', True, False, 8, 6, 2.3, 1]

In [ ]: # 2) List allow duplicate element

In [15]: L1=[4,5,7,7,7,'car',True,False,8,6,2.3,1]

localhost:8888/notebooks/VT-ON-MLP-B44-21ST OCTOBER.ipynb 3/18


11/18/21, 5:25 PM VT-ON-MLP-B44-21ST OCTOBER

In [16]: L1

Out[16]: [4, 5, 7, 7, 7, 'car', True, False, 8, 6, 2.3, 1]

In [17]: # 3) List is mutable or changable


L1=[4,5,7,7,7,'car',True,False,8,6,2.3,1]

In [20]: L1[2]='Hydrabad'

In [21]: L1

Out[21]: [4, 5, 'Hydrabad', 7, 7, 'car', 100, False, 8, 6, 2.3, 1]

In [22]: # 4) size of a list is not fixed.



L1=[4,5,7,7,7,'car',True,False,8,6,2.3,1]

In [23]: L1.append('kolkata')

In [24]: L1

Out[24]: [4, 5, 7, 7, 7, 'car', True, False, 8, 6, 2.3, 1, 'kolkata']

In [25]: L1.append(908)

In [26]: L1

Out[26]: [4, 5, 7, 7, 7, 'car', True, False, 8, 6, 2.3, 1, 'kolkata', 908]

In [27]: L1.pop()

Out[27]: 908

In [28]: L1

Out[28]: [4, 5, 7, 7, 7, 'car', True, False, 8, 6, 2.3, 1, 'kolkata']

In [29]: L1.pop()

Out[29]: 'kolkata'

In [30]: L1

Out[30]: [4, 5, 7, 7, 7, 'car', True, False, 8, 6, 2.3, 1]

localhost:8888/notebooks/VT-ON-MLP-B44-21ST OCTOBER.ipynb 4/18


11/18/21, 5:25 PM VT-ON-MLP-B44-21ST OCTOBER

In [31]: ## Take a List as i/p from user... ( use for loop)


l=[]
n=int(input("Enter the list size :: "))
for i in range(n):
v=eval(input("Enter the vale of the list ::: "))
l.append(v)
print('So, My list is ---',l)

Enter the list size :: 7

Enter the vale of the list ::: True

Enter the vale of the list ::: 'car'

Enter the vale of the list ::: 8.9

Enter the vale of the list ::: 4

Enter the vale of the list ::: False

Enter the vale of the list ::: 'kol'

Enter the vale of the list ::: 8.9

So, My list is --- [True, 'car', 8.9, 4, False, 'kol', 8.9]

In [32]: # 1) insert()

L1=[4,5,7,7,7,'car',True,False,8,6,2.3,1]

In [33]: # L1.insert(position, value)


L1.insert(5, 'kolkata')

In [34]: L1

Out[34]: [4, 5, 7, 7, 7, 'kolkata', 'car', True, False, 8, 6, 2.3, 1]

In [ ]: # 2) index()

In [35]: L1

Out[35]: [4, 5, 7, 7, 7, 'kolkata', 'car', True, False, 8, 6, 2.3, 1]

In [37]: L1.index(7)

Out[37]: 2

In [41]: # 3) count()

L1.count(7)

Out[41]: 3

localhost:8888/notebooks/VT-ON-MLP-B44-21ST OCTOBER.ipynb 5/18


11/18/21, 5:25 PM VT-ON-MLP-B44-21ST OCTOBER

In [42]: # 4) remove()

L1

Out[42]: [4, 5, 7, 7, 7, 'kolkata', 'car', True, False, 8, 6, 2.3, 1]

In [43]: L1.remove('kolkata')

In [44]: L1

Out[44]: [4, 5, 7, 7, 7, 'car', True, False, 8, 6, 2.3, 1]

In [45]: L1

Out[45]: [4, 5, 7, 7, 7, 'car', True, False, 8, 6, 2.3, 1]

In [46]: del L1[6]

In [49]: L1

Out[49]: [4, 5, 7, 7, 7, 'car', False, 8, 6, 2.3, 1]

In [50]: # 6) sort()

L2=[6,0,2,8]

In [51]: L2.sort()

In [52]: L2

Out[52]: [0, 2, 6, 8]

In [53]: L2

Out[53]: [0, 2, 6, 8]

In [54]: L2.sort(reverse=True)

In [55]: L2

Out[55]: [8, 6, 2, 0]

In [58]: a=['a','f','b','z','l']

localhost:8888/notebooks/VT-ON-MLP-B44-21ST OCTOBER.ipynb 6/18


11/18/21, 5:25 PM VT-ON-MLP-B44-21ST OCTOBER

In [59]: a.sort()

In [60]: a

Out[60]: ['a', 'b', 'f', 'l', 'z']

In [61]: # 7) reverse()
a=['a','f','b','z','l']

In [62]: a.reverse()

In [63]: a

Out[63]: ['l', 'z', 'b', 'f', 'a']

In [64]: # 8) len()
len(a)

Out[64]: 5

In [68]: # 9) extend()

x=[4,8,9,0]
y=[2,4,5,6]

In [66]: x.extend(y)

In [67]: x

Out[67]: [4, 8, 9, 0, 2, 4, 5, 6]

In [69]: x.append(y)

In [70]: x

Out[70]: [4, 8, 9, 0, [2, 4, 5, 6]]

localhost:8888/notebooks/VT-ON-MLP-B44-21ST OCTOBER.ipynb 7/18


11/18/21, 5:25 PM VT-ON-MLP-B44-21ST OCTOBER

In [8]: # 1) Take below two list L1=[2,5,8,9] and L2=[1,1,1,1], now sum two list
# and print a list like this [3,6,9,10],

L1=[2,5,8,9]
L2=[1,1,1,1]
L3=[]
for i in range(len(L2)):
m=L1.pop()
n=L2.pop()
s=m+n
L3.append(s)
L3.reverse()
print("The final ans afte summation is : ",L3)

The final ans afte summation is : [3, 6, 9, 10]

In [4]: L1=[2,5,8,9]

In [5]: len(L1)

Out[5]: 4

In [ ]: # 2) print only the common element from below given two list....
# [5,2,9,0], and [5,6,2,1]

In [10]: [5,2,9,0]
[5,6,2,1]
L3=[]

for i in [5,2,9,0]:
if i in [5,6,2,1]:
L3.append(i)
print("So, the final common list is :::: ",L3)

So, the final common list is :::: [5, 2]

In [13]: # 1) [L&T Technilogy Services-27/10/2021]


# print only the second largest element from below given
# list [6,1,0,3,8,9], (dont use sort() function)//use max()

L=[6,1,0,3,8,9]
m=max(L)
L.remove(m)
print('The second largest element is :: ',max(L))

The second largest element is :: 8

localhost:8888/notebooks/VT-ON-MLP-B44-21ST OCTOBER.ipynb 8/18


11/18/21, 5:25 PM VT-ON-MLP-B44-21ST OCTOBER

In [15]: L=[6,1,0,3,8,9]
L.sort()
print("So, the largest element is :: ",L[-2])

So, the largest element is :: 8

In [ ]: #10-11-2021
#Tuple

In [2]: # 1) Tuple also order. ( like list)


T='car',True, False,7.8,3.4,9,2

In [3]: T

Out[3]: ('car', True, False, 7.8, 3.4, 9, 2)

In [ ]: # 2) Tuple also allow duplicate elememt.(like list)

In [4]: T1=(True, True,8.9,4,4,4,4,9+9j,6,)

In [5]: T1

Out[5]: (True, True, 8.9, 4, 4, 4, 4, (9+9j), 6)

In [6]: s={6,6,6,6,3,3,3,1,9,9,9,4}

In [7]: s

Out[7]: {1, 3, 4, 6, 9}

In [15]: # 3) Tuple is immutable or not changable.


t=(True,8.9,5,False,'car','bus')

In [16]: t

Out[16]: (True, 8.9, 5, False, 'car', 'bus')

localhost:8888/notebooks/VT-ON-MLP-B44-21ST OCTOBER.ipynb 9/18


11/18/21, 5:25 PM VT-ON-MLP-B44-21ST OCTOBER

In [17]: t[3]=100

---------------------------------------------------------------------------

TypeError Traceback (most recent call last)

<ipython-input-17-318c3cd81b26> in <module>()

----> 1 t[3]=100

TypeError: 'tuple' object does not support item assignment

In [14]: t

Out[14]: [True, 8.9, 5, 100, 'car', 'bus']

In [ ]: # 4) Tuple size is fixed.( we can't increase or decrease the


# size of a tuple by using append() or pop() fun.)

In [24]: t=(6,8,9,0)

In [25]: t.pop()

---------------------------------------------------------------------------

AttributeError Traceback (most recent call last)

<ipython-input-25-0a5da13a6539> in <module>()

----> 1 t.pop()

AttributeError: 'tuple' object has no attribute 'pop'

In [22]: t

Out[22]: [6, 8, 9, 0, 20]

In [29]: # from tuple to list and list to tuple conversion



# tuple()

# list()

# to convert list to tuple use tuple() constructure

# to convert tuple to list use list() constructute


L=[4,7,9,0]

In [30]: L

Out[30]: [4, 7, 9, 0]

localhost:8888/notebooks/VT-ON-MLP-B44-21ST OCTOBER.ipynb 10/18


11/18/21, 5:25 PM VT-ON-MLP-B44-21ST OCTOBER

In [31]: print(type(T))

<class 'list'>

In [32]: T=tuple(L)

In [34]: T

Out[34]: (4, 7, 9, 0)

In [35]: print(type(T))

<class 'tuple'>

In [38]: t = tuple(input("enter elements:"))


print(t)

enter elements:7,2,2,2,2,2

('7', ',', '2', ',', '2', ',', '2', ',', '2', ',', '2')

In [40]: # 1) take a tuple as i/p from user.... (use for loop)



b=[]
n=int(input("Enter the Tuple size ::: "))
for i in range(n):
elemenet=eval(input("Enter tuple element one by one :::: "))
b.append(elemenet)
T=tuple(b)
print("My tuple is ::: ",T)

Enter the Tuple size ::: 7

Enter tuple element one by one :::: True

Enter tuple element one by one :::: 'car'

Enter tuple element one by one :::: False

Enter tuple element one by one :::: 9.0

Enter tuple element one by one :::: 6.7

Enter tuple element one by one :::: 34

Enter tuple element one by one :::: 9+8j

My tuple is ::: (True, 'car', False, 9.0, 6.7, 34, (9+8j))

In [ ]: # Functions in Tuple



# 1) index()
# 2) len()
# 3) count()

In [41]: T=('car',True, 8.9,4,4,4,9+7j,8.2)

localhost:8888/notebooks/VT-ON-MLP-B44-21ST OCTOBER.ipynb 11/18


11/18/21, 5:25 PM VT-ON-MLP-B44-21ST OCTOBER

In [42]: T

Out[42]: ('car', True, 8.9, 4, 4, 4, (9+7j), 8.2)

In [46]: T.index(9)

---------------------------------------------------------------------------

ValueError Traceback (most recent call last)

<ipython-input-46-e0564b2a144f> in <module>()

----> 1 T.index(9)

ValueError: tuple.index(x): x not in tuple

In [47]: len(T)

Out[47]: 8

In [48]: # 3) count()

T

Out[48]: ('car', True, 8.9, 4, 4, 4, (9+7j), 8.2)

In [53]: T.count(90)

Out[53]: 0

In [54]: T.remove(4)

---------------------------------------------------------------------------

AttributeError Traceback (most recent call last)

<ipython-input-54-f949fd54b18e> in <module>()

----> 1 T.remove(4)

AttributeError: 'tuple' object has no attribute 'remove'

In [1]: # 11-11-2021

L1=[1,2,[3,4,5,[6,7,[8,9,0]]]]

In [9]: L1[2][3][2][1]

Out[9]: 9

In [7]: D1={'name':'abc','roll':23, 'address':'kolkata'}

localhost:8888/notebooks/VT-ON-MLP-B44-21ST OCTOBER.ipynb 12/18


11/18/21, 5:25 PM VT-ON-MLP-B44-21ST OCTOBER

In [8]: D1

Out[8]: {'address': 'kolkata', 'name': 'abc', 'roll': 23}

In [10]: D1['name']

Out[10]: 'abc'

In [11]: D1['roll']

Out[11]: 23

In [ ]: # 1) Dict is un-order.

In [12]: D1={'name':'abc','roll':23, 'address':'kolkata'}

In [13]: D1

Out[13]: {'address': 'kolkata', 'name': 'abc', 'roll': 23}

In [ ]: # 2) Dict doesnot allow duplicate element or key. (but allow


# duplicate value).

In [24]: D1={'name':'abc','roll':'abc', 'address':'abc', 'dept':'cse'}

In [25]: D1

Out[25]: {'address': 'abc', 'dept': 'cse', 'name': 'abc', 'roll': 'abc'}

In [ ]: # 3) Dict is also mutable or changable like list.


In [26]: D1={'name':'abc','roll':23, 'address':'kolkata', 'dept':'cse/it/ece/mca'}

In [27]: D1

Out[27]: {'address': 'kolkata', 'dept': 'cse/it/ece/mca', 'name': 'abc', 'roll': 23}

In [28]: D1['name']='Tiger'

In [29]: D1

Out[29]: {'address': 'kolkata', 'dept': 'cse/it/ece/mca', 'name': 'Tiger', 'roll': 23}

localhost:8888/notebooks/VT-ON-MLP-B44-21ST OCTOBER.ipynb 13/18


11/18/21, 5:25 PM VT-ON-MLP-B44-21ST OCTOBER

In [30]: # Insert (key and value pair)



D1

Out[30]: {'address': 'kolkata', 'dept': 'cse/it/ece/mca', 'name': 'Tiger', 'roll': 23}

In [31]: D1['age']=21

In [32]: D1

Out[32]: {'address': 'kolkata',


'age': 21,

'dept': 'cse/it/ece/mca',

'name': 'Tiger',

'roll': 23}

In [33]: D1['sal']=50000

In [34]: D1

Out[34]: {'address': 'kolkata',


'age': 21,

'dept': 'cse/it/ece/mca',

'name': 'Tiger',

'roll': 23,

'sal': 50000}

In [35]: # delete

del D1['age']

In [36]: D1

Out[36]: {'address': 'kolkata',


'dept': 'cse/it/ece/mca',

'name': 'Tiger',

'roll': 23,

'sal': 50000}

localhost:8888/notebooks/VT-ON-MLP-B44-21ST OCTOBER.ipynb 14/18


11/18/21, 5:25 PM VT-ON-MLP-B44-21ST OCTOBER

In [38]: # 1) take a dict as i/p from user.... ( use for loop)


d={}
n=int(input("Enter the size of a dictonary :: "))
for i in range(n):
k=eval(input("Enter key ::: "))
v=eval(input("Enter value ::: "))
d[k]=v
print('The dict is ',d)

Enter the size of a dictonary :: 5

Enter key ::: 'name'

Enter value ::: 'abcd'


Enter key ::: 'roll'

Enter value ::: 21

Enter key ::: 'age'

Enter value ::: 20

Enter key ::: 'address'

Enter value ::: 'mumbai'

Enter key ::: 'Pass'

Enter value ::: True

The dict is {'address': 'mumbai', 'Pass': True, 'name': 'abcd', 'age': 20, 'ro
ll': 21}

In [39]: print(type(d))

<class 'dict'>

In [ ]: ## implement a stack by using list. ( stack-push() and stack-pop())

localhost:8888/notebooks/VT-ON-MLP-B44-21ST OCTOBER.ipynb 15/18


11/18/21, 5:25 PM VT-ON-MLP-B44-21ST OCTOBER

In [40]: st=[]
print("---::: Stack Posh:::----")
n=int(input("Enter the stack size ::: "))
for i in range(n):
v=eval(input("Enter element in the stack :: "))
st.append(v)
print("The stack content after insertion is ---",st)
print('----::: stack pop :::----')
print("The pop element are ::: ")
for i in range(len(st)):
m=st.pop()
print(m)

---::: Stack Posh:::----

Enter the stack size ::: 5

Enter element in the stack :: 'car'

Enter element in the stack :: 'bus'

Enter element in the stack :: 67

Enter element in the stack :: True

Enter element in the stack :: 3

The stack content after insertion is --- ['car', 'bus', 67, True, 3]

----::: stack pop :::----

The pop element are :::

True

67

bus

car

In [ ]: # pandas library

In [1]: import pandas as pd

In [2]: df=pd.DataFrame({'name':['sumit','ikramul','rajit','ajit'], 'roll':[1,2,3,4],


'age':[21,20,23,24],'dept':['cse','it','me','ece']})

In [3]: df

Out[3]:
age dept name roll

0 21 cse sumit 1

1 20 it ikramul 2

2 23 me rajit 3

3 24 ece ajit 4

localhost:8888/notebooks/VT-ON-MLP-B44-21ST OCTOBER.ipynb 16/18


11/18/21, 5:25 PM VT-ON-MLP-B44-21ST OCTOBER

In [4]: print(df)

age dept name roll

0 21 cse sumit 1

1 20 it ikramul 2

2 23 me rajit 3

3 24 ece ajit 4

In [5]: x=['a','b','c','d']
df=pd.DataFrame({'name':['sumit','ikramul','rajit','ajit'], 'roll':[1,2,3,4],
'age':[21,20,23,24],'dept':['cse','it','me','ece']},index=x)

In [6]: df

Out[6]:
age dept name roll

a 21 cse sumit 1

b 20 it ikramul 2

c 23 me rajit 3

d 24 ece ajit 4

In [7]: df1=df

In [8]: df1['address']=['kol','hyd','ulu','jam']

In [9]: df1

Out[9]:
age dept name roll address

a 21 cse sumit 1 kol

b 20 it ikramul 2 hyd

c 23 me rajit 3 ulu

d 24 ece ajit 4 jam

In [14]: d={'name':'First_name'}

In [15]: df1.rename(columns=d,inplace=True)

localhost:8888/notebooks/VT-ON-MLP-B44-21ST OCTOBER.ipynb 17/18


11/18/21, 5:25 PM VT-ON-MLP-B44-21ST OCTOBER

In [16]: print(df1)

age dept irst_name roll address

a 21 cse sumit 1 kol

b 20 it ikramul 2 hyd

c 23 me rajit 3 ulu

d 24 ece ajit 4 jam

In [17]: d={'address':'ADDRESS'}

In [19]: df1.rename(columns=d,inplace=True)

In [20]: df1

Out[20]:
age dept irst_name roll ADDRESS

a 21 cse sumit 1 kol

b 20 it ikramul 2 hyd

c 23 me rajit 3 ulu

d 24 ece ajit 4 jam

localhost:8888/notebooks/VT-ON-MLP-B44-21ST OCTOBER.ipynb 18/18

You might also like