The best way we learn anything is by practice and exercise solutions. This post is for those (beginner to intermediate) who are familiar with Python.
Hope, these exercises help you to improve your Python coding skills. We will add more Python coding problems and solutions for beginners in the next post... Happy Coding!
1. Average of numbers
Take numbers from a user and show the average of the numbers the user entered.
Hints
To solve this problem.
First, ask the user - How many numbers you want to enter?
Then, run a for-loop. Each time, take input from the user and put it in a list.
Once you get all the numbers, you can send the list to the sum function. The sum function will add all the numbers and give you the total.
Finally, divide the total by the number of elements the user entered.
That’s it, you will get the answer.
Solution
len = int(input("How many numbers do you want to enter? "))nums = [ ]for i in range(0, len):element = int(input("Enter element: "))nums.append(element)total = sum(nums)avg = total/lenprint("Average of elements you entered",round(avg,2))
Explanation
First, ask the user how many numbers he/she wants to enter. Once we have the number, run a for loop. To collect the numbers.
While collecting the numbers, we are adding those in the list called nums.
Then we pass the list to the sum function. The sum function returns us the sum of each number in the list
Eventually, we divide the total by the number of elements to get the average.
2. Max of Three
The problem
Find the largest of the three numbers.
Hints
Ask the user to enter three numbers.
Then, you can run multiple comparisons to compare which one is the largest.
At first, you can consider that the first number is the largest.
Then compare the second number with the first number and the third number. If the second number is greater or equal to the first number and the second number is greater or equal to the third number, then the second number is the largest.
Similarly, compare the third number with the first or second number.
Otherwise, the first number will be the largest.
Think about it. And try yourself first.
Solution
num1 = int(input("First number: "))num2 = int(input("Second number: "))num3 = int(input("Third number: "))largest = num1if (num2 >= num1) and (num2 >= num3):largest = num2elif (num3 >= num1) and (num3 >= num2):largest = num3else:largest = num1print("Largest number you entered is: ",largest)
Shortcut
num1 = int(input("First number: "))num2 = int(input("Second number: "))num3 = int(input("Third number: "))largest = max(num1, num2, num3)print("Largest number you entered is: ",largest)
The problem
For a given list, get the sum of each number in the list
Hints
Should be simple for you. Declare a sum variable. Then just loop through the list and add it to the sum.
Solution
def get_sum(nums):sum = 0for num in nums:sum = sum + numreturn sumnums = [13,89,65,42,12,11,56]total = get_sum(nums)print("The total of each elements:",total)
It’s super simple.
You got a list. Loop through the list. You have done that multiple times while learning Fundamentals.
Declare a variable named sum before the loop. And inside the loop, add the number to the sum variable.
And then finally return the sum.
The problem
Take a number as input. Then get the sum of the numbers. If the number is n. Then get
0^2+1^2+2^2+3^2+4^2+.............+n^2
Hints
Once again, run a for loop with a range. Inside the loop, use the power of 2. Then add that power to a sum variable. That’s it.
Solution
Take a number as input. Then get the sum of the numbers. If the number is n. Then get
0^2+1^2+2^2+3^2+4^2+.............+n^2
Hints
Once again, run a for loop with a range. Inside the loop, use the power of 2. Then add that power to a sum variable. That’s it.
Solution
def square_sum(num) :sum = 0for i in range(num+1) :square = (i ** 2)sum = sum + squarereturn sumnum = int(input('Enter a number: '))sum = square_sum(num)print('sum of square numbers is ', sum)
Explanation
This one is super easy. You declared a variable sum with an initial value 0.
Then you run a for loop. This loop will run for range (num +1).
The reason we are running it until num + 1. Because, we know that the range will stop just before the number inside it.
For example, if we want to run the loop including 10. We should write 11 inside the range function.
Shortcut
Sometimes there could be a better and easier way to solve a problem. For example, there is a simple math formula to calculate the sum of the square.
Sometimes there could be a better and easier way to solve a problem. For example, there is a simple math formula to calculate the sum of the square.
This is the reason, you should search online and learn from different sources. This will make you stronger and better.
If you want to calculate the sum of the square of n numbers, the formula is-
n*(n+1)(2*n+1)/6
Now you can use this formula.
def sum_of_square2(n):sum = n*(n+1)*(2*n+1)/6return sumnum = int(input('Enter a number: '))sum = sum_of_square2(num)print('Your sum of Square is: ', sum)
The problem
For a given number, find all the numbers smaller than the number. Numbers should be divisible by 3 and also by 5.
Hints
So, you have to check two conditions: make sure the number is divisible by 3, and also by 5. Hence, you will need to use two conditions.
So, you have to check two conditions: make sure the number is divisible by 3, and also by 5. Hence, you will need to use two conditions.
Solutions
def divisible_by_3and5(num):result = [ ]for i in range(num):if i%3 == 0 and i%5 == 0:result.append(i)return resultnum = int (input('Enter your number: '))result = divisible_by_3and5(num)print(result)
This one is easy. We took an input number from the user, and then pass it to a function.
In the function, we have a list and we ran a loop. This loop runs a range. Here, num is the number that the user entered.
Inside the loop, we have an if block. In the if block, we have two conditions. One condition says i % 3 ==0
This means if you divide i by 3 and there will not be any remainder. If there is no remainder then the number is divisible by 3.
Similarly, i%5==0 means the number is divisible by 5.
As we have and between both conditions, to go, insider, the if-block, the i has to be divisible by 3 and also has to be divisible by 5.
Inside the if block, we are appending the number in the result list. And then return the list.
That’s how we are getting every number divisible by 3 and 5.
Here we are focusing on thinking and methods to solve a problem. However, every problem could be solved in multiple ways. And other solutions could be better and faster.
Always keep an open mind to find out multiple solutions to solve a problem.
Source : ProgrammingHero1
Write your opinion