From 44763336cd6734b8a3bceaf4058ec6ca6ac8ae7e Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Mon, 8 Sep 2025 13:02:18 +0200 Subject: [PATCH 1/4] Sample code for the article on first steps with Python --- python-first-steps/README.md | 3 ++ python-first-steps/boolean.py | 15 +++++++++ python-first-steps/bytes.py | 23 ++++++++++++++ python-first-steps/classes.py | 12 +++++++ python-first-steps/comments.py | 6 ++++ python-first-steps/conditionals.py | 16 ++++++++++ python-first-steps/dictionaries.py | 13 ++++++++ python-first-steps/for_loops.py | 16 ++++++++++ python-first-steps/hello.py | 1 + python-first-steps/imports.py | 11 +++++++ python-first-steps/keywords.py | 5 +++ python-first-steps/lists.py | 50 ++++++++++++++++++++++++++++++ python-first-steps/number.py | 19 ++++++++++++ python-first-steps/sets.py | 25 +++++++++++++++ python-first-steps/strings.py | 36 +++++++++++++++++++++ python-first-steps/tuples.py | 30 ++++++++++++++++++ python-first-steps/variables.py | 8 +++++ python-first-steps/while_loops.py | 7 +++++ 18 files changed, 296 insertions(+) create mode 100644 python-first-steps/README.md create mode 100644 python-first-steps/boolean.py create mode 100644 python-first-steps/bytes.py create mode 100644 python-first-steps/classes.py create mode 100644 python-first-steps/comments.py create mode 100644 python-first-steps/conditionals.py create mode 100644 python-first-steps/dictionaries.py create mode 100644 python-first-steps/for_loops.py create mode 100644 python-first-steps/hello.py create mode 100644 python-first-steps/imports.py create mode 100644 python-first-steps/keywords.py create mode 100644 python-first-steps/lists.py create mode 100644 python-first-steps/number.py create mode 100644 python-first-steps/sets.py create mode 100644 python-first-steps/strings.py create mode 100644 python-first-steps/tuples.py create mode 100644 python-first-steps/variables.py create mode 100644 python-first-steps/while_loops.py diff --git a/python-first-steps/README.md b/python-first-steps/README.md new file mode 100644 index 0000000000..3004d94fd3 --- /dev/null +++ b/python-first-steps/README.md @@ -0,0 +1,3 @@ +# How to Use Python: Your First Steps + +This folder provides the code examples for the Real Python tutorial [How to Use Python: Your First Steps](https://realpython.com/python-first-steps/). diff --git a/python-first-steps/boolean.py b/python-first-steps/boolean.py new file mode 100644 index 0000000000..923e2a2eb5 --- /dev/null +++ b/python-first-steps/boolean.py @@ -0,0 +1,15 @@ +# Comparisons +print(2 < 5) +print(4 > 10) +print(4 <= 3) +print(3 >= 3) +print(5 == 6) +print(6 != 9) + +# The bool() function +print(bool(0)) +print(bool(1)) +print(bool("")) +print(bool("a")) +print(bool([])) +print(bool([1, 2, 3])) diff --git a/python-first-steps/bytes.py b/python-first-steps/bytes.py new file mode 100644 index 0000000000..85766232d9 --- /dev/null +++ b/python-first-steps/bytes.py @@ -0,0 +1,23 @@ +# Literal syntax (ASCII only) +print(b"Hello") +# From an iterable of integers (0 to 255) +print(bytes([72, 101, 108, 108, 111])) +# By encoding a string +print("café".encode("utf-8")) + +data = b"caf\xc3\xa9" +data.decode("utf-8") +print(data) + +packet = b"ABCDEF" +print(len(packet)) +print(packet[0]) +print(packet[1:4]) + +buffer = bytearray(b"ABC") +buffer[0] = 97 +print(buffer) +print(bytes(buffer)) + +print(b"Hello".hex()) +print(bytes.fromhex("48656c6c6f")) diff --git a/python-first-steps/classes.py b/python-first-steps/classes.py new file mode 100644 index 0000000000..c315a3f4c2 --- /dev/null +++ b/python-first-steps/classes.py @@ -0,0 +1,12 @@ +class Dog: + def __init__(self, name, age): + self.name = name + self.age = age + + def bark(self): + return "Woof! Woof!" + + +fido = Dog("Fido", 3) +print(fido.name, fido.age) +print(fido.bark()) diff --git a/python-first-steps/comments.py b/python-first-steps/comments.py new file mode 100644 index 0000000000..0d34b5b53d --- /dev/null +++ b/python-first-steps/comments.py @@ -0,0 +1,6 @@ +# This is a comment on its own line + +greeting = "Hello, World!" # This is an inline comment + +# This is a long comment that requires +# two lines to be complete. diff --git a/python-first-steps/conditionals.py b/python-first-steps/conditionals.py new file mode 100644 index 0000000000..cb37af76ae --- /dev/null +++ b/python-first-steps/conditionals.py @@ -0,0 +1,16 @@ +age = 21 +if age >= 18: + print("You're a legal adult") + +age = 16 +if age >= 18: + print("You're a legal adult") +else: + print("You're NOT an adult") + +age = 18 +if age > 18: + print("You're over 18 years old") +elif age == 18: + print("You're exactly 18 years old") + diff --git a/python-first-steps/dictionaries.py b/python-first-steps/dictionaries.py new file mode 100644 index 0000000000..623a29b30b --- /dev/null +++ b/python-first-steps/dictionaries.py @@ -0,0 +1,13 @@ +john = {"name": "John Doe", "age": 25, "job": "Python Developer"} +print(john) +jane = dict(name="Jane Doe", age=24, job="Web Developer") +print(jane) +print(john["name"]) +print(john["age"]) + +# Retrieve all the keys +print(john.keys()) +# Retrieve all the values +print(john.values()) +# Retrieve all the key-value pairs +print(john.items()) diff --git a/python-first-steps/for_loops.py b/python-first-steps/for_loops.py new file mode 100644 index 0000000000..229e9b3520 --- /dev/null +++ b/python-first-steps/for_loops.py @@ -0,0 +1,16 @@ +for i in (1, 2, 3, 4, 5): + print(i) +else: + print("The loop wasn't interrupted") + +for i in (1, 2, 3, 4, 5): + if i == 3: + print("Number found:", i) + break +else: + print("Number not found") + +for i in (1, 2, 3, 4, 5): + if i == 3: + continue + print(i) diff --git a/python-first-steps/hello.py b/python-first-steps/hello.py new file mode 100644 index 0000000000..7df869a15e --- /dev/null +++ b/python-first-steps/hello.py @@ -0,0 +1 @@ +print("Hello, World!") diff --git a/python-first-steps/imports.py b/python-first-steps/imports.py new file mode 100644 index 0000000000..a16b8afdae --- /dev/null +++ b/python-first-steps/imports.py @@ -0,0 +1,11 @@ +import math # Module import + +print(math.sqrt(16)) + +from math import sqrt # Function import + +print(sqrt(25)) + +from math import pi as PI # Import with alias + +print(PI) diff --git a/python-first-steps/keywords.py b/python-first-steps/keywords.py new file mode 100644 index 0000000000..0c0e861558 --- /dev/null +++ b/python-first-steps/keywords.py @@ -0,0 +1,5 @@ +import keyword + +print(keyword.kwlist) + +print(keyword.softkwlist) diff --git a/python-first-steps/lists.py b/python-first-steps/lists.py new file mode 100644 index 0000000000..81868dcd0b --- /dev/null +++ b/python-first-steps/lists.py @@ -0,0 +1,50 @@ +# Define an empty list +empty = [] +print(empty) +# Define a list of numbers +numbers = [1, 2, 3, 100] +print(numbers) +# Modify the list in place +numbers[3] = 200 +print(numbers) +# Define a list of strings +print(["batman", "superman", "spiderman"]) +# Define a list of objects with different data types +print(["Hello World", [4, 5, 6], False]) + +# Indexing +numbers = [1, 2, 3, 4] +print(numbers[0]) +print(numbers[1]) +superheroes = ["batman", "superman", "spiderman"] +print(superheroes[-1]) +print(superheroes[-2]) + +# Slicing +numbers = [1, 2, 3, 4] +new_list = numbers[0:3] +print(new_list) + +# Nested lists +mixed_types = ["Hello World", [4, 5, 6], False] +print(mixed_types[0][6]) +print(mixed_types[1][2]) + +# Concatenation +fruits = ["apples", "grapes", "oranges"] +veggies = ["corn", "kale", "mushrooms"] +print(fruits + veggies) + +# Built-in functions +numbers = [1, 2, 3, 4] +print(len(numbers)) + +# List methods +fruits = ["apples", "grapes", "oranges"] +fruits.append("blueberries") +print(fruits) +fruits.sort() +print(fruits) +numbers = [1, 2, 3, 4] +numbers.pop(2) +print(numbers) diff --git a/python-first-steps/number.py b/python-first-steps/number.py new file mode 100644 index 0000000000..69f1ececac --- /dev/null +++ b/python-first-steps/number.py @@ -0,0 +1,19 @@ +# Addition +print(5 + 3) +# Subtraction +print(5 - 3) +# Multiplication +print(5 * 3) +# True division +print(5 / 3) +# Floor division +print(5 // 3) +# Modulus (returns the remainder from division) +print(5 % 3) +# Power +print(5**3) + +# Methods +print((10.0).is_integer()) +print((10.2).is_integer()) +print((10).bit_length()) diff --git a/python-first-steps/sets.py b/python-first-steps/sets.py new file mode 100644 index 0000000000..34fc49a9ac --- /dev/null +++ b/python-first-steps/sets.py @@ -0,0 +1,25 @@ +print({"John", "Jane", "Linda"}) +print(set(["David", "Mark", "Marie"])) +empty = set() +print(empty) + +# Unique elements +print(set([1, 2, 2, 3, 4, 5, 3])) + +# Built-in functions +employees = {"John", "Jane", "Linda"} +print(len(employees)) + +# Set operations +primes = {2, 3, 5, 7} +evens = {2, 4, 6, 8} +print(primes | evens) # Union +print(primes & evens) # Intersection +print(primes - evens) # Difference + +# Set methods +primes = {2, 3, 5, 7} +primes.add(11) +print(primes) +primes.remove(11) +print(primes) diff --git a/python-first-steps/strings.py b/python-first-steps/strings.py new file mode 100644 index 0000000000..8685ba9e0a --- /dev/null +++ b/python-first-steps/strings.py @@ -0,0 +1,36 @@ +# Use single quotes +print("Hello there!") +# Use double quotes +print("Welcome to Real Python!") +# Use triple quotes +print("""Thanks for joining us!""") +# Escape characters +print("I can't believe it!") +print("can't") + +# Concatenation +print("Happy" + " " + "pythoning!") + +# Built-in functions +print(len("Happy pythoning!")) + +# String methods +print(" ".join(["Happy", "pythoning!"])) +print("Happy pythoning!".upper()) +print("HAPPY PYTHONING!".lower()) +name = "John Doe" +age = 25 +print("My name is {0} and I'm {1} years old".format(name, age)) + +# f-strings +print(f"My name is {name} and I'm {age} years old") + +# Indexing +welcome = "Welcome to Real Python!" +print(welcome[0]) +print(welcome[11]) +print(welcome[-1]) + +# Slicing +print(welcome[0:7]) +print(welcome[11:22]) diff --git a/python-first-steps/tuples.py b/python-first-steps/tuples.py new file mode 100644 index 0000000000..8b6a8762eb --- /dev/null +++ b/python-first-steps/tuples.py @@ -0,0 +1,30 @@ +employee = ("Jane", "Doe", 31, "Software Developer") +print(employee) +print(type((1))) +print(type((1,))) + +# Concatenation +first_tuple = (1, 2) +second_tuple = (3, 4) +third_tuple = first_tuple + second_tuple +print(third_tuple) + +# Built-in functions +numbers = (1, 2, 3) +print(len(numbers)) +numbers = (1, 2, 3) +print(list(numbers)) + +# Tuple methods +letters = ("a", "b", "b", "c", "a") +print(letters.count("a")) +print(letters.count("c")) +print(letters.count("d")) +print(letters.index("a")) +print(letters.index("c")) +print(letters.index("d")) + +# Indexing and slicing +employee = ("Jane", "Doe", 31, "Software Developer") +print(employee[0]) +print(employee[1:3]) diff --git a/python-first-steps/variables.py b/python-first-steps/variables.py new file mode 100644 index 0000000000..cfab225ede --- /dev/null +++ b/python-first-steps/variables.py @@ -0,0 +1,8 @@ +numbers = [1, 2, 3, 4, 5] +print(numbers) + +first_num = 1 +print(first_num) + +pi = 3.141592653589793 +print(pi) diff --git a/python-first-steps/while_loops.py b/python-first-steps/while_loops.py new file mode 100644 index 0000000000..966ce48699 --- /dev/null +++ b/python-first-steps/while_loops.py @@ -0,0 +1,7 @@ +count = 1 +while count < 5: + print(count) + count += 1 +else: + print("The loop wasn't interrupted") + From 4e4de928bd76b6d2d977ec668c1f2d773b85a1b2 Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Mon, 8 Sep 2025 13:09:04 +0200 Subject: [PATCH 2/4] Fix linter issues --- python-first-steps/imports.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/python-first-steps/imports.py b/python-first-steps/imports.py index a16b8afdae..199b8c38e6 100644 --- a/python-first-steps/imports.py +++ b/python-first-steps/imports.py @@ -1,11 +1,9 @@ import math # Module import +from math import pi as PI # Import with alias +from math import sqrt # Function import print(math.sqrt(16)) -from math import sqrt # Function import - print(sqrt(25)) -from math import pi as PI # Import with alias - print(PI) From 5a5f81eaeff4d2c9ef6c73ae832ed2df2b1770ea Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Mon, 8 Sep 2025 13:12:57 +0200 Subject: [PATCH 3/4] More fixes --- python-first-steps/conditionals.py | 4 +++- python-first-steps/while_loops.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/python-first-steps/conditionals.py b/python-first-steps/conditionals.py index cb37af76ae..c0f8367762 100644 --- a/python-first-steps/conditionals.py +++ b/python-first-steps/conditionals.py @@ -1,16 +1,18 @@ age = 21 + if age >= 18: print("You're a legal adult") age = 16 + if age >= 18: print("You're a legal adult") else: print("You're NOT an adult") age = 18 + if age > 18: print("You're over 18 years old") elif age == 18: print("You're exactly 18 years old") - diff --git a/python-first-steps/while_loops.py b/python-first-steps/while_loops.py index 966ce48699..8c2276a0f7 100644 --- a/python-first-steps/while_loops.py +++ b/python-first-steps/while_loops.py @@ -1,7 +1,7 @@ count = 1 + while count < 5: print(count) count += 1 else: print("The loop wasn't interrupted") - From a1ab1bb58101354600793841296cc13fcbb32197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Zaczy=C5=84ski?= Date: Sun, 21 Sep 2025 16:37:55 +0200 Subject: [PATCH 4/4] Final QA --- python-first-steps/lists.py | 2 +- python-first-steps/tuples.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/python-first-steps/lists.py b/python-first-steps/lists.py index 81868dcd0b..e7722c9466 100644 --- a/python-first-steps/lists.py +++ b/python-first-steps/lists.py @@ -32,7 +32,7 @@ # Concatenation fruits = ["apples", "grapes", "oranges"] -veggies = ["corn", "kale", "mushrooms"] +veggies = ["corn", "kale", "spinach"] print(fruits + veggies) # Built-in functions diff --git a/python-first-steps/tuples.py b/python-first-steps/tuples.py index 8b6a8762eb..0820f285ee 100644 --- a/python-first-steps/tuples.py +++ b/python-first-steps/tuples.py @@ -22,7 +22,10 @@ print(letters.count("d")) print(letters.index("a")) print(letters.index("c")) -print(letters.index("d")) +try: + print(letters.index("d")) +except Exception as e: + print(repr(e)) # Indexing and slicing employee = ("Jane", "Doe", 31, "Software Developer")