One can give a function as many arguments as one wants, the only fixed rules are that each argument name must be unique and that optional arguments must be after the not-optional ones:
def func(value1, value2, optionalvalue=10):
return '{0} {1} {2}'.format(value1, value2, optionalvalue1)
Wh...
Arbitrary number of positional arguments:
Defining a function capable of taking an arbitrary number of arguments can be done by prefixing one of the arguments with a *
def func(*args):
# args will be a tuple containing all values that are passed in
for i in args:
print(i)
fun...
A basic join (also called "inner join") queries data from two tables, with their relationship defined in a join clause.
The following example will select employees' first names (FName) from the Employees table and the name of the department they work for (Name) from the Departments table:...
The basic closure syntax is
{ [capture list] (parameters) throws-ness -> return type in body }.
Many of these parts can be omitted, so there are several equivalent ways to write simple closures:
let addOne = { [] (x: Int) -> Int in return x + 1 }
let addOne = { [] (x: Int) -> Int in...
A for loop iterates over a sequence, so altering this sequence inside the loop could lead to unexpected results (especially when adding or removing elements):
alist = [0, 1, 2]
for index, value in enumerate(alist):
alist.pop(index)
print(alist)
# Out: [1]
Note: list.pop() is being used t...
You can define a new class using the class keyword.
class MyClass
end
Once defined, you can create a new instance using the .new method
somevar = MyClass.new
# => #<MyClass:0x007fe2b8aa4a18>
There is a problem when using optional arguments with a mutable default type (described in Defining a function with optional arguments), which can potentially lead to unexpected behaviour.
Explanation
This problem arises because a function's default arguments are initialised once, at the point whe...
Python 3.x3.0
In Python 3, print functionality is in the form of a function:
print("This string will be displayed in the output")
# This string will be displayed in the output
print("You can print \n escape characters too.")
# You can print escape characters too.
Pyth...
def input_number(msg, err_msg=None):
while True:
try:
return float(raw_input(msg))
except ValueError:
if err_msg is not None:
print(err_msg)
def input_number(msg, err_msg=None):
while True:
try:
return ...
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...
The preferred method of file i/o is to use the with keyword. This will ensure the file handle is closed once the reading or writing has been completed.
with open('myfile.txt') as in_file:
content = in_file.read()
print(content)
or, to handle closing the file manually, you can forgo with...
with open('myfile.txt', 'w') as f:
f.write("Line 1")
f.write("Line 2")
f.write("Line 3")
f.write("Line 4")
If you open myfile.txt, you will see that its contents are:
Line 1Line 2Line 3Line 4
Python doesn't automatically add line b...
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) {...
In the event that geolocation fails, your callback function will receive a PositionError object. The object will include an attribute named code that will have a value of 1, 2, or 3. Each of these numbers signifies a different kind of error; the getErrorCode() function below takes the PositionError....
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; ...
Python's string type provides many functions that act on the capitalization of a string. These include :
str.casefold
str.upper
str.lower
str.capitalize
str.title
str.swapcase
With unicode strings (the default in Python 3), these operations are not 1:1 mappings or reversible. Most of thes...
window.setTimout() returns a TimeoutID, which can be used to stop that timeout from running. To do this, store the return value of window.setTimeout() in a variable and call clearTimeout() with that variable as the only argument:
function waitFunc(){
console.log("This will not be logged a...