Friday, 1 January 2021

Python programming AI and ML course @ APG SILC Pune : practice of string/while loop/for loop/ break/continue

 s= “Happy New Year 2021,  01 Jan 2021”

print(s)

Happy New Year 2021,  01 Jan 2021

 

# Exercise:

# range 10, even num  square, odd num double with while loop

i=1

while i<10:

    i =i+1

    if i%2==0:

        print(i**2)

    else:

        print(i*2)

 

#output       

4

6

16

10

36

14

64

18

100

 

# Now same exercise,but we wanna see whatz goin on inside loop, So

i=1

while i<10:

    i =i+1

    if i%2==0:

        print(i,'%2','remainder =',i%2,'so',i,'is Even num-->sqr',i,'**2','=',i**2)

    else:

        print(i,'%2','remainder =',i%2,'so',i,'is Odd num-->dble',i,'*2','=',i*2)

 

# Note: if i=0 à1 %2 remainder = 1 so 1 is Odd num-->dble 1 *2 = 2

#output

2 %2 remainder = 0 so 2 is Even num-->sqr 2 **2 = 4

3 %2 remainder = 1 so 3 is Odd num-->dble 3 *2 = 6

4 %2 remainder = 0 so 4 is Even num-->sqr 4 **2 = 16

5 %2 remainder = 1 so 5 is Odd num-->dble 5 *2 = 10

6 %2 remainder = 0 so 6 is Even num-->sqr 6 **2 = 36

7 %2 remainder = 1 so 7 is Odd num-->dble 7 *2 = 14

8 %2 remainder = 0 so 8 is Even num-->sqr 8 **2 = 64

9 %2 remainder = 1 so 9 is Odd num-->dble 9 *2 = 18

10 %2 remainder = 0 so 10 is Even num-->sqr 10 **2 = 100

break example

# print only Python, Java

for i in ('Python','Java','Swift',"C",'C++'):

if i== 'Swift':

                                break

                print(i) 

output

Python

Java

(1)    break example

while True: # to keep condition True forever to avoid freq run

    num = int(input('Guess your number: '))

    if num ==15:

        print('You won, Number was:',num)

        print("'break'loop acted")

        break

    # while loop will continue printing whatever input was and ask again to Enter Guess yr num.

# when input =15,

# output à You won, Number was 15 \n ‘break’ loop acted

 

 

(2)    break example with while loop

i = 1

while i < 10:

                print(i)

                i = i+1

                if i == 5:

                                break

 

#output              

0

1

2

3

4

# Notice ‘break’ loop acted and 5 is NOT printed.

 

 

(3)    break example with while loop

while i < 10:

                print(i)

                i = i+1

                if i == 5:

                            print (‘break’ loop acted and so 5 is NOT printed.)

                                break

 

#output              

0

1

2

3

4

‘break’ loop acted and so 5 is NOT printed.

 

(4)    break example with for loop

 

s = 'Anybody can learn Python coding only must practice daily '

for i in s:

                print(i,end='')

                if i=='m':

                                break

# output

Anybody can learn Python coding only m

 

 

(5)    #admission only for age between >=18 and ,=35

(6)    count=0

(7)    while True:# condition True forever

(8)        #count =0 inside loop-->count will never increment.

(9)            

(10)    age = int(input("Enter yr age "))

(11)    count=count+1

(12)    print('count:',count)

(13)   

(14)    if count==5:

(15)        print('enough Tries!!',"'break'loop acted")

(16)        break

(17)   

(18)    if (age>18 or age==18) and (age==35 or age<35) ==True:

(19)        print("admission granted")

(20)       

(21)      

(22)       

(23)    else:

(24)        print("admission rejected")

(25)       

(26) #output

(27)Enter yr age 17

(28)count: 1

(29)admission rejected

(30)Enter yr age 18

(31)count: 2

(32)admission granted

(33)Enter yr age 25

(34)count: 3

(35)admission granted

(36)Enter yr age 35

(37)count: 4

(38)admission granted

(39)Enter yr age 36

(40)count: 5

(41)admission rejected

(42)Enter yr age 50

(43)count: 6

(44)enough Tries!! 'break' loop acted

 

 

# Grades separation

# fail,<--40<marks>==60-->first class,marks==75>=distingtion

 

count = 0

while True:# to keep condition True forever

    marks = float(input('Enter yr marks '))

   

    if marks== ValueError:

        pass

   

    count=count+1

    print(count)

    if count==5:

        print("'break' loop acted")

        break

   

    if (marks >40 or marks == 40)and (marks<59 or marks==59)==True:

        print('Passed')

       

    elif (marks >61 or marks == 60)and (marks<74 or marks==74)==True:

        print('Passed with First Class!')

   elif (marks >75 or marks == 75)and (marks<100 or marks==100)==True:

        print('Passed with Distinction !!')

       

    else:

        print('Failed')

 

#output

Enter yr marks 25

1

Failed

Enter yr marks 40

2

Passed

Enter yr marks 60

3

Passed with First Class!

Enter yr marks 80

4

Passed with Distinction !!

Enter yr marks 100

5

'break' loop acted

 

2 comments: