Tutorial by Examples

In addition to receiving values from a generator, it is possible to send an object to a generator using the send() method. def accumulator(): total = 0 value = None while True: # receive sent value value = yield total if value is None: break # aggr...
It's possible to create generator iterators using a comprehension-like syntax. generator = (i * 2 for i in range(3)) next(generator) # 0 next(generator) # 2 next(generator) # 4 next(generator) # raises StopIteration If a function doesn't necessarily need to be passed a list, you can sa...
Generator expressions are similar to list, dictionary and set comprehensions, but are enclosed with parentheses. The parentheses do not have to be present when they are used as the sole argument for a function call. expression = (x**2 for x in range(10)) This example generates the 10 first perfe...
An external CSS stylesheet can be applied to any number of HTML documents by placing a <link> element in each HTML document. The attribute rel of the <link> tag has to be set to "stylesheet", and the href attribute to the relative or absolute path to the stylesheet. While usin...
Download Xcode from the Mac App Store. Click to create a new project or playground:
Create a new HTML file and paste the following content: <!DOCTYPE html> <html ng-app> <head> <title>Hello, Angular</title> <script src="https://code.angularjs.org/1.5.8/angular.min.js"></script> </head> <body ng-init="name=...
The background-color property sets the background color of an element using a color value or through keywords, such as transparent, inherit or initial. transparent, specifies that the background color should be transparent. This is default. inherit, inherits this property from its parent e...
Jsoup can be be used to easily extract all links from a webpage. In this case, we can use Jsoup to extract only specific links we want, here, ones in a h3 header on a page. We can also get the text of the links. Document doc = Jsoup.connect("http://stackoverflow.com").userAgent("Mozi...
a, b = 1, 2 # Using the "+" operator: a + b # = 3 # Using the "in-place" "+=" operator to add and assign: a += b # a = 3 (equivalent to a = a + b) import operator # contains 2 argument arithmetic functions for the examp...
CSS transforms are based on the size of the elements so if you don't know how tall or wide your element is, you can position it absolutely 50% from the top and left of a relative container and translate it by 50% left and upwards to center it vertically and horizontally. Keep in mind that with this...
Create a new file called hello.sh with the following content and give it executable permissions with chmod +x hello.sh. Execute/Run via: ./hello.sh #!/usr/bin/env bash # Note that spaces cannot be used around the `=` assignment operator whom_variable="World" # Use printf to sa...
HTML: <div id="element-one">Hello I am some text.</div> <div id="element-two">Hello I am some smaller text.</div> CSS: #element-one { font-size: 30px; } #element-two { font-size: 10px; } The text inside #element-one will be 30px ...
a, b = 1, 2 # Using the "-" operator: b - a # = 1 import operator # contains 2 argument arithmetic functions operator.sub(b, a) # = 1 Possible combinations (builtin types): int and int (gives an int) int and float (gives a float) int and comp...
Creating a database in MySQL CREATE DATABASE mydb; Return value: Query OK, 1 row affected (0.05 sec) Using the created database mydb USE mydb; Return value: Database Changed Creating a table in MySQL CREATE TABLE mytable ( id int unsigned NOT NULL auto_increme...
a, b = 2, 3 a * b # = 6 import operator operator.mul(a, b) # = 6 Possible combinations (builtin types): int and int (gives an int) int and float (gives a float) int and complex (gives a complex) float and float (gives a float) float and complex (gives a comple...
Python does integer division when both operands are integers. The behavior of Python's division operators have changed from Python 2.x and 3.x (see also Integer Division ). a, b, c, d, e = 3, 2, 2.0, -3, 10 Python 2.x2.7 In Python 2 the result of the ' / ' operator depends on the type of the nu...
a, b = 2, 3 (a ** b) # = 8 pow(a, b) # = 8 import math math.pow(a, b) # = 8.0 (always float; does not allow complex results) import operator operator.pow(a, b) # = 8 Another difference between the built-in pow and math.pow is that the built-in po...
By default, the math.log function calculates the logarithm of a number, base e. You can optionally specify a base as the second argument. import math import cmath math.log(5) # = 1.6094379124341003 # optional base argument. Default is math.e math.log(5, math.e) # = 1.6094379124341003 ...
Rounding Math.round() will round the value to the closest integer using half round up to break ties. var a = Math.round(2.3); // a is now 2 var b = Math.round(2.7); // b is now 3 var c = Math.round(2.5); // c is now 3 But var c = Math.round(-2.7); // c is now -3 va...
A practical use case of a generator is to iterate through values of an infinite series. Here's an example of finding the first ten terms of the Fibonacci Sequence. def fib(a=0, b=1): """Generator that yields Fibonacci numbers. `a` and `b` are the seed values""" ...

Page 39 of 1336