Thursday 11 February 2021

Python for, while loop, pass, break, continue

 a=1

while a<=5:

    print(a)

    a=a+1



for i in range(1,6):

    print(i)



print("**********")

n=int(input("Enter a number: "))

sum=0

while n>0:

    r=n%10

    n=n//10

    sum=sum+r


print("Sum of digits =",sum)


print("**********")

a=5

while a>0:

    print(a)

    a=a-1


print("**********")


a=1

while a<=10:

    if(a%2==0):

        print(a,"is even")

    else:

        print(a,"is odd")

    a=a+1




print("Hello")

#for i in range(10):

#while True:

#if True:

for i in range(10):

    pass

print(i)

print("************")

for i in [1,2,3,4,5]:

    if(i==4):

        pass

        print("This is a pass block")

    print(i)

print("************")

for i in range(5):

    print("i=",end="")

    pass

    print(i)


print("************")

for i in range(5):

    print("i=",i)

    if i==3:

        break


print("************")

for i in range(5):

    if i==3:

        break

    print("i=",i)


print("************")

for i in range(5):

    if i==3:

        continue

    print("i=",i)


print("************")

for i in range(5):

    if i==3:

        continue

        print("i=",i)

print("************")

for i in range(5):

    if i==3:

        print("i=",i)

        continue

    

print("************")

for i in range(5):

    if i==3:

        print("i=",i)

        continue

    print(i)




print("***********")
for a in range(1,6):
    for i in range(1,6):
        print(i,end="\t")
    print()

print("***********")
for a in range(1,6):
    for i in range(1,6):
        print(a,end="\t")
    print()

print("***********")

for a in range(1,6):
    for i in range(1,a+1):
        print(i,end="\t")
    print()

print("***********")

for a in range(1,6):
    for i in range(5,0,-1):
        print(i,end="\t")
    print()

print("***********")

for a in range(5,0,-1):
    for i in range(1,a+1):
        print(a,end="\t")
    print()


print("***********")
a=1
while a<=5:
    print(a)
    a=a+1




Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb.py
1
2
3
4
5
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb.py
1
2
3
4
5
0
1
2
3
4
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb.py
1
2
3
4
5
1
2
3
4
5
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb.py
1
2
3
4
5
1
2
3
4
5
**********
Enter a number456
Sum of digits = 15
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb.py
1
2
3
4
5
1
2
3
4
5
**********
Enter a number: 345
Sum of digits = 12
**********
6
5
4
3
2
1
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb.py
1
2
3
4
5
1
2
3
4
5
**********
Enter a number: 444
Sum of digits = 12
**********
5
4
3
2
1
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb.py
1
2
3
4
5
1
2
3
4
5
**********
Enter a number: 123
Sum of digits = 6
**********
5
4
3
2
1
**********
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_1.py
Hello
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_1.py
Hello
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_1.py
Hello
9
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_1.py
Hello
9
************
1
2
3
This is a pass block
4
5
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_1.py
Hello
9
************
1
2
3
This is a pass block
4
5
************
i=0
i=1
i=2
i=3
i=4
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_1.py
Hello
9
************
1
2
3
This is a pass block
4
5
************
i=0
i=1
i=2
i=3
i=4
************
i= 0
i= 1
i= 2
i= 3
i= 4
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_1.py
Hello
9
************
1
2
3
This is a pass block
4
5
************
i=0
i=1
i=2
i=3
i=4
************
i= 0
i= 1
i= 2
i= 3
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_1.py
Hello
9
************
1
2
3
This is a pass block
4
5
************
i=0
i=1
i=2
i=3
i=4
************
i= 0
i= 1
i= 2
i= 3
************
i= 0
i= 1
i= 2
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_1.py
Hello
9
************
1
2
3
This is a pass block
4
5
************
i=0
i=1
i=2
i=3
i=4
************
i= 0
i= 1
i= 2
i= 3
************
i= 0
i= 1
i= 2
************
i= 0
i= 1
i= 2
i= 4
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_1.py
Hello
9
************
1
2
3
This is a pass block
4
5
************
i=0
i=1
i=2
i=3
i=4
************
i= 0
i= 1
i= 2
i= 3
************
i= 0
i= 1
i= 2
************
i= 0
i= 1
i= 2
i= 4
************
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_1.py
Hello
9
************
1
2
3
This is a pass block
4
5
************
i=0
i=1
i=2
i=3
i=4
************
i= 0
i= 1
i= 2
i= 3
************
i= 0
i= 1
i= 2
************
i= 0
i= 1
i= 2
i= 4
************
************
i= 3
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_1.py
Hello
9
************
1
2
3
This is a pass block
4
5
************
i=0
i=1
i=2
i=3
i=4
************
i= 0
i= 1
i= 2
i= 3
************
i= 0
i= 1
i= 2
************
i= 0
i= 1
i= 2
i= 4
************
************
i= 3
************
0
1
2
i= 3
4
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_2.py
0 1 2 3 4
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_2.py
1 2 3 4 5
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_2.py
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_2.py
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_2.py
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_2.py
***********
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
***********





