Friday, September 28, 2018

Python Ninja Bootcamp 21-Q&A Second

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

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


Q.Write a program to split the given string and find the word starts with 'w'.
st='Your worth consists in what you are and not in what you have'

for var in st.split():
    if var[0]=='w':
        print(var)
>>worth
    what
    what



Q.Write the Program to print "fizz" for multiple of 3, 'buzz' for the multiple of 5 and 'fizzbuzz' for multiple of both for the ingetegr between 1 to 50?
 
for var in range(1,51):
    
    if var%15==0:
        print('{x}->fizzbuzz'.format(x=var))    
    
    elif var%5==0:
        print('{x}->buzz'.format(x=var))
        
    elif var%3==0:
        print('{x}->fizz'.format(x=var))
>> 
3->fizz
5->buzz
6->fizz
9->fizz
10->buzz
12->fizz
15->fizzbuzz
18->fizz
20->buzz
21->fizz
24->fizz
25->buzz
27->fizz
30->fizzbuzz
33->fizz
35->buzz
36->fizz
39->fizz
40->buzz
42->fizz
45->fizzbuzz
48->fizz
50->buzz


Q.Write the program to find the factorial of number given by the user.

Factorial is the product of all integers less than or equal to n but greater than or equal to 1. The factorial value of 0 is defined as equal to 1.
Hint: n!=n*(n-1)*(n-2)*........................3*2*1

n=int(input('Enter the number for factorial= '))
>>Enter the number for factorial= 5

fact=1
for var in range(2,n+1):
    fact*=var
print(fact)print
>>120


Q.Write a program to print Fibonacci series up to n terms.

Fibonacci series of numbers in which each number is the sum of the two preceding numbers.

Hint: 0,1,1,2,3,5,8,13,21,34
n=int(input('Enter the number for fibonacci series= '))  
>>Enter the number for fibonacci series=5

F=0
S=1
T=1
for var in range(0,n):
    print(F)
    T=F+S
    F=S
    S=T
>> 0
     1
     1
     2


Q.Write a program to find whether the number is prime or not.

n=int(input('Enter the number= '))
>> Enter the number= 5


flag=0
for var in range(1,n+1):
    if n%var==0:
        flag+=1
        
if flag==2:
    print('{x} is prime'.format(x=var))
else:
    print('{x} is not prime'.format(x=var))
>> 5 is prime



Q.Write program to find LCM of 2-number.


a=int(input('Enter the 1st number= '))
b=n=int(input('Enter the 2nd number= '))
>> Enter the 1st number= 6
Enter the 2nd number= 9

if a<b:
    min=a
else :
    min=b
    
while(True):
    if min%a==0 and min%b==0:
        print('LCM of {x} and {y} is {z}'.format(x=a,y=b,z=min))
        break
    min=min+1
>> LCM of 6 and 9 is 18




Q.Write program to find HCF of 2-number.


a=int(input('Enter the 1st number= '))
b=int(input('Enter the 2nd number= '))
>> Enter the 1st number= 6
Enter the 2nd number= 9

if a<b:
    min=a
else :
    min=b
    
for var in range(1,min+1):
    if a%var==0 and b%var==0:
        HCF=var
        
print('LCM of {x} and {y} is {z}'.format(x=a,y=b,z=HCF))
>> LCM of 60 and 90 is 30


Q.Write a program to check whether a number is a palindrome or not.

Reverse of the number is equal to the Original number is called palindrome.

for example-

121 is palindrome as Reverse of the number is equal to the Original number

Ist Method
n=int(input('Enter the number= ')) 
>> Enter the number= 121
 n is an integer in this case. Since int function returns an integer value. 

rev=0
temp=n

while temp>0:
    rev=rev*10+temp%10 
    temp//=10
    
if n==rev:
    print('{x} is palindrome'.format(x=n))
else:
    print('{x} is not palindrome'.format(x=n))
>> 121 is palindrome

2nd Method
n=input('Enter the number= ')
>> Enter the number= 121
n is a string in this case. Since input function returns a string value.



if n==n[::-1]:  # Comparing the original and reversed number 
    print('{x} is palindrome'.format(x=n))
else:
    print('{x} is not palindrome'.format(x=n))
>> 121 is palindrome



EDITS ARE WELCOMED!!

In the next Blog, we will discuss Method & Function 

https://sngurukuls247.blogspot.com/2018/10/python-ninja-bootcamp-22-method-function.html

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

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


View the Jupyter Notebook for this lecture

Download the Jupyter Notebook for this lecture 



Follow us on Instagram

Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com

No comments:

Post a Comment