-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_11functions.py
More file actions
129 lines (73 loc) · 2.74 KB
/
Copy pathday_11functions.py
File metadata and controls
129 lines (73 loc) · 2.74 KB
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
#---------------------------------------------------------------------------------------------------------------------------
#day_11functions.py
#---------------------------------------------------------------------------------------------------------------------------
#1.Create and call functions
def wish():
print("Welcome to Python")
wish()
#----------------------------------------------------------------------------------------------------------------------------
#2.Functions with paramaters
def greet(name):
print("Welcome," name)
greet("Ajith")
#----------------------------------------------------------------------------------------------------------------------------
#3.Function with return value
def add(a,b):
return a+b
res= add(11,22)
print("Sum of two numbers is",res)
#----------------------------------------------------------------------------------------------------------------------------
#4. even/odd using function
num=int(input("Enter a number:"))
def even_odd(num):
if num%2==0:
return("Even number")
else:
return("Odd number")
res= even_odd(num)
print(res)
#----------------------------------------------------------------------------------------------------------------------------
#5.Factorial using function
num = int(input("Enter a number: "))
def fact(num):
fact = 1
if num <= 1:
return 1
for i in range(1, num+1):
fact *= i
return fact
res = fact(num)
print("Factorial of the given number is:", res)
#-----------------------------------------------------------------------------------------------------------------------------
#6. Prime check
def prime(num):
if num <= 1:
return "Not Prime"
for i in range(2, num):
if num % i == 0:
return ("Not Prime")
else:
return ("Prime Number")
result = prime(78)
print(result)
#----------------------------------------------------------------------------------------------------------------------------
#7. lambda function
add = lambda a, b: a + b
result=add(10, 20)
print("Sum:",result)
#-----------------------------------------------------------------------------------------------------------------------------
#8. square of a number
sq=lambda t:t*t
res=sq(56)
print("Square of 56:",res)
#-----------------------------------------------------------------------------------------------------------------------------
#9.Recursive function
num=int(input("Enter a number:"))
def factorial(num):
if num==1:
return 1
else:
return num*factorial(num - 1)
result=factorial(num)
print("Factorial=",result)
#------------------------------------------------------------------------------------------------------------------------------