In [3]:
Agenda = '''
Introduction
Data Types
Arithmatic 
Data Structure
Mutable : List, Dictionary
Immutable : Tuple, String 
Control FLow
Function
User Define Function
'''
In [2]:
print(Agenda)
Introduction
Data Types
Data Structure
Control FLow
Function
User Define Function

In [4]:
'''Python is an Interpreted, Interactive, Object Oriented, 
High Level Scripting Language'''
Out[4]:
'Python is an Interpreted, Interactive, Object Oriented, \nHigh Level Scripting Language'
In [5]:
234
Out[5]:
234
In [6]:
23.443
Out[6]:
23.443
In [7]:
Hello
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-1d229271928d> in <module>
----> 1 Hello

NameError: name 'Hello' is not defined
In [8]:
'Hello Python'
Out[8]:
'Hello Python'
In [9]:
data = 43
In [10]:
type(data)
Out[10]:
int
In [11]:
type(89.0)
Out[11]:
float
In [12]:
type('hello')
Out[12]:
str
In [13]:
type(True)
Out[13]:
bool
In [14]:
type(5 + 10j)
Out[14]:
complex
In [15]:
data = 'hi'
In [16]:
dir(data)
Out[16]:
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getnewargs__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mod__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rmod__',
 '__rmul__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'capitalize',
 'casefold',
 'center',
 'count',
 'encode',
 'endswith',
 'expandtabs',
 'find',
 'format',
 'format_map',
 'index',
 'isalnum',
 'isalpha',
 'isdecimal',
 'isdigit',
 'isidentifier',
 'islower',
 'isnumeric',
 'isprintable',
 'isspace',
 'istitle',
 'isupper',
 'join',
 'ljust',
 'lower',
 'lstrip',
 'maketrans',
 'partition',
 'replace',
 'rfind',
 'rindex',
 'rjust',
 'rpartition',
 'rsplit',
 'rstrip',
 'split',
 'splitlines',
 'startswith',
 'strip',
 'swapcase',
 'title',
 'translate',
 'upper',
 'zfill']
In [18]:
S1 = "surendra panpaliya".upper()
print(S1)
SURENDRA PANPALIYA
In [19]:
343 + 565
Out[19]:
908
In [20]:
2 ** 10000
Out[20]:
19950631168807583848837421626835850838234968318861924548520089498529438830221946631919961684036194597899331129423209124271556491349413781117593785932096323957855730046793794526765246551266059895520550086918193311542508608460618104685509074866089624888090489894838009253941633257850621568309473902556912388065225096643874441046759871626985453222868538161694315775629640762836880760732228535091641476183956381458969463899410840960536267821064621427333394036525565649530603142680234969400335934316651459297773279665775606172582031407994198179607378245683762280037302885487251900834464581454650557929601414833921615734588139257095379769119277800826957735674444123062018757836325502728323789270710373802866393031428133241401624195671690574061419654342324638801248856147305207431992259611796250130992860241708340807605932320161268492288496255841312844061536738951487114256315111089745514203313820202931640957596464756010405845841566072044962867016515061920631004186422275908670900574606417856951911456055068251250406007519842261898059237118054444788072906395242548339221982707404473162376760846613033778706039803413197133493654622700563169937455508241780972810983291314403571877524768509857276937926433221599399876886660808368837838027643282775172273657572744784112294389733810861607423253291974813120197604178281965697475898164531258434135959862784130128185406283476649088690521047580882615823961985770122407044330583075869039319604603404973156583208672105913300903752823415539745394397715257455290510212310947321610753474825740775273986348298498340756937955646638621874569499279016572103701364433135817214311791398222983845847334440270964182851005072927748364550578634501100852987812389473928699540834346158807043959118985815145779177143619698728131459483783202081474982171858011389071228250905826817436220577475921417653715687725614904582904992461028630081535583308130101987675856234343538955409175623400844887526162643568648833519463720377293240094456246923254350400678027273837755376406726898636241037491410966718557050759098100246789880178271925953381282421954028302759408448955014676668389697996886241636313376393903373455801407636741877711055384225739499110186468219696581651485130494222369947714763069155468217682876200362777257723781365331611196811280792669481887201298643660768551639860534602297871557517947385246369446923087894265948217008051120322365496288169035739121368338393591756418733850510970271613915439590991598154654417336311656936031122249937969999226781732358023111862644575299135758175008199839236284615249881088960232244362173771618086357015468484058622329792853875623486556440536962622018963571028812361567512543338303270029097668650568557157505516727518899194129711337690149916181315171544007728650573189557450920330185304847113818315407324053319038462084036421763703911550639789000742853672196280903477974533320468368795868580237952218629120080742819551317948157624448298518461509704888027274721574688131594750409732115080498190455803416826949787141316063210686391511681774304792596709376
In [22]:
help(str.count)
Help on method_descriptor:

