3. Find the Sum of n Natural Numbers
n = int(input("Enter number: "))
sum = 0
while n > 0:
sum = sum + n
n = n - 1
print("The sum of natural numbers:", sum)
4. Display Multiplication Table
n = int(input("Enter number: "))
for i in range(1, 11):
c = n * i
print(n, '*', i, '=', c)
5. Check if a Given Number is Prime or Not
n = int(input("Enter number: "))
if n > 1:
for i in range(2, n):
if (n % i) == 0:
print(n, "is not a prime number")
break
else:
print(n, "is a prime number")
else:
print(n, "is not a prime number")
6. Implement Sequential Search
def sequential_search(dlist, item):
pos = 0
found = False
while pos < len(dlist) and not found:
if dlist[pos] == item:
found = True
else:
pos = pos + 1
return found, pos
print(sequential_search([11, 23, 58, 31, 56, 77, 45, 12, 65, 19], 31))
7. Create a Calculator Program
def addition(num1, num2):
return num1 + num2
def subtraction(num1, num2):
return num1 - num2
def multiplication(num1, num2):
return num1 * num2
def division(num1, num2):
return num1 / num2
print("Please select operation -\n"
"1. Addition\n"
"2. Subtraction\n"
"3. Multiplication\n"
"4. Division\n")
select = int(input("Select operations from 1, 2, 3, 4: "))
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
if select == 1:
print(num1, "+", num2, "=", addition(num1, num2))
elif select == 2:
print(num1, "-", num2, "=", subtraction(num1, num2))
elif select == 3:
print(num1, "*", num2, "=", multiplication(num1, num2))
elif select == 4:
print(num1, "/", num2, "=", division(num1, num2))
else:
print("Invalid input")
8. Explore String Functions
text = "welcome to python"
print("\nLowerCase string:")
print(text.lower())
print("\nUpperCase string:")
print(text.upper())
print("\nTitle string:")
print(text.title())
print("\nSwapcase string:")
print(text.swapcase())
print("\nCapitalize string:")
print(text.capitalize())
print("\nOriginal string:")
print(text)
print("\nLength of string:")
print(len(text))
x = text.replace("python", "supriya")
print(x)
9. Implement Selection Sort
def selection_sort(array):
length = len(array)
for i in range(length-1):
minIndex = i
for j in range(i+1, length):
if array[j] < array[minIndex]:
minIndex = j
array[i], array[minIndex] = array[minIndex],
array[i]
return array
array = [21, 6, 9, 33, 3]
print("The sorted array is:", selection_sort(array))
10. Implement Stack
stack = []
stack.append("a")
stack.append("b")
stack.append("c")
print("Initial stack:")
print(stack)
print("\nElements popped from stack:")
print(stack.pop())
print(stack.pop())
print(stack.pop())
print("\nStack after elements are popped:")
print(stack)
1. Program to Demonstrate usage of basic regular expressions.
import re
text = "Hello, World!"
pattern = r"Hello"
print("text =", text)
print("we are searching:", pattern)
match = re.search(pattern, text)
if match:
print(pattern, "Pattern found.")
else:
print(pattern, "Pattern not found.")
2. Program to Demonstrate use of advanced regular expressions for data validation.
import re
# Get user input
email = input("Enter your email address: ")
phone = input("Enter your phone number: ")
# Define regex patterns
email_pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
phone_pattern = r'^\d{10}$'
# Validate email
if re.match(email_pattern, email):
print("Valid email address.")
else:
print("Invalid email address.")
# Validate phone number
if re.match(phone_pattern, phone):
print("Valid phone number.")
else:
print("Invalid phone number.")
3. Demonstrate use of List
# Initial list
List = [10, 20, 14]
print("\nList of numbers:")
print(List)
# Append elements
List.append(1)
List.append(2)
List.append(4)
print("\nList after addition of three elements:")
print(List)
# Insert elements
List.insert(3, 12)
List.insert(0, "os")
print("\nList after performing insert operations:")
print(List)
# Extend the list
List.extend([8, "python", "always"])
print("\nList after performing extend operation:")
print(List)
# Reverse a list
myList = [1, 2, 3, 4, 5, "os", "python"]
myList.reverse()
print("\nmyList after reversing elements:")
print(myList)
# Remove elements
Comments
Post a Comment