DEV Community

S Mathan
S Mathan

Posted on

LOOPING IN PYTHON

LOOPING:

    no1 = int(input("enter no. "))
    no2 = int(input("enter no. "))
    if no1>no2:
      print(no1)
    if no1<no2:
      print(no2)
    else:
       print('no1 and no2 are same') 

    no = 1
    while no<=5;
      print(no,end='')
    no+=1
Enter fullscreen mode Exit fullscreen mode

VARIABLE LENGTH ARGUMENTS:

  print('hi')
  print('hi','hello')
  print('maddy','mathan')
  print(5,10,15)
  print('09','april','2025',sep='-',end='/')
  print('09','april','2025',end=' ')
Enter fullscreen mode Exit fullscreen mode

ADDITION OF FIRST N NUMBERS:

 no=1
  total=0
  while no<=5:
    print(no,end='')
    total=total+no
    no+=1
  print('\n',total,sep='')
Enter fullscreen mode Exit fullscreen mode

MULTIPLICATION OF FIRST N NUMBERS:

  no=1
   total=5
   while no<5:
      print(no,end='')
      total=total*no
      no+=1
   print('\n',total,sep='')
Enter fullscreen mode Exit fullscreen mode

MISTAKE:

  no=1
    total=1
    while no<=5:
      total=total*no
      print(no,total,end='')
      no+=1
     print('\n',total,sep='')
Enter fullscreen mode Exit fullscreen mode

BREAK STATEMENT:

no=1
  while no<=10:
    if no%6==0:
       break
    print(no)
    no+=1
Enter fullscreen mode Exit fullscreen mode
 no=1
  while no<=5:
    if no%4==0:
      break
    print(no)
    no+=1
  else:
    print("hi",no)
Enter fullscreen mode Exit fullscreen mode

ODD OR EVEN USING LOOP:

loop1 = 1
loop2 = 10

while loop1 <= loop2:
    if start % 2 == 0:
        print("It is Even")
    else:
        print("It is Odd")
    loop1 += 1
Enter fullscreen mode Exit fullscreen mode

PRIME NUMBERS USING LOOP:

n = int(input("Enter a number: "))
j = 2
if n <= 1:
    print("Not Prime")
else:
    while j < n:
        if n % j == 0:
            print("Not Prime")
            break
        j += 1
    else:
        print("Prime")
Enter fullscreen mode Exit fullscreen mode

Top comments (0)