From b61341266e9d58fe9a6388ca2fa3731db847fc71 Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Fri, 26 Jan 2024 18:37:52 +0100 Subject: [PATCH] Sample code for the BNF notation article --- python-bnf-notation/README.md | 3 +++ python-bnf-notation/conditional.py | 10 ++++++++++ python-bnf-notation/func_1.py | 5 +++++ python-bnf-notation/func_2.py | 5 +++++ python-bnf-notation/func_3.py | 5 +++++ python-bnf-notation/func_4.py | 5 +++++ python-bnf-notation/func_5.py | 8 ++++++++ python-bnf-notation/loop.py | 8 ++++++++ python-bnf-notation/walrus.py | 3 +++ 9 files changed, 52 insertions(+) create mode 100644 python-bnf-notation/README.md create mode 100644 python-bnf-notation/conditional.py create mode 100644 python-bnf-notation/func_1.py create mode 100644 python-bnf-notation/func_2.py create mode 100644 python-bnf-notation/func_3.py create mode 100644 python-bnf-notation/func_4.py create mode 100644 python-bnf-notation/func_5.py create mode 100644 python-bnf-notation/loop.py create mode 100644 python-bnf-notation/walrus.py diff --git a/python-bnf-notation/README.md b/python-bnf-notation/README.md new file mode 100644 index 0000000000..11d85f158b --- /dev/null +++ b/python-bnf-notation/README.md @@ -0,0 +1,3 @@ +# BNF Notation: Dive Deeper Into Python's Grammar + +This folder provides the code examples for the Real Python tutorial [BNF Notation: Dive Deeper Into Python's Grammar](https://realpython.com/python-bnf-notation/). diff --git a/python-bnf-notation/conditional.py b/python-bnf-notation/conditional.py new file mode 100644 index 0000000000..8df4dd2ef3 --- /dev/null +++ b/python-bnf-notation/conditional.py @@ -0,0 +1,10 @@ +def read_temperature(): + return 25 + + +if (temperature := read_temperature()) < 10: + print("The weather is cold!") +elif 10 <= temperature <= 25: + print("The weather is nice!") +else: + print("The weather is hot!") diff --git a/python-bnf-notation/func_1.py b/python-bnf-notation/func_1.py new file mode 100644 index 0000000000..5b46189df4 --- /dev/null +++ b/python-bnf-notation/func_1.py @@ -0,0 +1,5 @@ +def func(): + return + + +print(func()) diff --git a/python-bnf-notation/func_2.py b/python-bnf-notation/func_2.py new file mode 100644 index 0000000000..ea4d1ef1e6 --- /dev/null +++ b/python-bnf-notation/func_2.py @@ -0,0 +1,5 @@ +def func(): + return "Hello!" + + +func() diff --git a/python-bnf-notation/func_3.py b/python-bnf-notation/func_3.py new file mode 100644 index 0000000000..005a8ba0b1 --- /dev/null +++ b/python-bnf-notation/func_3.py @@ -0,0 +1,5 @@ +def func(): + return "Hello!", "Pythonista!" + + +func() diff --git a/python-bnf-notation/func_4.py b/python-bnf-notation/func_4.py new file mode 100644 index 0000000000..be128ba0d8 --- /dev/null +++ b/python-bnf-notation/func_4.py @@ -0,0 +1,5 @@ +def func(): + return ("Hello!",) + + +func() diff --git a/python-bnf-notation/func_5.py b/python-bnf-notation/func_5.py new file mode 100644 index 0000000000..f56205c8a7 --- /dev/null +++ b/python-bnf-notation/func_5.py @@ -0,0 +1,8 @@ +def func(): + return ( + "Hello!", + "Pythonista!", + ) + + +func() diff --git a/python-bnf-notation/loop.py b/python-bnf-notation/loop.py new file mode 100644 index 0000000000..75289e58b9 --- /dev/null +++ b/python-bnf-notation/loop.py @@ -0,0 +1,8 @@ +high = 5 + +for number in range(high): + if number > 5: + break + print(number) +else: + print("range covered") diff --git a/python-bnf-notation/walrus.py b/python-bnf-notation/walrus.py new file mode 100644 index 0000000000..f0e7f0fc66 --- /dev/null +++ b/python-bnf-notation/walrus.py @@ -0,0 +1,3 @@ +(length := len([1, 2, 3])) + +print(length)