Gramin Technical and Management Campus
Department of computer engineering
Subject/code: -Python/22616
Name:-Pranusha Shiddhodhan Birhade
DOP: DOS:-
Practical No 10
1) the number of upper case letters and lower case letters.
Code:-
def count_case_letters(input_string):
upper_case_count = 0
lower_case_count = 0
for char in input_string:
if char.isupper():
upper_case_count += 1
elif char.islower():
lower_case_count += 1
return upper_case_count, lower_case_count
input_str = "Hello World!"
upper_count, lower_count = count_case_letters(input_str)
print(f"Uppercase letters: {upper_count}")
print(f"Lowercase letters: {lower_count}")
Output:-
2) Write a Python program to generate a random float where the value is
between 5 and 50 using Python math module.
Code:-
import random
import math
random_float = random.uniform(5, 50)
print(f"Random float between 5 and 50: {random_float}")
Output:-
3) Write Python program to demonstrate math built-in functions.
Code:-
import math
def demonstrate_math_functions():
print("Math Constants:")
print(f"Pi: {math.pi}")
print(f"Euler's number (e): {math.e}")
print()
num = 16
print("Basic Mathematical Functions:")
print(f"Square root of {num}: {math.sqrt(num)}")
print(f"Factorial of {num}: {math.factorial(num)}")
print(f"Ceiling of {num}: {math.ceil(num)}")
print(f"Floor of {num}: {math.floor(num)}")
print()
angle = 45
radians = math.radians(angle)
print("Trigonometric Functions:")
print(f"Sine of {angle} degrees: {math.sin(radians)}")
print(f"Cosine of {angle} degrees: {math.cos(radians)}")
print(f"Tangent of {angle} degrees: {math.tan(radians)}")
print()
print("Exponential and Logarithmic Functions:")
print(f"Exponential of 2: {math.exp(2)}")
print(f"Natural logarithm of e: {math.log(math.e)}")
print(f"Logarithm base 10 of 100: {math.log10(100)}")
print()
base = 2
exponent = 3
print("Power and Modulus Functions:")
print(f"{base} raised to the power of {exponent}: {math.pow(base, exponent)}")
print(f"Modulus of {num} by 3: {math.fmod(num, 3)}")
print()
print("Rounding Functions:")
float_num = 5.7
print(f"Round {float_num} to nearest integer: {round(float_num)}")
print(f"Truncate {float_num}: {math.trunc(float_num)}")
if __name__ == "__main__":
demonstrate_math_functions()
Output:-
4) Write Python program to demonstrate string built-in functions
Code:-
def demonstrate_string_functions():
sample_string = " Hello, World! "
another_string = "Python is awesome!"
print("Original String:")
print(f"'{sample_string}'")
print()
print("1. Length of the string:")
print(f"Length: {len(sample_string)}")
print()
print("2. String Methods:")
print(f"Uppercase: {sample_string.upper()}")
print(f"Lowercase: {sample_string.lower()}")
print(f"Title Case: {sample_string.title()}")
print(f"Swap Case: {sample_string.swapcase()}")
print()
print("3. Stripping Whitespace:")
print(f"Original: '{sample_string}'")
print(f"Stripped: '{sample_string.strip()}'")
print(f"Left Stripped: '{sample_string.lstrip()}'")
print(f"Right Stripped: '{sample_string.rstrip()}'")
print()
print("4. Finding Substrings:")
print(f"Find 'World': {sample_string.find('World')}")
print(f"Find 'Python': {sample_string.find('Python')} (not found returns -1)")
print()
print("5. Replacing Substrings:")
print(f"Replace 'World' with 'Python': {sample_string.replace('World', 'Python')}")
print()
print("6. Splitting and Joining Strings:")
words = another_string.split()
print(f"Split into words: {words}")
joined_string = ' '.join(words)
print(f"Joined back: '{joined_string}'")
print()
print("7. Checking String Properties:")
print(f"Is Alphanumeric: {another_string.isalnum()}")
print(f"Is Alphabetic: {another_string.isalpha()}")
print(f"Is Numeric: '123'.isnumeric() -> {'123'.isnumeric()}")
print(f"Is Lowercase: {another_string.islower()}")
print(f"Is Uppercase: {another_string.isupper()}")
print()
name = "Alice"
age = 30
print("8. String Formatting:")
formatted_string = f"My name is {name} and I am {age} years old."
print(formatted_string)
if __name__ == "__main__":
demonstrate_string_functions()
Output:-