Python Q&A
Python Q&A - Part2
48) Print PCAP-31-01. You have to use 15*2+1 for 31
Answer: print(“PCAP”+”-“+str(2*15+1)+”-“+”01”)
49) x=”2021-01-01“. Find the data type of x
Answer: print(type(x))
50) What is the output of the following?
x=0
while x<=5:
x=x+1
if x==3:
continue
print(x)
Answer: 1,2,4,5,6
51) What is the output of the following?
def fn(x):
return x//4
print(fn(87) % fn(4)*5+6)
Answer: 6
52) print(21%4*3)
Answer: 3
53) print(21*4%2)
Answer: 0
54) print(4%2*21)
Answer: 0
55) for x in range(0):
print(x)
Answer: Nothing is printed
56) What are membership operators?
Answer: They are used to test if a sequence is present
57) Which are the membership operators?
Answer: “In” and “not in” are membership operators
58) str=[‘a’,’b’]
print(‘a’ in str)
Answer: true
print(‘c’ in str)
Answer: false
59) print(hex(10))
Answer: 0xa
60) print(oct(10))
Answer: 0o12
61) print(bin(10))
Answer: 0b1010
62) When is “pass” statement used?
Answer: When the code requires no action
63) Why is range() function used?
Answer: Used to iterate over a sequence of numbers
64) Write the code to print even numbers between 1 and 10 including 10.
Answer:
for x in range(1,11):
if x%2==0:
print(x,”even”)
continue
pass
65) x=3 and y=’5′
print(x*y)
Answer: 555
print(x*int(y))
Answer: 15
66) How do you find 2 cubed 3
Answer: print(2**3)
67) How do you print the time zone name?
Answer:
from datetime import datetime, timezone
dt = datetime(2021, 1, 1, 11, 00, tzinfo=timezone.utc)
dt1=dt.astimezone()
dt2=dt1.tzname()
print(dt2)
68) What symbol is used to comment a line?
Answer: #
69) How do you block comment multiple lines
You can block comment by placing the lines in between pair of triple quotes(“””)
70) What braces tuples use?
Answer: ( )
71) What braces lists use?
Answer: [ ]
72) What is the keyword to check data type?
Answer: type()
73) Which collections allow duplicates?
Answer: List and tuple allow duplicates
74) Which collections do not allow duplicates?
Answer: Sets and dictionary do not allow duplicates
75) Which collection is not ordered?
Answer: Set is not ordered
76) Which collection is not indexed?
Answer: Set is not indexed
77) How many collection data types are there?
Answer: There are 4 collection data types
78) What are the collection data types?
Answer: Lists, tuples, Sets and dictionaries
79) print(bool(0))
Answer: false
80) str1=[‘a’,’b’] and str2=[‘a’,’b’]
print(str1 is str2)
Answer: false
81) t=(“a”). print(type(t))
Answer: <class ‘str’>
82) t=(“a”,). print(type(t))
Answer: <class ‘tuple’>
83) print(ord(‘a’))
Answer: 97
84) print(chr(99))
Answer: c
85) How do you open a file in python?
Answer: Using open function
86) What are the parameters of open function?
Answer: Filename and mode
87) What are the file open modes?
Answer: read, write, append and create
88) What are the abbreviations for open modes?
Answer: ‘r’ for read; ‘w’ for write; ‘a’ for append and ‘x’ to create
89) import time
print(time.strftime(“%H %M %S”))
Answer: 17 34 34
print(time.strftime(“%m %d %y %H %M %S”))
Answer: 05 31 21 17 34 34
print(time.strftime(“%a”))
Answer: Mon
print(time.strftime(“%A”))
Answer: Monday
print(time.strftime(“%w”))
Answer: 1
Explanation: weekday is 1 is for Monday
print(time.strftime(“%Z”))
Answer: Indian Standard time
90) What is strftime()?
Answer: Returns string representing date and time
91) What is strptime()?
Answer: It parses string representing time
92) Which is newline character?
Answer: n