Note: This will contain a mix of problems I have created with the problems on platform such as HackerRank. If you do not have an account, I would recommend making one.
-
What is the difference between a list, tuple, and set in Python? Think of an example where you might use each of these.
-
Look at the following program. What does this program print?
temp_list = [] for item in range(25): if item % 2 == 0: temp_list.append(item) print(temp_list)
-
The code snippets below have bugs in them. You task is to debug, find, and fix the bug. Debugging is one of the most essential skills for a programmer. If you cannot find the bugs just looking at the code below, run the programs in a python environment (script/shell) and find the bugs.
-
Program 1: The program below needs to check the letter stored in the list
letter_list
match the letters stored in the string variableletter_str
. It should printTrue
for every matching letter, andFalse
if any letter do not match.Example1:
letter_list = ['H', 'E', 'L', 'P']
,letter_str = 'HELP'
. This printsTrue, True, True, True
.Example2:
letter_list = ['h', 'i']
,letter_str = 'hy'
. This printsTrue, False
.""" Find the bug and fix it """ letter_list = ['P', 'Y', 'T', 'H'] letter_str = 'PYTH' if letter_list == letter_str: print('True') else: print('False')
-
Program 2: The program needs to update the
lang
data type with few more programming languagesJava, JavaScript, Kotlin
.lang = ('Python', 'C') to_be_added = ('Java', 'JavaScript', 'Kotlin') for item in to_be_added: # this loops through to_be_added -> Java -> JavaScript -> Kotlin lang.append(item) print(lang)
-
Program 3: There is a bug in the program below. Find it and think about the reason the program does not work.
my_set = set() my_set.update([2, 3, 4, 9]) # this appends 2, 3, 4, 9 to my_set. my_set[2] = 7
-
Program 4: The program needs to count the number of times a number appears in a given list. Any decimal, integer or complex number inside a string parenthesis is also considered a number for the sake of this problem.
'3', '3.14'
are also numbers. Our program does not work properly. Find the bug and fix it!""" Find and fix the bug! """ my_list = ['P', 't', 2, 'h', 92, '8', '2.345', 'hi'] count = 0 for item in my_list: if isinstance(item, int): count += 1 print(count)
-
-
This is a Hackerrank problem: Tuples
-
This is a Hackerrank problem: Lists
-
This is a Hackerrank problem: Sets
-
This is a Hackerrank problem: Set .add
-
What is tuple unpacking?
-
Examine the code below and write what the print function will print:
new_tup = (2, 3, 6, 12, 99) x, *y, z = new_tup print(x) print(z)
-
What is wrong with the following attempt in initializing a tuple with a single element. How would you fix it?
new_tup = ('python')