Tutorial by Examples: c

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...
About Protocols A Protocol specifies initialisers, properties, functions, subscripts and associated types required of a Swift object type (class, struct or enum) conforming to the protocol. In some languages similar ideas for requirement specifications of subsequent objects are known as ‘interfaces...
Protocols may define associated type requirements using the associatedtype keyword: protocol Container { associatedtype Element var count: Int { get } subscript(index: Int) -> Element { get set } } Protocols with associated type requirements can only be used as generic constraints:...
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...
Standard Creation It is recommended to use this form only when creating regex from dynamic variables. Use when the expression may change or the expression is user generated. var re = new RegExp(".*"); With flags: var re = new RegExp(".*", "gmi"); With a backsl...
Match Using .exec() RegExp.prototype.exec(string) returns an array of captures, or null if there was no match. var re = /([0-9]+)[a-z]+/; var match = re.exec("foo123bar"); match.index is 3, the (zero-based) location of the match. match[0] is the full match string. match[1] is the t...
var re = /[a-z]+/; if (re.test("foo")) { console.log("Match exists."); } The test method performs a search to see if a regular expression matches a string. The regular expression [a-z]+ will search for one or more lowercase letters. Since the pattern matches the string,...
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...
Instead of importing the complete module you can import only specified names: from random import randint # Syntax "from MODULENAME import NAME1[, NAME2[, ...]]" print(randint(1, 10)) # Out: 5 from random is needed, because the python interpreter has to know from which resource it...
#include <stdio.h> /* increment: take number, increment it by one, and return it */ int increment(int i) { printf("increment %d by 1\n", i); return i + 1; } /* decrement: take number, decrement it by one, and return it */ int decrement(int i) { printf("...
#include <stdio.h> enum Op { ADD = '+', SUB = '-', }; /* add: add a and b, return result */ int add(int a, int b) { return a + b; } /* sub: subtract b from a, return result */ int sub(int a, int b) { return a - b; } /* getmath: return the appropriate m...
Using typedef It might be handy to use a typedef instead of declaring the function pointer each time by hand. The syntax for declaring a typedef for a function pointer is: typedef returnType (*name)(parameters); Example: Posit that we have a function, sort, that expects a function pointer to ...
You can compare multiple items with multiple comparison operators with chain comparison. For example x > y > z is just a short form of: x > y and y > z This will evaluate to True only if both comparisons are True. The general form is a OP b OP c OP d ... Where OP represents ...
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]]
Arrays of strings can be created using ruby's percent string syntax: array = %w(one two three four) This is functionally equivalent to defining the array as: array = ['one', 'two', 'three', 'four'] Instead of %w() you may use other matching pairs of delimiters: %w{...}, %w[...] or %w<...&...
2.0 array = %i(one two three four) Creates the array [:one, :two, :three, :four]. Instead of %i(...), you may use %i{...} or %i[...] or %i!...! Additionally, if you want to use interpolation, you can do this with %I. 2.0 a = 'hello' b = 'goodbye' array_one = %I(#{a} #{b} world) array_tw...

Page 20 of 826