#11 Python :- In Keywords , Empty or not, While Loop

 IN Keyword:-

        This is used for checking given character or word in a given string.

e.q. :-  mystring="Gurpreet"

          #if with in

           if  'p' in mystring:

                    print("Yes present")

          else:

                   print("Not found")

           Output:-    Yes present

Empty or not:-

               To check weather the given string is empty or not.

e.q.1 :-  name="Gurpreet"

           if  name:   #true if string is not empty

                    print("Yes present")

          else:

                   print("Not found")

           Output:-    Yes present

e.q 2. :-  name= input("Enter your name:- ") 

           if  name:   #true if string is not empty

                    print(f"Your name is {name}")

          else:

                   print("You haven't entered anything")

           Output:-    Enter your name:- Gurpreet Singh #press enter

                           Your name is Gurpreet Singh


While Loop:-

         To write the given string more than once. We can also print one statement many times easily by               using while loop.

e.q :-   #print Gurpreet Singh 5 times using while loop

            i = 1

            while i <=5:

                     print("Gurpreet Singh")

                     i = i + 1

      Output:-     Gurpreet Singh

                         Gurpreet Singh

                         Gurpreet Singh

                         Gurpreet Singh

                         Gurpreet Singh

  Q:- Write a code for sum of 1 to 10 numbers using while loop:

 Soln:- i=0

            sum=0

            while i<=10: 

                      print(sum)

                       i=i+1 

                      sum = i + sum

 Output:- 

               1

               3

               6

              10

              15

              21

              28

              36

              45

              55

While loop in list:-

Input:-  fruit=['banana', 'mango', 'kiwi', 'apple']

i=0

while i < len(fruit):

             print(fruit[i])

             i+=1

Output:-  

banana

mango

kiwi

apple


Input:-

i=1
sum=1

while i<=10:
print(f"sum of first {i} integers: {sum}")
i=i+1
sum = i + sum

Output:-

sum of first 1 integers: 1

sum of first 2 integers: 3

sum of first 3 integers: 6

sum of first 4 integers: 10

sum of first 5 integers: 15

sum of first 6 integers: 21

sum of first 7 integers: 28

sum of first 8 integers: 36

sum of first 9 integers: 45

sum of first 10 integers: 55

    

Input:-                     

i=0
sum=0

while i<=10:
sum = i + sum
i=i+1
print(f"sum of all integers upto {i-1} : {sum}")

Output:-

sum of all integers upto 10 : 55

#note:- here we use print statement without indentation.

Exercise Question:- 

Q1:- Write a code in which you have to take a natural number 'N' as input and you                                           have to print the sum of natural numbers from 1 to N.

num= int(input("Enter the number upto which you want to sum:- "))
i=0
sum=0

while i<=num:
sum = i + sum
i=i+1
print(f"sum of all integers upto {i-1} : {sum}")


 Q2: Take input from the user in integers and sum all of the digits of given integer

            e.q.. user give input as 786. then add 7+8+6= 21

num=input("Enter any number:- ") #1234
length= len(num)
i=0
sum=0
while i<length:
sum=int(num[i])+sum
i+=1
print(sum)

                        

Q3:- Take input as name and the print the statement of each digit with its positions

name = input("Enter your name:- ") 
length=len(name)
i=0
while i<length:
print(f"The {i+1} digit is : {name[i]}")
i+=1

Q4: Take input from user and count no. of times each character comes 

name = input("Enter your name") #gurpreet)
length=len(name)
i=0
while i<length:
if name[i] not in name[0:i]:
print(f"The character {name[i]} comes : {name.count(name[i])} times")
i=i+1

Output:-

Enter your nameGurpreet

The character G comes : 1 times

The character u comes : 1 times

The character r comes : 2 times

The character p comes : 1 times

The character e comes : 2 times

The character t comes : 1 times



Comments

Popular posts from this blog

#4 Python :- Variables

#3 Python :- Comments, Printing First Statement and Escape sequences:-