Skip to main content

loops - If else, for ,while

IF 

1. conditions with If

code:


 a= 8

    1if-elif-else ladder in Python
    if(a<3): 
        print("The value of a is greater than 3")
    elif(a>13):
        print("The value of a is greater than 13")
    elif(a>7):
        print("The value of a is greater than 7")
    elif(a>17):
        print("The value of a is greater than 17")
    else:
        print("The value is not greater than 3 or 7")

    # 2. Multiple if statements
    if(a<3): 
        print("The value of a is greater than 3")

    if(a>13):
        print("The value of a is greater than 13")
        
    if(a>7):
        print("The value of a is greater than 7")

    if(a>17):
        print("The value of a is greater than 17")
    else:
        print("The value is not greater than 3 or 7")

    print("Done")


O/P:

The value of a is greater than 7

The value of a is greater than 7

The value is not greater than 3 or 7

Done

2.

    a = 45
    if(a<3):
        print('The value of a is greater than 3')
    elif(a<7):
        print('the value of a is greater that 7')
    elif(a>17):
        print('the value of a is greater than 13')
    else:
        print('the value is not greater than 3 or 7')


    b = 34
    if(b>10):
        print('the vlue of b is less than 10')
    if(b>20):
        print('the value of b is less than 20')
    if(b>30):
        print('the value of b is less than 30')
    if(b<40):
        print('the value of b is less than 40')

    a = 22
    if(a>9):
        print('Greater')
    else:
        print('lesser')

O/P:

the value of a is greater than 13

the vlue of b is less than 10

the value of b is less than 20

the value of b is less than 30

the value of b is less than 40

Greater

3. or

    age = int(input("Enter your age: "))
    if(age>34 or age<56):
        print("You can work with us")

    else:
        print("You cannot work with us")

O/P:

Enter your age: 23

You can work with us

4. IN

a = [45566]
print(435 in a)

O/P:

False

5. if else optional

    a = 6
    if(a==7):
        print("yes")
    elif(a>56):
        print("no and yes")
    else:
        print("I am optional")

O/P:

I am optional

