Python 3 – Extra Exercise 1 (Basics)
Reference:
https://www.w3resource.com/python-exercises/python-basic-exercises.php
https://www.w3resource.com/python-exercises/basic/
1. Write a Python program which accepts the radius of a circle from the user and
compute the area.
Sample Output
Input the radius of the circle: 1.1
The area of the circle with radius 1.1 is: 3.8013271108436504
Solution
from math import pi
r = float(input ("Input the radius of the circle : "))
print ("The area of the circle with radius " + str(r) + " is: " + str(pi * r**2))
https://www.w3resource.com/python-exercises/python-basic-exercise-4.php
2. Write a Python program to display the first and last colors from the following list.
color_list = ["Red","Green","White" ,"Black"]
Sample Output
Red Black
Solution
color_list = ["Red","Green","White" ,"Black"]
print( "%s %s"%(color_list[0],color_list[-1]))
https://www.w3resource.com/python-exercises/python-basic-exercise-8.php
3. Write a Python function to find the maximum and minimum numbers from a
sequence of numbers. Go to the editor
* Note: Do not use built-in functions.
def max_min(data):
#to be completed
print(max_min([0, 10, 15, 40, -5, 42, 17, 28, 75]))
Sample Output
(75, -5)
Solution
def max_min(data:List[int]) -> Tuple[int,int]:
l = data[0]
s = data[0]
for num in data:
if num > l:
l = num
elif num < s:
s = num
return l, s
print(max_min([0, 10, 15, 40, -5, 42, 17, 28, 75]))
https://www.w3resource.com/python-exercises/python-basic-exercise-148.php
4. Write a Python program to cut out words of 3 to 6 characters length from a given
sentence not more than 1024 characters.
Sample Input
English sentences consisting of delimiters and alphanumeric characters are given
on one line
Sample Output
Input a sentence (1024 characters. max.)
This is a quick checking of the program
3 to 6 characters’ length of words:
This quick the
Solution
print("Input a sentence (1024 characters. max.)")
yy = input()
yy = yy.replace(",", " ")
yy = yy.replace(".", " ")
print("3 to 6 characters length of words:")
print(*[y for y in yy.split() if 3 <= len(y) <= 6])
https://www.w3resource.com/python-exercises/basic/python-basic-1-exercise-
60.php
Further explanation about the usage of ‘*’ for List comprehension:
5. if you draw a straight line on a plane, the plane is divided into two regions. For
example, if you pull two straight lines in parallel, you get three areas, and if you
draw vertically one to the other you get 4 areas.
Write a Python program to create maximum number of regions obtained by
drawing n given straight lines.
Sample Input
5
Sample Output
Input number of straight lines (o to exit):
5
Number of regions:
16
Solution
while True:
print("Input number of straight lines (o to exit): ")
n=int(input())
if n<=0:
break
print("Number of regions:")
print((n*n+n+2)//2)
https://www.w3resource.com/python-exercises/basic/python-basic-1-exercise-
54.php
6. Write a Python function that takes a sequence of numbers and determines if all
the numbers are different from each other.
def test_distinct(data):
#to be completed
print(test_distinct([1,5,7,9]))
print(test_distinct([2,4,5,5,7,9]))
Hint
set() creates a set object. The items in a set list are unordered, so it will appear in
random order and no duplicates, and it returns:
> an empty set if no parameters are passed
Solution
def test_distinct(data: List[int]) -> bool:
if len(data) == len(set(data)):
return True
else:
return False
print(test_distinct([1,5,7,9]))
print(test_distinct([2,4,5,5,7,9]))
https://www.w3resource.com/python-exercises/basic/python-basic-1-exercise-
1.php
7. Write a Python program to find the number of notes (Sample of notes: 10, 20, 50,
100, 200 and 500 ) against an given amount.
Range - Number of notes(n) : n (1 ≤ n ≤ 1000000).
Program
def no_notes(a):
#to be completed
print(no_notes(880))
print(no_notes(1000))
Sample Output
6
2
Reminder
quotient = 7//2
quotient2 = int(7//2)
# the value of quotient is 3, even though the result of the division here is 3.5
quotient3 = 7/2
# the value of quotient is 3.5
Solution
def no_notes(a: int) -> int:
Q = [500, 200, 100, 50, 20, 10]
x=0
for i in range(6):
q = Q[i]
x += int(a / q)
a = int(a % q)
if a > 0:
x = -1
return x
print(no_notes(880))
print(no_notes(1000))
https://www.w3resource.com/python-exercises/basic/python-basic-1-exercise-
21.php
8. Write a Python program to compute the digit number of sum of two given
integers.
Each test case consists of two non-negative integers x and y which are separated
by a space in a line.
0 ≤ x, y ≤ 1,000,000
Program
print("Input two integers(a b): ")
#to be completed
Sample Input
57
Sample Output
Input two integers(a b):
57
Number of digit of a and b.:
2
Solution
print("Input two integers(a b): ")
a,b = map(int,input().split(" "))
print("Number of digit of a and b.:")
print(len(str(a+b)))
https://www.w3resource.com/python-exercises/basic/python-basic-1-exercise-
33.php