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..c0f8367762 --- /dev/null +++ b/python-first-steps/conditionals.py @@ -0,0 +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/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..199b8c38e6 --- /dev/null +++ b/python-first-steps/imports.py @@ -0,0 +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)) + +print(sqrt(25)) + +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..e7722c9466 --- /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", "spinach"] +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..0820f285ee --- /dev/null +++ b/python-first-steps/tuples.py @@ -0,0 +1,33 @@ +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")) +try: + print(letters.index("d")) +except Exception as e: + print(repr(e)) + +# 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..8c2276a0f7 --- /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")