forked from tanishh/HH-PA2609
-
Notifications
You must be signed in to change notification settings - Fork 0
/
15Functions.py
154 lines (81 loc) · 2.13 KB
/
15Functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-
#Functions
#Block of code which only runs when it is called
#can pass data, known parameters into function
#it can return data as result
#defined using keyword def with colon :
#declaration or defination of function
def oper():
print(a+b)
print(a-b)
print(a*b)
#calling that declared Functions
a=10
b=20
oper()
oper()
oper()
def printHello():
print("Hello ")
printHello()
def printHello2(fname, lname):
print("Hello \t" + fname + lname)
printHello2() #error
printHello2('vikas', 'khullar')
def printHello3(name='Student'):
print("Hello \t" + name)
printHello3() #without value
printHello3('Dhiraj')
def printdef1(a,b):
print("name "+a+b)
printdef1("Amit", "Saho")
L1=['a','b','c']
L2=['d','e','f']
L3=['g','h','i']
for i in L1:
print("Hello \t" + i)
for i in L2:
print("Hello \t" + i)
for i in L3:
print("Hello \t" + i)
#passing a list
def printHello4(names):
for i in names:
print("Hello \t" + i)
printHello4(L1)
printHello4(L2)
printHello4(L3)
printHello4()
printHello4('Dhiraj')
printHello4(['Dhiraj','Kounal','Pooja'])
namelist = ['Student1','Student2','Student3']
printHello4(namelist)
#retun values
def totalSale(x=0,y=0):
return(x + y)
totalSale()
totalSale(5,10)
"""Define a function reverse() that computes the reversal
of a string. For example, reverse("I am testing") should
return the string "gnitset ma I"."""
def reverse(str):
return str[::-1]
#test
print (reverse("I am testing"))
print reverse("It's cool, isn't it?")
"""The function max() from exercise 1) and the function
max_of_three() from exercise 2) will only work for two
and three numbers, respectively. But suppose we have a
much larger number of numbers, or suppose we cannot tell
in advance how many they are? Write a function max_in_list()
that takes a list of numbers and returns the largest one."""
def max_in_list(lst):
max = 0
for n in lst:
if n > max:
max = n
return max
#test
print (max_in_list([2,3,4,5,6,7,8,8,9,10,1,5]))
print max_in_list([290,2,3,4])
print max_in_list([9,2,3,4,19])