Tutorial by Examples: di

The division operator (/) perform arithmetic division on numbers (literals or variables). console.log(15 / 3); // 5 console.log(15 / 4); // 3.75
Given a list comprehension you can append one or more if conditions to filter values. [<expression> for <element> in <iterable> if <condition>] For each <element> in <iterable>; if <condition> evaluates to True, add <expression> (usually a function...
dictionary = {"Hello": 1234, "World": 5678} print(dictionary["Hello"]) The above code will print 1234. The string "Hello" in this example is called a key. It is used to lookup a value in the dict by placing the key in square brackets. The number 1234 is ...
The dict() constructor can be used to create dictionaries from keyword arguments, or from a single iterable of key-value pairs, or from a single dictionary and keyword arguments. dict(a=1, b=2, c=3) # {'a': 1, 'b': 2, 'c': 3} dict([('d', 4), ('e', 5), ('f', 6)]) # {'d': 4, 'e': ...
1/2 #=> 0 Since we are dividing two integers, the result is an integer. To solve this problem, we need to cast at least one of those to Float: 1.0 / 2 #=> 0.5 1.to_f / 2 #=> 0.5 1 / Float(2) #=> 0.5 Alternatively, fdiv may be used to return the floating point result of di...
The JSONSerialization class is built into Apple's Foundation framework. 2.2 Read JSON The JSONObjectWithData function takes NSData, and returns AnyObject. You can use as? to convert the result to your expected type. do { guard let jsonData = "[\"Hello\", \"JSON\"]&q...
Normally, enums can't be recursive (because they would require infinite storage): enum Tree<T> { case leaf(T) case branch(Tree<T>, Tree<T>) // error: recursive enum 'Tree<T>' is not marked 'indirect' } The indirect keyword makes the enum store its payload with...
Sometimes you don't want to have your function accessible/stored as a variable. You can create an Immediately Invoked Function Expression (IIFE for short). These are essentially self-executing anonymous functions. They have access to the surrounding scope, but the function itself and any internal va...
In addition to the built-in round function, the math module provides the floor, ceil, and trunc functions. x = 1.55 y = -1.55 # round to the nearest integer round(x) # 2 round(y) # -2 # the second argument gives how many decimal places to round to (defaults to 0) round(x, 1) ...
import random randint() Returns a random integer between x and y (inclusive): random.randint(x, y) For example getting a random number between 1 and 8: random.randint(1, 8) # Out: 8 randrange() random.randrange has the same syntax as range and unlike random.randint, the last value is no...
git remote add upstream git-repository-url Adds remote git repository represented by git-repository-url as new remote named upstream to the git repository
You can make Git ignore certain files and directories — that is, exclude them from being tracked by Git — by creating one or more .gitignore files in your repository. In software projects, .gitignore typically contains a listing of files and/or directories that are generated during the build proces...
import java.awt.Image; import javax.imageio.ImageIO; ... try { Image img = ImageIO.read(new File("~/Desktop/cat.png")); } catch (IOException e) { e.printStackTrace(); }
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 ...
There are several special variable types that a class can use for more easily sharing data. Instance variables, preceded by @. They are useful if you want to use the same variable in different methods. class Person def initialize(name, age) my_age = age # local variable, will be destroyed ...
The simplest way to iterate over a file line-by-line: with open('myfile.txt', 'r') as fp: for line in fp: print(line) readline() allows for more granular control over line-by-line iteration. The example below is equivalent to the one above: with open('myfile.txt', 'r') as fp: ...
with open(input_file, 'r') as in_file, open(output_file, 'w') as out_file: for line in in_file: out_file.write(line) Using the shutil module: import shutil shutil.copyfile(src, dst)
The following variables set up the below example: var COOKIE_NAME = "Example Cookie"; /* The cookie's name. */ var COOKIE_VALUE = "Hello, world!"; /* The cookie's value. */ var COOKIE_PATH = "/foo/bar"; /* The cookie's path. */ var COOKIE_EXPIRES; ...
var name = name + "=", cookie_array = document.cookie.split(';'), cookie_value; for(var i=0;i<cookie_array.length;i++) { var cookie=cookie_array[i]; while(cookie.charAt(0)==' ') cookie = cookie.substring(1,cookie.length); if(cookie.indexOf(name)==0) ...
import json d = { 'foo': 'bar', 'alice': 1, 'wonderland': [1, 2, 3] } json.dumps(d) The above snippet will return the following: '{"wonderland": [1, 2, 3], "foo": "bar", "alice": 1}'

Page 5 of 164