from module_name import *
for example:
from math import *
sqrt(2) # instead of math.sqrt(2)
ceil(2.7) # instead of math.ceil(2.7)
This will import all names defined in the math module into the global namespace, other than names that begin with an underscore (which indicates that the wri...
Finding the minimum/maximum of a sequence of sequences is possible:
list_of_tuples = [(0, 10), (1, 15), (2, 8)]
min(list_of_tuples)
# Output: (0, 10)
but if you want to sort by a specific element in each sequence use the key-argument:
min(list_of_tuples, key=lambda x: x[0]) # Sorting ...
You can't pass an empty sequence into max or min:
min([])
ValueError: min() arg is an empty sequence
However, with Python 3, you can pass in the keyword argument default with a value that will be returned if the sequence is empty, instead of raising an exception:
max([], default=42) ...
Create a file hello.html with the following content:
<!DOCTYPE html>
<html>
<head>
<title>Hello, World!</title>
</head>
<body>
<div>
<p id="hello">Some random text</p>
</div>
<script src=...
Arrays can be created by enclosing a list of elements in square brackets ([ and ]). Array elements in this notation are separated with commas:
array = [1, 2, 3, 4]
Arrays can contain any kind of objects in any combination with no restrictions on type:
array = [1, 'b', nil, [3, 4]]
Arrays of strings can be created using ruby's percent string syntax:
array = %w(one two three four)
This is functionally equivalent to defining the array as:
array = ['one', 'two', 'three', 'four']
Instead of %w() you may use other matching pairs of delimiters: %w{...}, %w[...] or %w<...&...
2.0
array = %i(one two three four)
Creates the array [:one, :two, :three, :four].
Instead of %i(...), you may use %i{...} or %i[...] or %i!...!
Additionally, if you want to use interpolation, you can do this with %I.
2.0
a = 'hello'
b = 'goodbye'
array_one = %I(#{a} #{b} world)
array_tw...
An empty Array ([]) can be created with Array's class method, Array::new:
Array.new
To set the length of the array, pass a numerical argument:
Array.new 3 #=> [nil, nil, nil]
There are two ways to populate an array with default values:
Pass an immutable value as second argument.
P...
The groupingBy(classifier, downstream) collector allows the collection of Stream elements into a Map by classifying each element in a group and performing a downstream operation on the elements classified in the same group.
A classic example of this principle is to use a Map to count the occurrence...
This is the basic use of the <a> (anchor element) element:
<a href="http://example.com/">Link to example.com</a>
It creates a hyperlink, to the URL http://example.com/ as specified by the href (hypertext reference) attribute, with the anchor text "Link to example...
If you ignore files by using a pattern but have exceptions, prefix an exclamation mark(!) to the exception. For example:
*.txt
!important.txt
The above example instructs Git to ignore all files with the .txt extension except for files named important.txt.
If the file is in an ignored folder, y...
struct Repository {
let identifier: Int
let name: String
var description: String?
}
This defines a Repository struct with three stored properties, an integer identifier, a string name, and an optional string description. The identifier and name are constants, as they've been decla...
In order to access the value of an Optional, it needs to be unwrapped.
You can conditionally unwrap an Optional using optional binding and force unwrap an Optional using the ! operator.
Conditionally unwrapping effectively asks "Does this variable have a value?" while force unwrapping sa...
Relational operators check if a specific relation between two operands is true. The result is evaluated to 1 (which means true) or 0 (which means false). This result is often used to affect control flow (via if, while, for), but can also be stored in variables.
Equals "=="
Checks whether...
Assigns the value of the right-hand operand to the storage location named by the left-hand operand, and returns the value.
int x = 5; /* Variable x holds the value 5. Returns 5. */
char y = 'c'; /* Variable y holds the value 99. Returns 99
* (as the character 'c' is repr...
Basic Arithmetic
Return a value that is the result of applying the left hand operand to the right hand operand, using the associated mathematical operation. Normal mathematical rules of commutation apply (i.e. addition and multiplication are commutative, subtraction, division and modulus are not).
...
Logical AND
Performs a logical boolean AND-ing of the two operands returning 1 if both of the operands are non-zero. The logical AND operator is of type int.
0 && 0 /* Returns 0. */
0 && 1 /* Returns 0. */
2 && 0 /* Returns 0. */
2 && 3 /* Returns 1. */
Lo...
Getting the minimum or maximum or using sorted depends on iterations over the object. In the case of dict, the iteration is only over the keys:
adict = {'a': 3, 'b': 5, 'c': 1}
min(adict)
# Output: 'a'
max(adict)
# Output: 'c'
sorted(adict)
# Output: ['a', 'b', 'c']
To keep the dictionary ...