count(...)
    S.count(sub[, start[, end]]) -> int
    
    Return the number of non-overlapping occurrences of substring sub in
    string S[start:end].  Optional arguments start and end are
    interpreted as in slice notation.

In [23]:
'surendra'.count('r')
Out[23]:
2
In [24]:
'hello hello hello'.count('hello')
Out[24]:
3
In [27]:
'hello hello hello'.index('o')
Out[27]:
4
In [28]:
True
Out[28]:
True
In [29]:
True + True + True - False
Out[29]:
3
In [31]:
data1 = complex(5,10)
In [32]:
type(data1)
Out[32]:
complex
In [33]:
data1
Out[33]:
(5+10j)
In [34]:
data1.real
Out[34]:
5.0
In [35]:
data1.imag
Out[35]:
10.0
In [36]:
data1.conjugate()
Out[36]:
(5-10j)

Data Structure

1. Mutable : List, Dictionary 2. Immutable : tuple, string

List

List is mutable sequence of any objects which include int, float, string, list, tuple, dictionary.
In [37]:
Books = []
type(Books)
Out[37]:
list

Stack Operation on List

In [39]:
Books = []
Books.append('python')
Books.append('jython')
Books.append('iron python')
Books.append('cpp')
Books.append('java')
Books.append('ruby')
print(Books)
Books.pop()
print(Books)
['python', 'jython', 'iron python', 'cpp', 'java', 'ruby']
['python', 'jython', 'iron python', 'cpp', 'java']

Queue Operation on List

In [40]:
Books = []
Books.append('python')
Books.append('jython')
Books.append('iron python')
Books.append('cpp')
Books.append('java')
Books.append('ruby')
print(Books)
Books.pop(0)  # pop with index
print(Books)
['python', 'jython', 'iron python', 'cpp', 'java', 'ruby']
['jython', 'iron python', 'cpp', 'java', 'ruby']

Link List Operation on List

In [41]:
Books = []
Books.append('python')
Books.append('jython')
Books.append('iron python')
Books.append('cpp')
Books.append('java')
Books.append('ruby')
print(Books)
Books.insert(2, 'spark')  # pop with index
print(Books)
Books.remove('java')
print(Books)
['python', 'jython', 'iron python', 'cpp', 'java', 'ruby']
['python', 'jython', 'spark', 'iron python', 'cpp', 'java', 'ruby']
['python', 'jython', 'spark', 'iron python', 'cpp', 'ruby']
In [42]:
Books = []
Books.append('python')
Books.append('jython')
Books.append('iron python')
Books.append('cpp')
Books.append('java')
Books.append('ruby')
print(Books)
Books.insert(2, 'spark')  # pop with index
print(Books)
Books.sort()
print(Books)
['python', 'jython', 'iron python', 'cpp', 'java', 'ruby']
['python', 'jython', 'spark', 'iron python', 'cpp', 'java', 'ruby']
['cpp', 'iron python', 'java', 'jython', 'python', 'ruby', 'spark']
In [43]:
dir(Books)
Out[43]:
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__delitem__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__gt__',
 '__hash__',
 '__iadd__',
 '__imul__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__reversed__',
 '__rmul__',
 '__setattr__',
 '__setitem__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'append',
 'clear',
 'copy',
 'count',
 'extend',
 'index',
 'insert',
 'pop',
 'remove',
 'reverse',
 'sort']
In [46]:
Books[2] = 'pyspark'
print(Books)
['cpp', 'iron python', 'pyspark', 'jython', 'python', 'ruby', 'spark']

Tuple

List is an immutable sequence of any objects which include int, float, string, list, tuple, dictionary.
In [47]:
Books = 'python', 'jython'
type(Books)
Out[47]:
tuple
In [48]:
Books1 = ()
type(Books1)
Out[48]:
tuple
In [50]:
Books1 = ('python',)
type(Books1)
Out[50]:
tuple
In [53]:
Books[0] = 'jython'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-53-36d113e86fbd> in <module>
----> 1 Books[0] = 'jython'

