Day 16-20 Python
Day 16to20 Python
For days 16 to 20 of your Python learning journey, you should continue working on your small project and expand upon it. You can also start exploring more advanced topics in Python such as data persistence, network programming, and parallel programming.
Here are a few examples of concepts you might encounter during these days, along with some code snippets that demonstrate how they work:
Data Perisistence:
import sqlite3
# connecting to a database
conn = sqlite3.connect("database.db")
# creating a table
conn.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)")
# inserting a row
conn.execute("INSERT INTO users (name, age) VALUES (?, ?)", ("Alice", 30))
conn.commit()
# querying the database
cursor = conn.execute("SELECT * FROM users")
for row in cursor:
print(row)
# Output:
# (1, 'Alice', 30)
conn.close()
Network programming:
import socket
# creating a TCP server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("localhost", 8000))
server.listen()
# accepting a client connection
client, addr = server.accept()
# receiving data from the client
data = client.recv(1024)
print(data.decode())
# sending data to the client
client.send(b"ACK")
client.close()
server.close()
Parallel programming:
import multipyparallel
# creating a client and connecting to a cluster of engines
client = ipyparallel.Client()
view = client.load_balanced_view()
# executing tasks in parallel
results = view.map(lambda x: x ** 2, range(10))
print(results.get()) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
I hope that these Python Preparation Plans will help you enhance your interest in programming as well as python and then all python topics. Please feel free to provide insightful remarks in the space below.