Python practice tests

in coding •  21 days ago 

python.JPG

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.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

How come this outputs with 0?

def print_nums(x):
for i in range(x):
print(i)
return
print_nums(10)

return is a construction used in the code block of a function definition + after the return instruction is inserted the code block will stop executing anything so :

def print_nums(x):
for i in range(x):
print(i)
return
print_nums(10)

Program takes 0, verifies that is in range of 10 and prints 0
Because of the return statement program stops executing the code

If return is deleted from code the output will be
0
1
2
3
4
5
6
7
8
9

That depends on the indentation:-)

The indentation went wrong because of improper markdown