Tutorial by Examples

def input_number(msg, err_msg=None): while True: try: return float(raw_input(msg)) except ValueError: if err_msg is not None: print(err_msg) def input_number(msg, err_msg=None): while True: try: return ...
Python 2.x2.3 In Python 2.x, to continue a line with print, end the print statement with a comma. It will automatically add a space. print "Hello,", print "World!" # Hello, World! Python 3.x3.0 In Python 3.x, the print function has an optional end parameter that is what...
There are different modes you can open a file with, specified by the mode parameter. These include: 'r' - reading mode. The default. It allows you only to read the file, not to modify it. When using this mode the file must exist. 'w' - writing mode. It will create a new file if it does no...
The simplest way to iterate over a file line-by-line: with open('myfile.txt', 'r') as fp: for line in fp: print(line) readline() allows for more granular control over line-by-line iteration. The example below is equivalent to the one above: with open('myfile.txt', 'r') as fp: ...
The preferred method of file i/o is to use the with keyword. This will ensure the file handle is closed once the reading or writing has been completed. with open('myfile.txt') as in_file: content = in_file.read() print(content) or, to handle closing the file manually, you can forgo with...
with open('myfile.txt', 'w') as f: f.write("Line 1") f.write("Line 2") f.write("Line 3") f.write("Line 4") If you open myfile.txt, you will see that its contents are: Line 1Line 2Line 3Line 4 Python doesn't automatically add line b...
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) {...
In the event that geolocation fails, your callback function will receive a PositionError object. The object will include an attribute named code that will have a value of 1, 2, or 3. Each of these numbers signifies a different kind of error; the getErrorCode() function below takes the PositionError....
with open(input_file, 'r') as in_file, open(output_file, 'w') as out_file: for line in in_file: out_file.write(line) Using the shutil module: import shutil shutil.copyfile(src, dst)
The following variables set up the below example: var COOKIE_NAME = "Example Cookie"; /* The cookie's name. */ var COOKIE_VALUE = "Hello, world!"; /* The cookie's value. */ var COOKIE_PATH = "/foo/bar"; /* The cookie's path. */ var COOKIE_EXPIRES; ...
var name = name + "=", cookie_array = document.cookie.split(';'), cookie_value; for(var i=0;i<cookie_array.length;i++) { var cookie=cookie_array[i]; while(cookie.charAt(0)==' ') cookie = cookie.substring(1,cookie.length); if(cookie.indexOf(name)==0) ...
var expiry = new Date(); expiry.setTime(expiry.getTime() - 3600); document.cookie = name + "=; expires=" + expiry.toGMTString() + "; path=/" This will remove the cookie with a given name.
This a Basic example for using the MVVM model in a windows desktop application, using WPF and C#. The example code implements a simple "user info" dialog. The View The XAML <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> ...
import json d = { 'foo': 'bar', 'alice': 1, 'wonderland': [1, 2, 3] } json.dumps(d) The above snippet will return the following: '{"wonderland": [1, 2, 3], "foo": "bar", "alice": 1}'
import json s = '{"wonderland": [1, 2, 3], "foo": "bar", "alice": 1}' json.loads(s) The above snippet will return the following: {u'alice': 1, u'foo': u'bar', u'wonderland': [1, 2, 3]}
The following snippet encodes the data stored in d into JSON and stores it in a file (replace filename with the actual name of the file). import json d = { 'foo': 'bar', 'alice': 1, 'wonderland': [1, 2, 3] } with open(filename, 'w') as f: json.dump(d, f)
The following snippet opens a JSON encoded file (replace filename with the actual name of the file) and returns the object that is stored in the file. import json with open(filename, 'r') as f: d = json.load(f)
git diff This will show the unstaged changes on the current branch from the commit before it. It will only show changes relative to the index, meaning it shows what you could add to the next commit, but haven't. To add (stage) these changes, you can use git add. If a file is staged, but was modi...
git diff --staged This will show the changes between the previous commit and the currently staged files. NOTE: You can also use the following commands to accomplish the same thing: git diff --cached Which is just a synonym for --staged or git status -v Which will trigger the verbose sett...
<table> <tr> <th>Heading 1/Column 1</th> <th>Heading 2/Column 2</th> </tr> <tr> <td>Row 1 Data Column 1</td> <td>Row 1 Data Column 2</td> </tr> <tr> <td>Row 2 Data Column...

Page 36 of 1336