TypeError: 'tuple' object does not support item assignment
In [54]:
dir(Books)
Out[54]:
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getnewargs__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rmul__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'count',
 'index']
In [55]:
first = 30
second = 40
temp = first
first = second
second = temp
print(first, second)
40 30
In [61]:
first, second, third , fourth = 100, 200, 300, 400 
# parallel Assignment in python
In [62]:
# swapping in Python
first, second, third , fourth = fourth, third, second, first
In [63]:
first, second, third, fourth
Out[63]:
(400, 300, 200, 100)
In [64]:
import time
In [65]:
time.localtime()
Out[65]:
time.struct_time(tm_year=2020, tm_mon=1, tm_mday=20, tm_hour=12, tm_min=2, tm_sec=19, tm_wday=0, tm_yday=20, tm_isdst=0)
In [66]:
year, month, day, hour, minute, \
second, wday, yday, dst = time.localtime()
In [71]:
T = (year, month, day, hour, minute, second, wday, yday, dst)
type(T)
Out[71]:
tuple
In [72]:
T
Out[72]:
(2020, 1, 20, 12, 4, 43, 0, 20, 0)

Control Structure

In [73]:
data = 98
 print(data)
  File "<ipython-input-73-1aa911860fcc>", line 2
    print(data)
    ^
IndentationError: unexpected indent
In [75]:
first = int(input("Enter first data:"))
second = int(input("Enter second data:"))

if first > second:
    print("First is greater", first)
else:
    print("Second is greater", second)
    
Enter first data:100
Enter second data:50
First is greater 100
In [76]:
first = int(input("Enter first data:"))
second = int(input("Enter second data:"))
third = int(input("Enter third data:"))

if first > second and first > third:
    print("First is greater", first)
elif second > third:
    print("Second is greater", second)
else:
    print("Third is greater", third)
Enter first data:23
Enter second data:53
Enter third data:65
Third is greater 65
In [77]:
%%writefile greatest_while.py

while True:
    first = int(input("Enter first data:"))
    second = int(input("Enter second data:"))
    third = int(input("Enter third data:"))

    if first > second and first > third:
        print("First is greater", first)
    elif second > third:
        print("Second is greater", second)
    else:
        print("Third is greater", third)
        break
Writing greatest_while.py
In [78]:
run greatest_while.py
Enter first data:34
Enter second data:65
Enter third data:7
Second is greater 65
Enter first data:77
Enter second data:55
Enter third data:89
Third is greater 89
In [79]:
range(2,25,2)
Out[79]:
range(2, 25, 2)
In [80]:
list(range(2,25,2))
Out[80]:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24]
In [81]:
for var in range(2,25,2):
    print(var)
2
4
6
8
10
12
14
16
18
20
22
24
In [86]:
tuple(range(-5,-25,-2))
Out[86]:
(-5, -7, -9, -11, -13, -15, -17, -19, -21, -23)

Words = ['abc','def','ghi','jkl' ]

'a', 'b', 'c' .. 'l'

In [90]:
Words = ['abc','def','ghi','jkl' ]

for word in Words:
    for ch in word:
        print(ch)
a
b
c
d
e
f
g
h
i
j
k
l

Function

In [91]:
def function_name(arg1, arg2):
    '''function document'''
    print("Hello function",arg1, arg2)
    
In [92]:
function_name(4,9)
Hello function 4 9
In [93]:
fn = function_name
In [95]:
fn(4,5)
Hello function 4 5
In [96]:
fn.__doc__
Out[96]:
'function document'
In [97]:
dir(fn)
Out[97]:
['__annotations__',
 '__call__',
 '__class__',
 '__closure__',
 '__code__',
 '__defaults__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__get__',
 '__getattribute__',
 '__globals__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__kwdefaults__',
 '__le__',
 '__lt__',
 '__module__',
 '__name__',
 '__ne__',
 '__new__',
 '__qualname__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__']
In [98]:
help(fn)
Help on function function_name in module __main__:

function_name(arg1, arg2)
    function document

In [99]:
def add(arg1, arg2):
    '''Addition of two number'''
    return arg1 + arg2
In [100]:
add(5,6)
Out[100]:
11
In [101]:
5*4*3*2*1
Out[101]:
120

Assignment

Factorial of any number

Fibonacci Sequence

0,1,1,2,3,5,8,13,21, ...

