Tutorial by Examples

5 Object.keys(obj) returns an array of a given object's keys. var obj = { a: "hello", b: "this is", c: "javascript!" }; var keys = Object.keys(obj); console.log(keys); // ["a", "b", "c"]
A list comprehension creates a new list by applying an expression to each element of an iterable. The most basic form is: [ <expression> for <element> in <iterable> ] There's also an optional 'if' condition: [ <expression> for <element> in <iterable> if <c...
A dictionary comprehension is similar to a list comprehension except that it produces a dictionary object instead of a list. A basic example: Python 2.x2.7 {x: x * x for x in (1, 2, 3, 4)} # Out: {1: 1, 2: 4, 3: 9, 4: 16} which is just another way of writing: dict((x, x * x) for x in (1, 2...
Generator expressions are very similar to list comprehensions. The main difference is that it does not create a full set of results at once; it creates a generator object which can then be iterated over. For instance, see the difference in the following code: # list comprehension [x**2 for x in r...
The fundamental part of most classes is its constructor, which sets up each instance's initial state and handles any parameters that were passed when calling new. It's defined in a class block as though you're defining a method named constructor, though it's actually handled as a special case. cla...
Set comprehension is similar to list and dictionary comprehension, but it produces a set, which is an unordered collection of unique elements. Python 2.x2.7 # A set containing every value in range(5): {x for x in range(5)} # Out: {0, 1, 2, 3, 4} # A set of even numbers between 1 and 10: {x f...
This program will output "Hello World!" #import <Foundation/Foundation.h> int main(int argc, char * argv[]) { NSLog(@"Hello World!"); } #import is a pre-processor directive, which indicates we want to import or include the information from that file into the pr...
Django is a web development framework based on Python. Django 1.11 (the latest stable release) requires Python 2.7, 3.4, 3.5 or 3.6 to be installed. Assuming pip is available, installation is as simple as running the following command. Keep in mind, omitting the version as shown below will install t...
The map function is the simplest one among Python built-ins used for functional programming. map() applies a specified function to each element in an iterable: names = ['Fred', 'Wilma', 'Barney'] Python 3.x3.0 map(len, names) # map in Python 3.x is a class; its instances are iterable # Out: &...
To filter discards elements of a sequence based on some criteria: names = ['Fred', 'Wilma', 'Barney'] def long_name(name): return len(name) > 5 Python 2.x2.0 filter(long_name, names) # Out: ['Barney'] [name for name in names if len(name) > 5] # equivalent list comprehension #...
Write your code in a file named hello.swift: print("Hello, world!") To compile and run a script in one step, use swift from the terminal (in a directory where this file is located): To launch a terminal, press CTRL+ALT+T on Linux, or find it in Launchpad on macOS. To change dire...
In some cases, when working with a web server, overriding the web server's default content type may be required. There may be cases where you need to send data as plain text, JSON, or XML, for example. The header() function can send a raw HTTP header. You can add the Content-Type header to notify t...
Variables can be accessed via dynamic variable names. The name of a variable can be stored in another variable, allowing it to be accessed dynamically. Such variables are known as variable variables. To turn a variable into a variable variable, you put an extra $ put in front of your variable. $va...
The addition operator (+) adds numbers. var a = 9, b = 3, c = a + b; c will now be 12 This operand can also be used multiple times in a single assignment: var a = 9, b = 3, c = 8, d = a + b + c; d will now be 20. Both operands are converted to primitive types. ...
The subtraction operator (-) subtracts numbers. var a = 9; var b = 3; var c = a - b; c will now be 6 If a string or boolean is provided in place of a number value, it gets converted to a number before the difference is calculated (0 for false, 1 for true): "5" - 1; // 4 7 - ...
The multiplication operator (*) perform arithmetic multiplication on numbers (literals or variables). console.log( 3 * 5); // 15 console.log(-3 * 5); // -15 console.log( 3 * -5); // -15 console.log(-3 * -5); // 15
The division operator (/) perform arithmetic division on numbers (literals or variables). console.log(15 / 3); // 5 console.log(15 / 4); // 3.75
The remainder / modulus operator (%) returns the remainder after (integer) division. console.log( 42 % 10); // 2 console.log( 42 % -10); // 2 console.log(-42 % 10); // -2 console.log(-42 % -10); // -2 console.log(-40 % 10); // -0 console.log( 40 % 10); // 0 This operator returns th...
The Increment operator (++) increments its operand by one. If used as a postfix, then it returns the value before incrementing. If used as a prefix, then it returns the value after incrementing. //postfix var a = 5, // 5 b = a++, // 5 c = a // 6 In this case, a is incr...
The decrement operator (--) decrements numbers by one. If used as a postfix to n, the operator returns the current n and then assigns the decremented the value. If used as a prefix to n, the operator assigns the decremented n and then returns the changed value. var a = 5, // 5 b = a...

Page 27 of 1336