-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunny_python.py
More file actions
213 lines (134 loc) · 4.25 KB
/
funny_python.py
File metadata and controls
213 lines (134 loc) · 4.25 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
"""
=========================================
🐍 Python Funny & Weird Tricks Collection
=========================================
A collection of cursed, funny, and surprising Python behaviors.
Made for GitHub 😈
"""
print("\n==============================")
print("1. THE ZEN OF PYTHON")
print("==============================")
import this
print("\n==============================")
print("2. HELLO SECRET MODULE")
print("==============================")
import __hello__
print("\n==============================")
print("3. STRING MULTIPLICATION")
print("==============================")
print("LOL " * 5)
print("\n==============================")
print("4. WEIRD COMPARISONS")
print("==============================")
print("1 < 2 < 3 ->", 1 < 2 < 3)
print("1 < 2 > 3 ->", 1 < 2 > 3)
print("\nActually Python checks:")
print("(1 < 2) and (2 > 3)")
print("\n==============================")
print("5. TUPLE CONFUSION")
print("==============================")
a = (1)
b = (1,)
print("(1) type ->", type(a))
print("(1,) type ->", type(b))
print("\n==============================")
print("6. BOOLEAN MATH")
print("==============================")
print("True + True =", True + True)
print("True * 10 =", True * 10)
print("False + 5 =", False + 5)
print("\nBecause:")
print("True == 1")
print("False == 0")
print("\n==============================")
print("7. LIST MULTIPLICATION CURSE")
print("==============================")
a = [[]] * 3
print("Before:", a)
a[0].append(1)
print("After :", a)
print("\nWHY DID ALL LISTS CHANGE?! 😭")
print("\n==============================")
print("8. MUTABLE DEFAULT ARGUMENT")
print("==============================")
def add_item(item, my_list=[]):
my_list.append(item)
return my_list
print(add_item(1))
print(add_item(2))
print(add_item(3))
print("\nSame list reused every function call 😈")
print("\n==============================")
print("9. FLOATING POINT PAIN")
print("==============================")
print("0.1 + 0.2 =", 0.1 + 0.2)
print("\nExpected: 0.3")
print("Reality :", 0.1 + 0.2)
print("\n==============================")
print("10. NaN IS WEIRD")
print("==============================")
x = float("nan")
print("x == x ->", x == x)
print("\nNaN is NOT equal to itself 🤯")
print("\n==============================")
print("11. EMPTY LIST IS FALSE")
print("==============================")
if []:
print("This runs")
else:
print("Empty list is False")
print("\n==============================")
print("12. PYTHON SWAP MAGIC")
print("==============================")
a = 5
b = 10
print("Before swap:", a, b)
a, b = b, a
print("After swap :", a, b)
print("\nNo temporary variable needed 😎")
print("\n==============================")
print("13. EVERYTHING IS OBJECT")
print("==============================")
print(type(10))
print(type("hello"))
print(type(print))
print("\nEven functions are objects 👀")
print("\n==============================")
print("14. INFINITE LOOP (DON'T RUN)")
print("==============================")
print("""
while True:
print("help")
""")
print("\n==============================")
print("15. BRACES ARE NOT WELCOME")
print("==============================")
print("""
from __future__ import braces
# SyntaxError: not a chance
""")
print("\n==============================")
print("16. SET REMOVES DUPLICATES")
print("==============================")
nums = [1, 1, 1, 2, 2, 3]
print("Original :", nums)
print("Unique :", list(set(nums)))
print("\n==============================")
print("17. SAME MEMORY REFERENCE")
print("==============================")
x = y = [1, 2, 3]
y.append(4)
print("x =", x)
print("y =", y)
print("\nBoth variables point to SAME list 😭")
print("\n==============================")
print("18. PRINTING WITHOUT print()")
print("==============================")
import sys
sys.stdout.write("Hello from sys.stdout.write\\n")
print("\n==============================")
print("19. GOODBYE")
print("==============================")
print("Python is beautiful...")
print("Python is cursed...")
print("Python is both 😌🐍")