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...
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...
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...
<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="...
class MyClass {
func sayHi() { print("Hello") }
deinit { print("Goodbye") }
}
When a closure captures a reference type (a class instance), it holds a strong reference by default:
let closure: () -> Void
do {
let obj = MyClass()
// Captures a strong re...
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 ...
We have three methods:
attr_reader: used to allow reading the variable outside the class.
attr_writer: used to allow modifying the variable outside the class.
attr_accessor: combines both methods.
class Cat
attr_reader :age # you can read the age but you can never change it
attr_writer...
Python 2.x2.3
raw_input will wait for the user to enter text and then return the result as a string.
foo = raw_input("Put a message here that asks the user for input")
In the above example foo will store whatever input the user provides.
Python 3.x3.0
input will wait for the user ...
Python 2.x2.3
In Python 2.x, to continue a line with print, end the print statement with a comma. It will automatically add a space.
print "Hello,",
print "World!"
# Hello, World!
Python 3.x3.0
In Python 3.x, the print function has an optional end parameter that is what...
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(geolocationSuccess, geolocationFailure);
} else {
console.log("Geolocation is not supported by this browser.");
}
// Function that will be called if the query succeeds
var geolocationSuccess = function(pos) {...
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; ...
This a Basic example for using the MVVM model in a windows desktop application, using WPF and C#. The example code implements a simple "user info" dialog.
The View
The XAML
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
...
Overview
Checkboxes and radio buttons are written with the HTML tag <input>, and their behavior is defined in the HTML specification.
The simplest checkbox or radio button is an <input> element with a type attribute of checkbox or radio, respectively:
<input type="checkbox&quo...
Functions in Swift may return values, throw errors, or both:
func reticulateSplines() // no return value and no error
func reticulateSplines() -> Int // always returns a value
func reticulateSplines() throws // no return value, but may throw an error
func reticulat...
It is possible to send a value to the generator by passing it to the next() method.
function* summer() {
let sum = 0, value;
while (true) {
// receive sent value
value = yield;
if (value === null) break;
// aggregate values
sum += value;
...