Tutorial by Examples: al

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]]
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...
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 ...
For every infix operator, e.g. + there is a operator-function (operator.add for +): 1 + 1 # Output: 2 from operator import add add(1, 1) # Output: 2 even though the main documentation states that for the arithmetic operators only numerical input is allowed it is possible: from operator impo...
Instead of this lambda-function that calls the method explicitly: alist = ['wolf', 'sheep', 'duck'] list(filter(lambda x: x.startswith('d'), alist)) # Keep only elements that start with 'd' # Output: ['duck'] one could use a operator-function that does the same: from operator import metho...
Optional arguments can be defined by assigning (using =) a default value to the argument-name: def make(action='nothing'): return action Calling this function is possible in 3 different ways: make("fun") # Out: fun make(action="sleep") # Out: sleep # The argumen...
In Python, variables inside functions are considered local if and only if they appear in the left side of an assignment statement, or some other binding occurrence; otherwise such a binding is looked up in enclosing functions, up to the global scope. This is true even if the assignment statement is ...
There is a problem when using optional arguments with a mutable default type (described in Defining a function with optional arguments), which can potentially lead to unexpected behaviour. Explanation This problem arises because a function's default arguments are initialised once, at the point whe...
Python's string type provides many functions that act on the capitalization of a string. These include : str.casefold str.upper str.lower str.capitalize str.title str.swapcase With unicode strings (the default in Python 3), these operations are not 1:1 mappings or reversible. Most of thes...
str.split(sep=None, maxsplit=-1) str.split takes a string and returns a list of substrings of the original string. The behavior differs depending on whether the sep argument is provided or omitted. If sep isn't provided, or is None, then the splitting takes place wherever there is whitespace. Howe...
Python's str type also has a method for replacing occurences of one sub-string with another sub-string in a given string. For more demanding cases, one can use re.sub. str.replace(old, new[, count]): str.replace takes two arguments old and new containing the old sub-string which is to be replace...
function waitFunc(){ console.log("This will be logged every 5 seconds"); } window.setInterval(waitFunc,5000);
window.setInterval() returns an IntervalID, which can be used to stop that interval from continuing to run. To do this, store the return value of window.setInterval() in a variable and call clearInterval() with that variable as the only argument: function waitFunc(){ console.log("This wil...
Copying an array will copy all of the items inside the original array. Changing the new array will not change the original array. var originalArray = ["Swift", "is", "great!"] var newArray = originalArray newArray[2] = "awesome!" //originalArray = ["...
The following examples will use this array to demonstrate accessing values var exampleArray:[Int] = [1,2,3,4,5] //exampleArray = [1, 2, 3, 4, 5] To access a value at a known index use the following syntax: let exampleOne = exampleArray[2] //exampleOne = 3 Note: The value at index two is th...
It is possible to send a value to the generator by passing it to the next() method. function* summer() { let sum = 0, value; while (true) { // receive sent value value = yield; if (value === null) break; // aggregate values sum += value; ...
There are multiple ways to append values onto an array var exampleArray = [1,2,3,4,5] exampleArray.append(6) //exampleArray = [1, 2, 3, 4, 5, 6] var sixOnwards = [7,8,9,10] exampleArray += sixOnwards //exampleArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and remove values from an array exampleAr...
Since PHP 5.0, PDO has been available as a database access layer. It is database agnostic, and so the following connection example code should work for any of its supported databases simply by changing the DSN. // First, create the database handle //Using MySQL (connection via local socket): $d...

Page 7 of 269