DEV Community

varatharajan
varatharajan

Posted on

while loop, predefind modules

The while Loop

With the while loop we can execute a set of statements as long as a condition is true.

input

i = 1
while i < 6:
  print(i)
  i = i+1
Enter fullscreen mode Exit fullscreen mode

output

1
2
3
4
5

For writing multiple "if"conditions we can use "while"loop for repeated condition.
example :
To get this output 1 1 1 1 1 we have various syntax but we can use if and while now.

case 1 :

#using if condition

count = 1
if count<=5:
    print(1, end=' ')
    count=count+1 #count = 2
if count<=5:
    print(1, end=' ')
    count=count+1 # count = 3

if count<=5:
    print(1, end=' ')
    count=count+1 # count = 4

if count<=5:
    print(1, end=' ')
    count=count+1 # count = 5

if count<=5:
    print(1, end=' ')
    count=count+1 # count = 6
Enter fullscreen mode Exit fullscreen mode

output

1 1 1 1 1

case 2 :

count = 1
while count<=5:
    print(1, end=' ')
    count=count+1 #count = 2
Enter fullscreen mode Exit fullscreen mode

output

1 1 1 1 1

Task 1:

Create a python module called Bank.
Add functions: deposit(amount), withdraw(amount)
Create one more python module called Customer
From customer module, call deposit and withdraw functions of Bank module.

#bank.py

def deposit(amount):
    print("Total deposit amount is ",amount)
    return(amount)

def withdraw(amount):
    print("Total withdrawal amount is ",amount)
    return(amount)
Enter fullscreen mode Exit fullscreen mode
#customer.py

import Bank

total_deposit=Bank.deposit(200000)
total_withdrawal=Bank.withdraw(20000)

print("Bank balance is ",(total_deposit-total_withdrawal))
Enter fullscreen mode Exit fullscreen mode

output


Total deposit amount is 200000
Total withdrawal amount is 20000
Bank balance is 180000

predefined modules:

random :

input

import random
otp = random.randint(0,9999)
print(otp)
Enter fullscreen mode Exit fullscreen mode

output


4321
3185
4238

math

input

import math
print(math.sqrt(9))
Enter fullscreen mode Exit fullscreen mode

output

3.0

datetime

input

from datetime import datetime

now = datetime.now()
print("Current Date and Time:", now)
Enter fullscreen mode Exit fullscreen mode

output


Current Date and Time: 2024-11-25 13:31:37.449457

Top comments (0)