In [105]:
def factorial(num):
    '''Factorial of number'''
    result = 1
    for i in range(1,num+1):
        result *= i
    return result

factorial(5)
Out[105]:
120
In [106]:
def factorial_r(num):
    '''Factorial for recursive function'''
    if num <= 1:
        return 1
    else:
        return num * factorial_r( num -1 )
    
In [107]:
factorial_r(5)
Out[107]:
120
In [108]:
def fibonacci(num):
    '''Fibonacci of any number '''
In [113]:
for i in range(1,20,2):
    print(i)
1
3
5
7
9
11
13
15
17
19
In [124]:
for ch in 'SUREDNRA PANPALIYA'[::1]:
    print(ch)
S
U
R
E
D
N
R
A
 
P
A
N
P
A
L
I
Y
A
In [125]:
'SURENDRA'[::-1]
Out[125]:
'ARDNERUS'
In [126]:
[ 3,4,5,65,6,9,'hello'] [::2]
Out[126]:
[3, 5, 6, 'hello']
In [129]:
[ 3,4,5,65,6,9,'hello'] [2:5]
Out[129]:
[5, 65, 6]
In [130]:
[ 3,4,5,65,6,9,'hello'] [::-1]
Out[130]:
['hello', 9, 6, 65, 5, 4, 3]
In [134]:
def fibonacci(maxnum):
    '''Fibonacci of any number '''
    first, second = 0, 1  # parallel assignment 
    print(first)
    while second < maxnum:
        print(second)
        first, second = second, first + second
        
In [135]:
fibonacci(20)
0
1
1
2
3
5
8
13
In [157]:
def fibonacci(maxnum):
    '''Fibonacci of any number '''
    first, second = 0, 1  # parallel assignment 
    for i in range(0,maxnum):
        print(first)
        first, second = second, first + second
In [158]:
fibonacci(10)
0
1
1
2
3
5
8
13
21
34
In [164]:
def fibonacci(maxnum):
    '''Fibonacci of any number '''
    first, second = 0, 1  # parallel assignment 
    count = 0
    while count < maxnum:
        print(first)
        count += 1
        first, second = second, first + second
In [165]:
fibonacci(10)
0
1
1
2
3
5
8
13
21
34
In [166]:
def local_fun(num):
    '''Local Variable Demo'''
    print("Value of num before assignment", num)
    num = 200
    print("Value of num after assignment", num)


num = 400

local_fun(300)

print("Value of num after function call", num)
Value of num before assignment 300
Value of num after assignment 200
Value of num after function call 400
In [167]:
def global_fun():
    '''Global Variable Demo'''
    global num
    print("Value of num before assignment", num)
    num = 200
    print("Value of num after assignment", num)


num = 400

global_fun()

print("Value of num after function call", num)
Value of num before assignment 400
Value of num after assignment 200
Value of num after function call 200
In [168]:
def default_values(arg1, arg2 = 5, arg3 = 10):
    '''Default Values of function'''
    print(arg1, arg2, arg3)
In [169]:
default_values(20)
20 5 10
In [170]:
default_values(20,50,200)
20 50 200
In [178]:
default_values(700, 500,800)
700 500 800
In [174]:
def default_values(arg1, arg2, arg3):
    '''Default Values of function'''
    print(arg1, arg2, arg3)
In [175]:
default_values(arg2=300, arg3=500, arg1 = 800)
800 300 500
In [179]:
def var_arg(*arg):
    "Variable argument Parameter"
    print(arg)
In [180]:
var_arg(4,5,6)
(4, 5, 6)
In [182]:
var_arg('hi','how are you',32,56)
('hi', 'how are you', 32, 56)
In [183]:
def default_var_arg(*arg, **kwarg):
    "Default Variable argument Parameter"
    print(arg)
    print(kwarg)
In [187]:
default_var_arg()
default_var_arg(5,6)
default_var_arg('hi','how are you',32,56)
default_var_arg(a='apple',b='banana')
default_var_arg('hi','how are you',32,56, a='apple',b='banana')
()
{}
(5, 6)
{}
('hi', 'how are you', 32, 56)
{}
()
{'a': 'apple', 'b': 'banana'}
('hi', 'how are you', 32, 56)
{'a': 'apple', 'b': 'banana'}

Assignment

Create Multiplier using Variable arguments parameter. 
Multiplier should take any number of numeric argument and 
result should be multiplication of all numbers / arguments. 
In [ ]: