Ansh 313 Python file
Ansh 313 Python file
THERE ARE VARIOUS NUMBER DATA TYPE IN THE PYTHON ARE LIKE INTEGER , FLOAT ,
COMPLEX,THESE ALL ARE NUMERIC DATA TYPES , ALSO OTHER DATA TYPES ARE DICTIONARY,
LIST, STRINGS , TUPLES ETC.
In [11]:
x=313
y=522.33
z="ansh sharma"
m= 3+19j
n=[12,34,56,78,44]
In [2]:
print(x)
313
In [4]:
print(type(x))
<class 'int'>
In [5]:
print(y)
522.33
In [6]:
print(type(y))
<class 'float'>
In [7]:
print(z)
ansh sharma
In [8]:
print(type(z))
<class 'str'>
In [9]:
print(m)
(3+19j)
In [10]:
print(type(m))
<class 'complex'>
In [12]:
print(n)
In [13]:
print(type(n))
<class 'list'>
In [ ]:
THERE ARE VARIOUS ARTHEMATIC OPERATION IN PYTHON ARE LIKE DIVISON, MULTIPLICATION,
ADDITION, SUBTRACTION, MODULOUS,AND POWER
In [2]:
x=9
y=7
ADDITION
In [5]:
print("ADDITION IS:",x+y)
ADDITION IS: 16
SUBTRACTION
In [6]:
subtraction is: 2
multiplication
In [7]:
multiplication is: 63
division
In [8]:
Modulous
In [9]:
modulous is: 2
Power
In [11]:
x=8
y=5
In [12]:
power of x is 32768
In [2]:
print(str1)
print(str2)
PYTHON IS VERY
EASY TO LEARN
In [5]:
In [6]:
result
Out[6]:
In [7]:
PY
In [8]:
print(result[0:10])
PYTHON IS
In [14]:
In [ ]:
In [5]:
import time;
currenttime=time.localtime();
print(time.strftime("The current time is :-""%a %b %d %H:%M:%S %Z %Y",currenttime));
The current time is :-Wed Dec 29 11:50:18 India Standard Time 2021
localhost:8888/notebooks/Task4.ipynb 1/1
12/31/21, 3:39 PM task 5(LISTS) - Jupyter Notebook
In [1]:
list1 = ["apple","mango","banana"]
print(list1)
In [2]:
list1
Out[2]:
APPEND FUNCTION
In [3]:
list1.append("hrishab")
print(list1)
In [8]:
list1.append("kiwi")
In [9]:
Out[9]:
INDEXING OF LIST
In [10]:
list1
Out[10]:
Loading [MathJax]/extensions/Safe.js
In [11]:
list1[0]
Out[11]:
'apple'
In [12]:
list1[2]
Out[12]:
'banana'
In [13]:
list1[-1]
Out[13]:
'kiwi'
In [14]:
list1[-2]
Out[14]:
'kiwi'
REVERSE OF A LIST
In [15]:
list1[::-1]
Out[15]:
LENGTH OF LIST
In [17]:
print(len(list1))
SLICING OF A LIST
Loading [MathJax]/extensions/Safe.js
In [18]:
list1
Out[18]:
In [19]:
list1[0:3]
Out[19]:
In [20]:
list1[0:3]
Out[20]:
In [22]:
list2=[12,34,56,78,90]
list2
Out[22]:
In [23]:
list2[0:2:6]
Out[23]:
[12]
In [25]:
list2[0::]
Out[25]:
In [ ]:
CONCATINATION OF A LIST
Loading [MathJax]/extensions/Safe.js
In [27]:
list3=[13,32,45,65,78]
list4=[313,345,367,321,567]
In [29]:
list3+list4
Out[29]:
[13, 32, 45, 65, 78, 313, 345, 367, 321, 567]
In [30]:
print(list3)
In [32]:
list12=['name',234,12332]
list13=['ansh','m']
newlist=list12+list13
print(newlist)
REMOVE FUNCTION
In [34]:
list1.remove("mango")
list1
Out[34]:
In [ ]:
Loading [MathJax]/extensions/Safe.js
In [35]:
list1>list2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-35-e40886078308> in <module>
----> 1 list1>list2
In [36]:
list12<list13
Out[36]:
False
INSERT FUNCTION
In [37]:
list1.insert(12,89)
print(list1)
In [38]:
list1
Out[38]:
In [43]:
list1.insert(70)
print(list1)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-43-88e670591b46> in <module>
----> 1 list1.insert(70)
2 print(list1)
Loading [MathJax]/extensions/Safe.js
In [40]:
list1
Out[40]:
DEL FUNCTION
In [44]:
del list1
list1
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-44-60be933dfb00> in <module>
1 del list1
----> 2 list1
In [45]:
list1
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-45-32e3a55f6bef> in <module>
----> 1 list1
POP METHOD
In [46]:
list2
Out[46]:
In [47]:
list2.pop()
Out[47]:
90
Loading [MathJax]/extensions/Safe.js
In [48]:
list2
Out[48]:
REMOVE METHOD
In [49]:
list2.remove(34)
In [50]:
list2
Out[50]:
CLEAR FUNCTION
In [53]:
list2.clear()
list2
Out[53]:
[]
SORTING OF LIST
In [54]:
list2
Out[54]:
[]
In [55]:
list5=[9,8,65,31,23]
list5.sort()
Loading [MathJax]/extensions/Safe.js
In [56]:
list5
Out[56]:
SUM FUNCTION
In [57]:
list23=[22,42,66,180]
total =sum(list23)
print(total)
310
In [ ]:
EXTEND FUNCTION
In [58]:
list23.extend(list5)
print(list23)
In [60]:
list23
Out[60]:
In [61]:
list5
Out[61]:
In [ ]:
COUNT METHOD
Loading [MathJax]/extensions/Safe.js
In [62]:
list5.count(32)
Out[62]:
In [63]:
list5.count(9)
Out[63]:
In [64]:
list23.count(31)
Out[64]:
Loading [MathJax]/extensions/Safe.js
Tuple is immutable , it means we can not chnge their value after the
declaration.
In [3]:
tuple1=(122,243,1233)
In [4]:
tuple1
Out[4]:
In [5]:
tuple1=('ansh',8,0,'work',23.45)
In [6]:
tuple1
Out[6]:
INDEXING OF A TUPLES
In [7]:
tuple1[1]
Out[7]:
In [9]:
tuple1[-1]
Out[9]:
23.45
SLICING OF A TUPLE
In [11]:
tuple1
Out[11]:
In [13]:
tuple1[0:4]
Out[13]:
('ansh', 8, 0, 'work')
In [14]:
tuple1[0:2:30]
Out[14]:
('ansh',)
In [ ]:
In [15]:
tuple1[0:2:2]
Out[15]:
('ansh',)
LENGTH FUNCTION
In [16]:
tuple1
Out[16]:
In [17]:
len(tuple1)
Out[17]:
TYPE FUNCTION
localhost:8888/notebooks/TASK6 (TUPLES).ipynb 2/5
12/31/21, 3:40 PM TASK6 (TUPLES) - Jupyter Notebook
In [18]:
type(tuple1)
Out[18]:
tuple
In [19]:
print(type(tuple1))
<class 'tuple'>
JOIN METHOD
In [22]:
tuples2 =("ansh","sharma","aditya")
x=" ,".join(tuples2)
print(x)
In [23]:
tuples2
Out[23]:
INDEXING METHOD
In [25]:
tuples2.index("ansh")
Out[25]:
In [28]:
tuples2.index("aditya")
Out[28]:
COUNT METHOD
In [30]:
tuples2.count("aditya")
Out[30]:
In [35]:
tuples3=(12,12,33,313,344,349)
tuples3.count(12)
Out[35]:
In [36]:
tuples3.count(313)
Out[36]:
In [39]:
tuples3.insert(32)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-39-994d4e131066> in <module>
----> 1 tuples3.insert(32)
POP METHOD
In [40]:
tuples3
Out[40]:
In [41]:
tuples3.pop()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-41-8acbaf2271b1> in <module>
----> 1 tuples3.pop()
In [44]:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-44-6f1655a0e141> in <module>
----> 1 tuples3.clear()
In [ ]:
Dictionaries are unordered and we can not sort. dictionaries uses the key :value pair
In [3]:
dict1={"Name":"Ansh","Branch":"CSE","Roll_no":313}
print(dict1)
dict can also print the indivual key and value pairs.
In [4]:
dict1['Name']
Out[4]:
'Ansh'
In [6]:
dict1["Branch"]
Out[6]:
'CSE'
In [8]:
dict2={"arpit":12,"hrishab":34,"aditya":304,
"LOVISH":{"roll":349,"work":"NOTHING","salary":0.0000}
}
print (dict2)
{'arpit': 12, 'hrishab': 34, 'aditya': 304, 'LOVISH': {'roll': 349, 'work':
'NOTHING', 'salary': 0.0}}
In [ ]:
COPY A DICTIONARY
localhost:8888/notebooks/TASK7(DICT).ipynb 1/7
12/31/21, 3:41 PM TASK7(DICT) - Jupyter Notebook
In [10]:
d2={"ansh":"sharma","hrishab":"raj"}
d3={"fruit":"mango","veg":"paneer"}
d3=d2 # copy function.
print(d3)
d3=dict(d2)
CHANGE METHOD
In [11]:
d2["ansh"]="shshsha"
d2
Out[11]:
In [13]:
d3["fruit"]="BANANA"
d3
Out[13]:
In [14]:
In [15]:
d3
Out[15]:
In [16]:
print(len(d3))
localhost:8888/notebooks/TASK7(DICT).ipynb 2/7
12/31/21, 3:41 PM TASK7(DICT) - Jupyter Notebook
In [17]:
print(len(d2))
In [18]:
Out[18]:
In [19]:
Out[19]:
dict_values(['shshsha', 'raj'])
GET METHOD
In [21]:
Out[21]:
'raj'
In [22]:
d3.get("fruit")
Out[22]:
'BANANA'
In [23]:
d2.get("ansh")
Out[23]:
'shshsha'
localhost:8888/notebooks/TASK7(DICT).ipynb 3/7
12/31/21, 3:41 PM TASK7(DICT) - Jupyter Notebook
In [24]:
d3
Out[24]:
UPDATE FUNCTION
In [26]:
d2.update({"keshav":"garg"})
print(d2)
In [27]:
d2
Out[27]:
TYPE FUNCTION
In [28]:
type(d2)
Out[28]:
dict
In [29]:
type(d3)
Out[29]:
dict
In [31]:
d2
Out[31]:
localhost:8888/notebooks/TASK7(DICT).ipynb 4/7
12/31/21, 3:41 PM TASK7(DICT) - Jupyter Notebook
In [32]:
d2["keshav"]="garg"
d2
Out[32]:
DEL FUNCTION
In [33]:
In [34]:
d2
Out[34]:
POP METHOD
In [36]:
d3.pop("hrishab")
Out[36]:
'raj'
In [37]:
d3
Out[37]:
In [38]:
d2.pop("ansh")
Out[38]:
'shshsha'
localhost:8888/notebooks/TASK7(DICT).ipynb 5/7
12/31/21, 3:41 PM TASK7(DICT) - Jupyter Notebook
In [39]:
d2
Out[39]:
{'hrishab': 'raj'}
In [40]:
d2.popitem()
d2
Out[40]:
{}
In [41]:
d2
Out[41]:
{}
In [42]:
d3.popitem()
Out[42]:
('LOVISH', 349)
In [43]:
d3
Out[43]:
In [45]:
d3.popitem()
Out[45]:
('fruit', 'BANANA')
In [46]:
d3
Out[46]:
{'ansh': 'sharma'}
localhost:8888/notebooks/TASK7(DICT).ipynb 6/7
12/31/21, 3:41 PM TASK7(DICT) - Jupyter Notebook
In [ ]:
localhost:8888/notebooks/TASK7(DICT).ipynb 7/7
12/31/21, 3:42 PM TASK 8 - Jupyter Notebook
def greater(a,b,c):
list1=[a,b,c]
greaterno=max(list1)
print("the greater of three number is: ",greaterno)
In [ ]:
In [ ]:
In [ ]:
In [3]:
In [4]:
In [ ]:
In [ ]:
In [4]:
for i in range(n,0,-1):
print(" " + " *" *i)
i=i-1
print(" " + " *")
*
* *
* * *
* * * *
* * *
* *
*
*
In [ ]:
TASK 11 :Write a Python script that prints prime numbers less than 20
In [5]:
In [ ]:
In [ ]:
def factorial(n):
if n==0:
return 1 #this method is used to find factorial using the recursion.
else:
return n*factorial(n-1)
print("the factorial of the no. is :",factorial(7))
In [ ]:
The program output should indicate whether or not the triangle is a right
triangle
(Recall from the Pythagorean Theorem that in a right triangle, the square of
one
side equals the sum of the squares of the other two sides).
In [ ]:
In [2]:
if hypo**2==((base**2)+(perp**2)):
print("It's a right triangle")
else:
print("It's not a right triangle")
localhost:8888/notebooks/task13.ipynb 1/1
12/31/21, 3:43 PM task 14 - Jupyter Notebook
a = int(input("enter the no. upto which u want to print the fibonacci series
"))
fibonsrs = [0, 1]
In [4]:
import fibonacci
#by importing
enter the no. upto which u want to print the fibonacci series 5
The Fibonnaci series upto 5 is : [0, 1, 1, 2, 3]
In [ ]:
a=10
b=25
def h(a,b):
return a+b
In [5]:
import test
print("here we will add the two numbers by use of importing is: ->")
test.h(2,4) # by importing
here we will add the two numbers by use of importing is: ->
Out[5]:
localhost:8888/notebooks/task15.ipynb 1/1
12/30/21, 7:56 PM task16 - Jupyter Notebook
In [1]:
fn2.close()
print("Content of first file copied to second file ")
fn1.close()
fn2.close()
localhost:8888/notebooks/task16.ipynb 1/2
12/30/21, 7:57 PM task17 - Jupyter Notebook
Task 17: Write a program that inputs a text file. The program
should print all of the unique words in the file in alphabetical
order.
In [ ]:
In [2]:
localhost:8888/notebooks/task17.ipynb 1/1
12/30/21, 7:57 PM task18 - Jupyter Notebook
In [9]:
class introman:
def int_to_Roman(self, num):
val = [
1000, 900, 500, 400,
100, 90, 50, 40,
10, 9, 5, 4,
1
]
syb = [
"M", "CM", "D", "CD",
"C", "XC", "L", "XL",
"X", "IX", "V", "IV",
"I"
]
roman_num = ''
i = 0
while num > 0:
for _ in range(num // val[i]):
roman_num += syb[i]
num -= val[i]
i += 1
return roman_num
localhost:8888/notebooks/task18.ipynb 1/1
12/30/21, 7:58 PM task19 - Jupyter Notebook
In [4]:
class py_pow:
def power(self, x, n):
if x==0 or x==1 or n==1:
return x
if x==-1:
if n%2 ==0:
return 1
else:
return -1
if n==0:
return 1
if n<0:
return 1/self.power(x,-n)
val = self.power(x,n//2)
if n%2 ==0:
return val*val
return val*val*x
Enter x value :3
Enter n value :3
pow(x,n) value is : 27
localhost:8888/notebooks/task19.ipynb 1/1
12/30/21, 7:58 PM task 20 - Jupyter Notebook
In [4]:
class revstrwbw:
def reverse_words(self, s):
return ' '.join(reversed(s.split()))