-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into python-set-comprehension
- Loading branch information
Showing
20 changed files
with
363 additions
and
138 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,9 @@ | ||
# Basic Input and Output in Python | ||
|
||
This folder contains the code examples for the Real Python tutorial [Basic Input and Output in Python](https://realpython.com/python-input-output/). | ||
|
||
You can run all of the scripts directly by specifying their name: | ||
|
||
```sh | ||
$ python <filename>.py | ||
``` |
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,26 @@ | ||
import random | ||
|
||
health = 5 | ||
enemy_health = 3 | ||
|
||
while health > 0 and enemy_health > 0: | ||
# Normalize input to handle extra spaces and case variations. | ||
action = input("Attack or Run? ").strip().lower() | ||
if action not in {"attack", "run"}: | ||
print("Invalid choice. Please type 'Attack' or 'Run'.") | ||
continue | ||
|
||
if action == "attack": | ||
enemy_health -= 1 | ||
print("You hit the enemy!") | ||
# Implement a 50% chance that the enemy strikes back. | ||
enemy_attacks = random.choice([True, False]) | ||
if enemy_attacks: | ||
health -= 2 | ||
print("The enemy strikes back!") | ||
else: | ||
print("You ran away!") | ||
break | ||
print(f"Your health: {health}, Enemy health: {enemy_health}") | ||
|
||
print("Victory!" if enemy_health <= 0 else "Game Over") |
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,2 @@ | ||
name = input("Please enter your name: ") | ||
print("Hello", name, "and welcome!") |
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 @@ | ||
import random | ||
|
||
number = random.randint(1, 10) | ||
guess = int(input("Guess a number between 1 and 10: ")) | ||
|
||
if guess == number: | ||
print("You got it!") | ||
else: | ||
print("Sorry, the number was", number) |
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,4 @@ | ||
import readline # noqa F401 | ||
|
||
while (user_input := input("> ")).lower() != "exit": | ||
print("You entered:", user_input) |
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,66 @@ | ||
# Expression vs Statement in Python: What's the Difference? | ||
|
||
This folder contains sample code from the Real Python tutorial [Expression vs Statement in Python: What's the Difference?](https://realpython.com/python-expression-vs-statement/) | ||
|
||
## Code Inspector | ||
|
||
Identify whether a piece of Python code is an expression or a statement: | ||
|
||
```shell | ||
$ python code_inspector.py | ||
Type a Python code snippet or leave empty to exit. | ||
>>> yield | ||
statement | ||
>>> (yield) | ||
expression | ||
>>> 2 + | ||
invalid | ||
``` | ||
|
||
## GUI App | ||
|
||
Register a lambda expression as a callback, which delegates to a function with statements: | ||
|
||
```shell | ||
$ python gui_app.py | ||
``` | ||
|
||
## Echo Program | ||
|
||
Compile with a C compiler and pipe stdin to the echo program: | ||
|
||
```shell | ||
$ gcc echo.c -o echo.x | ||
$ echo "Hello, World!" | ./echo.x | ||
Hello, World! | ||
``` | ||
|
||
## HEX Reader | ||
|
||
Read a binary file and display its bytes in hexadecimal format: | ||
|
||
```shell | ||
$ python hex_reader.py /path/to/HelloJava.class --columns 8 | ||
ca fe ba be 00 00 00 41 | ||
00 0f 0a 00 02 00 03 07 | ||
00 04 0c 00 05 00 06 01 | ||
(...) | ||
``` | ||
|
||
## Generators | ||
|
||
Generate a random signal and use a low-pass filter to make it smooth: | ||
|
||
```shell | ||
$ python generators.py | ||
-0.96: -0.96 | ||
-0.81: -0.89 | ||
-0.52: -0.67 | ||
0.22: -0.15 | ||
0.51: 0.37 | ||
0.40: 0.46 | ||
-0.08: 0.16 | ||
-0.24: -0.16 | ||
0.80: 0.28 | ||
0.47: 0.64 | ||
``` |
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,31 @@ | ||
import ast | ||
|
||
|
||
def main(): | ||
print("Type a Python code snippet or leave empty to exit.") | ||
while code := input(">>> "): | ||
print(describe(code)) | ||
|
||
|
||
def describe(code): | ||
if valid(code, mode="eval"): | ||
return "expression" | ||
elif valid(code, mode="exec"): | ||
return "statement" | ||
else: | ||
return "invalid" | ||
|
||
|
||
def valid(code, mode): | ||
try: | ||
ast.parse(code, mode=mode) | ||
return True | ||
except SyntaxError: | ||
return False | ||
|
||
|
||
if __name__ == "__main__": | ||
try: | ||
main() | ||
except EOFError: | ||
pass |
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,11 @@ | ||
#include <stdio.h> | ||
|
||
int main() { | ||
int x; | ||
while (x = fgetc(stdin)) { | ||
if (x == EOF) | ||
break; | ||
putchar(x); | ||
} | ||
return 0; | ||
} |
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,24 @@ | ||
import random | ||
|
||
|
||
def main(): | ||
lf = lowpass_filter() | ||
lf.send(None) | ||
for value in generate_noise(10): | ||
print(f"{value:>5.2f}: {lf.send(value):>5.2f}") | ||
|
||
|
||
def generate_noise(size): | ||
for _ in range(size): | ||
yield 2 * random.random() - 1 | ||
|
||
|
||
def lowpass_filter(): | ||
a = yield | ||
b = yield a | ||
while True: | ||
a, b = b, (yield (a + b) / 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,19 @@ | ||
import tkinter as tk | ||
|
||
|
||
def main(): | ||
window = tk.Tk() | ||
button = tk.Button(window, text="Click", command=lambda: on_click(42)) | ||
button.pack(padx=10, pady=10) | ||
window.mainloop() | ||
|
||
|
||
def on_click(age): | ||
if age > 18: | ||
print("You're an adult.") | ||
else: | ||
print("You're a minor.") | ||
|
||
|
||
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,24 @@ | ||
import argparse | ||
import itertools | ||
|
||
MAX_BYTES = 1024 | ||
|
||
|
||
def main(args): | ||
buffer = bytearray() | ||
with open(args.path, mode="rb") as file: | ||
while chunk := file.read(MAX_BYTES): | ||
buffer.extend(chunk) | ||
for row in itertools.batched(buffer, args.columns): | ||
print(" ".join(f"{byte:02x}" for byte in row)) | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("path") | ||
parser.add_argument("-c", "--columns", type=int, default=16) | ||
return parser.parse_args() | ||
|
||
|
||
if __name__ == "__main__": | ||
main(parse_args()) |
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 @@ | ||
Company,Sector,Mon,Tue,Wed,Thu,Fri | ||
Company_A,technology,100.5,101.2,102,101.8,112.5 | ||
Company_B,finance,200.1,199.8,200.5,201.0,200.8 | ||
Company_C,healthcare,50.3,50.5,51.0,50.8,51.2 | ||
Company_D,technology,110.5,101.2,102,111.8,97.5 | ||
Company_E,finance,200.1,200.8,200.5,211.0,200.8 | ||
Company_F,healthcare,55.3,50.5,53.0,50.8,52.2 |
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
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
Oops, something went wrong.