do while loop in Python
Python does not have built-in functionality to explicitly create a do while loop like other
languages. But it is possible to emulate a do while loop in Python.
How to emulate a do while loop in Python
To create a do while loop in Python, you need to modify the while loop a bit in order to get similar
behavior to a do while loop in other languages.
As a refresher so far, a do while loop will run at least once. If the condition is met, then it will run again.
The while loop, on the other hand, doesn't run at least once and may in fact never run. It runs when and
only when the condition is met.
while True:
do_something()
if condition():
break
Python doesn't have do-while loop. But we can create a program like this.
The do while loop is used to check condition after executing the statement. It is like
while loop but it is executed at least once.
Syntax while if
while True:
# statement (s)
If not condition:
break;
i=1
while True:
print(i)
i=i+1
if(i > 5):
break
i = int(input("enter no 1 to 5 = "))
while True:
print(i)
i=i+1
if(i > 5):
break
#The code will run at least one time, asking for user input.
secret_word = "python"
counter = 0
while True:
word = input("Enter the secret word: ").lower()
counter = counter + 1
if word == secret_word:
break
if word != secret_word and counter > 7:
input("you lost")
break
#: take multiple input from user until press y / n
while True:
a = int(input("Enter no.: "))
print("square root is " , a*a)
reply = input('do you want to continue (y/n): ')
if reply == 'n': break
Python Switch Case Statement
Is there any python switch statement?
It is usually found that the use of switch statements is very rare while programming in the python language. So,
always a question arises whether the python language supports the switch case statements or not? Well, the answer
to this question is NO. Unlike any other programming language, python language does not have switch statement
functionality. This is also a trick question for students to get in their python assignments. So, we use the other
alternatives which can replace the switch case statement functionality and make the programming easier and faster.
Let us study each python switch statement one by one below.
Method 1) If-elif-else
lang = input("What's the programming language you want to learn?- JavaScript, Python, PHP, Solidity,
Java== ")
if lang == "JavaScript":
print("You can become a web developer.")
elif lang == "Python":
print("You can become a Data Scientist")
elif lang == "PHP":
print("You can become a backend developer")
elif lang == "Solidity":
print("You can become a Blockchain developer")
elif lang == "Java":
print("You can become a mobile app developer")
else:
print("The language doesn't matter, what matters is solving problems.")
Method 2) match-case
#EXAMPLE2: An example of a switch statement written with the match case syntax is shown below. It
is a program that prints what you can become when you learn various programming languages:
lang = input("What's the programming language you want to learn?- JavaScript, Python, PHP, Solidity,
Java== ")
match lang:
case "JavaScript":
print("You can become a web developer.")
case "Python":
print("You can become a Data Scientist")
case "PHP":
print("You can become a backend developer")
case "Solidity":
print("You can become a Blockchain developer")
case "Java":
print("You can become a mobile app developer")
case _:
print("The language doesn't matter, what matters is solving problems.")
#example3:
num1 = int(input("enter ist no. "))
num2 = int(input("enter iind no. "))
choice = int(input("1-addition, 2-subtraction 3-multiplication, 4-division "))
match choice:
case 1:
print("THE ADDITION OP IS",num1+num2)
case 2:
print("THE ADDITION OP IS",num1-num2)
case 3:
print("THE ADDITION OP IS",num1*num2)
case 4:
print("THE ADDITION OP IS",num1/num2)
case _:
print(" SORRY WRONG OPTIONS")
a=int(input("entre a no"))
b=1
c=1
while b<=a:
c=c*b
b+=1
print("factorial",c)
a=int(input("entre"))
b=int(input(" entre"))
while b<=a:
if b%2==0:
print("even",b)
else:
print("odd",b)
b=b+1
HOMEWORK
SWITCH = AREA OF CIRCLE,RECT,TRI,SQUARE
WHITE TRUE: SIMPLE INTEREST