Python Practice Problems For Beginners (Part 1)

 

Python Practice Problems For Beginners-1

Problem Statement:-Write a Python program to compute the future value of a specified principal amount, rate of  interest, and a number of years.

Explanation:- 
Take the input from the user for amount , interest and no of years and store it in the variable accordingly.
Compute the future value using the formula amount*((1+(0.01*interest))**years) and input parameters.

Code:-

#take the input from the user

amount=int(input("Enter the amount: "))

interest=float(input("Enter interest: "))

years=int(input("Enter no of years: "))

# compute the future value using the formula

future_value=amount*((1+(0.01*interest))**years)

#print the value computed

print(future_value)

Output:-


Run the code

                Python Practice Problems For Beginners-2

Problem Statement:-Write a Python program to count the number of even and odd numbers from a series of numbers and append then in the different list.

Explanation:- 

Create an 3 empty list for elements , even elements & odd elements.
Take the of no of elements & elements for the list as an input from the user.
Iterate over all the elements of the list and check of elements are odd or even.
If even then add the elements to even list else add then to the odd list.

Code:-

#create 3 list 

x=[] 

e=[]

o=[]

#intialize variable even & odd to 0 

even=odd=0

#take a input for no of elements

n=int(input("ënter the no elements :"))

# iterate over the loop for n times and store the values in list x

for i in range(n):

    print("enter the number",i)

    x.append(int(input()))

#iterate over the list x 

for n in x:

#check if the element is even, then append the elementto the list e

    if(n%2==0):

       even=even+1

       e.append(n)

#else add the element to odd list 

    else:

        odd=odd+1

        o.append(n)

print("No of Even no in a series are {} and the no's are {}".format(even,e))

print("No of Even no in a series are {} and the no's are {}".format(odd,o))

Output:-

Run the code

 Python Practice Problems For Beginners-3

Problem Statement:-Write a Python program to check a triangle is equilateral, isosceles or scalene.


Code:-

#take the input from the user for the length of the sides

Length= int(input("Enter the Length:"))

Breath= int(input("Enter the height:"))

Height= int(input("Enter the height:"))

#compare of all the sides are same it's a Equilateral triangle

if(Length==Breath)&(Length==Height):

  print("Equilateral triangle")

# compare if any of the sides are same then it's Isosceles triangle

elif(Length==Breath)|(Breath==Height)|(Length==Height):

  print("Isosceles triangle")

#else it's a scalene triangle

else:

    print("scalene triangle")


Output:-

Output:- Type of the Triangle

https://onlinegdb.com/aI954FIXkc

Python Practice Problems For Beginners-4

Problem Statement:- Write a function, add_it_up(), that takes a single integer as input and returns the sum of the integers from zero to the input parameter. 
Code:-

# create a function to add n elements

def add_it_up(No):

#initialize sum to 0

    Sum=0

# iterate over n elements and sum them

    for i in range(No+1):

        Sum=Sum+i

    print("Sum of {} elements is {}".format(No,Sum))

       

#take the input form the user for no of elements

No=int(input("Enter the no of elements to be added: "))

add_it_up(No)

Output:-


Run the code

Python Practice Problems For Beginners-5

Problem Statement:-Write a Python program to calculate a^b or a power b


Code:- 
    def power(num,idx):

    if(idx==1):

       return(num)

    else:

       return(num*power(num,idx-1))

base=int(input("Enter number: "))

exp=int(input("Enter Power: "))

rpow=power(base,exp)

print("{} raised to {}: {}".format(base,exp,rpow))


Output:-


Run the code

Python Practice Problems For Beginners-6

Problem Statement:-Write a Python program find the length of list


Code:- 

l=[12,'new',23,45,'Apple']
#using len Function
print("Length of list",len(l))
count=0

#using iterative Loop 
for i in l:
    count+=1
print("Length of list",count)
    

Output:-



Python Practice Problems For Beginners-7

Problem Statement:-Write a Python Program to calculate Maximum of 2 no’s


Code:-

x,y=12,10
#Using Max Function 
print("Maximum from two No's is ",max(x,y))
#Using If else loop
if(x>y):
    print(x,"is maximum No")
else:
    print(y,"is maximum No")

Output:-


 Python Practice Problems For Beginners-8

Problem Statement:-  Calculate the Volume of the Sphere in Python


Code:-
pi=3.14
volume=1.33*pi*radius*radius*radius
print("Volume of the Sphere:",+(volume))

radius=int(input("Enter the radius: "))

Output:-

Volume of the Sphere


Python Practice Problems For Beginners-9

Problem Statement:- Write a Python program to calculate the Factorial of a no

Code:-


n=int(input("Enter the No: "))

fact=1

for i in range(n):

    fact=fact+fact*i

print("The factorial of {} is {}".format(n,fact))

Output:-



 

Python Practice Problems For Beginners-10

Problem Statement:-Write a python program to calculate the sum of all the elements in tuple

Code:- 

Tuple=(1,2,3,4,5)

total=0

#using sum function

print("Find the sum of tuple elements using sum function",sum(Tuple))

#using for loop

for i in Tuple:

    total+=i

print("Find the sum of tuple elements using for loop",total)

#using range function

s=0

t=len(Tuple)

for i in range(t+1):

    s+=i

print("Find the sum of tuple elements using range function",s)

Output:-


Python Practice Problems For Beginners-11

Problem Statement:-Write a python program to Find the unique elements in the list 

Code:- 

def unique(l):

    x=[]

    for a in l:

        if a not in x:

            x.append(a)

    return(x)



l=[]

n=int(input("enter the  no of elements:-"))

for i in range(n):

    print("enter element:-")

    l.append(int(input()))

print("The elements the list:-",l)

print("Unique Elements in the list:-",unique(l))


Output:-

Python Practice Problems For Beginners-12

Problem Statement:- Write a python program to  find the sum of all the elements 1+2+3..n in the series

Code:- 

def sum(n): 

if n <= 1: 

return n 

return n + sum(n - 1)

n=int(input("Enter the no of elements "))

print("The Sum of n elements 1+2+3....+n",sum(n))

Output:-


Python Practice Problems For Beginners-13

Problem Statement:- Write a python program to find the largest element from the list

Code:- 

def max(l):

    maximum = l[ 0 ]

    for a in l:

        if a > maximum:

            maximum = a

    print("The largest no in the list",maximum)


l= []

elements=0

n = int(input("Enter number of elements : "))

for i in range(0, n):

    print("Enter elements for the list:")

    elements = int(input()) 

    l.append(elements)

print(l)

max(l)

Output:-



Python Practice Problems For Beginners-14

Problem Statement:-Write a python program to Find the Fibonacci series 

Code:-  

a=0

b=1

n=int(input("Enter the no:-"))

if n<0:

    print("Invalid No")

elif n==0:

     print(a)

elif n==1:

    print(b)

else:

    print("Fibonacci Series")

    for i in range (n):

        c=a+b

        print(c)

        a=b

        b=c

Output:-


Python Practice Problems For Beginners-15

Problem Statement:-Write a python program to find the sum of all the elements 1+(1+2)+(1+2+3)+.....(1+2+3...+n)  in the series

Code:-  

 n=int(input("Enter the no of elements in the series:- "))
sum=0
Total=0
for i in range(n+1):
    sum=sum+i
    Total+=sum
print("The sum of Series 1+(1+2)+(1+2+3)+.....(1+2+3...+n) is :-",Total)

Output:-




 

Post a Comment

0 Comments