This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sum = lambda x, y: x + y | |
result = sum(5, 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
squares = (num**2 for num in range(1, 11)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
even_numbers = {num for num in range(1, 11) if num % 2 == 0} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fruits = ['apple', 'banana', 'orange'] | |
for index, fruit in enumerate(fruits): | |
print(f"Index: {index}, Fruit: {fruit}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
names = ['Alice', 'Bob', 'Charlie'] | |
ages = [25, 32, 40] | |
people = {name: age for name, age in zip(names, ages)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
names = ['Alice', 'Bob', 'Charlie'] | |
ages = [25, 32, 40] | |
people = list(zip(names, ages)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
with sqlite3.connect("mydb.db") as conn: | |
cursor = conn.cursor() | |
cursor.execute("SELECT * FROM users") | |
results = cursor.fetchall() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Traditional approach | |
squares = [] | |
for num in range(1, 11): | |
squares.append(num**2) | |
# List comprehension | |
squares = [num**2 for num in range(1, 11)] |
NewerOlder