Tutorial by Examples: an

Enums without payloads can have raw values of any literal type: enum Rotation: Int { case up = 0 case left = 90 case upsideDown = 180 case right = 270 } Enums without any specific type do not expose the rawValue property enum Rotation { case up case right cas...
Logical OR (||), reading left to right, will evaluate to the first truthy value. If no truthy value is found, the last value is returned. var a = 'hello' || ''; // a = 'hello' var b = '' || []; // b = [] var c = '' || undefined; // c = undefined var d = 1 |...
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...
Using the def statement is the most common way to define a function in python. This statement is a so called single clause compound statement with the following syntax: def function_name(parameters): statement(s) function_name is known as the identifier of the function. Since a function def...
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')) ...
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...
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
git add -A 2.0 git add . In version 2.x, git add . will stage all changes to files in the current directory and all its subdirectories. However, in 1.x it will only stage new and modified files, not deleted files. Use git add -A, or its equivalent command git add --all, to stage all ch...
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...
Changing the text of an existing UILabel can be done by accessing and modifying the text property of the UILabel. This can be done directly using String literals or indirectly using variables. Setting the text with String literals Swift label.text = "the new text" Objective-C // Do...
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(); }
x > y x < y These operators compare two types of values, they're the less than and greater than operators. For numbers this simply compares the numerical values to see which is larger: 12 > 4 # True 12 < 4 # False 1 < 4 # True For strings they will compare lexicographical...
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]]
This is the basic use of the <a> (anchor element) element: <a href="http://example.com/">Link to example.com</a> It creates a hyperlink, to the URL http://example.com/ as specified by the href (hypertext reference) attribute, with the anchor text "Link to example...
In order to access the value of an Optional, it needs to be unwrapped. You can conditionally unwrap an Optional using optional binding and force unwrap an Optional using the ! operator. Conditionally unwrapping effectively asks "Does this variable have a value?" while force unwrapping sa...
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...

Page 8 of 307