Top Python Questions with Answers
B57. How to work with numbers that are not included in the decimal number system?
In python, you can enter binary, octal and hexadecimal numbers.
Binary. These are numbers made up of 0 and 1. For binary input, the prefix 0b or 0B is used:
>>> int(0b1010)
ten
A number can be converted to binary using the bin() function:
>>>bin(0xf)
'0b1111'
Octal numbers can consist of digits from 0 to 7, and the prefix 0o or 0O is also used:
>>> oct(8)
'0o10'
Hexadecimal numbers can consist of digits from 0 to 15, the prefix 0x or 0X is also used:
>>> hex(15)
'0xf'
B58. What is the result of this code:
>>> def extendList(val, list=[]):
list append(val)
return list
>>> list1 = extendList(10)
>>> list2 = extendList(123,[])
>>> list3 = extendList('a')
>>> list1, list2, list3
[10, 'a'], [123], [10, 'a']
Possible but incorrect answer: ([10], [123], ['a'])
The function has a list=[] argument that does not start at null every time this function is called. The first time we define a function, it creates a new list. Then, every time we call this function without a list argument, it uses the same list. Python evaluates expressions that have null values when defining a function, not when calling the function.
B59. How many arguments can range() take?
One to three:
>>> list(range(5))
[0, 1, 2, 3, 4]
>>> list(range(-5))
[]
>>> list(range(2, 7))
[2, 3, 4, 5, 6]
>>> list(range(-3, 4))
[-3, -2, -1, 0, 1, 2, 3]
>>> list(range(2, 9, 2))
[2, 4, 6, 8]
>>> list(range(9, 2, -1))
[9, 8, 7, 6, 5, 4, 3]
At 60. What such PEP 8?
This is a python programming convention that contains guidelines for improving code readability.
B61. How is Python different from Java?
If we compare Python and Java:
- Java is faster
- Python uses indentation, Java needs parentheses
- Python has dynamic typing, Java has static typing
- Python is simple and concise, and Java is a verbose language
- Python is an interpreted language
- Java is platform independent
- Java has a JDBC interface that improves database access
B62. What is the best way to swap the numeric values of objects?
>>> a, b = b, a
How this code is executed:
>>> a, b = 2, 3
>>> a, b = b, a
>>> a, b
(3, 2)
B63. How can you perform multiple assignment operations in a single clause?
First way (multiple objects with unique values):
>>> a, b, c = 3, 4, 5
Second way (multiple objects with identical values):
>>> a = b = c = 3
B64. How to get out of an endless loop?
You can press the key combination Ctrl + C, which interrupts the execution.
Do'stlaringiz bilan baham: |