Input

# Get input from user and display (input's type is `string`)
age = input("Your age? ") # python 3, raw_input for python 2
print("Your age:", age) # don't need space after "age"
# Get the input and store to numbers list
numbers = list(map(int, input().split()))
# Get multi inputs on 1 line
x, y, z, n = (int(input()) for _ in range(4))

Comment

Using # on each line.

# print("This is not showed.")
print("This is showed.)
This is showed.
# Print normally
print("Hello!") # python 3
print "Hello!" # python 2

# print with `format`
print("Hello {} and {}.".format("A", "B"))

# change order
print("Hello {2} and {1}.".format("A", "B"))

# Directly insert (python 3.6 or above)
b = "B"
print(f'Hello {"A"} and {b}.')

# long strings
print('This is a part of sentence.'
      'This is other part.')
# Print normally
Hello!
Hello!

# print with `format`
Hello A and B.

# change order
Hello B and A.

# Directly insert
Hello A and B.


# long strings
This is a part of sentence. This is other part.
# print decimals
print("{:.6f}".format(number))
# print decimals
1.000000
# print multiples
print("1", 5, "thi") # there are spaces
# print multiples
1 5 thi
# first 20 characters, second 10 characters
print( '{:<20s} {:<10s}'.format(string_one, string_two) )

Display separated results (like in executing multiple code cells),

display(df_1)
display(df_2)

Logging[ref]

import logging
log = logging.getLogger(__name__)

log.warning('something')
log.info('something')
log.debug('something')
log.error('something')
logger.critical('something')

If the log.info() doesn’t work, set below[ref] ,

logging.getLogger().setLevel(logging.INFO)
# or
logging.basicConfig(level=logging.DEBUG)

Docstring

What?

If you wanna make a docstring (showing the information of a function when using help(<func>) or func.__doc__).

def reverse(text):
    """Reverse a text.
    Input the text.
    Return text reversed.
    """
    return text[::-1]

help(reverse)
Help on function reverse in module __main__:

reverse(text)
    Reverse a text.
    Input the text.
    Return text reversed.
reverse.__doc__

print(reverse.__doc__)
'Reverse a text.\n    Input the text.\n    Return text reversed.\n    '

Reverse a text.
    Input the text.
    Return text reversed.

Sample structure

Using nympydoc format (there are others). It’s recommended to be used later in Sphinx. See more here and Example NumPy Style Python Docstrings.

def ex_class(var1, var2):
    """
    Quick description.
    Longer description with `keywords`.

    Parameters
    ----------
    var1 : int
        Desc for var1.
    var2 : {0 or 'index', 1 or 'columns'}, default 0
        Long desc for var2. It may take a long line and we can break
        this like that.

    Returns
    -------
    Resampler object

    See Also
    --------
    groupby : Group by mapping, function, label, or list of labels.
    Series.resample : Resample a Series.

    Notes
    -----
    See the `user guide
    <https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#resampling>`_
    for more.

    Examples
    --------
    Start by creating a series with 9 one minute timestamps.
    >>> index = 1 +1
    2
    Description for this example.
    """
    # some commands
    return return_values
    Quick description.
    Longer description with `keywords`.

    Parameters
    ----------
    var1 : int
        Desc for var1.
    var2 : {0 or 'index', 1 or 'columns'}, default 0
        Long desc for var2. It may take a long line and we can break
        this like that.

    Returns
    -------
    Resampler object

    See Also
    --------
    groupby : Group by mapping, function, label, or list of labels.
    Series.resample : Resample a Series.

    Notes
    -----
    See the `user guide
    <https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#resampling>`_
    for more.

    Examples
    --------
    Start by creating a series with 9 one minute timestamps.
    >>> index = 1 +1
    2
    Description for this example.

Hierarchical Data Format (HDF)

  • Designed to store and organize large amounts of data.
  • Store multiple data files in a single data file!
    • Different types of information.
    • Self describing (metadata included in the file)
  • Properties[ref] :
    • Datasets (numpy arrays): fast slicing, compression.
    • Group (dictionaries): nesting, POSIX path syntax.
    • Attributrs (metadata): datasets/group, key-value.
  • HDF5 is row based and really effient than csv for very large file size[ref] .
  • Extensions: .h5, .hdf, .hdf4, …
  • Tool: HDFView
  • Example[ref] :

An example of HDF5 structure An example HDF5 file structure which contains groups, datasets and associated metadata.

import h5py

f = h5py.File('mytestfile.hdf5', 'r') # read a file
# h5py.File acts like Python dict
dset = f['mydataset']
dset.attrs # attribute