forked from ArjanCodes/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add examples for weird things about python
- Loading branch information
Showing
12 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
def main() -> None: | ||
if 1: | ||
print("1 is True") | ||
|
||
print(1 == True) | ||
print(0 == False) | ||
print(1 + True) | ||
print(True + True) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
def append_to_list[T](value: T, my_list: list[T] = []) -> list[T]: | ||
my_list.append(value) | ||
return my_list | ||
|
||
|
||
def main() -> None: | ||
# First call | ||
result1 = append_to_list(1) | ||
print(result1) # Output: [1] | ||
|
||
# Second call | ||
result2 = append_to_list(2) | ||
print(result2) # Output: [1, 2] | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import time | ||
|
||
|
||
def main() -> None: | ||
print("Hello world", end="_") | ||
time.sleep(3) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
def check_element_existence[T](data: list[T], to_find: T) -> None: | ||
for num in data: | ||
if num == to_find: | ||
print("Exists!") | ||
break | ||
else: | ||
print("Does not exist") | ||
|
||
|
||
def main() -> None: | ||
data = [1, 2, 3, 4, 5] | ||
check_element_existence(data, 4) | ||
check_element_existence(data, -1) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
def timer(): | ||
pass | ||
|
||
|
||
def main() -> None: | ||
timer.counter = 0 | ||
timer.counter += 1 | ||
print(timer.counter) # Output: 1 | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
def main() -> None: | ||
a = 256 | ||
b = 256 | ||
print(a is b) # Output: True | ||
|
||
c = 257 | ||
d = 257 | ||
print(c is d) # Output: False | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Run the following in the Python console | ||
# Expected bevahior! data is (1,2) | ||
data = (1, 2) | ||
data[0] += 1 # Returns an error | ||
data # Is (1,2) | ||
|
||
# Not expected! data is ([1, 3], 2) | ||
data = ([1],2) | ||
data[0] += [3] | ||
data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[tool.poetry] | ||
name = "weird-things" | ||
version = "0.1.0" | ||
description = "" | ||
authors = ["Andreas Bergman <[email protected]>"] | ||
readme = "README.md" | ||
|
||
[tool.poetry.dependencies] | ||
python = "^3.11" | ||
|
||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
build-backend = "poetry.core.masonry.api" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
def main() -> None: | ||
a = [1, 2, 3] | ||
b = a # b is now a reference to a | ||
a += [4] | ||
print(b) # Output: [1, 2, 3, 4] | ||
|
||
s = "hello" | ||
t = s # t is now a reference to s | ||
s += " world" | ||
print(t) # Output: "hello" | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
def main() -> None: | ||
a = 420 | ||
a -=- 1 | ||
print(a) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
def main() -> None: | ||
for x in range(7): | ||
if x == 6: | ||
print(x, ": for x inside loop") | ||
print(x, ": x in global/scope") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
def main() -> None: | ||
[x for x in range(7) if x == 6 and print(f"{x}: for x inside loop")] | ||
print(x, ": x in global") # will return an error | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |