-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest4.py
More file actions
72 lines (60 loc) · 1.88 KB
/
test4.py
File metadata and controls
72 lines (60 loc) · 1.88 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
# This file contains sample code for various Python concepts such as functions, recursion, and list manipulation.
# Function to calculate the length of a list
def lenList(list):
length = len(list)
print("Length of the list is: ", length)
list = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
lenList(list)
# Function to print the elements of a list
def printList(list):
length = len(list)
for item in list:
if item == list[length-1]:
print(item)
else:
print(item, end=" ")
printList(list)
#Function to calculate the factorial of a number using while loop
def factorialNumber(n):
i = 1
multiply = 1
while i <= n:
multiply *= i
i += 1
print("Factorial of number", n, "is:", multiply)
factorialNumber(5)
# Function to convert USD to INR
def convertUSDtoINR(usd):
inr = usd * 82.74
print(usd, "USD is equal to", inr, "INR")
convertUSDtoINR(100)
# Function to check if a number is odd or even
def oddEvenNumVerify(num):
if num%2 == 0:
print(num, "is an even number")
else:
print(num, "is an odd number")
num = int(input("Enter a number to check if it is odd or even: "))
oddEvenNumVerify(num)
# Function to calculate the sum of first n numbers using recursion
def sumOfNumber(n):
if (n == 0):
return 0
else:
return n + sumOfNumber(n-1)
print(sumOfNumber(10))
# Function to print the elements of a list using recursion
def recursivePrintList(list, index):
if index < len(list):
print(list[index])
recursivePrintList(list, index + 1)
recursivePrintList(list, 0)
# Function to print the elements of a list in reverse order using recursion
def reverseRecursivePrintList(list, index):
if index == -1:
return
if index < len(list):
print(list[index])
reverseRecursivePrintList(list, index - 1)
length = len(list)
reverseRecursivePrintList(list, length - 1)