0% found this document useful (0 votes)
16 views2 pages

Python String and Number Functions

Uploaded by

bc250204849har
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)
16 views2 pages

Python String and Number Functions

Uploaded by

bc250204849har
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

Python Programming Assignment 01

Question 1: Reverse String and Count Vowels

Objective:​
Write a function that takes a string input, reverses it, and counts how many vowels are in the
original string.

Python Code:​
def reverse_string_and_count_vowels():​
user_input = input("Enter your favorite quote or sentence: ")​
reversed_string = user_input[::-1]​
vowels = "aeiouAEIOU"​
count = sum(1 for ch in user_input if ch in vowels)​
print("Reversed string:", reversed_string)​
print("Total vowels found:", count)

Example Output:​
Enter your favorite quote or sentence: Dreams are valid​
Reversed string: dilav era smaerD​
Total vowels found: 6

Question 2: Check If Number Is Even or Odd

Objective:​
Take an integer input and print whether it's even or odd. If the user enters invalid input, show an
error message.

Python Code:​
def check_even_or_odd():​
try:​
number = int(input("Enter any whole number: "))​
if number % 2 == 0:​
print(f"{number} is Even")​
else:​
print(f"{number} is Odd")​
except ValueError:​
print("Error: Please enter a valid integer.")
Example Output:​
Enter any whole number: 15​
15 is Odd

Question 3: Sorting Numbers with NumPy (Using Virtual Environment)

Objective:​
Set up a virtual environment and use NumPy to sort a list of numbers entered by the user.

Steps:

1.​ Open terminal or command prompt.​

2.​ Create environment: python -m venv numpyenv​

3.​ Activate it:​

○​ Windows: numpyenv\Scripts\activate​

○​ macOS/Linux: source numpyenv/bin/activate​

4.​ Install NumPy: pip install numpy​

Python Code:​
import numpy as np​
def sort_numbers_using_numpy():​
try:​
nums = input("Enter numbers separated by spaces: ")​
num_list = list(map(int, [Link]()))​
sorted_nums = [Link](num_list)​
print("Sorted Numbers:", sorted_nums.tolist())​
except ValueError:​
print("Error: Only enter whole numbers separated by spaces.")

Example Output:​
Enter numbers separated by spaces: 10 5 8 1 4​
Sorted Numbers: [1, 4, 5, 8, 10]

You might also like