10 Python Code Challenges for Beginners

Zohaib Ahmed | Kaggle Master
5 min readDec 17, 2022

One of the best ways to test and practice your skills with Python is by solving coding challenges. You can learn much from books and online courses, but coding isn’t an armchair activity. You have to write some code to make genuine progress.

Coding challenges are perfect for this. Coding challenges are minor problems you can solve with code. Just because they’re small doesn’t mean they won’t put your knowledge to the test. Each bite-size challenge will focus on skills that you’ll use later working on complete software projects.

1. Convert radians into degrees

Write a function in Python that accepts one numeric parameter. This parameter will be the measure of an angle in radians. The function should convert the radians into degrees and then return that value.

While you might find a Python library to do this for you, you should write the function yourself. One hint you get is that you’ll need to use Pi in order to solve this problem. You can import the value for Pi from Python’s math module.

from math import pi
def radian_degree(value):
radian_to_degree = value * 180 / pi
return radian_to_degree
radian_degree(1)

Output: 57.29577951308232

The conversion of the measure of an angle from radians to degrees can be done using the following formula: Angle in Radians × 180°/π = Angle in Degrees. For example, consider an angle π/9 rad. Now, using the radians to degrees formula, we have π/9 rad × 180°/π = (Angle in Degrees).

2. Sort a list

Create a function in Python that accepts two parameters. The first will be a list of numbers. The second parameter will be a string that can be one of the following values: asc, desc, and none.

If the second parameter is “asc,” then the function should return a list with the numbers in ascending order. If it’s “desc,” then the list should be in descending order, and if it’s “none,” it should return the original list unaltered.

def sort_func(values,key):
asc = "asc"
desc = "desc"
non = "none"
if key == asc:
values.sort()
new_val = values
elif key == desc:
values.sort(reverse=True)
new_val = values
elif key == non:
new_val = values
else:
print("choose your option. asc/desc/none take your list back")
return values
return new_val

sort_func([9,45,76,34,78,32,0,81,2,4,5,9],"desc")

Output : [81, 78, 76, 45, 34, 32, 9, 9, 5, 4, 2, 0]

3. Convert a decimal number into binary

Write a function in Python that accepts a decimal number and returns the equivalent binary number. To make this simple, the decimal number will always be less than 1,024, so the binary number returned will always be less than ten digits long.

Method #1

# Function to convert decimal number
# to binary using recursion
def DecimalToBinary(num):

if num >= 1:
DecimalToBinary(num // 2)
print(num % 2, end = '')


# decimal value
dec_val = 8

# Calling function
DecimalToBinary(dec_val)

Output: 01000

Method #2 using in-built function

# Python program to convert decimal to binary

# Function to convert Decimal number
# to Binary number
def decimalToBinary(n):
return bin(n).replace("0b", "")

print(decimalToBinary(8))
print(decimalToBinary(18))
print(decimalToBinary(7))

Output:

1000
10010
111

4. Count the vowels in a string

Create a function in Python that accepts a single word and returns the number of vowels in that word. In this function, only a, e, i, o, and u will be counted as vowels — not y.

def fun(word):
lis = []
vow = ['a','e','i','o','u']
for c in word:
for v in vow:
if c == v:
if c not in lis:
lis.append(c)
print("Total unique vowels : ",len(lis))
return lis

name = "nameioeeeeeee"
print(fun(name))

Output:

Total unique vowels: 4
[‘a’, ‘e’, ‘i’, ‘o’]

5. Hide the credit card number

Write a function in Python that accepts a credit card number. It should return a string where all the characters are hidden with an asterisk except the last four. For example, if the function gets sent “4444444444444444”, then it should return “4444”.

def take_card(card):
# if credit card its lenth must be sixteen . we consider like that
if len(card) == 16:
last_code = card[-4:]
print("************",last_code)
else:
print("Invelid card number")
take_card("0123457891012734")

Output: ************ 2734

6. Are the Xs equal to the Os?

Create a Python function that accepts a string. This function should count the number of Xs and the number of Os in the string. It should then return a boolean value of either True or False.

If the count of Xs and Os are equal, then the function should return True. If the count isn’t the same, it should return False. If there are no Xs or Os in the string, it should also return True because 0 equals 0. The string can contain any type and number of characters.

def xo(val):
x = 0
o = 0
for i in val:
if i == "x":
x += 1
elif i == "o":
o += 1
else:
continue
if x == o:
final = True
else:
final = False
return final

# calling function
xo("xxoo")

Output: True

7. Create a calculator function

Write a Python function that accepts three parameters. The first parameter is an integer. The second is one of the following mathematical operators: +, -, /, or . The third parameter will also be an integer.

The function should perform a calculation and return the results. For example, if the function is passed 6 and 4, it should return 24.

# Program make a simple calculator

# This function adds two numbers
def add(x, y):
return x + y

# This function subtracts two numbers
def subtract(x, y):
return x - y

# This function multiplies two numbers
def multiply(x, y):
return x * y

# This function divides two numbers
def divide(x, y):
return x / y

multiply(6, 4)

Output: 24

8. Give me the discount

Create a function in Python that accepts two parameters. The first should be the full price of an item as an integer. The second should be the discount percentage as an integer.

The function should return the price of the item after the discount has been applied. For example, if the price is 100 and the discount is 20, the function should return 80.

def discount(actual_price,discount_per):
discount_price = (actual_price/100)*discount_per
return actual_price - discount_price

discount(100,20)

Output: 80.0

9. Just the numbers

Write a function in Python that accepts a list of any length that contains a mix of non-negative integers and strings. The function should return a list with only the integers in the original list in the same order.

def take_list(value):
lst = []
for i in value:
if type(i) == int:
lst.append(i)
return lst
lis = [1,2,3,"ahd","dhd",34,54,"dj"]
take_list(lis)

Output: [1, 2, 3, 34, 54]

10. Repeat the characters

Create a Python function that accepts a string. The function should return a string, with each character in the original string doubled. If you send the function “now” as a parameter, it should return “nnooww,” and if you send “123a!”, it should return “112233aa!!”.

def str_double(value):
new = ''
for i in value:
new = new + i + i
return new
str_double('Dataerms')

Output: ‘DDaattaaeerrmmss’

If you like these Python coding challenges and want to try your hand at solving more, follow for more challenges. thanks

--

--

Zohaib Ahmed | Kaggle Master

Kaggle Master - Highly interested in data science and machine learning.