Python Q&A

Python Q&A - Part1

1) print(17/4) 

Answer: 4.25

2) print(17//4)

Answer: 4

3) print (17%4)

Answer: 1

4) print((2**3)/2*3-2+2)

Answer: 12

5) print(2*4**2)

Answer: 32

6) str=”This is python test”. print(str)

Answer: “This is python test”

7)  “This is python test”. print(str[5:7])

Answer: is

8) “This is python test”. print(str[-1])

Answer: t

9) “This is python test”. print(str[-6])

Answer: n

10) “This is python test”. print(str[-6:])

Answer: n test

11) lst = [‘a’,1,’b’,2], lst.append(‘c’). 

print(lst)   Answer: [‘a’,1,’b’,2,’c’]

print(lst[1])   Answer: 1

print(lst*2)  Answer: [‘a’, 1, ‘b’, 2, ‘c’, ‘a’, 1, ‘b’, 2, ‘c’]

12) tup = (‘a’,1,’b’,2)

print(tup[1])  Answer: 1

tup[1]=9  Answer: Error

print(tup[1:]) Answer: (1, ‘b’, 2)

print(tup*2)  Answer: (‘a’, 1, ‘b’, 2, ‘a’, 1, ‘b’, 2)

13) print(2%3)

Answer: 2

14) print(1+2%3)

Answer: 3

15) print(1//2%3)

Answer: 0

16) print(int(2.3))

Answer: 2

17) print(2<<3)

Answer: 16

18) print(bin(10))

Answer: 0b1010

19) x=1 and y=3

print(x^y)      Answer: 2
print(x|y)       Answer:
print(x&y)     Answer: 1

20) print(~3)

Answer: -4

21) print(~4)

Answer: -5

22) print(~5)

Answer: -6

23) print(bool())

Answer: false

24) print(bool(‘true’))

Answer: true

25) print(bool(‘false’))

Answer: true

26) print(2>1)

Answer: true

27) print(‘%s %d’ %(“Python test”,1))

Answer: Python test 1

28) What is the length of identifiers

Answer: Identifiers can be of any length

29) print(math.pow(2,3))

Answer: 8.0

30) How do you get today’s date and time?

Answer: 

import datetime

print(datetime.datetime.today())

print(datetime.datetime.now())

31) How do you get today’s date?

Answer:

import datetime

print(datetime.date.today())

32) How do you get UTC time?
Answer:

import datetime

print(datetime.datetime.utcnow())

33) How do you get the current month?

Answer:

import datetime

print(datetime.date.today().month)

34) How do you get the current weekday?

Answer:

import datetime

print(datetime.date.today().weekday())

35) How do you get date difference?

Answer:

import datetime

print(datetime.date.today()-datetime.date(2021,1,1))

36) How do you add 2 days to today?

Answer:

import datetime

print(datetime.date.today()+datetime.timedelta(days=2))

37) How do you subtract 2 days from today

Answer:

import datetime

print(datetime.date.today()+datetime.timedelta(days=-2))

38) What is the length of variable names?

Answer: Variable names can be of any length

39) Can variable names start with number?

Answer: Variable names cannot start with number

40) Is class a system defined data type?

Answer: Class is a user-defined data type

41) Are list and tuple sequences?

Answer: Yes. List and tuple are sequences

42) Are list and tuple ordered collections?

Answer: Yes. List and tuple are ordered collections

43) Are list objects mutable?

Answer: Yes. List objects are mutable

44) Are tuple objects mutable?

Answer: No. Tuple objects are not mutable. They are immutable

45) Are list and tuple indexed?

Answer: Yes. List and tuple are indexed

46) Is dictionary object mutable?

Answer: Yes. Dictionary object is mutable

47) Is dictionary object indexed?

Answer: No. Dictionary object is not indexed