By default, attempting to lookup the value for a key which does not exist will return nil. You can optionally specify some other value to return (or an action to take) when the hash is accessed with a non-existent key. Although this is referred to as "the default value", it need not be a s...
The differences between null and undefined
null and undefined share abstract equality == but not strict equality ===,
null == undefined // true
null === undefined // false
They represent slightly different things:
undefined represents the absence of a value, such as before an identifier/...
You can use slices to very easily reverse a str, list, or tuple (or basically any collection object that implements slicing with the step parameter). Here is an example of reversing a string, although this applies equally to the other types listed above:
s = 'reverse me!'
s[::-1] # '!em esrever...
When the commits on two branches don't conflict, Git can automatically merge them:
~/Stack Overflow(branch:master) » git merge another_branch
Auto-merging file_a
Merge made by the 'recursive' strategy.
file_a | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
A generator object supports the iterator protocol. That is, it provides a next() method (__next__() in Python 3.x), which is used to step through its execution, and its __iter__ method returns itself. This means that a generator can be used in any language construct which supports generic iterable o...
The next() built-in is a convenient wrapper which can be used to receive a value from any iterator (including a generator iterator) and to provide a default value in case the iterator is exhausted.
def nums():
yield 1
yield 2
yield 3
generator = nums()
next(generator, None) # 1
...
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...
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...
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=...
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...
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 ...
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) # = 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...
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"""
...
<input type="hidden" name="inputName" value="inputValue">
A hidden input won't be visible to the user, but its value will be sent to the server when the form is submitted nonetheless.
Direction-Specific Properties
CSS allows you to specify a given side to apply margin to. The four properties provided for this purpose are:
margin-left
margin-right
margin-top
margin-bottom
The following code would apply a margin of 30 pixels to the left side of the selected div. View Resu...
You can include another Git repository as a folder within your project, tracked by Git:
$ git submodule add https://github.com/jquery/jquery.git
You should add and commit the new .gitmodules file; this tells Git what submodules should be cloned when git submodule update is run.