A set of practice questions that a beginner of Python should be able to answer quite quickly if he/she has grasped the subject. Practice can help and one should not just learn it by heart.
- x = 3
y = x + 3
y = int(str(y) + "2")
print(y)
Output - 62
- x = 3
num = 17
print(num % x)
Output - 2
- list = [1, 1, 2, 3, 5, 8, 16]
print(list[list[4]])
Output - 8
A bit confusing for beginners but it is easy to grasp and do try substituting various numbers and practice it.
- letters = ['x', 'y', 'z']
letters.insert(1, 'w')
print(letters[2])
Output - y
- def print_nums(x):
for i in range(x):
print(i)
return
print_nums(10)
Output - 0
- def func(x):
res = 0
for i in range(x):
res += i
return res
print(func(4))
Output - 6
- try:
print(1)
assert 2 + 2 == 5
except AssertionError:
print(3)
except:
print(4)
Output - 3
- nums = (55, 44, 33, 22, 11)
print(max(min(nums[:2]), abs(-42)))
Output - 44
Questions are from the Python learning app - Solo learn.
How come this outputs with 0?
def print_nums(x):
for i in range(x):
print(i)
return
print_nums(10)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
That depends on the indentation:-)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
The indentation went wrong because of improper markdown
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit