7/1/22, 4:51 PM Day5 - Jupyter Notebook
Functions and Data Structures
continue with strings
function
Introduction to Data Structures
working with strings
str methods
methods can be applied on only strings
dir(str)
In [1]:
1 dir(str)
Out[1]:
['__add__',
'__class__',
'__contains__',
'__delattr__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__getnewargs__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__iter__',
' le ',
In [10]:
1 name="ruthu"
2 name
Out[10]:
'ruthu'
In [3]:
1 name+"vanitha"
Out[3]:
'ruthuvanitha'
localhost:8888/notebooks/Desktop/python workshop/Day5.ipynb#function-definition 1/11
7/1/22, 4:51 PM Day5 - Jupyter Notebook
In [4]:
1 name
Out[4]:
'ruthu'
In [5]:
1 name+="vanitha"
In [6]:
1 name
Out[6]:
'ruthuvanitha'
string methods
str.capitalize()
which capatilizes the first char of str
str.isalpha()
str.isdigit()
isnumeric()
islower()
isupper()
lower()
upper()
title()
startswith()
endswith()
count()
index()
replace()
strip()
lstrip()
rstrip()
split()
center()
format() etc..
In [19]:
1 st=input("enter the string:")
2 st
...
localhost:8888/notebooks/Desktop/python workshop/Day5.ipynb#function-definition 2/11
7/1/22, 4:51 PM Day5 - Jupyter Notebook
In [25]:
1 st.isupper()
Out[25]:
False
In [20]:
1 st.isalpha() # checks the str whether it is lower or not
...
In [21]:
1 st.isnumeric() # integer possibility of str
...
In [22]:
1 st.islower() # lower case
...
In [23]:
1 name
...
In [24]:
1 name.islower()
...
In [14]:
1 st
...
In [15]:
1 st.capitalize() #
...
In [17]:
1 st=st.capitalize()
2 st
Out[17]:
'Nandini'
localhost:8888/notebooks/Desktop/python workshop/Day5.ipynb#function-definition 3/11
7/1/22, 4:51 PM Day5 - Jupyter Notebook
In [18]:
1 st.isalpha()
Out[18]:
True
In [27]:
1 st.lower()
Out[27]:
'student@123'
In [28]:
1 name.islower()
Out[28]:
True
In [29]:
1 name.upper()
Out[29]:
'RUTHU'
In [34]:
1 new=st.center(40)
2 # centrailse the string with given value of places/bits
In [35]:
1 new
Out[35]:
' Student@123 '
In [36]:
1 len(new)
Out[36]:
40
localhost:8888/notebooks/Desktop/python workshop/Day5.ipynb#function-definition 4/11
7/1/22, 4:51 PM Day5 - Jupyter Notebook
In [37]:
1 new.lstrip()
Out[37]:
'Student@123 '
In [38]:
1 new.rstrip()
Out[38]:
' Student@123'
In [39]:
1 new.strip()
Out[39]:
'Student@123'
In [42]:
1 # format or fstring
2 "I am {0} and from {1}".format('Ruthu','Apssdc')
Out[42]:
'I am Ruthu and from Apssdc'
In [48]:
1 # format or fstring
2 name,org=input(),input()
3 details="I am {0} and from {1}".format(name,org)
Vanitha
APSSDC
In [49]:
1 details
Out[49]:
'I am Vanitha and from APSSDC'
In [50]:
1 details.count('a') #
Out[50]:
localhost:8888/notebooks/Desktop/python workshop/Day5.ipynb#function-definition 5/11
7/1/22, 4:51 PM Day5 - Jupyter Notebook
In [51]:
1 details.title()
...
In [52]:
1 details.split() # list of words
...
In [53]:
1 details.swapcase()
...
In [54]:
1 details.index('and')
...
In [55]:
1 details.index('a') # gives you the first occurence
Out[55]:
In [56]:
1 details
...
In [57]:
1 # replace(old,new)
2 details.replace('and','new')
...
In [58]:
1 details.strip()
...
In [61]:
1 details.endswith('C')
Out[61]:
True
localhost:8888/notebooks/Desktop/python workshop/Day5.ipynb#function-definition 6/11
7/1/22, 4:51 PM Day5 - Jupyter Notebook
In [62]:
1 details.startswith('I')
Out[62]:
True
In [67]:
1 # read a str from user and
2 # print vowels present in it
3 st=input()
4 vowels="aeiouAEIOU"
5 print("lowercase vowels ",end=":")
6 for ch in st:
7 if ch in vowels:
8 if ch.islower():
9 print(ch,end=" ")
10
11 print("\nupper case vowels",end=":")
12 for ch in st:
13 if ch in vowels:
14 if ch.isupper():
15 print(ch,end=" ")
...
In [68]:
1 # find the frequency of sub string
2 # read n space separated integers from user
3
In [87]:
1 sub="vrsec"
2 clg=input("string:")
3 count=0
4 for word in clg:
5 if word==sub:
6 count+=1
7 print("frequency=",count)
string:vrsec in vijayawada vrsec working for student skill improvement vrsec
good at placements
frequency= 0
In [88]:
1 len(clg)
Out[88]:
88
localhost:8888/notebooks/Desktop/python workshop/Day5.ipynb#function-definition 7/11
7/1/22, 4:51 PM Day5 - Jupyter Notebook
In [79]:
1 clg[3]
2
Out[79]:
'e'
In [80]:
1 clg[0]
Out[80]:
'v'
In [89]:
1 sub="vrsec"
2 clg=input("string:").split()
3 count=0
4 for word in clg:
5 if word==sub:
6 count+=1
7 print("frequency=",count)
string:vrsec in vijayawada vrsec working for student skill improvement vrsec
good at placements
frequency= 3
In [83]:
1 clg[0]
Out[83]:
'vrsec'
In [90]:
1 len(clg)
Out[90]:
13
In [91]:
1 print(clg)
...
localhost:8888/notebooks/Desktop/python workshop/Day5.ipynb#function-definition 8/11
7/1/22, 4:51 PM Day5 - Jupyter Notebook
In [92]:
1 # read n space separated values from user
2 # find the sum of middle digits in it
3 # n=7
4 #908 765 345 123 904 567
5 #
In [100]:
1 n=int(input("number:"))
2 nums=input().split()[:n]
3 s=0
4 for num in nums:
5 s+=int(num[1])
6 print("sum of middle digits:",s)
...
In [97]:
1 clg[:6]
...
Functions
a block of statements executed to perform a perticular task
reusability of code
modularity of code
2types of functions
1. pre-defined and
these were defined when the language is developed
we simply call and use them
print(),len(),type(),input(),bin(),ord(),chr(),int(),dir(),str() etc.
2. user defined
defined by user
in 4 many ways
syntax
def function_name(args):
statements
return
function definition and
arguments,dummy variables
the main logic
function call
using the defined function
parameters / actual values
In [101]:
1 print("sdhfkdfkdfkj")
sdhfkdfkdfkj
localhost:8888/notebooks/Desktop/python workshop/Day5.ipynb#function-definition 9/11
7/1/22, 4:51 PM Day5 - Jupyter Notebook
In [102]:
1 len("erutuue")
Out[102]:
function definition
function with args and with return
func with args and without return
func without args and with return
fun without args and without return
In [103]:
1 # function that adds three values
2 def add(a,b,c):
3 return a+b+c
4 # function definition
5
6 add(9,3,4)
Out[103]:
16
In [104]:
1 def product(x,y):
2 print("product=",x*y)
3
4 x,y=int(input()),int(input())
5 product(x,y)
9
45
product= 405
In [105]:
1 # 3rd fun without args and with return
2 st=input("string:")
3 ch=input("char:")
4 def frequency():
5 return st.count(ch)
6
7 frequency()
string:Ruthu from APSSDC upersdf user
char:u
Out[105]:
localhost:8888/notebooks/Desktop/python workshop/Day5.ipynb#function-definition 10/11
7/1/22, 4:51 PM Day5 - Jupyter Notebook
In [110]:
1 #4th type
2 def message():
3 msg=input("message:") # local
4 print("hello my dear",msg)
5 message()
message:enemy
hello my dear enemy
In [107]:
1 msg
...
In [108]:
1 x
Out[108]:
In [109]:
1 y
Out[109]:
45
In [ ]:
localhost:8888/notebooks/Desktop/python workshop/Day5.ipynb#function-definition 11/11