Skip to content

Commit a5ffe80

Browse files
authored
Functions
1 parent 297750f commit a5ffe80

File tree

1 file changed

+317
-0
lines changed

1 file changed

+317
-0
lines changed

Functions

Lines changed: 317 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,317 @@
1+
#FUNCTIONS
2+
#Practice:
3+
# 1
4+
def increment(p,q):
5+
global inc
6+
if q in range(1, 15):
7+
inc=float(20/100)
8+
elif q in range(15,20):
9+
inc=float(10/100)
10+
elif q in range(20,23):
11+
inc=float(5/100)
12+
salary=(float(p*inc)+p)
13+
return salary
14+
15+
n=input("Employee's Name:")
16+
s=float(input("Employee's Salary:"))
17+
g=int(input("Employee's Grade Scale:"))
18+
19+
print("Increment Amount Is:",increment(s,g)-s)
20+
print("The Updated Salary of ",n,"is:",increment(s,g))
21+
22+
# 2
23+
def update(l2):
24+
print("l2 is",l2)
25+
print("location of l2 is",id(l2))
26+
l2[2]=30
27+
print("l2 is",l2)
28+
print("location of l2 is",id(l2))
29+
return l2
30+
31+
l1=[10,20,30]
32+
print(l1)
33+
print("location of l1 is",id(l1))
34+
update(l1)
35+
print("After returning from function my list is")
36+
print(l1)
37+
print("location of li is",id(l1))
38+
39+
#3
40+
# Cannot input a whole listb/c then there will be no changes in original list!!
41+
# It'll be a new list with separate location
42+
def update(l2):
43+
print("l2=",l2,"and location is",id(l2))
44+
l2=[10,20,30]
45+
print("now l2=",l2,"and location is", id(l2))
46+
return l2
47+
48+
l1=[100,200,300]
49+
print("l1=",l1,"and location is", id(l1))
50+
update(l1)
51+
print("After returning the fuction")
52+
print("l1=",l1,"and location is", id(l1))
53+
54+
# 4
55+
# In immutable datatype,The original data will not change but there will be a change in location!!
56+
def modify(a):
57+
print("a=",a,id(a))
58+
a=50.4
59+
print("now a=",a,id(a))
60+
return a
61+
x=20
62+
print("x=",20)
63+
modify(x)
64+
print("After returning the fuction")
65+
print("x=",x)
66+
67+
# 5
68+
def modify(a):
69+
print("a=",a,"and location is", id(a))
70+
a=50.4
71+
print("now a=",a,"and location is", id(a))
72+
return a
73+
x=20.5
74+
print("x=",20)
75+
modify(x)
76+
print("After returning the fuction")
77+
print("x=",x,"and location is", id(x))
78+
79+
#6
80+
def update(l2):
81+
print(l2,"location of l2 is",id(l2))
82+
l2[0]=[2.09,"NED",45]
83+
l2[1]=100
84+
l2[2]=400
85+
print(l2,"location of l2 is",id(l2))
86+
return l2
87+
l1=[10,20,30]
88+
print(l1,"location of l1 is",id(l1))
89+
update(l1)
90+
print("After returning from function my list is")
91+
print(l1,"location of l1 is",id(l1))
92+
93+
# 7
94+
#airthematic sequence
95+
def seq(p,q,r):
96+
Tn=a+(n-1)*d
97+
return Tn
98+
99+
a=int(input("Enter The First Term (a):"))
100+
d=int(input("Enter The Common Difference (d):"))
101+
n=int(input("Enter The No Of Terms (n):"))
102+
seq(a,d,n)
103+
print("The nth Term Of The Sequence Is:",seq(a,d,n))
104+
105+
106+
107+
# 8
108+
#FACTORIALS
109+
def fact(no):
110+
fact=1
111+
for i in range(no,1,-1): #OR for i in range(1,no+1)
112+
fact=fact*i
113+
return fact
114+
num=int(input("enter no"))
115+
print(num,"!","=",fact(num))
116+
117+
#AVERAGE BY APPLYING FUNCTIONS
118+
119+
#9 (by providing the numbers)
120+
def avg(l2):
121+
size=len(l2)
122+
sum=0
123+
for i in l2:
124+
sum=sum+i
125+
avg=sum/size
126+
return avg
127+
l1=[66,80,90,77,98]
128+
print("The Average result of a class is",avg(l1))
129+
130+
#10 (by user input)
131+
def avg(l2):
132+
size=len(l2)
133+
sum=0
134+
for i in l2:
135+
sum=sum+i
136+
avg=sum/size
137+
return avg
138+
l1=[]
139+
print("Enter marks")
140+
for i in range(0,5):
141+
l1.append(int(input()))
142+
print(l1)
143+
print("The average result of a class is",avg(l1))
144+
145+
#11
146+
def avg(l2):
147+
avg=sum(l2)/len(l2)
148+
return avg
149+
l1=[]
150+
print("Enter marks")
151+
for i in range(0,5):
152+
l1.append(int(input()))
153+
print(l1)
154+
print("The average result of a class is",avg(l1))
155+
156+
# 12
157+
def acronym(a):
158+
p=""
159+
for i in a:
160+
if i.isupper()==True:
161+
p=p+i #Or simply print(i)
162+
else:
163+
pass
164+
return p
165+
166+
s=str(input("Enter A Phrase:").title())
167+
print("The Acronym Of",s,"Is:",acronym(s))
168+
169+
# OR
170+
def acronym(p):
171+
words=p.split()
172+
data=" "
173+
for i in words:
174+
data=data+i[0].upper() #OR print(i[0].upper())
175+
return data
176+
s=str(input("Enter a String:"))
177+
print("The Acronym Of",s,"Is:",acronym(s))
178+
179+
# Printing last letter of each word in capital:
180+
def cap(p):
181+
print("The last letters of Strings Are:",end="")
182+
word=p.split()
183+
for i in word:
184+
print(i[-1].upper(),end="")
185+
186+
s=str(input("Enter a phrase:"))
187+
cap(s)
188+
189+
# 13 (Product Of Two Lists)
190+
def xmult(a,b):
191+
l3=[]
192+
for (num1,num2) in zip (a,b):
193+
l3.append(num1*num2)
194+
return l3
195+
196+
l1=[]
197+
l2=[]
198+
199+
n=int(input("No Of Integers In l1:"))
200+
print("Input Integers For List 'l1'")
201+
for i in range(n):
202+
p=int(input())
203+
l1.append(p)
204+
205+
m=int(input("No Of Integers In l2:"))
206+
print("Input Integers For List 'l2'")
207+
for i in range(m):
208+
q=int(input())
209+
l2.append(q)
210+
211+
print("")
212+
print(l1)
213+
print(l2)
214+
print("")
215+
print("Product Of Integers From l1 and l2:",xmult(l1,l2))
216+
217+
# 14
218+
# Table by While Loop:
219+
def table(tno,trange):
220+
i=0
221+
while(i<trange):
222+
i = i + 1
223+
t=print(tno,"*",i,"=",tno*i)
224+
225+
no=int(input("Enter table no:"))
226+
range=int(input("Enter table range:"))
227+
table(no,range)
228+
229+
# Table by for loop
230+
def table(a,b):
231+
for i in range(1,b+1):
232+
print(a,"*",i,"=",tno*i)
233+
234+
tno=(int(input("Enter table no:")))
235+
trange=(int(input("Enter table range:")))
236+
table(tno,trange)
237+
238+
# 15
239+
def novowel(v):
240+
for i in v:
241+
if i not in "aeiouAEIOU":
242+
return (print(True))
243+
else:
244+
return (print(False))
245+
246+
247+
s = str(input("Enter string:"))
248+
novowel(s)
249+
250+
# 16
251+
# All Integers in the List Are Even:
252+
def alleven(a):
253+
for i in a:
254+
if i%2 == 0:
255+
return (print("True"))
256+
else:
257+
return (print("False"))
258+
259+
l = []
260+
n=int(input("Enter The no of integers:"))
261+
for i in range(1,n+1):
262+
print("Enter integer",i,":")
263+
n = int(input())
264+
l.append(n)
265+
print(l)
266+
alleven(l)
267+
268+
# 17
269+
# Printing Negative Numbers Only
270+
def negative(p):
271+
for i in p:
272+
if i<0:
273+
print(i)
274+
return(print("Negatives"))
275+
else:
276+
return(print("No Negative Integers"))
277+
278+
l=[]
279+
n=int(input("Enter no of Integers:"))
280+
for i in range(1,n+1):
281+
print("Enter integer",i)
282+
x=eval(input())
283+
l.append(x)
284+
print(l)
285+
negative(l)
286+
287+
# 18
288+
def even(a):
289+
print("The numbers divisible by 2 or by 3 are:")
290+
for i in range(1,a+1):
291+
if i%2==0:
292+
print(i,end=" ")
293+
elif i%3==0:
294+
print(i,end=" ")
295+
296+
n=int(input("Enter Number:"))
297+
even(n)
298+
299+
# 19
300+
def month(m):
301+
months=('Jan Feb Mar Apr May June July Aug Sep Oct Nov Dec').split()
302+
return (months[m-1])
303+
304+
n=int(input("Enter number between 1-12:"))
305+
print("The month is:",month(n))
306+
307+
# 20
308+
def cheer(n):
309+
print("How do you spell Winner?\nI know I know!")
310+
for i in n:
311+
print(i.upper(),end=" ")
312+
print('!')
313+
print("")
314+
print("And that's how you spell Winner!\nGO",n.title(),"!")
315+
316+
name=input("Enter A Team Name:")
317+
cheer(name)

0 commit comments

Comments
 (0)