>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_2.py
***********
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
***********
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_2.py
***********
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
***********
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
***********
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_2.py
***********
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
***********
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
***********
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
***********
6 5 4 3 2 1
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_2.py
***********
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
***********
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
***********
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
***********
5 4 3 2 1
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_2.py
***********
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
***********
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
***********
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
***********
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_2.py
***********
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
***********
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
***********
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
***********
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
***********
5 5 5 5 5
4 4 4 4 4
3 3 3 3 3
2 2 2 2 2
1 1 1 1 1
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_2.py
***********
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
***********
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
***********
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
***********
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
***********
5 5 5 5 5
4 4 4 4 4
3 3 3 3 3
2 2 2 2 2
1 1 1 1 1
>>> 
= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/11feb_2.py
***********
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
***********
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
***********
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
***********
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
***********
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
>>> A=[1,2,3]
>>> A.append(4)
>>> A
[1, 2, 3, 4]
>>> A.append([5,6,7])
>>> A
[1, 2, 3, 4, [5, 6, 7]]
>>> A.append(5,6,7)
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    A.append(5,6,7)
TypeError: append() takes exactly one argument (3 given)
>>> A+[5,6,7]
[1, 2, 3, 4, [5, 6, 7], 5, 6, 7]
>>> A
[1, 2, 3, 4, [5, 6, 7]]
>>> A=A+[5,6,7]
>>> A
[1, 2, 3, 4, [5, 6, 7], 5, 6, 7]
>>> A=["Hello"]
>>> A
['Hello']
>>> A.append("India")
>>> A
['Hello', 'India']
>>> len(A)
2
>>> A=[1,2,3,4,5]
>>> A
[1, 2, 3, 4, 5]
>>> A.insert(3,"Hello")
>>> A
[1, 2, 3, 'Hello', 4, 5]
>>> A=[9,8,7,6,5]
>>> A.append(4)
>>> A.insert(2,10)
>>> A
[9, 8, 10, 7, 6, 5, 4]
>>> len(A)
7
>>> A.index(10)
2
>>> A.index(5)
5
>>> A.index(7)
3
>>> max(A)
10
>>> min(A)
4
>>> A.insert(4,8)
>>> A
[9, 8, 10, 7, 8, 6, 5, 4]
>>> A.count(8)
2
>>> A.count(10)
1
>>> A.remove(10)
>>> A
[9, 8, 7, 8, 6, 5, 4]
>>> A.reverse()
>>> A
[4, 5, 6, 8, 7, 8, 9]
>>> None
>>> print(None)
None
>>> print(5)
5
>>> None == None
True
>>> A=[1,2,3]
>>> A
[1, 2, 3]
>>> A=(1,2,3)
>>> A
(1, 2, 3)
>>> A[0]
1
>>> A[1]
2
>>> A[2]
3
>>> A[1]=5
Traceback (most recent call last):
  File "<pyshell#48>", line 1, in <module>
    A[1]=5
TypeError: 'tuple' object does not support item assignment
>>> A=1,2,3
>>> A
(1, 2, 3)
>>> a,b,c=1,2,3
>>> a
1
>>> b
2
>>> c
3
>>> a,b,c
(1, 2, 3)
>>> a, b, c=1,2,3
>>> a, b, c=5,6,7
>>> a
5
>>> b
6
>>> c
7
>>> a,b,c
(5, 6, 7)
>>> type(a)
<class 'int'>
>>> type(a,b,c)
Traceback (most recent call last):
  File "<pyshell#63>", line 1, in <module>
    type(a,b,c)
TypeError: type.__new__() argument 1 must be str, not int
>>> C=a,b,c
>>> C
(5, 6, 7)
>>> type(C)
<class 'tuple'>
>>> K=[]
>>> k
Traceback (most recent call last):
  File "<pyshell#68>", line 1, in <module>
    k
NameError: name 'k' is not defined
>>> K
[]
>>> T=()
>>> T
()
>>> T=5,
>>> T
(5,)
>>> type(T)
<class 'tuple'>
>>> T1=5
>>> type(T1)
<class 'int'>
>>> T2=(5,)
>>> type(T2)
<class 'tuple'>
>>> T3=(5)
>>> T3
5
>>> T=(1,(1,2,3))
>>> T[1]
(1, 2, 3)
>>> T[1][1]
2
>>> T[3][1]
Traceback (most recent call last):
  File "<pyshell#84>", line 1, in <module>
    T[3][1]
IndexError: tuple index out of range
>>> T[1,1]
Traceback (most recent call last):
  File "<pyshell#85>", line 1, in <module>
    T[1,1]
TypeError: tuple indices must be integers or slices, not tuple
>>> T=(1,4,9,16,25)
>>> T[:]
(1, 4, 9, 16, 25)
>>> T[:4]
(1, 4, 9, 16)
>>> max(T)
25
>>> min(T)
1
>>> len(T)
5
>>> T.append(36)
Traceback (most recent call last):
  File "<pyshell#92>", line 1, in <module>
    T.append(36)
AttributeError: 'tuple' object has no attribute 'append'
>>> T.insert(2,4)
Traceback (most recent call last):
  File "<pyshell#93>", line 1, in <module>
    T.insert(2,4)
AttributeError: 'tuple' object has no attribute 'insert'
>>> T.count(4)
1
>>> T.count(2)
0
>>> "Hello".lower()
'hello'
>>> "Hello".upper()
'HELLO'
>>> S="Hello"
>>> S.lower()
'hello'
>>> S.upper()
'HELLO'
>>> S[0]
'H'
>>> S[1:3]
'el'
>>> S[::-1]
'olleH'
>>> 

No comments:

Post a Comment