0% found this document useful (0 votes)
33 views

CS 4 Ans - Introduction To Python

This document contains 5 Python code examples: 1. A program to find the factors of a number and determine if they are even or odd. 2. A program to accept a string, split it into words, sort the words alphabetically and print them. 3. A program to find all numbers between 1000 and 3000 that have even digits and print them in a comma separated list. 4. A program to accept a string, count the number of letters and digits, and print the results. 5. A program to determine if a string is a palindrome by comparing it to its reverse.

Uploaded by

asha.py81
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)
33 views

CS 4 Ans - Introduction To Python

This document contains 5 Python code examples: 1. A program to find the factors of a number and determine if they are even or odd. 2. A program to accept a string, split it into words, sort the words alphabetically and print them. 3. A program to find all numbers between 1000 and 3000 that have even digits and print them in a comma separated list. 4. A program to accept a string, count the number of letters and digits, and print the results. 5. A program to determine if a string is a palindrome by comparing it to its reverse.

Uploaded by

asha.py81
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
You are on page 1/ 5

Introduction to Python

Case Study -4(CS 04)

1. Write a program which will find factors of given number and find whether
the factor is even or odd.

Hint: Use Loop with if-else statements

Input:
Number: 50

Output:

factor is odd: 1
Factor is even: 2
factor is odd: 5
Factor is even: 10
factor is odd: 25
Factor is even: 50

Ans:

num=50
for i in range(1,num+1):
rem=num%i

if(rem==0):
if(i%2==0):
print("Factor is even:",i)
else:
print("factor is odd: ",i)

else:
pass

2. Write a code which accepts a sequence of words as input and prints the words
in a sequence after sorting them alphabetically.
Hint: In case of input data being supplied to the question, it should be
assumed to be a console input.

Input:

"Welcome to Python"

Output:
The sorted words are:

Python
Welcome
to

Split () , Split into words


Ans:

my_str = "Welcome to Python"

# breakdown the string into a list of words


words = my_str.split()

# sort the list


words.sort()

# display the sorted words

print("The sorted words are:")


for word in words:
print(word)

3. Write a program, which will find all the numbers between 1000 and 3000
(both included) such that each digit of a number is an even number. The
numbers obtained should be printed in a comma separated sequence on a
single line.

Hint: In case of input data being supplied to the question, it should be


assumed to be a console input. Divide each digit with 2 and verify is it even or
not.

Output:
2000,2002,2004,2006,2008,2020,2022,2024,2026,2028,2040,2042,2044,2046,20
48,2060,2062,2064,2066,2068,2080,2082,2084,2086,2088,2200,2202,2204,2206,
2208,2220,2222,2224,2226,2228,2240,2242,2244,2246,2248,2260,2262,2264,22
66,2268,2280,2282,2284,2286,2288,2400,2402,2404,2406,2408,2420,2422,2424,
2426,2428,2440,2442,2444,2446,2448,2460,2462,2464,2466,2468,2480,2482,24
84,2486,2488,2600,2602,2604,2606,2608,2620,2622,2624,2626,2628,2640,2642,
2644,2646,2648,2660,2662,2664,2666,2668,2680,2682,2684,2686,2688,2800,28
02,2804,2806,2808,2820,2822,2824,2826,2828,2840,2842,2844,2846,2848,2860,
2862,2864,2866,2868,2880,2882,2884,2886,2888

Ans:

values = []
for i in range(1000,3001):
s = str(i)
if (int(s[0])%2==0) and (int(s[1])%2==0) and (int(s[2])%2==0) and
(int(s[3])%2==0):
values.append(s)

print (",".join(values))

4. Write a program that accepts a sentence and calculate the number of letters
and digits.
Input:
Suppose if the entered string is: Python0325
Output:
Then the output will be:
LETTERS: 6
DIGITS:4
Hint: Use built-in functions of string.- isdigit() and isalpha()
Ans:

s = "aac34520"
d={"DIGITS":0, "LETTERS":0}
for c in s:
if c.isdigit():
d["DIGITS"]+=1
elif c.isalpha():
d["LETTERS"]+=1
else:
pass
print("LETTERS", d["LETTERS"])
print("DIGITS", d["DIGITS"])

5. Design a code which will find the given number is Palindrome number or not.

Hint: Use built-in functions of string reversed() (Not case Sensitive)

The reversed() function returns a reversed iterator object.

Example
alph = ["a", "b", "c", "d"]
ralph = reversed(alph)
for x in ralph:
print(x)

Input:
my_str = 'aIbohPhoBiA'

Output:

Ans:

It is palindrome

# change this value for a different output


my_str = 'aIbohPhoBiA'

# make it suitable for caseless comparison


my_str = my_str.casefold()
# reverse the string
rev_str = reversed(my_str)

# check if the string is equal to its reverse


if list(my_str) == list(rev_str):
print("It is palindrome")
else:
print("It is not palindrome")

Option 2

my_str = 'aIbohPhoBiA'
my_str = my_str.casefold() # make it suitable for caseless comparison
if my_str == my_str[::-1]:
print("It is palindrome")
else:
print("It is not palindrome")

You might also like