Tutorial by Examples: and

Program options can be handled with the getopt() function. It operates with a similar syntax to the POSIX getopt command, with additional support for GNU-style long options. #!/usr/bin/php // a single colon indicates the option takes a value // a double colon indicates the value may be omitted ...
At the command line, first verify that you have Git installed: On all operating systems: git --version On UNIX-like operating systems: which git If nothing is returned, or the command is not recognized, you may have to install Git on your system by downloading and running the installer. See...
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...
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...
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...
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...
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 ...
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) {...

Page 4 of 153