Skip to content

Commit 9c3ef87

Browse files
lpozobrendawelesbzaczynski
authored
Sample code for the article on first steps with Python (#699)
* Sample code for the article on first steps with Python * Fix linter issues * More fixes * Final QA --------- Co-authored-by: brendaweles <[email protected]> Co-authored-by: Bartosz Zaczyński <[email protected]>
1 parent 755e016 commit 9c3ef87

File tree

18 files changed

+299
-0
lines changed

18 files changed

+299
-0
lines changed

python-first-steps/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# How to Use Python: Your First Steps
2+
3+
This folder provides the code examples for the Real Python tutorial [How to Use Python: Your First Steps](https://realpython.com/python-first-steps/).

python-first-steps/boolean.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Comparisons
2+
print(2 < 5)
3+
print(4 > 10)
4+
print(4 <= 3)
5+
print(3 >= 3)
6+
print(5 == 6)
7+
print(6 != 9)
8+
9+
# The bool() function
10+
print(bool(0))
11+
print(bool(1))
12+
print(bool(""))
13+
print(bool("a"))
14+
print(bool([]))
15+
print(bool([1, 2, 3]))

python-first-steps/bytes.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Literal syntax (ASCII only)
2+
print(b"Hello")
3+
# From an iterable of integers (0 to 255)
4+
print(bytes([72, 101, 108, 108, 111]))
5+
# By encoding a string
6+
print("café".encode("utf-8"))
7+
8+
data = b"caf\xc3\xa9"
9+
data.decode("utf-8")
10+
print(data)
11+
12+
packet = b"ABCDEF"
13+
print(len(packet))
14+
print(packet[0])
15+
print(packet[1:4])
16+
17+
buffer = bytearray(b"ABC")
18+
buffer[0] = 97
19+
print(buffer)
20+
print(bytes(buffer))
21+
22+
print(b"Hello".hex())
23+
print(bytes.fromhex("48656c6c6f"))

python-first-steps/classes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Dog:
2+
def __init__(self, name, age):
3+
self.name = name
4+
self.age = age
5+
6+
def bark(self):
7+
return "Woof! Woof!"
8+
9+
10+
fido = Dog("Fido", 3)
11+
print(fido.name, fido.age)
12+
print(fido.bark())

python-first-steps/comments.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# This is a comment on its own line
2+
3+
greeting = "Hello, World!" # This is an inline comment
4+
5+
# This is a long comment that requires
6+
# two lines to be complete.

python-first-steps/conditionals.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
age = 21
2+
3+
if age >= 18:
4+
print("You're a legal adult")
5+
6+
age = 16
7+
8+
if age >= 18:
9+
print("You're a legal adult")
10+
else:
11+
print("You're NOT an adult")
12+
13+
age = 18
14+
15+
if age > 18:
16+
print("You're over 18 years old")
17+
elif age == 18:
18+
print("You're exactly 18 years old")

python-first-steps/dictionaries.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
john = {"name": "John Doe", "age": 25, "job": "Python Developer"}
2+
print(john)
3+
jane = dict(name="Jane Doe", age=24, job="Web Developer")
4+
print(jane)
5+
print(john["name"])
6+
print(john["age"])
7+
8+
# Retrieve all the keys
9+
print(john.keys())
10+
# Retrieve all the values
11+
print(john.values())
12+
# Retrieve all the key-value pairs
13+
print(john.items())

python-first-steps/for_loops.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
for i in (1, 2, 3, 4, 5):
2+
print(i)
3+
else:
4+
print("The loop wasn't interrupted")
5+
6+
for i in (1, 2, 3, 4, 5):
7+
if i == 3:
8+
print("Number found:", i)
9+
break
10+
else:
11+
print("Number not found")
12+
13+
for i in (1, 2, 3, 4, 5):
14+
if i == 3:
15+
continue
16+
print(i)

python-first-steps/hello.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("Hello, World!")

python-first-steps/imports.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import math # Module import
2+
from math import pi as PI # Import with alias
3+
from math import sqrt # Function import
4+
5+
print(math.sqrt(16))
6+
7+
print(sqrt(25))
8+
9+
print(PI)

0 commit comments

Comments
 (0)