6. Find greatest number out of 4 number

    num1 = int(input("Enter number 1: "))
    num2 = int(input("Enter number 2: "))
    num3 = int(input("Enter number 3: "))
    num4 = int(input("Enter number 4: "))

    if(num1>num4):
        f1 = num1
    else:
        f1 = num4

    if(num2>num3):
        f2 = num2
    else:
        f2 = num3

    if(f1>f2):
        print(str(f1+ " is greatest")
    else:
        print(str(f2+ " is greatest")

O/P:

>> Enter number 1: 25

Enter number 2: 47

Enter number 3: 69

Enter number 4: 40

69 is greatest

7. Pirnt pass or fail

    sub1 = int(input("Enter first subject marks\n"))
    sub2 = int(input("Enter second subject marks\n"))
    sub3 = int(input("Enter third subject marks\n"))

    if(sub1<33 or sub2<33 or sub3<33):
        print("You are fail because you have less than 33% in one of the subjects")
    elif(sub1+sub2+sub3)/3 < 40:
        print("You are fail due to total percentage less than 40")
    else:
        print("Congatulations! You passed the exam")

O/P:

>> Enter first subject marks 25

Enter second subject marks 56

Enter third subject marks  45

You are fail because you have less than 33% in one of the subjects

8. spam or not spam

    text = input("Enter the text\n")

    if("make a lot of money" in text):
        spam = True
    elif("buy now" in text):
        spam = True
    elif("click this" in text):
        spam = True
    elif("subscribe this" in text):
        spam = True
    else:
        spam = False

    if(spam):
        print("This text is spam")
    else:
        print("This text is not spam")

O/P:

>> click this

This text is spam

9. Lenth of name

    a = input("Enter your name")

    if(len(a)<10):
        print("the character lenth is less than 10")
    else:
        print("the character lenth is more than 10")

O/P:

>> Enter your name avinash

the character lenth is less than 10

10. Marks Grade

    marks = int(input('Enter your marks'))
    a = marks

    if (a>90):
        print("your grade is Ex")
    elif(a>80):
        print("your grade is A+")
    elif(a>70):
        print("your grade is A")
    elif(a>60):
        print("your grade is B")
    elif(a>50):
        print("your grade is C")
    elif(a>=40):
        print("your grade is D")
    else:
        print("Sorry your fail in exam")

O/P:

>>  Enter your marks 65

your grade is B

          WHILE                                               
                                                                                                  

11. while

i = 0

while (i <= 10) :
    print("yes" + str(i))
    i= i+1

O/P:

>> yes0

yes1

yes2

yes3

yes4

yes5

yes6

yes7

yes8

yes9

yes10

12.print 1 to 59

i = 1

while i<=50:
    print(i)
    i = i + 1

O/P: 

>>  print 1to 50

13. print list

fruits = ["mango""apple""banana"'peru'"orange"]
i=0
while i<len(fruits):
    print(fruits[i])
    i= i+1
    

O/P:

>> mango

apple

banana

peru

orange

14.  range

    for i in range(181): start end step
        print(i)

    for i in range(1,8):
        print(i)

O/P:

>>  print 1 to 7

15. for with else

    for i in range(10):
        print(i)
    else:
        print("This is inside else of for")

O/P:

>>  print 0 to 9

This is inside else of for

16. Break

    for i in range(10):
        print(i)
        if i==5:
            break
    else:
            print('you are very clever')

O/P:

>>  print 0 to 5 and break

17. Continue

    for i in range(10):
        if i == 5:
            continue
        print(i)

O/P:

>>  print 0 to 9

18. Pass

    i = 4

    def run(player):
        pass

    def ouch(player):
        pass

    if i>0:
        pass

    while i>6:
    pass

    print("avi is a good boy")

O/P:

>>  avi is a good boy

19. print table (padha)

    num = int(input("Enter the number"))
    for i in range(111):
        # print(str(num) + " X " + str(i) + "=" + str(i*num))
        print(f"{num}X{i}={num*i}")

O/P:

>> Enter the number 10

10X1=10

10X2=20

10X3=30

10X4=40

10X5=50

10X6=60

10X7=70

10X8=80

10X9=90

10X10=100

20.  for and if 

    l1 = ['avinash''rahul''aniket''satish''akash']

    for a in l1:
        # if a.startswith(input("Enter first letter of your name")):
        #     print("Hello " + a)

        if a.startswith(("a")):
            print("Hello " + a)

O/P:

>> Hello avinash

Hello aniket

Hello akash

21. prime number

    num = int(input("Enter the number: "))
    prime = True

    for i in range(2num):
        if(num%i == 0):
            prime = False
            break
    if prime:
        print("This number is Prime")
    else:
        print("This number is not Prime")

O/P:

>>  Enter the number: 20

This number is not Prime

22. Factorial

    num = int(input("Enter the number: "))
    factorial = 1
    for i in range(1num+1):
        factorial = factorial * i
    print(f"The factorial of {num} is {factorial}")

O/P:

>>  Enter the number: 6

The factorial of 6 is 720

23.  print star

    n = 4

    for i in range(4):
        print("*" * (i+1))

O/P:

>> *

     **

     ***

     **** 

24. print star

    n =3
    for i in range (3):
        print(" "*(n-i-1), end="")
        print("*"*(2*i+1), end="")
        print(" "*(n-i-1))

O/P:

>> 

25. print star

O/P:

>> 

  *  

 *** 

*****

26. sum

    num = int(input("enter the number"))
    sum = 0
    i=1
    while i<=num :
        sum+=i
        i+=1
    print(sum)

O/P:

>>   36

27.

O/P:

>> 

1.

O/P:

>> 

1.O/P:

>> 

1.

O/P:

>> 

1.

Popular posts from this blog

Bagging and Boosting

  What is an Ensemble Method? The ensemble is a method used in the machine learning algorithm. In this method, multiple models or ‘weak learners’ are trained to rectify the same problem and integrated to gain desired results. Weak models combined rightly give accurate models. Bagging Bagging is an acronym for ‘Bootstrap Aggregation’ and is used to decrease the variance in the prediction model. Bagging is a parallel method that fits different, considered learners independently from each other, making it possible to train them simultaneously. Bagging generates additional data for training from the dataset. This is achieved by random sampling with replacement from the original dataset. Sampling with replacement may repeat some observations in each new training data set. Every element in Bagging is equally probable for appearing in a new dataset.  These multi datasets are used to train multiple models in parallel. The average of all the predictions from different ensemble models i...