Search By Label
cat $VIRTUAL_ENV/pyvenv.cfg
home = /Library/Frameworks/Python.framework/Versions/3.12/bin include-system-site-packages = false version = 3.12.2 executable = /Library/Frameworks/Python.framework/Versions/3.12/bin/python3.12 command = /Library/Frameworks/Python.framework/Versions/3.12/bin/python3 -m venv /Users/oswaldo/projects/<project_name>
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
is
to check identity and ==
to check equality.value1
and value2
refer to an int
instance storing the value 1000
:value1 = 1000
value2 = value1
>>> value1 == value2
True
>>> value1 is value2
True
value1
and value2
refer to different int
instances, even if both store the same integer. Because the same value (integer) is stored ==
will be True
, that's why it's often called "value comparison". However is
will return False
because these are different objects:>>> value1 = 1000 >>> value2 = 1000
>>> value1 == value2 True >>> value1 is value2 False
import datetime help(datetime.datetime)
datetime
class within the datetime
module, including its methods and usage.import *
. It overrides the default of hiding everything that begins with an underscore.__all__
in a module, e.g. module.py
:__all__ = ['foo', 'Bar']
import *
from the module, only those names in the __all__
are imported:from module import * # imports foo and Bar
thistuple = ("apple", "banana", "cherry") print(thistuple) // ('apple', 'banana', 'cherry')
x = 10 y = x print(id(x)) # Output: 140737469306640 print(id(y)) # Output: 140737469306640
x
and y
refer to the same integer object with the memory address 140737469306640
. The reference count of this object is 2.del x print(id(y)) # Output: 140737469306640
x
, the reference count of the integer object becomes 1. Since y
is still pointing to it, the object is not garbage collected.languages = ["English", "Spanish", "French"] current_lang = "Spanish" try: # Use the index() method to find the index of the element index = languages.index(current_lang) print(f"The index of '{current_lang}' in the languages list is: {index}") except ValueError: print(f"'{current_lang}' is not found in the languages list.")
json.dumps()
JSON.stringify()
, Python offers json.dumps()
to convert a Python object into a JSON string. This is the preferred method for most scenarios as JSON is a widely used and well-structured format.import json my_object = {"name": "Alice", "age": 30, "city": "New York"} json_string = json.dumps(my_object) print(json_string) # Output: {"name": "Alice", "age": 30, "city": "New York"}
runtime.txt
file to your app’s root directory that declares the exact version number to use.$ cat runtime.txt
python-3.12.4
enumerate()
.xs = [8, 23, 45] for index, x in enumerate(xs): print(f"item #{index + 1} = {x}")
item #1 = 8 item #2 = 23 item #3 = 45
def
keyword and a function name.def double(x): """Doubles a number.""" return x * 2 result = double(5) print(result) # Output: 10
double
that takes a number (x
) as input and returns its double.# Lambda function to double a number double_lambda = lambda x: x * 2 result = double_lambda(5) print(result) # Output: 10
.strip()
method in Python is used to remove leading and trailing characters from a string. It's a versatile method with various applications for string manipulation.data = " Hello, world! " # String with leading and trailing spaces stripped_data = data.strip() print(stripped_data) # Output: "Hello, world!" (whitespace removed)
data = "**Hello, world!**" # String with leading and trailing asterisks stripped_data = data.strip("*") print(stripped_data) # Output: "Hello, world!" (asterisks removed)