Education, Learning, python

Documentation Strings

>> print(list.__doc__) list() -> new empty list list(iterable) -> new list initialized from iterable's items >>> print(range.__doc__) range(stop) -> range object range(start, stop[, step]) -> range object Return an object that produces a sequence of integers from start (inclusive) to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1. start defaults to… Continue reading Documentation Strings

Advertisements
Education, Learning, python

Lambda : Anonymous functions

lambda expressions can be used wherever function objects are required. lambda keyword is used create anonymous functions. Assigning a lambda function to a variable f. function object is stored in f.You can put a lambda definition anywhere a function is expected, and you don't have to assign it to a variable at all. >>> (lambda… Continue reading Lambda : Anonymous functions

Education, Learning, python, Strings, Technology

Unpack an iterable object to separate variables

# Unpack a Tuple of N-elements to a collection of N-variables x,y = (2,3) print(x) print(y) ## A more practical use ## Given a tuple of N-lists, you can use unpacking into variables sharepricedata = ([ 'Citi', 50, 91.1, (2012, 12, 21) ], [ 'JPMC', 50, 91.1, (2012, 12, 21) ]) for i in sharepricedata:… Continue reading Unpack an iterable object to separate variables

Education, Learning, python, Strings, Technology

Convert Numeric to ASCII/Unicode and vice-versa

Convert ASCII to Numeric and vice-versa using ord and chr In [2]: print(ord('a')) 97 In [3]: print(chr(97)) a Convert Unicode String to Unicode Numeric and vice-versa In [4]: print(ord(u'\u2020')) 8224 In [6]: print(repr(unichr(8224))) u'\u2020' Turn a list of Numerics to their corresponding ASCII's using map and chr In [8]: print(map(chr,[97,98,99,100])) ['a', 'b', 'c', 'd']

Education, Learning, python, Strings, Technology

Reading Text and Printing Strings using Python

Difference between Text and Binary data: Text is a Sequence of characters and Binary data is a Sequence of bytes. Different texts are written using different character sets or encodings. Character sets : ASCII, EBCDIC Encoding : Unicode,EUC,JIS For example There are several standard methods to encode Japanese characters for use on a computer, including JIS, Shift-JIS,… Continue reading Reading Text and Printing Strings using Python

Education, Learning, PySpark, python, Technology

Performing Count on a Group of Values

Scenario: Read a list of records. The records are stored in a list as strings e.g. ["1;3;1989","2;3;1989","3;2;1989","4;2;1989","5;1;1989","6;5;1990","7;4;1990"] Each string has three fields, 1st id, 2nd rating and 3rd year. The data represented as a table would be as follows id rating year 1 3 1989 2 3 1989 3 2 1989 …. …. …. Our… Continue reading Performing Count on a Group of Values