Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

While and for loop - Python examples practices

Uploaded by

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

While and for loop - Python examples practices

Uploaded by

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

Notes:

Loops:
- You can execute same statement multiple times using loop.
- You can specify number of iterations(loop).
- Number of iterations depend on the condition.

1. While loop
2. For loop

1. While loop:

syntax:
Initialization
while condition:
statements
increment/decrement
else:
statement

iterations = 10
print good morning 10 times.
i=1
while i<=5:
print("Good Morning")
i = i + 1

i=1
while i<=5:
print(i)
i = i + 1

#print odd numbers from 1 to 10


i=1
while i<=10:
print(i)
i = i + 2

#print Even numbers from 1 to 10


i=2
while i<=10:
print(i)
i = i + 2

#print 1 to 10 in reverse order


i=10
while i>=1:
print(i)
i = i - 1

#print odd numbers from 1 to 10 in reverse order


i=9
while i>=1:
print(i)
i = i - 2

#print even numbers from 1 to 10 in reverse order


i=10
while i>=1:
print(i)
i = i - 2
else:
print("end of the loop")

#infinite loop - condition must be true


while True:
print("Good Morning")

2. For loop
- You can use for loop with numbers.
- You can use for loop with string.
- You can use for loop with data structure(list,array,tuple,dictionary).

1. Number:
range(start,end,step)
range(1,10)
1 2 3 4 5 6 7 8 9
range(1,10,2)
1 3 5 7 9
range(2,15,3)
2 5 8 11 14

syntax:
for variablename in range(start,end,step):
statements

#print 1 to 10
for i in range(1,11):
print(i)

#print odd numbers from 1 to 10


for i in range(1,11,2):
print(i)

#print 0 to 9 because iteration(index) starts from 0


for i in range(10):
print(i)

#print hello friends 15 times


for i in range(15):
print("hello friends")

#print 1 to 10 in reverse order


for i in range(10,0,-1):
print(i)

Write code to take input from user as number and print multiplication table of that
number.
n = 11

11 * 1 = 11
11 * 2 = 22
11 * 3 = 33
.
.
.
.
11 * 10 = 330
n = int(input("Enter the number: "))
for i in range(1,11):
print(n,"*",i,"=",n*i)

1. break statement
- It is used to stop or break the loop.

Example:
for i in range(1,20):
if i==10:
break
print(i)

i=1
while i<20:
if i==10:
break
print(i)
i+=1

2. Continue statement
- It is used to skip the loop.

Example:
for i in range(1,20):
if i==10:
continue
print(i)

i=1
while i<20:
if i==10:
i=i+1
continue
print(i)
i+=1

while True:
greeting=input("Enter the greeting: ")
if greeting=="bye":
print("Thank You for using my system")
break
print(greeting)

Write code to implement ATM Example.


#ATM Example - Menu Driven Program
accountNumber = int(input("Enter the account number: "))
accountHolderName = input("Enter the account holder name: ")
accountBalance = float(input("Enter the account balance: "))
while True:
print("1. Withdraw")
print("2. Deposit")
print("3. View Account Balance")
print("4. Exit")
choice = int(input("Enter your choice: "))
if choice==1:
amount=float(input("Enter the amount that you want to withdraw: "))
if amount < 0:
print("Invalid Amount! Please enter the amount greater than 0.")
elif amount > accountBalance:
print("Insufficient Balance! Please enter the amount less than account
balance.")
else:
accountBalance = accountBalance - amount
print("Remaining Account Balance: ",accountBalance)
elif choice==2:
amount = float(input("Enter the amount that you want to deposit: "))
if amount < 0:
print("Invalid Amount! Please enter the amount greater than 0.")
else:
accountBalance = accountBalance + amount
print("Account Balance: ",accountBalance)
elif choice==3:
print("Account Number: ",accountNumber)
print("Account Holder Name: ",accountHolderName)
print("Account Balance: ",accountBalance)
elif choice==4:
print("Thank you! See you Again!")
break
else:
print("Invalid Choice! Please enter value from 1 to 4.")

Python String:
- String is collection of characters and You can access character using index
number.
- Index starts from 0.

firstName = "Hello Veronica"


print(firstName[6])

Count length of the string without inbuilt function:


firstName = "Hello Veronica"
counter=0
for c in firstName:
counter+=1
print("Number of characters: ",counter)

Count length of the string using inbuilt function:


firstName = "Hello Veronica"
print(len(firstName))

str = "This is my first example of string"


#count number of words.
str = "This is my first example of string hello friends"
counter=1
#count number of words.
for c in str:
if c == " ":
counter+=1
print("Number of Words: ",counter)

str = "This is my first example of string hello"


print(len(str.split()))

split():
This function is used to convert string to words.
str = "This is my first example of string hello friends"
words = str.split()
print(words[6])

Slicing string:
str = "Hello Python"
print(str[0:4])
- print 0,1,2,3 character

str = "Hello Python"


print(str[6:9])
- print 6,7,8 character

str = "Hello Python"


print(str[6:])
- print 6 to end of string

str = "Hello Python"


print(str[:4])
- print 0,1,2,3 character

str = "Hello Python"


print(str[-5])

str = "Hello Python"


print(str[-4:-1])

str = "Hello Python"


print(str[:-2])

str = "Hello Python"


print(str[-4:])

You might also like