-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
executable file
·789 lines (539 loc) · 12.5 KB
/
Copy pathtest.py
File metadata and controls
executable file
·789 lines (539 loc) · 12.5 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
print ( 2 < 4 )
min_score = 13
score = 13
print(score > min_score)
print(score <= min_score)
'python'>'Python'
x = -10
if x < 0:
print("The negative number ", x, " is not valid here.")
print("This is always printed")
if 4 + 5 == 10:
print("TRUE")
else:
print("FALSE")
print("TRUE")
1st if statement
if True: print('hello')
2nd if statement
if (5,10):
print('hello')
3rd if statement
if (yes):
print('hello')
4th if statement
if (5,10): print('hello')
x = 0
a = 6
b = 6
if a > 0:
if b < 0:
x = x + 6
elif a > 6:
x = x + 5
else:
x = x + 4
else:
x = x + 3
print(x)
num_1 = int(input("enter first number = "))
num_2 = int(input("Enter second number = "))
total = num_1 + num_2
print("the total is = ", total)
for letter in "python":
print(letter)
import pyjokes
jokes = pyjokes.get_joke()
print(jokes)
import math
for i in range(1, 11):
i = i * 5
print(i)
import pyttsx3
engine = pyttsx3.init()
engine.say("""
I met an old woman at the bus stop today.
She talked to me, but I didn’t know what to say.
‘Your little brother looks smart,’ she said.
But I didn’t know what to say.
‘I’m from Romania,’
‘I’ve been here six years,’
‘I sleep in the shelters.”
I held my brother’s hand tighter.""")
engine.runAndWait()
import os
print(os.listdir('.'))
print(os.getcwd())
print(os.open('test.py', os.O_RDONLY))
directory_path = "/"
contents = os.listdir(directory_path)
for item in contents:
print(item)
a = "This is the first line \nthis is the second line"
print(a)
name = input("Enter your name: ")
print(f"Good Afternoon {name}")
letter = """Dear <|Name|>,
You are selected!
<|Date|>"""
print(letter.replace("<|Name|>", "Anirban").replace("<|Date|>", '13th sept, 1996'))
name = "harry isagoodboyandagreatabuddy"
if (name.find("goo")):
print(name.index("goo"))
name = "harry is a good boy and a great buddy"
print(name.replace(" ", " "))
letter = "Dear Harry, \n\tThis is Python course. \nThanks!"
print(letter)
friends = ["Anirban", "Satyarth", "Orange", 1, 345.6 , False, "Rohit", "Satyarth"]
print(friends[1:5])
friends[0] = "Anirban Das" #Unlike strings, lists are mutable, which means we can change their content without changing their identity.
print(friends)
friends.append("Harry")
print(friends)
friends.insert(1, "Satyarth")
print(friends)
friends.remove("Satyarth")
print(friends)
friends.pop(1)
print(friends)
l1 = [1, 5, 3, 9, 2]
l1.sort()
print(l1)
l1.reverse()
print(l1)
l1.insert(3, 333333)
print(l1)
marks = []
m1 = input("Enter the marks of 5 students separated by space: ").split()
marks = [int(i) for i in m1]
print(marks)
a = (1, 2, 3, 4, 5, 5, 5, 5, 5, 5)
n = a.count(5)
print(n)
marks = {
"Anirban": 95,
"Satyarth": 90,
"Rohit": 85,
"Harry": 80
}
# # print(marks, type(marks))
# print(marks["Anirban"])
marks.update({"Anirban": 99})
print(marks.get("Anirban"))
print(marks.keys())
print(marks.values())
print(marks.items())
empty_set = set() #This is an empty set, also no values can be repeated
s = {1, 2, 3, 4, 5, 5, 5, 5}
s.add(6)
print(s)
s1 = {1, 2, 3, 4, 5}
s2 = {4, 5, 6, 7, 8}
print(s1.union(s2)) #union of two sets₹
print(s1.intersection(s2)) #intersection of two sets
translations = {
"hello": "hola",
"goodbye": "adios",
"thank you": "gracias",
"please": "por favor"
}
# print(translations.get("hello"))
word = input("Enter a word to translate: ")
print(translations.get(word, "Translation not found."))
numbers = int(input("Enter 8 numbers separated by space: "))
print(numbers)
s = set()
s.add(20)
s.add('10')
s.add(20.0)
print(len(s))
dic = {}
n1 = input("Enter the name of the student: ")
m1 = int(input("Enter the marks of the student: "))
dic.update({n1: m1})
print(dic)
age = int(input("Enter your age: "))
if age < 18:
print("You are a minor.")
elif age >= 18 and age < 65:
print("You are an adult.")
else:
print("You are a senior citizen.")
****SPAM FILTER****
m1 = "buy now"
m2 = "limited time offer"
m3 = "click this"
message = input("Enter your message: ")
if (m1 in message) or (m2 in message) or (m3 in message):
print(f"This message is spam, In contains {message}" )
else:
print("This message is not spam.")
loops in python#
for i in range(1, 11, 2):
print(i)
i = 1
while i <= 10:
print(i)
i += 1
for i in "harry":
print(i)
i = 0
while i < 5:
i = i + 1
print("harry")
list = [1, "hello", 3.14, True]
i = 0
while (i < len(list)):
print(list[i])
i += 1
for i in range(4):
print(i)
l = [1, 2, 3, 4, 5]
for item in l:
print(item)
else:
print("This is end...")
for i in range(100):
if i == 34:
break #exirt the loop right now
print(i)
for i in range(100):
if i == 34:
continue #skip the current iteration and move to the next one
print(i)
table = int(input("Enter the number to print its multiplication table: "))
for i in range(1, 11):
print(f"{table} x {i} = {table * i}")
l = ["harry", "Soham", "Sachin", "rohit"]
for name in l:
if(name.startswith("S")):
print(f"Hello, {name}!")
i = 1
while i < 11:
print(f"{table} x {i} = {table * i}")
i += 1
n = int(input("Enter a number: "))
i = 1
sum = 0
while i <= n:
sum += i
i += 1
print(sum)
n = int(input("Enter a number: "))
i = 1
factorial = 1
while i <= num:
factorial *= i
i += 1
print(factorial)
for i in range(1, n+1):
print(" " * (n-i), end="")
print("*" * (2*i-1), end="")
print("")
def input_number(num):
return int(input("Enter a number : ")) * num
print(input_number(num = 10))
a = 0
def add_three(a):
return a+3
result = add_three(3)
print(result)
def add_func(num1,num2):
return num1 + num2
print ( add_func(5 , 5) )
def my_function(*friends):
print("The tallest student is " + friends[0])
my_function("john", "Ella", "mark")
def fullname_func(fname):
print(fname + " Mark")
fullname_func("John")
def fullname_func(fname, lname):
print(fname + " " + lname)
fullname_func("John", "Mark")
def print_sum (num1, num2):
sum = num1 + num2
if(sum==0):
return True
print("The sum is: " + str(sum))
print_sum(-1, 1)
def square(i):
j = i * i
return j
print(square(3))
def is_true(a):
return bool(a)
result = is_true(3<6)
print("The result is", result)
def square(i):
j = i * i
return j
num = 2
result = square(num)
print("The result of ", num, " is ", result)
def return_greeting():
return "Hello, World"
print(return_greeting())
def my_function(x):
return 5 + x
print(my_function(3))
def add(x, y):
return x+y
def my_function(x):
return 10 - x
print(my_function(4))
def my_function(x):
return 10 / x
print(my_function(2))
def is_true(a):
return bool(a)
result = is_true(6<3)
print("The result is", result)
def multiply_values(list):
multiplied_values = []
for item in list:
multiplied_values.append(item * 2)
return multiplied_values
numbers = [1, 2, 3, 4, 5]
print(multiply_values(numbers))
def get_odd_func(numbers):
odd_numbers = [num for num in numbers if num % 2]
return odd_numbers
print(get_odd_func([1, 2, 3, 4, 5, 6]))
def get_even_func(numbers):
even_numbers = [num for num in numbers if not num % 2]
return even_numbers
print(get_even_func([1, 2, 3, 4, 5, 6]))
def mean_func(list1):
return sum(list1) / len(list1)
print(mean_func([5, 2, 2, 4]))
def my_function(numbers):
for i in numbers:
print(i+1, end=' ')
numbers = [1, 2, 3]
my_function(numbers)
def my_function(numbers):
for i in numbers:
print(i*2+10, end=' ')
numbers = [1, 2, 3]
my_function(numbers)
def my_function(names):
for i in names:
print(i, end=' ')
names = ["john", "mark", "emmy"]
my_function(names)
def double_list(numbers):
return 2 * numbers
numbers = [1, 2, 3]
print(double_list(numbers))
def mean_func(list1):
return sum(list1) / len(list1)
print(mean_func([5, 6, 7, 8]))
def my_function():
def my_inner_function():
x = 20
print(x)
my_inner_function()
my_function()
x = 30
def my_function():
global x
x = 20
my_function()
print(x)
x = 20
def my_function():
print(x, end=' ')
my_function()
print(x, end=' ')
def my_function():
def my_inner_function():
x = 20
print(x)
my_inner_function()
my_function()
x = 20
def my_function():
x = 30
print(x, end=' ')
my_function()
print(x, end=' ')
def myfunc():
a = 20
print(a)
myfunc()
def myfunc():
a = 20
myfunc()
print(a)
def my_function():
global x
x = 30
my_function()
print(x)
def my_function():
x = 20
def my_inner_function():
print(x)
my_inner_function()
my_function()
x = 20
def my_function():
x = 30
print(x, end=' ')
my_function()
def sum(*args):
for arg in args:
result += arg
return result
print(sum(2,3,1))
def my_function(*argv):
print(argv[0])
my_function('Hello', 'World!')
def division(a,b):
return a/b
print(division(8,2))
def sum(a,b):
return a+b
print(sum(2,3))
def my_function(*ages):
print("The older friend is " + ages[0] + " years")
my_function("13", "12", "11")
def my_function(*argv):
for arg in argv:
print(arg)
my_function('Hello', 'World!')
def my_function(*argv):
print(argv)
my_function('Hello', 'World!')
def my_function(arg1, *argv):
print ("First argument:", arg1)
for arg in argv:
print("Next argument:", arg)
my_function('Welcome', 'to', 'Python!')
tuple1 = (0, 1, 2, 3, 4, 5)
print(tuple1[0:4])
x = tuple(3)
print(x)
a = (10, [20, 30], 40, 50)
print(a.index(30))
a = (10, 20, 30, 40, 50)
a = a[::-1]
print(a)
testdict = {
"brand": "apple",
"ram": "3",
"year": 2020,
"year": 2021
}
print(testdict)
testdict = {
"brand": "Samsung",
"ram": "3",
"Os": "Android",
"year": 2020
}
testdict.update({'brand':'oppo' })
print(testdict)
testdict = {
"brand": "Samsung",
"ram": "3",
"Os": "Android",
"year": 2020
}
print(testdict.keys())
testdict = {
"brand": "Samsung",
"ram": "3",
"Os": "Android",
"year": 2020
}
print(testdict.items())
num = int(input("Enter a number: "))
for i in range(1, 11):
print(f"{num} x {11 - i} = {num * (11 - i)}")
#### functions #####
def average(num1, num2):
return (num1 + num2) / 2
print(average(5, 10))
def func(): #function definition
print("This is a function")
func() #function call
def goodDay(name, ending):
print(f"Good day, {name} {ending}")
return "great day!"
a = goodDay("Anirban", "thank you!")
print(a)
##### recursion ######
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
n = int(input("Enter a number to find its factorial: "))
print(f"The factorial of {n} is {factorial(n)}")
def pattern(n):
if (n == 0):
return
print("*" * n)
pattern(n-1)
pattern(10)
def inches_to_cm(inches):
return inches * 2.54
n = float(input("Enter the number of inches: "))
print(f"{n} inches is equal to {inches_to_cm(n)} cm")
def rem(l, word):
n = []
for item in l:
if item != word:
n.append(item.strip(word))
return n
l = ["Harry", "Soham", "Shubham", "am"]
print(rem(l, "am"))
def multiples(n):
for i in range(1, 11):
print(f"{n} x {i} = {n * i}")
n = int(input("Enter a number to print its multiplication table: "))
multiples(n)
###### file input and output #######
f = open("test.txt")
data = f.read()
print(data)
f.close()
str = "This is a test file.\nIt contains multiple lines of text.\nThis is the end of the file."
f = open("test.txt", "w")
f.write(str)
f.close()
f = open("test.txt", "r")
# data = f.readlines()
line1 = f.readline()
line2 = f.readline()
line3 = f.readline()
print(line1, line2, line3)
f.close()
line = f.readline()
while(line != ""):
print(line)
line = f.readline()
f.close()
The same can be achieved using with statement:
with open("test.txt", "r") as f:
print(f.read())
word = input("Enter a word to search in the poem: ")
words = word.split()
for word in words:
with open("poem.txt", "r") as f:
content = f.read()
if word in content:
print(f"The word '{word}' is present in the poem.")
else:
print(f"The word '{word}' is not present in the poem.")
word = input("Enter a word to search in the poem: ")
with open("poem.txt", "r") as f:
content = f.read()
if word in content:
print(f"The word '{word}' is present in the poem.")
else:
print(f"The word '{word}' is not present in the poem")
# Walrus Operator #
if (n := len([1, 2, 3, 4, 5])) > 3:
print(f"List is too long {n}")