Cs Tutorial On Python
Cs Tutorial On Python
String operations -
txt = "For only {price:.2f} dollars!"
print(txt.format(price = 49))
isalpha()
isdigit()
islower()
isupper()
isspace()
strip(), lstrip(), rstrip()
Regular expressions
import re
The try block lets you test a block of code for errors.
The else block lets you execute code when there is no error.
The finally block lets you execute code, regardless of the result of the try- and
except blocks.
try:
print(x)
except NameError:
print("Variable x is not defined")
except:
print("Something else went wrong")
try:
print("Hello")
except:
print("Something went wrong")
else:
print("Nothing went wrong")
try:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")
try:
f = open("demofile.txt")
try:
f.write("Lorum Ipsum")
except:
print("Something went wrong when writing to the file")
finally:
f.close()
except:
print("Something went wrong when opening the file")
ANSWERS
Q1.
word1[i] = space
Q2.
word = input()
word1 = list(word)
no = len(word)
space = '/'
for i in range(no):
if word[i] == " ":
word1[i] = space
word1 = ''.join(word1)
print(word1)
Q3.
print(" ".join[l])
TypeError: 'builtin_function_or_method' object is not subscriptable
Q4.
b)
string = "this university is best for CS aspirants"
s = string.split()[::-1]
l = []
for i in s:
l.append(i)
print(" ".join(l))
Q5.
Q6.
Q7.
['ai', 'ai']
[]
Q8.
The9rain9in9Spain
The9rain9in Spain
Q9.
print(x.string())
Q10.
(12, 17)
Spain
txt = "The rain in Spain"
x = re.search(r"\bS\w+",txt)
print(x.span())
print(x.group(0))
Q11.
b) Syntax error
print(a/b)
fn(0,0)
Q12.
Q13.
my_fuction("Emili")
my_fuction("Emili","name")
Q14.
d)
if 5>2:
print("Five is greater than two!")
if 5>2:
print("Five is greater than two!")
Q15.
d = max(a,b.c)
AttributeError: 'int' object has no attribute 'c'
def max(x,y,z):
if x>y:
if x>z:
return x
else:
return z
else:
if y>z:
return y
else:
return z
d = max(a,b,c)
print("maximum is",d)