Lab 5 Strings and Scope
Lab 5 Strings and Scope
1. Write a function areatriangle() that takes 3 sides as commandline arguments and calculate the area
of the triangle.
2. Write a function that takes a string as a parameter and returns a string with every successive repetitive
character replaced with a star(*). For example, ‘hello’ is returned as ‘he**o’.
3. What will be the output on executing each of the statements, following the assignment statement:
address = ‘Plot-6, Nayapalli, Bhubaneswar ‘
a. len(address)
b. address[17:-1]
c. address[-len(address): len(address)]
d. address[:2] + address[-11:]
e. address.find(‘bhubaneswar’)
f. address.swapcase()
g. address.split(‘,’)
h. address.isalpha()
5. Write a function that takes two strings and returns True if they are anagrams and False otherwise. A
pair of strings is anagrams if the letters in one word can be arranged to form the second one.
6. Write a function that takes a sentence as an input parameter and displays the number of words
starting with a vowel in the sentence.
7. Write a function that takes a sentence as an input parameter and replaces the first letter of every word
with the corresponding uppercase letter and rest of the letters in the word by corresponding letters in
lowercase without using built-in function.
8. Write a function that takes a string as an input and determines the count of the number of words.
10. Study the progr am segments given below. Give the output produced, if any .
1.
globalVar = 10
def test():
localVar = 20
print(‘Inside function test :globalVar =‘, globalVar)
print(‘Inside function test : localVar=‘, localVar)
test()
print(‘Outside function test : globalVar=‘, globalVar)
print(‘Outside function test : localVar=‘, localVar)
2.
globalVar = 10
def test():
localVar = 20
globalVar = 30
print(‘Inside function test :globalVar =‘, globalVar)
print(‘Inside function test : localVar=‘, localVar)
test()
print(‘Outside function test : globalVar=‘, globalVar)
3.
globalVar = 10
def test():
global globalVar
localVar = 20
globalVar = 30
print(‘Inside function test :globalVar =‘, globalVar)
print(‘Inside function test : localVar=‘, localVar)
test()
print(‘Outside function test : globalVar=‘, globalVar)
4.
globalVar = 10
def test():
global globalVar
localVar = 20
globalVar = 30
print(‘Inside function test :globalVar =‘, globalVar)
print(‘Inside function test : localVar=‘, localVar)
test()
print(‘Outside function test : globalVar=‘, globalVar)
5.
a=4
def f():
a=5
def g():
nonlocal a
a = 10
print(‘inside function g,’, ‘a = ‘,a)
def h():
nonlocal a
a = 20
print(‘inside function h,’, ‘a = ‘,a)
h()
g()
print(‘inside function f,’, ‘a = ‘, a)
f()
6.
x=2
def test():
x=x+1
print(x)
print(x)
7.
x=2
def test():
global x
x=x+1
print(x)
print(x)