Tutorial by Examples

Slices are objects in themselves and can be stored in variables with the built-in slice() function. Slice variables can be used to make your code more readable and to promote reuse. >>> programmer_1 = [ 1956, 'Guido', 'van Rossum', 'Python', 'Netherlands'] >>> programmer_2 = [ 18...
A factory decreases coupling between code that needs to create objects from object creation code. Object creation is not made explicitly by calling a class constructor but by calling some function that creates the object on behalf the caller. A simple Java example is the following one: interface Ca...
Imports System.IO Dim filename As String = "c:\path\to\file.txt" File.WriteAllText(filename, "Text to write" & vbCrLf)
Dim filename As String = "c:\path\to\file.txt" If System.IO.File.Exists(filename) Then Dim writer As New System.IO.StreamWriter(filename) writer.Write("Text to write" & vbCrLf) 'Add a newline writer.close() End If
using System.Text; using System.IO; string filename = "c:\path\to\file.txt"; //'using' structure allows for proper disposal of stream. using (StreamWriter writer = new StreamWriter(filename")) { writer.WriteLine("Text to Write\n"); }
using System.IO; using System.Text; string filename = "c:\path\to\file.txt"; File.writeAllText(filename, "Text to write\n");
A basic plot is created by calling plot(). Here we use the built-in cars data frame that contains the speed of cars and the distances taken to stop in the 1920s. (To find out more about the dataset, use help(cars)). plot(x = cars$speed, y = cars$dist, pch = 1, col = 1, main = "Distance ...
matplot is useful for quickly plotting multiple sets of observations from the same object, particularly from a matrix, on the same graph. Here is an example of a matrix containing four sets of random draws, each with a different mean. xmat <- cbind(rnorm(100, -3), rnorm(100, -1), rnorm(100, 1),...
Histograms allow for a pseudo-plot of the underlying distribution of the data. hist(ldeaths) hist(ldeaths, breaks = 20, freq = F, col = 3)
The following program says hello to the user. It takes one positional argument, the name of the user, and can also be told the greeting. import argparse parser = argparse.ArgumentParser() parser.add_argument('name', help='name of user' ) parser.add_argument('-g', '--greeting', ...
To join two or more path components together, firstly import os module of python and then use following: import os os.path.join('a', 'b', 'c') The advantage of using os.path is that it allows code to remain compatible over all operating systems, as this uses the separator appropriate for the pl...
Use os.path.abspath: >>> os.getcwd() '/Users/csaftoiu/tmp' >>> os.path.abspath('foo') '/Users/csaftoiu/tmp/foo' >>> os.path.abspath('../foo') '/Users/csaftoiu/foo' >>> os.path.abspath('/foo') '/foo'
To split one component off of the path: >>> p = os.path.join(os.getcwd(), 'foo.txt') >>> p '/Users/csaftoiu/tmp/foo.txt' >>> os.path.dirname(p) '/Users/csaftoiu/tmp' >>> os.path.basename(p) 'foo.txt' >>> os.path.split(os.getcwd()) ('/Users/csafto...
Every package requires a setup.py file which describes the package. Consider the following directory structure for a simple package: +-- package_name | | | +-- __init__.py | +-- setup.py The __init__.py contains only the line def foo(): return 100. The following setup.py...
docopt turns command-line argument parsing on its head. Instead of parsing the arguments, you just write the usage string for your program, and docopt parses the usage string and uses it to extract the command line arguments. """ Usage: script_name.py [-a] [-b] <path> ...
Comparing sets In R, a vector may contain duplicated elements: v = "A" w = c("A", "A") However, a set contains only one copy of each element. R treats a vector like a set by taking only its distinct elements, so the two vectors above are regarded as the same: set...
The %in% operator compares a vector with a set. v = "A" w = c("A", "A") w %in% v # TRUE TRUE v %in% w # TRUE Each element on the left is treated individually and tested for membership in the set associated with the vector on the right (consisting of all its...
To find every vector of the form (x, y) where x is drawn from vector X and y from Y, we use expand.grid: X = c(1, 1, 2) Y = c(4, 5) expand.grid(X, Y) # Var1 Var2 # 1 1 4 # 2 1 4 # 3 2 4 # 4 1 5 # 5 1 5 # 6 2 5 The result is a data.frame with one...
unique drops duplicates so that each element in the result is unique (only appears once): x = c(2, 1, 1, 2, 1) unique(x) # 2 1 Values are returned in the order they first appeared. duplicated tags each duplicated element: duplicated(x) # FALSE FALSE TRUE TRUE TRUE anyDuplicated(x) >...
To count how many elements of two sets overlap, one could write a custom function: xtab_set <- function(A, B){ both <- union(A, B) inA <- both %in% A inB <- both %in% B return(table(inA, inB)) } A = 1:20 B = 10:30 xtab_set(A, B) # inB ...

Page 170 of 1336