Beginner Python Practice Question
The best way to learn any programming is by solving different kind of problem .
Here I am providing you a basic python question with answer . If you have completed your basic of python then I am sure that you will solve it and if not then you really require a practice.
Lets Start-
Q1. Find the largest number from the given list, use for loop.
numbers = [12, 75, 150, 180, 145, 525, 50]
Solution-
largest=numbers[0]
for large in numbers:
if large > largest:
largest=large
print(largest)
→ 525
Q2. Print all the even number between (1, 12).
Solution-
for i in range(1,13):
if i%2!=0:
continue
print(i)
→
2
4
6
8
10
12
Q3. Calculate the sum of all numbers from 1 to a given number
Expected Output:Enter number 10Sum is: 55
Solution-
s= 0
n = int(input(“Enter number “))
# run loop n times
# stop: n+1 (because range never include stop number in result)
for i in range(1, n + 1):
# add current number to sum variable
s = s+ iprint(“Sum is: “, s)
Q4. Write a program to display only those numbers from a list that satisfy the following conditions
- The number must be divisible by five
- If the number is greater than 150, then skip it and move to the next number
- If the number is greater than 500, then stop the loop
numbers = [12, 75, 150, 180, 145, 525, 50]
Solution-
for i in numbers:
if i > 500:
break
elif i > 150:
continue
elif i % 5 == 0:
print(i)
→
75
150
145
Q5. Print the given list in reverse order.
list1 = [10, 20, 30, 40, 50]
list1[::-1]
→[50, 40, 30, 20, 10]
I hope you liked it. And if you are not able to get it , its ok below link will help you to get started with python-where I have talked about youtube channels which has really helped me to get started.
Till then Keep learning and Keep growing..