Module 3 part1
Module 3 part1
Creating Loops with while and for, Different versions of Loops, Nested
Loops, Loop Control Statements, Loop Modification with break,
continue and pass, Regular Expressions.
i=1
n=int(input("enter n value"))
while i<=n:
print(i)
i=i+2
print("end of loop")
Output:
Enter a number: 127
Sum of digits: 10
01-07-2024 Dr. V.Srilakshmi 6
Write a Python program to check if a number is a palindrome or not
while i<=10:
print(“VIT”)
i++
else:
print("testing")
print("end of loop")
• Here, variable takes the value of the item inside the sequence on each iteration.
• Loop continues until it reach the last item in the sequence.
01-07-2024 Dr. V.Srilakshmi 11
# Program to find the sum of all numbers stored in a list#
numbers = [6, 5, 3, 8]
sum = 0
for i in numbers:
sum = sum+i
print("The sum is", sum)
Output:
The sum is 22
text = “University"
vowels = "aeiouAEIOU"
vowel_count = 0
for char in text:
if char in vowels:
vowel_count += 1
print("Vowel count:", vowel_count)
Output:
Vowel count: 4
num = 152
if num > 1: 152 is not a prime number
for i in range(2, num):
if (num % i) == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number")
Output:
Output: V
1 I
2 T
3 -
It display values before 4
n=int(input("enter n"))
for i in range(1, n+1):
for j in range(1, i + 1):
print(j,end=" ")
print()
n=int(input("enter n"))
for i in range(1, n+1):
for j in range(1, i + 1):
print(“*”,end=" ")
print()
n= 5
for i in range(n, 0, -1):
for j in range(1, i + 1):
print("* ", end="")
print()
n = 5
for i in range(1, n+1):
for j in range(n - i):
print(' ', end='')
for k in range(2*i - 1):
print('*', end='')
print()
n = 5
for i in range(1, n+1):
for j in range(n - i):
print(' ', end='')
for k in range(2*i - 1):
print(k+1, end='')
print()
size = 5
for i in range(size): # print spaces
for j in range(i):
print(" ", end="") # print numbers
for k in range(2 * (size - i) - 1):
print(k+1, end="")
print()
• A perfect number is a positive integer that is equal to the sum of its proper
divisors (excluding itself).
• For example, 6 is a perfect number in Python because 6 is divisible by 1, 2,
3, and 6.
• So, the sum of these values are 1+2+3 = 6 (Remember, we have to exclude
the number itself. That’s why we haven’t added 6 here).
• Some of the perfect numbers are 6, 28, 496, etc.
n = 5
for i in range(n+1): 54321
for j in range(n-i,0,-1):
print(j,end=' ')
4321
print() 321
21
1
n = 5
alpha = 65
for i in range(n+1):
A
# print spaces ABC
for j in range(n - i): ABCDE
print(" ", end="")
ABCDEFG
ABCDEFGHI
# print alphabets
for k in range(2 * i -1):
print(chr(alpha + k), end="")
print()
print("Sorted array:",a)
Sorted array: [9, 11, 12, 22, 25, 34, 64]
import re
txt = "abacus"
patterns= 'ab*'
if(re.search(patterns,txt)):
print("Found a match!")
Found a match!
else:
print('Not matched!')
import re
text = "aba_cus"
patterns = '[a-z]_[a-z]'
if re.search(patterns, text):
print('Found a match!')
else:
print('Not matched!')
import re
text = "abaub"
patterns = 'a.*b$'
if re.search(patterns, text):
print('Found a match!')
else:
Found a match!
print('Not matched!')
import re
text = "The quick brown fox jumps over the lazy dog."
patterns = '\wz'
if re.search(patterns, text):
print('Found a match!')
else:
print('Not matched!')
import re
url=
"https://www.washingtonpost.com/news/wp/2016/09/02/odell/"
d=re.findall('/(\d{4})/(\d{1,2})/(\d{1,2})/', url)
[('2016', '09', '02')]
print(d)
import re
mobile = "9502734525"
pattern = "[789]\d{9}$"
if re.search(pattern, mobile):
print("Valid mobile:")
else:
print("Invalid mobile!")
import re
email = "[email protected]"
pattern = "^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$"
if re.search(pattern, email):
print("Valid email address:")
else:
print("Invalid email address!!")
Construct a regex that matches abc followed by zero to many digits (0-9).
abc\d*
Construct a regex that matches br followed by any single character except for a new line and then 3.
br.3
Construct a regex that matches a word that starts with an uppercase letter followed by at least one
lowercase letter, like “Apple”, “Banana”, or “Carrot”.
[A-Z][a-z]+
01-07-2024 Dr. V.Srilakshmi 48
Construct a regex that matches eight word characters (letter, digit, or underscore).
\w{8}
Construct a regex that matches one or more lowercase letters (a-z) followed by a space character and
then two to four digits.
[a-z]+\s\d{2,4}
Construct a regex that captures strings that have two digits followed by a period and then four letters
from a to z.
\d{2}\.[a-z]{4}
import re