Tutorial by Examples: d

The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds. It is also a trivial way to achieve an asynchronous operation. In this example calling the wait function resolves the promise after the time specified as first argument: function wait(ms) ...
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) ...
Tk is the standard graphical user interface (GUI) for Ruby. It provides a cross-platform GUI for Ruby programs. Example code: require "tk" TkRoot.new{ title "Hello World!" } Tk.mainloop The result: Step by Step explanation: require "tk" Load the tk package...
Open a new blank document in the MATLAB Editor (in recent versions of MATLAB, do this by selecting the Home tab of the toolstrip, and clicking on New Script). The default keyboard shortcut to create a new script is Ctrl-n. Alternatively, typing edit myscriptname.m will open the file myscriptname.m ...
import random shuffle() You can use random.shuffle() to mix up/randomize the items in a mutable and indexable sequence. For example a list: laughs = ["Hi", "Ho", "He"] random.shuffle(laughs) # Shuffles in-place! Don't do: laughs = random.shuffle(laughs) p...
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...
jQuery is the starting point for writing any jQuery code. It can be used as a function jQuery(...) or a variable jQuery.foo. $ is an alias for jQuery and the two can usually be interchanged for each other (except where jQuery.noConflict(); has been used - see Avoiding namespace collisions). Assumi...
Protocols may define associated type requirements using the associatedtype keyword: protocol Container { associatedtype Element var count: Int { get } subscript(index: Int) -> Element { get set } } Protocols with associated type requirements can only be used as generic constraints:...
break statement When a break statement executes inside a loop, control flow "breaks" out of the loop immediately: i = 0 while i < 7: print(i) if i == 4: print("Breaking from loop") break i += 1 The loop conditional will not be evaluated a...
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
Assuming you set the upstream (as in the "setting an upstream repository") git fetch remote-name git merge remote-name/branch-name The pull command combines a fetch and a merge. git pull The pull with --rebase flag command combines a fetch and a rebase instead of merge. git pull ...
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...
Setting a specific Seed will create a fixed random-number series: random.seed(5) # Create a fixed state print(random.randrange(0, 10)) # Get a random integer between 0 and 9 # Out: 9 print(random.randrange(0, 10)) # Out: 4 Resetting the seed will create the same &qu...
import java.awt.Image; import javax.imageio.ImageIO; ... try { Image img = ImageIO.read(new File("~/Desktop/cat.png")); } catch (IOException e) { e.printStackTrace(); }
Use the import statement: >>> import random >>> print(random.randint(1, 10)) 4 import module will import a module and then allow you to reference its objects -- values, functions and classes, for example -- using the module.name syntax. In the above example, the random module...
Instead of importing the complete module you can import only specified names: from random import randint # Syntax "from MODULENAME import NAME1[, NAME2[, ...]]" print(randint(1, 10)) # Out: 5 from random is needed, because the python interpreter has to know from which resource it...
A basic "Hello, World!" program in Haskell can be expressed concisely in just one or two lines: main :: IO () main = putStrLn "Hello, World!" The first line is an optional type annotation, indicating that main is a value of type IO (), representing an I/O action which "...
from module_name import * for example: from math import * sqrt(2) # instead of math.sqrt(2) ceil(2.7) # instead of math.ceil(2.7) This will import all names defined in the math module into the global namespace, other than names that begin with an underscore (which indicates that the wri...
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=...

Page 16 of 691