Tutorial by Examples

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 ...
Using one sequence: sorted((7, 2, 1, 5)) # tuple # Output: [1, 2, 5, 7] sorted(['c', 'A', 'b']) # list # Output: ['A', 'b', 'c'] sorted({11, 8, 1}) # set # Output: [1, 8, 11] sorted({'11': 5, '3': 2, '10': 15}) # dict # Output: ['10', '11...
Getting the minimum of a sequence (iterable) is equivalent of accessing the first element of a sorted sequence: min([2, 7, 5]) # Output: 2 sorted([2, 7, 5])[0] # Output: 2 The maximum is a bit more complicated, because sorted keeps order and max returns the first encountered value. In case th...
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...
Grouping the key-value pairs of a dictionary by the value with itemgetter: from itertools import groupby from operator import itemgetter adict = {'a': 1, 'b': 5, 'c': 1} dict((i, dict(v)) for i, v in groupby(adict.items(), itemgetter(1))) # Output: {1: {'a': 1, 'c': 1}, 5: {'b': 5}} which ...
What can be documented? Examples of functions for various languages. A brief introduction of each tag. What is the difference between a question and a topic? Topics have a broader scope than questions; documentation topics that are asked as a question can be closed because they are too bro...
The Promise.all() static method accepts an iterable (e.g. an Array) of promises and returns a new promise, which resolves when all promises in the iterable have resolved, or rejects if at least one of the promises in the iterable have rejected. // wait "millis" ms, then resolve with &quot...
The Promise.race() static method accepts an iterable of Promises and returns a new Promise which resolves or rejects as soon as the first of the promises in the iterable has resolved or rejected. // wait "milliseconds" milliseconds, then resolve with "value" function resolve(va...
Use array_key_exists() or isset() or !empty(): $map = [ 'foo' => 1, 'bar' => null, 'foobar' => '', ]; array_key_exists('foo', $map); // true isset($map['foo']); // true !empty($map['foo']); // true array_key_exists('bar', $map); // true isset($map['bar']); // false...
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...
<a href="example.com" target="_blank">Text Here</a> The target attribute specifies where to open the link. By setting it to _blank, you tell the browser to open it in a new tab or window (per user preference). SECURITY VULNERABILITY WARNING! Using target="...
A basic join (also called "inner join") queries data from two tables, with their relationship defined in a join clause. The following example will select employees' first names (FName) from the Employees table and the name of the department they work for (Name) from the Departments table:...
Joins can also be performed by having several tables in the from clause, separated with commas , and defining the relationship between them in the where clause. This technique is called an Implicit Join (since it doesn't actually contain a join clause). All RDBMSs support it, but the syntax is usua...

Page 34 of 1336