Sunday, October 28, 2018

Python Ninja Bootcamp 25-Q&A Third

Learn Python like a Professional! Take You from 0 to Hero



In this Python lecture, we will learn about the Q&A Third.


Answer the following Question-


Q. Write a function to find the Area of a square if diagonal d is passed as a parameter

Hint: Area = (Diagonal)*(Diagonal)/2

def area_square(d):
     return d*d/2
area_square(2)
>>2.0

Q.Create a lambda expression for given program


area_square1= lambda d:d*d/2
area_square1(4)
>> 2.0

Q. Write a function that accepts the length of the sides of the triangle and finds whether it is equilateral, isosceles or right angle triangle.


def triangle(a,b,c):
    if a==b and b==c:
        print('Equilateral Triangle')
    elif a==b or b==c or c==a:
        print('Isosceles Triangle')
    elif (a**2==(b**2 + c**2))or(b**2==(c**2 + a**2))or(c**2==(a**2 + b**2)):
        print('Right angel Triangle')
        
triangle(5,4,3)
>>Right Angel Triangle

Q. Write a function that accepts a string s and calculates the number of uppercase, lowercase, vowel, consonant & digits.


def up_lo_vo_co(strings):
    
    d={'uppercase':0,'lowercase':0,'vowel':0,'consonant':0,'digit':0}
    
    for s in strings:
    
        if s.isupper():
            d['uppercase'] +=1
        if s.islower() :
            d['lowercase']+=1
        if s in "[aeiou]":
            d['vowel']+=1
        if s not in "[aeiou]" and s.isalpha():
            d['consonant']+=1
        if s.isdigit():
            d['digit']+=1
            
    
    print('Number of Uppercase= {x}'.format(x=d['uppercase']))
    print('Number of Lowercase= {x}'.format(x=d['lowercase']))
    print('Number of Vowel= {x}'.format(x=d['vowel']))
    print('Number of Consonant= {x}'.format(x=d['consonant']))
    print('Number of Digit= {x}'.format(x=d['digit']))
    
up_lo_vo_co('Hi Guys, Welcome back to Python Ninja Bootcamp 007')
>>
Number of Uppercase= 6
Number of Lowercase= 32
Number of Vowel= 13
Number of Consonant= 25
Number of Digit= 3

Q.Write a python function that accepts a list and returns a new list with a unique element.

 Method-1

 

def unique_list1(list1):
    list2=[]
    for var in list1:
        if var not in list2:
            list2.append(var)
    return list2

unique_list1([1,2,2,3,3,3,4,4,4,4,5,5,5,5,5])
>> [1, 2, 3, 4,5]

Method-2

def unique_list2(list1):
    return list(set(list1))
    
unique_list2([1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]) 
>> [1, 2, 3, 4,5]

Q.Write a function that accepts a number n and returns the factorial of n.



def factorial(n):
    fact=1
    for var in range(2,n+1):
        fact*=var
        
    return fact

factorial(5)
>> 120

Q.Write a function that accepts a number n and returns the sum of n natural numbers.

 

def sum_natural(n):
    sum=0
    for var in range(1,n+1):
        sum+=var
        
    return sum

sum_natural(5)
>> 15


Q.Write a function that accepts a string and checks whether it is palindrome or not.

 

def palindrome(strings):
    return strings==strings[::-1]

palindrome('nitin')
>>True

Q.Write a function that accepts a string and checks whether it is pangram or not.


Hint: Pangram is the string that contains all letter of the alphabet

import string 
print( string.ascii_lowercase )
>> abcdefghijklmnopqrstuvwxyz

def pangram(s):
    return set(string.ascii_lowercase)<=set(s)

pangram('The quick brown fox jumps over the lazy dog 007')
>> True


EDITS ARE WELCOMED!!

In the next Blog, we will discuss Oops 

https://sngurukuls247.blogspot.com/2018/11/python-ninja-bootcamp-26-oop-class.html

......................................................................................................................................

Follow the link below to access Free Python Lectures-
https://www.youtube.com/channel/UCENc9qI7_r8KMf6-_1R1xnw

Instagram-
https://www.instagram.com/python.india/


View the Jupyter Notebook for this lecture

Download the Jupyter Notebook for this lecture 








Feel free contact me on-

No comments:

Post a Comment