Python Practice Problems For Beginners (Part 2)

Python Practice Problems For Beginners-16

Problem Statement:-Write a python program to count the no of times the word occurred in a sentence.

Code:- 

x=input("Enter the sentence:- ")
c={}
n=x.split()
for i in n:
    if i not in c:
        c[i]=0
    c[i]+=1
print(c)

Output:-


Python Practice Problems For Beginners-17

Problem Statement:-Write a python program to sort dictionary in Ascending & descending order

Code:- 

dic = {"Apple":12,"Mango":4,"Grapes":3,"Pear":1,"Cherry":15}

print('Original dictionary : ',dic)

sorted_dic = dict(sorted(dic.items()))

print('Dictionary in ascending order by value : ',sorted_dic)

sorted_dic = dict(sorted(dic.items(),reverse=True))

print('Dictionary in descending order by value : ',sorted_dic)


Python Practice Problems For Beginners-18

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

Code:- 

li=[]
for i in range (0,3):
    ele=int(input("Enter the {} Elements:- ".format(i)))
    li.append(ele)
    
print(li)
li.sort()
print("Second Largest",li[1])

Output:-


Python Practice Problems For Beginners-19

Problem Statement:-Write a python program to check whether the string is Palindrome.

Code:- 

a=input("Enter a string:- ")
b=a[::-1]
if a==b:
    print("The given string is a Palindrome")
else:
    print("The given string is not a Palindrome")


Output:-




Python Practice Problems For Beginners-20

Problem Statement:-Write a python program to print n random no's in a given range

Print the x random in the given range 
range:- 0<n<10^6

Test case 1
input: 5
output:
12345
345678
45673
1234
3456

Test case 2
input:7
output:
123
12345
23456
34567
98765
120987
123456

Code:- 

import random
# x will store no of random no's to generate
x=int(input("no of random no's:- "))
for i in range (x):
    # random no generated in the given range would be stored in n
    n = random.randint(0,10**6)
    print(n)

Output:-




Python Practice Problems For Beginners-21

Problem Statement:-Write a python program for Multiplication of two numbers using type casting

Given two integer numbers N1 and N2, find the multiplication of those and store result in long type number R.

 

The code is already provided to read N1 and N2 integer numbers and to print result value R.

 

Hint: use type-casting.

 

Input

    5

    10

 

Output

    50

Code:- 

N1=int(input())

N2=int(input())

R=N1*N2

print(R)

Python Practice Problems For Beginners-22

Problem Statement:-Given an integer and floating point number, find their division and print the result up to 3 places after the decimal point. 

Input:

    6

    2.5

    

    where:

  • First line represents the integer value.
  • Second line represents the floating value.

Output:

    2.400

 

Explanation: Output displays the division of the integer and float number, and precision is up to 3 places.


Code:-

N1=int(input())

N2=float(input())

R=N1/N2

print ('%.3f'%R)

Python Practice Problems For Beginners-23

Problem Statement:-Write a python program for Division of two integers to be performed as a floating-point operation. 

Given two integers N1 and N2, perform their division as a floating-point operation.

 

Input:

    17

    5

    

    where:

  • First line represents the first integer N1.
  • Second line represents the second integer N2.

 

Output:

    3.400000

 

Explanation: Output displays the exact division of the given two integers as a floating point number.


Code:-


N1=int(input())

N2=int(input())

R=N1/N2

print ('%f'%R)


Python Practice Problems For Beginners-24

Problem Statement:-Write a python program to Check whether 2 strings are same or not 

Check whether 2 strings are same or not

  • First line represents the first string.
  • Second line represents the second string.
  • Here both strings are not equal, hence the output "No".

Given two strings as input, check whether they are equal or not. Display "Yes" if they are equal, otherwise "No".

 

Input:

    google

    google is search engine

 

    where:

 

Output:

    No

 

Explanation:

  • Here both strings are not equal, hence the output "No".

Code:- 

N1=input()
N2=input()
if(N1==N2):
    print("Yes")
else:
    print("No")

Python Practice Problems For Beginners-25

Problem Statement:Write a python program to Check whether a number is divisible by 5 and 11 or not

Given an integer N as input, check whether N is divisible by 5 and 11 or not. Display "Yes" if is divisible by 5 and 11, otherwise "No".

 

