Tutorial by Examples: ar

Standard usage for (var i = 0; i < 100; i++) { console.log(i); } Expected output: 0 1 ... 99 Multiple declarations Commonly used to cache the length of an array. var array = ['a', 'b', 'c']; for (var i = 0; i < array.length; i++) { console.log(array[i]); } Expect...
A decorator takes just one argument: the function to be decorated. There is no way to pass other arguments. But additional arguments are often desired. The trick is then to make a function which takes arbitrary arguments and returns a decorator. Decorator functions def decoratorfactory(message): ...
The math module contains the math.sqrt()-function that can compute the square root of any number (that can be converted to a float) and the result will always be a float: import math math.sqrt(9) # 3.0 math.sqrt(11.11) # 3.3331666624997918 math.sqrt(Decimal('6.25')) ...
git log will display all your commits with the author and hash. This will be shown over multiple lines per commit. (If you wish to show a single line per commit, look at onelineing). Use the q key to exit the log. By default, with no arguments, git log lists the commits made in that reposito...
Standard Creation It is recommended to use this form only when creating regex from dynamic variables. Use when the expression may change or the expression is user generated. var re = new RegExp(".*"); With flags: var re = new RegExp(".*", "gmi"); With a backsl...
You can compare multiple items with multiple comparison operators with chain comparison. For example x > y > z is just a short form of: x > y and y > z This will evaluate to True only if both comparisons are True. The general form is a OP b OP c OP d ... Where OP represents ...
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...
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). ...
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 ...
Arguments are defined in parentheses after the function name: def divide(dividend, divisor): # The names of the function and its arguments # The arguments are available by name in the body of the function print(dividend / divisor) The function name and its list of arguments are called...
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...
One can give a function as many arguments as one wants, the only fixed rules are that each argument name must be unique and that optional arguments must be after the not-optional ones: def func(value1, value2, optionalvalue=10): return '{0} {1} {2}'.format(value1, value2, optionalvalue1) Wh...
Arbitrary number of positional arguments: Defining a function capable of taking an arbitrary number of arguments can be done by prefixing one of the arguments with a * def func(*args): # args will be a tuple containing all values that are passed in for i in args: print(i) fun...
The basic closure syntax is { [capture list] (parameters) throws-ness -> return type in body }. Many of these parts can be omitted, so there are several equivalent ways to write simple closures: let addOne = { [] (x: Int) -> Int in return x + 1 } let addOne = { [] (x: Int) -> Int in...

Page 6 of 218