Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python-first-steps/README.md
Original file line number Diff line number Diff line change
@@ -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/).
15 changes: 15 additions & 0 deletions python-first-steps/boolean.py
Original file line number Diff line number Diff line change
@@ -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]))
23 changes: 23 additions & 0 deletions python-first-steps/bytes.py
Original file line number Diff line number Diff line change
@@ -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"))
12 changes: 12 additions & 0 deletions python-first-steps/classes.py
Original file line number Diff line number Diff line change
@@ -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())
6 changes: 6 additions & 0 deletions python-first-steps/comments.py
Original file line number Diff line number Diff line change
@@ -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.
18 changes: 18 additions & 0 deletions python-first-steps/conditionals.py
Original file line number Diff line number Diff line change
@@ -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")
13 changes: 13 additions & 0 deletions python-first-steps/dictionaries.py
Original file line number Diff line number Diff line change
@@ -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())
16 changes: 16 additions & 0 deletions python-first-steps/for_loops.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions python-first-steps/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hello, World!")
9 changes: 9 additions & 0 deletions python-first-steps/imports.py
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 5 additions & 0 deletions python-first-steps/keywords.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import keyword

print(keyword.kwlist)

print(keyword.softkwlist)
50 changes: 50 additions & 0 deletions python-first-steps/lists.py
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 19 additions & 0 deletions python-first-steps/number.py
Original file line number Diff line number Diff line change
@@ -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())
25 changes: 25 additions & 0 deletions python-first-steps/sets.py
Original file line number Diff line number Diff line change
@@ -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)
36 changes: 36 additions & 0 deletions python-first-steps/strings.py
Original file line number Diff line number Diff line change
@@ -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])
30 changes: 30 additions & 0 deletions python-first-steps/tuples.py
Original file line number Diff line number Diff line change
@@ -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])
8 changes: 8 additions & 0 deletions python-first-steps/variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
numbers = [1, 2, 3, 4, 5]
print(numbers)

first_num = 1
print(first_num)

pi = 3.141592653589793
print(pi)
7 changes: 7 additions & 0 deletions python-first-steps/while_loops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
count = 1

while count < 5:
print(count)
count += 1
else:
print("The loop wasn't interrupted")