Search By Label
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.")
connect
package from the registry, you would run:npm view connect
npm view connect versions
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"}
docker image ls
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
function calculatePrice(total) { if (total > 1000) { return total * 0.9; // 0.9 is a magic number } else { return total * 0.95; // 0.95 is another magic number } }
0.9
and 0.95
are magic numbers because their meaning is not clear. If someone else reads this code in the future, it will be difficult to understand their significance without additional context.function getErrorMessage(code) {
switch (code) {
case "ERR001":
return "Connection Error"; // "ERR001" is a magic string
case "ERR002":
return "Authentication Error"; // "ERR002" is another magic string
default:
return "Unknown Error";
}
}
<%= button_to "Delete", [quote, line_item_date] %>
<%= button_to "Delete", quote_line_item_date_path(quote, line_item_date) %>
redirect_to @quote
redirect_to quote_path(@quote)
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
array = [1, 2, 3, 4] new_array = [] array.each do |element| new_array << element * 2 end new_array # => [2, 4, 6, 8]
each
handle and return the same array and for this reason, it is necessary to create a new_array
variable to inject the new values of the product to element * 2
map
:array = [1, 2, 3, 4] array.map do |element| element * 2 end # => [2, 4, 6, 8]
.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)
breakpoint()
to any line.h: help w: where n: next s: step (steps into function) c: continue p: print l: list q: quit