Interview questions / interviews about basic aspects of python programming with answers
B31. What is a dictionary?
The dictionary contains key:value pairs:
>>> roots={25:5, 16:4, 9:3, 4:2, 1:1}
>>>type(roots)
>>> roots[9]
3
Dictionary refers to mutable objects. It can be created with:
- literal (symbols {})
- dict() functions
- generator (comprehension)
B32. Tell us about the arithmetic operators //, %, and **.
The // operator performs integer division and returns the integer part of the result of the operation:
>>> 7 // 2
3
The ** operator raises to a power:
>>> 2**10
1024
The % operator returns the result of modulo division, that is, the remainder after division:
>>> 13%7
6
B33. What do you know about Python comparison operators?
These operators compare values with each other.
Less than (<) operator: If the value on the left side of the operator is less than, it returns True:
>>> "hi"<"Hi"
False
Greater than (>) operator: If the value on the left side of the operator is greater than, it returns True:
>>> 1.1+2.2>3.3
True
Less than or equal to operator (<=): If the value on the left side of the operator is less than or equal to the value on the right side, it returns True:
>>> 3.0 <= 3
True
Greater than or equal to operator (>=): If the value on the left side of the operator is greater than or equal to the value on the right side, it returns True:
>>> True >= False
True
Equality operator (==): if the values are equal, it returns True:
>>> {1,3,2,2} == {1,2,3}
True
Inequality operator (!=): if the values are not equal, it returns True:
>>> False != 0.1
True
B34. What are assignment operators in python?
All arithmetic operators can be combined with the assignment symbol.
>>> a = 7
>>> a += 1
>>> a
eight
>>> a -= 1
>>> a
7
>>> a *= 2
>>> a
fourteen
>>> a /= 2
>>> a
7.0
>>> a **= 2
>>> a
49.0
>>> a //=3
>>> a
16.0
>>> a %= 4
>>> a
0.0
B35. Explain about logical operators in python.
Total them three : and, or, not.
>>> False and True
False
>>> 7 < 7 or True
True
>>> not 2 == 2
False
B36. What is the membership operator?
These are the in and not in operators. They indicate whether one value is part of another.
>>> "me" in "disappointment"
True
>>> "us" not in "disappointment"
True
B37. Tell me about identity operators.
The is and is not operators indicate whether two values are identical.
>>> 10 is "10"
False
>>> True is not False
True
B38. What are bit operators?
These operators perform operations in bit format.
>>> 0b110 & 0b010
2
>>> 3 | 2
3
>>>3^2
one
>>> ~2
-3
>>> 1<<2
four
>>> 4>>2
one
B39. What data types are supported in python?
Python uses five data types:
- numbers that contain numeric values;
- strings, which are a sequence of characters; denoted by single or double quotes.
- lists, which are a collection of values; are denoted by square brackets.
- tuples, which are similar to lists, but differ in that they cannot be changed.
- dictionaries that contain "key: value" pairs; are indicated by curly brackets.
B40. What is a docstring?
It is entered as the first line in the block that defines the content of the function, class or method. Contains a description of their purpose and method of execution. Denoted by three single or double quotes on each side.
>>> def sayhi():
"""
The function prints Hi
"""
print("Hi")
>>> sayhi()
Hi
We can see its contents using __doc__:
>>> sayhi.__doc__
"\n\tThis function prints Hi\n\t"
Unlike a comment, a docstring is read at runtime.
B41. How can you convert a string to a number?
If the string contains only numeric characters, you can use the int() function:
>>> int("227")
227
B42. How can I accept the result of input on the keyboard?
If the user is typing something using the keyboard, the input() function can be used. As an argument, you can give this function the text of the input request. The input result is always a string.
>>> a = input("Enter a number")
Enter a number7
B43. What is a function?
When we want to execute a certain sequence of statements, we can give it a name. For example, let's define a function that takes two numbers and returns the larger one.
>>> def greater(a,b):
return a is a>b else b
>>> greater(3,3.5)
3.5
B44. What is recursion?
This is when the function calls itself. However, it must have a basic condition so as not to create an infinite loop:
>>> def facto(n):
if n == 1: return 1
return n * facto(n - 1)
>>> fact(4)
24
B45. What does the zip() function do?
Returns an iterator with tuples:
>>> list(zip(["a", "b", "c"],[1, 2, 3]))
[("a", 1), ("b", 2), ("c", 3)]
In this case, it combines the elements of two lists and creates tuples from them. Works not only with lists.
B46. How to calculate the length of a string (string)?
To do this, we call the len() function:
>>> len("Ayushi Sharma")
13
B47. Tell us about list comprehension generators.
They allow you to create lists with one line of code:
>>> [i for i in range(1, 11, 2)]
[1, 3, 5, 7, 9]
B48. How can I get all values from a dictionary?
For this, the values() method is used.
>>> 4 in {"a":1,"b":2,"c":3,"d":4}.values()
True
B49. How can I switch the case of a string?
You can use the swapcase() method provided for the str class:
>>> "AyuShi".swapcase()
"aYUsHI"
B50. Take the string "I love Python". Write code that prints characters up to the letter "t".
>>> s = "I love Python"
>>> i = 0
>>> while s[i] != "t":
print(s[i], end="")
i += 1
>>> I love you
At 51. Let 's take the string "I love Python". Write code that will print this string without spaces.
>>> s = "I love Python"
>>> for i in s:
if i == ' ': continue
print(i, end='')
>>>IlovePython
B 52. Let 's take the string "I love Python". Write code that prints this line five times in a row.
>>> s = "I love Python"
>>> for i in range(6):
print(s)
>>> I love Python
>>> I love Python
>>> I love Python
>>> I love Python
>>> I love Python
B53. What is bytes() used for?
This is a built-in python function that returns an immutable byte object.
>>> bytes([2,4,8])
b'\x02\x04\x08′
>>> bytes(5)
b'\x00\x00\x00\x00\x00'
>>> bytes('world','utf-8')
b'world'
B54. What is a control flow statement?
Normally, a python program starts execution at the first line. After it, the program executes each sentence once. When the last sentence is completed, the program ends. Also, sequence control helps to complicate the usual order of program execution.
B55. Create a new sheet by converting a list of numeric strings to a list of numbers.
>>> nums = ['22', '68', '110', '89', '31', '12']
Now let's take the int() function and create a list generator that converts strings to numbers and lists them:
>>> [int(i) for i in nums]
[22, 68, 110, 89, 31, 12]
B56. What is the best way to store the names and surnames of our employees?
You can create a dictionary containing key:value pairs:
{"first name": "Ayushi", "last name": "Sharma"}
2>
Do'stlaringiz bilan baham: |