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

Infinite Loops and Examples in Python

An infinite loop is a loop that never terminates or stops executing. The document provides examples of infinite loops in Python that occur when the condition in a while loop never becomes False, causing the loop to repeat indefinitely. It also shows examples of counting digits, vowels, consonants, and special characters in a string, as well as swapping the first two characters between two strings and replacing all occurrences of a character in a string except the first.

Uploaded by

indu7405
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Infinite Loops and Examples in Python

An infinite loop is a loop that never terminates or stops executing. The document provides examples of infinite loops in Python that occur when the condition in a while loop never becomes False, causing the loop to repeat indefinitely. It also shows examples of counting digits, vowels, consonants, and special characters in a string, as well as swapping the first two characters between two strings and replacing all occurrences of a character in a string except the first.

Uploaded by

indu7405
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

INFINITE LOOPS

AND FEW PYTHON


PROGRAMS
References
• Introduction to computing and problem
solving using python by Balaguruswamy Rincy T A
Asst Professor in Computer Science
Prajyoti Niketan College, Pudukad
THE INFINITE LOOP
▪ A loop becomes infinite loop if a condition never becomes FALSE.
▪ You must use caution when using while loops because of the
possibility that this condition never resolves to a FALSE value.
▪ This results in a loop that never ends. Such a loop is called an infinite
loop.

Infine loops and few Examples 2


EXAMPLE ================== RESTART: F:/Python
ex/infiniteloopex.py ==================
var = 1 Enter a number :10
You entered: 10
while var == 1 : # This constructs an infinite loop
Enter a number :2
num = int(input("Enter a number :")) You entered: 2
Enter a number :3
print ("You entered: ", num) You entered: 3
Enter a number :1
You entered: 1
Enter a number :5
You entered: 5
Enter a number :20
You entered: 20
Enter a number :

Infine loops and few Examples 3


COUNT OF DIGITS VOWELS,
SPECIAL CHARS AND VOWELS
#count of digits, vowels, special chars and vowels

str=input("Enter a string")
v=c=d=s=0 ==================== RESTART: F:/Python
for char in str: ex/countvnumsp.py ====================
if char in "aeiou":v+=1
Enter a stringwelcom_python@class1234
Number of vowels= 4
if char in "bcdfghjklmnpqrstvwxyz":c+=1
Number of digits= 4
if char in "0123456789":d+=1
Number of Consonants= 13
if char in "&#@":s+=1 Number of Special Characters= 1
print("Number of vowels=",v) >>>
print("Number of digits=",d)
print("Number of Consonants=",c)
print("Number of Special Characters=",s)

4
SWAP FIRST TWO
CHARACTERS BETWEEN TWO
STRING
str=input("Enter first string:")
str2=input("Enter second string:")
print(str2[:2]+str[2:])
print(str[:2]+str2[2:])

==================== RESTART: F:\Python


ex\Deepthi\ex4.py ====================
Enter first string:hello
Enter second string:python
pyllo
hethon
>>>
5
REPLACE ALL OCCURRENCES OF
CHARACTER IN A STRING BY $
EXCEPT THE
str=input("Enter a string:")
FIRST CHAR
str1=input("Enter what is to be replaced:")
char=str[0]
==================== RESTART: F:\Python
str3=str.replace(str1,’$’) ex\Deepthi\ex5.py ====================
print(char+str3[1:]) Enter a string:python is a good language
Enter what is to be replaced:a
python is $ good l$ngu$ge
>>>
==================== RESTART: F:\Python
ex\Deepthi\ex5.py ====================
Enter a string:python is a good programming language
Enter what is to be replaced:p
python is a good $rogramming language
Infine loops and few Examples >>> 6
Thank
You

Infine loops and few Examples 7

You might also like