Question 1:
The function that converts an string into an integer is int(string_name,[base]). The example program is
string_var = "123"
print(int(string_var)) #String_var in base10 int =123
print(int(string_var,base = 8)) #String_var in base8 = 83
Question 2:
The function that returns an random item from list is random.choice(List_name). The example program is
import random
L = [1,2,3,4,5]
print(random.choice(L)) #Returns random item from the list L
Question 3:
The module that supports regular expressions in Python is re
Question 4:
The program that doubles all the elements in the list is
L = [10,20,30]
double_list =[x*2 for x in L]
print(double_list) #[20, 40, 60]
Question 5:
The output of print("xyz QWE".capitalize()) is Xyz qwe
Note: capitalize() makes the first letter of the string as capital and rest as small letters
Question 6:
The output of the
try:
if '1' != 1:
raise "Error"
else:
print("No Error")
except "Error":
print("Error")
is
Error as the exceptions must derive from BaseException
Question 7:
The value of a after the execution of below program:
a = 50
def fun(a):
print("a is ",a)
a=2
print("Changed local a to ",a)
fun(a)
print("a is still",a)
Is 50
Question 8:
The output of print("xyzqwe".center(7,'1')) is 1xyzqwe
Note: center() function in Python fills the ‘1’ to make the length of the string as 7
Question 9:
The output of the print(sum(map(lambda x: x*2,range(1,11)))) is 110
Note: map() forms a list of integers from 1-10 each being multiplied by 2. The sum of all those elements is 110
Question 10:
The output of program
i = 5
while True:
if i %0O11 == 0:
break
print(i)
i+=1
5
6
7
8
Note: The value of 0O11 is 9. The number 0O11 is in octal system.
No comments:
Post a Comment