-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython.pl
executable file
·78 lines (64 loc) · 1.69 KB
/
python.pl
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
#!/usr/bin/env python3
import json
greeting = "Hello, world"
print (f"{greeting} is the first program in any language")
def print_collection(collection_arg, header = ""):
print (header)
if isinstance(collection_arg, dict):
for k, v in collection_arg.items():
print(f"{k}: {v}")
else:
for x in collection_arg:
print (x)
# dict
mydict = dict()
mydict['hello'] = "world"
mydict['true'] = True
for k in mydict:
print (k, ":", mydict[k])
if (k == 'true'):
print ("true is indeed True")
else:
pass # do nothing here
# set
literal_set = {"apple", "banana", "hello", "world"}
myset = set()
myset.add("strawberry")
# list
literal_list = ['a', 'b', 'c', 'd']
mylist = list()
mylist.append('e')
# tuple, which is immutable
literal_tuple = ("apple", "banana", "cherry")
# range, reversed
for i in range(10, 0, -1):
print (i)
mylist.append(i)
print_collection(reversed(mylist), "\n\nmylist reversed:")
#split
txt = "welcome to the jungle"
txt_split = txt.split()
# files
f = open("demofile.txt", "w")
f.write("Now the file has more content!")
f.write(str(mylist))
json_dict=json.dumps(mydict)
f.write(json_dict)
f = open("demofile.txt", "r")
print(f.read())
# logic - is checks mem address
if ("a" is not None):
print ("a is not None")
if ("a" == "b"):
pass
elif ("a" != "a" or "a" is "a"):
print ("a is a")
else:
print ("a is b")
print_collection(mydict, "\n\nmydict:")
print_collection(literal_set,"\n\nliteral_set:")
print_collection(myset, "\n\nmyset:")
print_collection (literal_list, "\n\nliteral_list:")
print_collection (mylist, "\n\nmylist:")
print_collection(literal_tuple, "\n\nliteral_tuple:")
print(txt_split)