Basic - 1

1. Import

import numpy as np
from six.moves import cPickle as pickle

2. Multi-line statement

a = 1 + 2 + 3 + \
    4 + 5 + 6 + \
    7 + 8 + 9

a = 1; b = 2; c = 3

a = (1 + 2 + 3 +
    4 + 5 + 6 +
    7 + 8 + 9)

3. __doc__

def double(num):
    """Function to double the value"""
    return 2*num

>>> print(double.__doc__)
Function to double the value

4. list, tuple, set, dictionary

  • list = ordered collections

  • tuple = immutable list

  • set = unordered, unique collections

  • dictionary = key-value pair

# List = ordered sequence of items.
a = [5,10,15,20,25,30,35,40]
a[2] # 15

# Tuple = immutable list
t = (5,'program', 1+3j)
t[1] # 'program'
t[0] = 10 # error

# Set = unordered collection of unique items.
s = {5,2,3,1,4}
s[1] # error

# Dictionary = key-value pairs
d = {1:'value','key':2}
d[1] # 'value'

5. string format

x = 5; y = 10
print('The value of x is {} and y is {}'.format(x,y))
#      The value of x is 5 and y is 10

x = 12.3456789
print('The value of x is %3.4f' %x)
#      The value of x is 12.3457

Last updated

Was this helpful?