-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython basics.py
More file actions
120 lines (87 loc) · 3.19 KB
/
Copy pathpython basics.py
File metadata and controls
120 lines (87 loc) · 3.19 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
#write a program in py to accept a number from the user and print digits of the number in a reverse order
a = int(input("Enter a number: "))
reversed_number = int(str(a)[::-1])
print("Reversed Number:", reversed_number)
# write a py program to chek leap year or not
def function():
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100!= 0) or (year % 400 == 0):
print(year, "is a leap year.")
else:
print(year, "is not a leap year.")
function()
# 2)accept string from user and perform following operations calculate length of string reverse the string
def function2():
string = input("Enter a string: ")
length = len(string)
print("Length of the string is: ", length)
reversed_string = string[::-1]
function2()
# 3) python program that accept string from user and perform following operations calculate length of string and reverse string.
def function3():
string = input("Enter a string: ")
length = len(string)
print("Length of the string is: ", length)
reversed_string = string[::-1]
# 4) py program that accepts string from user and chek equality of 2 string
def function4():
string1 = input("Enter the first string: ")
string2 = input("Enter the second string: ")
if string1 == string2:
print("Both strings are equal.")
else:
print("Both strings are not equal.")
# 5) py program that finds the sum of all the numbers in a list using a while loop
def function5():
numbers = [1, 2, 3, 4, 5]
total_sum = 0
i = 0
while i < len(numbers):
total_sum += numbers[i]
i += 1
print("The sum of the numbers is:", total_sum)
# Call the function
function5()
#6) write a python to find whether the perticuler element is present in the list using a loop
def function6():
numbers = [1, 2, 3, 4, 5]
element = 4
for num in numbers:
if num == element:
print(element, "is present in the list.")
break
else:
print(element, "is not present in the list.")
#7) program to find out the greatest number from three number using function
def find_greatest():
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))
if num1 > num2 and num1 > num3:
return num1
elif num2 > num1 and num2 > num3:
return num2
else:
return num3
# Call the function and display the result
greatest_number = find_greatest()
print("The greatest number is:", greatest_number)
#8) string methods
#Upper
def function8():
string = input("Enter a string: ")
print("Uppercase:", string.upper())
#Lower
def function9():
string = input("Enter a string: ")
print("Lowercase:", string.lower())
#Title
def function10():
string = input("Enter a string: ")
print("Title Case:", string.title())
#Replace
def function11():
string = input("Enter a string: ")
old_char = input("Enter the old character: ")
new_char = input("Enter the new character: ")
print("Replaced String:", string.replace(old_char, new_char))