forked from AliOsmanYilmaz/Pyhton-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.py
More file actions
252 lines (142 loc) · 5.18 KB
/
python.py
File metadata and controls
252 lines (142 loc) · 5.18 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#welcome to python exercises
#Select with Ctrl + K then Ctrl + U to uncomment the code blocks
#-----------------------------------------------------------------
# Check if a number is prime #
# print("Check if a number is prime")
# isPrime = True
# a = 2
# number = int(input("Enter a number: "))
# if number <= 1 :
# isPrime = False
# while a != number:
# if number % a == 0:
# isPrime = False
# break
# a += 1
# if isPrime:
# print(f"{number} is a prime number.")
# else:
# print(f"{number} is not a prime number.")
#-------------------------------------------------------------------
# Count the number of divisors of a number #
# print("Count the number of divisors of a number")
# count = 0
# a = 1
# i = 1
# number = int(input("Enter a number: "))
# while a <= number:
# if number % a == 0:
# count += 1
# print(i,". element is ",a)
# i += 1
# a += 1
# print("Count : ",count)
#-------------------------------------------------------------------
# Sum of digits of a number #
#print("Sum of digits of a number")
# sum = 0
# number = int(input("Enter a number: "))
# number_str = str(number)
# for digit in number_str:
# sum += int(digit)
# print("Sum of digits : ",sum)
#OR
# print("Sum of digits of a number")
# sum = 0
# number = int(input("Enter a number: "))
# while number > 0:
# digit = number % 10
# sum += digit
# number = number // 10 #integer division
# print("Sum of digits : ",sum)
#-------------------------------------------------------------------
# Find maximum and minimum number from a list of numbers #
# print("Find maximum and minimum number from a list of numbers")
# numbers = [1,2,3,4,5]
# numbers[0] = int(input("Enter first number: "))
# numbers[1] = int(input("Enter second number: "))
# numbers[2] = int(input("Enter third number: "))
# numbers[3] = int(input("Enter fourth number: "))
# numbers[4] = int(input("Enter fifth number: "))
# max_number = numbers[0]
# for num in numbers:
# if num > max_number:
# max_number = num
# min_number = numbers[0]
# for num in numbers:
# if num < min_number:
# min_number = num
# print("Maximum number is : ",max_number)
# print("Minimum number is : ",min_number)
#OR
# print("Find maximum and minimum number from a list of numbers")
# numbers = []
# for index in range(5):
# number = int(input("Enter number : "))
# numbers.append(number)
# print("Maximum number is : ",max(numbers))
# print("Minimum number is : ",min(numbers))
#-------------------------------------------------------------------
# Check if a number is a perfect square #
# print("Check if a number is a perfect square")
# a = 1
# number = int(input("Enter a number: "))
# while(a * a <= number):
# if (a * a == number):
# print(f"{number} is a perfect square.")
# break
# a += 1
# if (a * a != number):
# print(f"{number} is not a perfect square.")
#OR
# print("Check if a number is a perfect square")
# import math
# number = int(input("Enter a number: "))
# if math.sqrt(int(number)).is_integer():
# print(f"{number} is a perfect square.")
# else:
# print(f"{number} is not a perfect square.")
#OR
# print("Check if a number is a perfect square")
# number = int(input("Enter a number: "))
# sqrt = number ** 0.5
# if sqrt == int(sqrt):
# print(f"{number} is a perfect square.")
# else:
# print(f"{number} is not a perfect square.")
#-------------------------------------------------------------------
# Count frequency of each character in a string #
# print("Count frequency of each character in a string")
# string = input("Enter a string: ")
# string = string.replace(" ", "") # Remove spaces
# string = string.lower() # Convert to lowercase
# dictionary = dict()
# for char in string:
# if char in dictionary:
# dictionary[char] += 1
# else:
# dictionary[char] = 1
# print("Character frequencies:")
# for char, freq in dictionary.items():
# print(f"'{char}': {freq}")
#-------------------------------------------------------------------
# Replace all occurrences of 'a' with 'A' in a string #
# print("Replace all occurrences of 'a' with 'A' in a string")
# string = input("Enter a string: ")
# string = string.replace(" ", "") # Remove spaces
# string2 = ""
# for char in string:
# if char == "a":
# string2 += "A"
# else:
# string2 += char
# print("Modified string:",string2)
#OR
# print("Replace all occurrences of 'a' with 'A' in a string")
# string = input("Enter a string: ")
# string = string.replace(" ", "") # Remove spaces
# for char in string:
# if char == "a":
# string = string.replace("a","A")
# print("Modified string:",string)
#-------------------------------------------------------------------