Agenda = '''
Introduction
Data Types
Arithmatic
Data Structure
Mutable : List, Dictionary
Immutable : Tuple, String
Control FLow
Function
User Define Function
'''
print(Agenda)
'''Python is an Interpreted, Interactive, Object Oriented,
High Level Scripting Language'''
234
23.443
Hello
'Hello Python'
data = 43
type(data)
type(89.0)
type('hello')
type(True)
type(5 + 10j)
data = 'hi'
dir(data)
S1 = "surendra panpaliya".upper()
print(S1)
343 + 565
2 ** 10000
help(str.count)
'surendra'.count('r')
'hello hello hello'.count('hello')
'hello hello hello'.index('o')
True
True + True + True - False
data1 = complex(5,10)
type(data1)
data1
data1.real
data1.imag
data1.conjugate()
List is mutable sequence of any objects which include int, float, string, list, tuple, dictionary.
Books = []
type(Books)
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)
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)
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)
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)
dir(Books)
Books[2] = 'pyspark'
print(Books)
List is an immutable sequence of any objects which include int, float, string, list, tuple, dictionary.
Books = 'python', 'jython'
type(Books)
Books1 = ()
type(Books1)
Books1 = ('python',)
type(Books1)
Books[0] = 'jython'
dir(Books)
first = 30
second = 40
temp = first
first = second
second = temp
print(first, second)
first, second, third , fourth = 100, 200, 300, 400
# parallel Assignment in python
# swapping in Python
first, second, third , fourth = fourth, third, second, first
first, second, third, fourth
import time
time.localtime()
year, month, day, hour, minute, \
second, wday, yday, dst = time.localtime()
T = (year, month, day, hour, minute, second, wday, yday, dst)
type(T)
T
data = 98
print(data)
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)
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)
%%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
run greatest_while.py
range(2,25,2)
list(range(2,25,2))
for var in range(2,25,2):
print(var)
tuple(range(-5,-25,-2))
Words = ['abc','def','ghi','jkl' ]
'a', 'b', 'c' .. 'l'
Words = ['abc','def','ghi','jkl' ]
for word in Words:
for ch in word:
print(ch)
def function_name(arg1, arg2):
'''function document'''
print("Hello function",arg1, arg2)
function_name(4,9)
fn = function_name
fn(4,5)
fn.__doc__
dir(fn)
help(fn)
def add(arg1, arg2):
'''Addition of two number'''
return arg1 + arg2
add(5,6)
5*4*3*2*1
Factorial of any number
Fibonacci Sequence
0,1,1,2,3,5,8,13,21, ...
def factorial(num):
'''Factorial of number'''
result = 1
for i in range(1,num+1):
result *= i
return result
factorial(5)
def factorial_r(num):
'''Factorial for recursive function'''
if num <= 1:
return 1
else:
return num * factorial_r( num -1 )
factorial_r(5)
def fibonacci(num):
'''Fibonacci of any number '''
for i in range(1,20,2):
print(i)
for ch in 'SUREDNRA PANPALIYA'[::1]:
print(ch)
'SURENDRA'[::-1]
[ 3,4,5,65,6,9,'hello'] [::2]
[ 3,4,5,65,6,9,'hello'] [2:5]
[ 3,4,5,65,6,9,'hello'] [::-1]
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
fibonacci(20)
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
fibonacci(10)
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
fibonacci(10)
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)
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)
def default_values(arg1, arg2 = 5, arg3 = 10):
'''Default Values of function'''
print(arg1, arg2, arg3)
default_values(20)
default_values(20,50,200)
default_values(700, 500,800)
def default_values(arg1, arg2, arg3):
'''Default Values of function'''
print(arg1, arg2, arg3)
default_values(arg2=300, arg3=500, arg1 = 800)
def var_arg(*arg):
"Variable argument Parameter"
print(arg)
var_arg(4,5,6)
var_arg('hi','how are you',32,56)
def default_var_arg(*arg, **kwarg):
"Default Variable argument Parameter"
print(arg)
print(kwarg)
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')
Create Multiplier using Variable arguments parameter.
Multiplier should take any number of numeric argument and
result should be multiplication of all numbers / arguments.