Functions in Python
Functions in Python
n=int(input("Enter no"))
rec(n)
Stack Diagram
def fact(n):
f=1
if n>0:
f=n*fact(n-1)
return f
else:
return 1
n=int(input("Enter no"))
f=fact(n)
print("Fact=",f)
f=1
n=3
n=3 3*fact(3-1)
if n>0 ???? 2
f=f*fact(n-1)
n=2
Back to main 2*fact(2-1)
1
n=1
1*fact(1-1)
1
n=0
Return 1
Strings
• Strings are amongst the most popular types in
Python.
• We can create them simply by enclosing
characters in quotes.
• Python treats single quotes the same as
double quotes.
• For example −
str1 = 'Hello World!'
str2 = "Python Programming“
Accessing Strings
• In Python strings are stored as individual
characters in a continuous memory location.
• Strings can be accessed in both directions in
Python i.e. backward and forward
• Forward indexing starts with 0,1,2,…
• Backward indexing starts with -1,-2,-3,…
0 1 2 3 4
H E L L O
-5 -4 -3 -2 -1
Ex. 1
str="hello"
for i in range(-5,0):
print (str[i],end="")
Ex. 2
str="hello"
for i in range(0,5):
print (str[i],end="")
• Traversing using for loop:
Ex.
str="hello"
for i in str:
print (i,end="")
o/p hello
Traversing using while loop
Ex.
str="hello"
i=0
while i<len(str):
print (str[i],end="")
i=i+1
o/p:hello
String Slices
Extracting the subset of string is called as string
Slice (using [] operator)
Syntax: string[start:end]
This will return part of the string from start to
index end -1
String Comparison
Python compares strings using ASCII value.
Comparison operators(>,<,>=,<=,==,!=) are used
to compare strings.
>>> "python"=="python"
True
>>> "Python"=="python"
False
>>> "python">="Python"
True
The IN and NOT IN Operator
• These Boolean operators takes two string .
• In return true if first string appears as subsstring in second.
>>> str="Hello Python"
>>> 'h' in str
True
>>> 'c' in str
False
>>> 'h' not in str
False
>>> 'c' not in str
True
>>>
String Methods
•String.capitalize():Capitalize first letter of string
Ex:
>>>“sam”.capitalize()
>>>Sam
•String.Find(str,beg=0,end=len(string)):
Determines if substring occurs in string and returns -
1 if string not found.
>>>"hello and welcome".find("e")
>>>1
>>>"hello and welcome".find("e",2)
>>>11
>>> "hello and welcome".find("z",1)
>>>-1
String Methods
• String.rfind(str,beg=0,end=len(string)):
Same as find but search backward in string.
>>> "hello and welcome".rfind("e")
16
>>> "hello and welcome".rfind("e",0,5)
1
String Methods
• String.Count(str,beg=0,end=len(string))
Count how may times str occures in string.
Ex.
>>> "hello and welcome".count("e")
3
>>> "hello and welcome".count("e",5)
2
String Methods
• String.index(str,beg=0,end=len(string)):
Returns index same as found but throws an
exception if not found
>>>hello and welcome".index("e")
>>>1
>>>"hello and welcome".index("z",1)
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
"hello and welcome".index("z",1)
ValueError: substring not found
String Methods
String.islower():returns true if string has atleast
1 cased character.
>>> "sam".islower()
True
>>> "SAM".islower()
False
>>>
String Methods
Script Mode:
import math
print(math.sqrt(100))
Function Description Example
def disp():
print("******")
#main
a=disp()
print (a)
import
• There are many built in libraries, module and
packages are provided by Python.
• These libraries we can use using import
statement.
• There are two ways to use import statements-
1. Import statement
2. From import statement
3. From..import * statement
import statement:
It import a module and access definitions using dot
operator.
Syntax:
import m1[,m2,m3,..]
Ex.
>>> import math, time
>>> print(math.sqrt(100),"\n",time.time())
10.0
1498580005.252804
from ..import statement
from..import statement is used to import
specific attributes from a module.
Syntax: from modulename import a1[,a2,a3..]
Ex.
>>> from math import pi
>>> print(pi)
3.141592653589793
from.. Import * statement
To import all items from module.
Syntax:
from modulename import *
Ex.
>>> from math import *
>>> print(pi, e)
3.141592653589793 2.718281828459045
Boolean Function
• This function return true or false
def fun(n):
if n%2==0:
return True
else:
return False
n=int(input("Enter num:"))
print(fun(n))
Incremental Development
• The goal of incremental development is to
avoid long debugging sessions by adding and
testing only small code at a time.
• As example suppose if u want to find roots of
quadratic equations.
The formula for annual compound interest, including
principal sum, is: A = P (1 + r/n) (nt)
Where: