0% found this document useful (0 votes)
18 views

Looping in Python-2

Uploaded by

jai chandran
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Looping in Python-2

Uploaded by

jai chandran
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

EX:

---
year=int(input("Enter the Year"))

if(year%4==0):
print("Leap Year")
else:
print("Not Leap Year")

Looping
--------

Types:
-------
1)while
2)for
3)Nested Loop

I.while
---------
---->Entry Check Loop

syntax:
----------
initialization

while(condition):
statement(s)
incre/decre

EX:
---

a=1

while(a<10):#1<10#1<10#1<10
print(a)#1#1

EX:
---
a=1

while(a<10):#1<10#2<10#3<10#4<10,...#10<10--->false
print(a)#1#2#3
a=a+1#a=1+1->2-->a=2+1--->3--->a=3+1-->4

EX:
---
a=5

while(a<=10):
print(a)
a=a+1
------------------------------------
a=50

while(a<=100):#50<=100
print(a)#50
a=a+3

-------------------------------------
a=100

while(a>=75):
print(a)
a=a-1

EX:
---
a=int(input("Enter the start value"))
b=int(input("Enter the End value"))

while(a<=b):
print(a)
a+=5#a=a+5

while with else


-----------------
while--->true--->while+else

while-->false--->else to be Executed

a=int(input("Enter the start value"))


b=int(input("Enter the End value"))

while(a<=b):
print(a)
a+=5#a=a+5

else:
print("Program Exit!!! Thank You...")
--------------------------------------------------------------
Reverse of a number
-------------------
input
-----
EX:
---
1024

Output
--------
4201

n=1024,s=0
while(n!=0):
a=n%10
s=s*10+a
n=n//10
1024!=0
--------
a=1024%10--->4
s=0*10+4---->4
n=1024//10--->102

102!=0
----------
a=102%10--->2
s=4*10+2--->42
n=102//10--->10

10!=0
-----
a=10%10-->0
s=42*10+0-->420
n=10//10-->1

1!=0
------
a=1%10--->1
s=420*10+1--->4200+1-->4201
n=1//10-->0

0!=0
----
false

n=int(input("Enter the value"))


s=0
while(n!=0):
a=n%10
s=s*10+a
n=n//10
print("Reverse of a number is",s)

palindrome of a number
---------------------
121--->121
909--->909

n=int(input("Enter the value"))


s=0
x=n
while(n!=0):
a=n%10
s=s*10+a
n=n//10
print("Reverse of a number is",s)

if(s==x):
print(s,"is a Palindrome Number")
else:
print(s,"is not a Palindrome Number")
------------------------------------------------------------------
1)Is Python is case sensitive ,why dealing with identifier
-------------------------------------------------------------
a)Yes(correct)
b)No

2)Python Supports(interpreted,scripting)
------------------
a)object-Oriented
b)Structured
c)Functional

d)All d Above(correct)

3)What Arithmetic Operators can't used with String in Python?


---------------------------------------------------------------
1)+(concationation)
2)-(correct)
3)*(Replication)

4)All keyword in python written in Lowercase


----------------------------------------------
1)True
2)False(correct)

5)single command line starts with


----------------------------------
1)#(commandline)(correct)
2)" "
3)'

6)which of the following extention of the Python file?


------------------------------------------------------
1).python
2).py(correct)
3).p

looping(while loop)

for loop
-----------
range()

for x in range(10):#0,1,2,3,...9
print(x)

for x in range(4,16):#4,5,6,7,...15#4-->start,16-->ending value


print(x)

for x in range(4,16+1):#4,5,6,7,...15
print(x)

for x in range(4,20,3):#4-->start,20-->end value,3--->incremental value


print(x)

for x in range(10,5,-1):#10,9,8,7,6
print(x)

for x in range(100,50,-3):
print(x)

for x in [1,3,5,7,8]:
print(x)

for x in (1,3,5,7,8):
print(x)

for x in {1,3,5,7,8,45,3,5}:
print(x)

for x in "python":
print(x)
-------------------------------
a=["Chennai","banglore","salem"]

print(a)

for a1 in a:
print(a1)
-------------------------------------------
for with else
---------------
EX:-(for loop is false)
--------------------------
for i in range(10,5):
print(i)
else:
print("Program Exist!!Thank You!!")

EX:(for loop is true)


---
for i in range(10,5,-1):
print(i)
else:
print("Program Exist!!Thank You!!")
---------------------------------------------------------
III.Nested loop
----------------
1)while inside while
2)while inside for
3)for inside while
4)for inside for

1)while inside while


----------------------

In c,program
a=10
printf("%d",a)

"""while inside while"""

i=1
while(i<=10):#1<=10#2<=10
print(i,"Table")#1,Table,2 Table
j=1
while(j<=10):#1<=10#2<=10,....10<=10,.11<=10
k=i*j#k=1*1-->1#k=1*2-->2.....#k=2*1-->1
print("{}*{}={}".format(i,j,k))#1*1=1,#1*2=2...#2*1-->2
j=j+1#j=1+1-->j=2
print("*************************")
i=i+1#i=1+1-->i=2

2)while inside for


-------------------

"""while inside for"""

i=1
while(i<=10):
print(i,"Table")
for j in range(1,11):
k=i*j
print("{}*{}={}".format(i,j,k))

print("*************************")
i=i+1#i=1+1-->i=2

3)while inside for

for i in range(1,11):
print(i,"Table")
j=1
while(j<=10):
k=i*j
print("{}*{}={}".format(i,j,k))
j=j+1
print("*************************")

4)for inside for


--------------------
for i in range(1,11):
print(i,"Table")
for j in range(1,11):

k=i*j
print("{}*{}={}".format(i,j,k))

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

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

----------------------------------------
for i in range(1,6):#1,2,3,4,5
for j in range(1,6):#1,2,3,4,5
print("*",end=" ")
print("\n")

"""
1 2 3 4 5
1 * * * * *
2 * * * * *
3 * * * * *
4 * * * * *
5 * * * * *

"""
------------------------------------------------------------
Questions
------------
1)print(2**3+(5+6)**(1+1))

a)129(correct)

b)8
c)121

2**3-->2*2*2-->8

(5+6)-->11

(1+1)-->2

11**2-->11*11-->121

8+121--->129

2)find the program output?


----------------------
variable=10
print(type(variable))
variable="Hello"
print(type(variable))

a)int str(correct)
b)str str
c)int int
d)str int

3)find the program output?

a=[1,2,3]
a=tuple(a)#(1,2,3)#Immutable
a[0]=2#error
print(a)

a)2,2,3
b)1,2,3
c)Error(correct)

4)
print(type(5/2))#2.5
print(type(5//2))#2

a)int float
b)int int
c)float int(correct)
d)float float

5)how is code block indicated in python?

a)bracket
b)key
c)identendation(correct)
d)none of d above
----------------------------------------------------------
1)what is the method inside the in python language

a)object
b)function(correct)
c)arguments

2)which of the following declaration is incorrect?


--------------------------------------------------
a)_x=2
b)__x=3
c)__xyz__=5
d)none of the above(correct)

3)why does the name of local variable start with and underscrore discourage?
-----------------------------------------------------------------
a)to identify the variable
b)if confues the interpreter
c)it indicates a private variable of a class(correct)
d)none of the above

4)which of the following is a not keyword in python language?


----------------------------------------------------------
a)value(correct)
b)raise
c)try
d)with

5)which of the following function is a built in function python language?


-----------------------------------------------------------------------------
a)value()
b)print()-->correct
c)none of the above
--------------------------------------------------------------------------------

You might also like