0% found this document useful (0 votes)
14 views4 pages

Assignment 2

my assignment

Uploaded by

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

Assignment 2

my assignment

Uploaded by

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

1.

A.

def analyze_string(user_input):

if user_input.isdigit():

return "You entered a string containing only digits."

elif user_input.isalpha():

return "You entered a string containing only alphabets."

else:

return "You entered a string containing both alphabets and digits."

user_input = input("Enter a string: ")

result = analyze_string(user_input)

print(result)

print("The length of the string is:", len(user_input))

OUTPUT:

Enter a string: Hello123

You entered a string containing both alphabets and digits.

The length of the string is: 7

B.

user_input = input("Enter a string: ")

uppercase_string = user_input.upper()

print("Uppercase string:", uppercase_string)

OUTPUT:

Enter a string: Hello World

Uppercase string: HELLO WORLD

C.

user_input = input("Enter a string: ")

reversed_string = user_input[::-1]
print("Reversed string:", reversed_string)

OUTPUT:

Enter a string: Hello World

Reversed string: dlroW olleH

D.

string = "ChatGPT"

vowels = "aeiouAEIOU"

count = 0

for char in string:

if char in vowels:

count += 1

print("Number of vowels in the string:", count)

OUTPUT:

Number of vowels in the string: 2

2.

A.

def check_number(number):

if number > 0:

return "The number is positive."

elif number < 0:

return "The number is negative."

else:

return "The number is zero."

OUTPUT:

print(check_number(5)) # Output: The number is positive.

print(check_number(-3)) # Output: The number is negative.


print(check_number(0)) # Output: The number is zero.

B.

def check_number(number):

if number > 0:

print("The number is positive.")

elif number < 0:

print("The number is negative.")

else:

print("The number is zero.")

OUTPUT:

check_number(5) # Output: The number is positive.

check_number(-3) # Output: The number is negative.

check_number(0) # Output: The number is zero.

3.

A.

def print_even_numbers(numbers):

even_numbers = []

for number in numbers:

if number % 2 == 0:

even_numbers.append(number)

return even_numbers

OUTPUT:

numbers_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

even_numbers = print_even_numbers(numbers_list)

print("Even numbers:", even_numbers)

B.
def print_even_numbers(numbers):

for number in numbers:

if number % 2 == 0:

print(number)

OUTPUT:

numbers_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

print_even_numbers(numbers_list)

4.

A.

def countdown(start):

while start > 0:

print(start)

start -= 1

OUTPUT:

countdown(5)

B.

def countdown(start):

while start >= 0:

print(start)

start -= 1

OUTPUT:

countdown(5)

You might also like