Skip to content

Instantly share code, notes, and snippets.

View eder-projetos-dev's full-sized avatar

Éder Luís Britto Garcia eder-projetos-dev

View GitHub Profile
sum = lambda x, y: x + y
result = sum(5, 3)
squares = (num**2 for num in range(1, 11))
even_numbers = {num for num in range(1, 11) if num % 2 == 0}
fruits = ['apple', 'banana', 'orange']
for index, fruit in enumerate(fruits):
print(f"Index: {index}, Fruit: {fruit}")
@eder-projetos-dev
eder-projetos-dev / gist:aa8a568d1e1321cd42616ae64d718925
Created June 3, 2023 17:00
Python - Dictionary Comprehensions
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 32, 40]
people = {name: age for name, age in zip(names, ages)}
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 32, 40]
people = list(zip(names, ages))
@eder-projetos-dev
eder-projetos-dev / gist:282d9f143caf43ec26e6a71e0e188b5b
Created June 3, 2023 16:54
Python - Context Manager / Database Connection
import sqlite3
with sqlite3.connect("mydb.db") as conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM users")
results = cursor.fetchall()
# Traditional approach
file = open("data.txt", "r")
try:
data = file.read()
# Process the data
finally:
file.close()
# Context manager
with open("data.txt", "r") as file:
# Traditional approach
point = (3, 7)
x = point[0]
y = point[1]
# Tuple unpacking
x, y = point
# Pro Tip: When unpacking tuples, you can use an asterisk (*) to capture multiple elements into a single variable.
# This is particularly useful when dealing with variable-length tuples.
# Traditional approach
squares = []
for num in range(1, 11):
squares.append(num**2)
# List comprehension
squares = [num**2 for num in range(1, 11)]