Input:

    55

 

    where:

  • First line represents the integer N.
  • 55 is divisible by both 5 and 11, hence the output "Yes".

 

    Output:

        Yes



    Explanation:

    • 55 is divisible by both 5 and 11, hence the output "Yes".

    Code:-

    N=int(input())
    if (N%5==0 and N%11==0):
        print("Yes")
    else:
        print("No")


    Python Practice Problems For Beginners-26

    Problem Statement:Write a python program to Check whether entered character is alphabet or not, If yes check whether uppercase or lowercase

    Given a character as an input, check whether it is an alphabet or not, if yes then find whether it is uppercase or lowercase if uppercase Display 'U' and if lowercase display 'L'.

    In case if it is not an alphabet display '-1'.

     

    Input:

        a

        

        where:

    • Input is a character.

     

    Output:

        L

     

    Explanation:

    • Input character is a lowercase alphabet 'a', hence the output 'L'.

     

    Code:-


    C=input()

    if (C.isalpha()):

        if (C.isupper()):

            print('U')

        else:

            print('L')

    else:

        print(-1)


    Python Practice Problems For Beginners-27

    Problem Statement:-  Write a python program for Menu-Driven code to compute the area of the chosen geometrical shape.

    Given an integer as an input, decides the geometrical figure for which the area has to be calculated, e.g. N=1 for circle, N=2 for rectangle, and N=3 for triangle.

    Calculate and display the area of the respective figure.

     

    Input:

        1

        5

        

        where:

    • First line represents the value of N, which represents the choice of the shape selected.
    • Second line represents the dimension required by the chosen figure, here it is circle so only radius of circle is required.

     

    Output:

        78.500000

     

    Explanation: Output displays the area of the chosen geometrical figure.

     

    Note:

    • There are 3 available figures, that are circle, rectangle and triangle.
    • Use the PI value as "3.14".

    Code:-

    import math
    N=int(input())
    pi = 3.14
    if (N==1):
        R=int(input())
        A=pi*R*R
        print ('%f'%A)
    elif(N==2):
        L=int(input())
        B=int(input())
        A=L*B
        print ('%f'%A)
    elif(N==3):
        B=int(input())
        H=int(input())
        A=0.5*B*H
        print ('%f'%A)
    else:
        print("Inavlid")

    Python Practice Problems For Beginners-28

    Problem Statement: Write a python program to find a number is divisible by 2, 3 or 6 using Switch Statement. 

    Given an integer N, find a number is divisible by 2, 3 or 6 using Switch Statement.

     

    Write a function solution that accepts an integer number N and returns a largest number among 2, 3 and 6 by which N is divisible. If a number is not divisible by 2,3 or 6 then return -1. 

    Use switch statement.


    Input 
        4 

     

    Output 
        2 

     

    4 is divisible by 2. Not by 3 and 6.


    Code:-


    def solution(N):

        if N % 2 ==0 and N % 3 ==0: return 6

        elif N % 2 ==0: return 2

        elif N % 3==0: return 3

        return -1

        

    N = int(input())

    print(solution(N),end='')


    Python Practice Problems For Beginners-29

    Problem Statement:Write a python program to find sum of first and last digit of a number x

    Given numbers N, find sum of first and last digit of a number N using loop

    Input:

        12345

    • First line represents value of N.

    Output:-

        6
    Assumptions:
    • N can be in the range 10 to 100000.
    • Length of digit is greater than 1.

    Code:-

    n=int(input())
    #ld is last digit & fd is a first digit
    ld=n%10
    while(n >10):
        n=n/10
    fd=int(n)
    print(fd+ld)

    Python Practice Problems For Beginners-30

    Problem Statement:Write a python program to display all the leap years in a specific range

    Given a year N, display all the leap years from 1 to N.

     

    Input:
        50

     

        where:

    • First line represents the value of N.

     

    Output:
        4 8 12 16 20 24 28 32 36 40 44 48

     

    Assumptions:

    • N can be in the range 0 to 3000.

    Code:

    # Write your code here...
    n=int(input())
    for i in range(1,n+1):
        if i % 4 == 0 and i != 100: 
            print(i,end=" ")

    Post a Comment

    0 Comments