Pythonfile2
Pythonfile2
Pythonfile2
Question 1:
Write a program which accepts a sequence of comma console and generate a list and a tuple which
contains every number. Suppose the following input is supplied to the program:
Input: 34,67,55,33,12,98
Then, the output should be: ['34', '67', '55', '33', '12', '98']
('34', '67', '55', '33', '12', '98').
Code:
number_list = input_values.split(',')
number_tuple = tuple(number_list)
print("List:", number_list)
print("Tuple:", number_tuple)
Output:
Question 2:
Accept a hyphen and prints the sorted words Write a Python program that accepts a hyphen input
and prints the words in a hyphen alphabetically.
Sample Output: green-red-black-white
black-green-red-white
Explanation: In the exercise above the code takes user input, splits it based on the hyphen stores the
parts in a list called 'items'. It then sorts these order (ascending order for text strings and numerical
order for numbers). Finally, it joins the sorted items using the hyphen string.
Code:
input_string = input("Enter words separated by hyphens: ")
items = input_string.split('-')
items.sort()
sorted_string = '-'.join(items)
print("Sorted output:", sorted_string)
Output:
Question 3:
Write a Python program to create a chain of function decorators (bold, italic, underline etc.).
Sample Output: hello world
Explanation: The above code shpuld defines three decorators ('make_bold', 'make_italic',
a'make_underline') that wrap a function by adding HTML tags for bold, italic, and underline,
respectively. Then, it decorates the "hello()" function by applying multiple decorators (@make_bold,
@make_italic, @make_underline) in a chained manner. Finally, it prints the result of the decorated
"hello()" function, which includes HTML tags for bold, italic, and underline applied to the original
return value "hello world".
Code:
def make_bold(func):
def wrapper():
return f"<b>{func()}</b>"
return wrapper
def make_italic(func):
def wrapper():
return f"<i>{func()}</i>"
return wrapper
def make_underline(func):
def wrapper():
return f"<u>{func()}</u>"
return wrapper
@make_bold
@make_italic
@make_underline
def hello():
return "hello world"
print(hello())
Output:
Question 4:
Write a Python function to check whether a string is a pangram or not.
Note : Pangrams are words or sentences containing every letter of
least once.
For example : "The quick brown fox jumps over the lazy dog"
Sample Output: true
The above code should define a given string is a pangram (contains all the letters of the
utilizes the 'string' module to access the lowercase alphabet and creates a set of all
lowercase letters. The function then converts the input string to lowercase, creates
a set from it, and checks if the set of lowercase characters in the lowercase alphabet. Finally, it
prints the result of checking if the provided string is a pangram or not.
Code:
import string
def is_pangram(input_string):
alphabet_set = set(string.ascii_lowercase)
input_set = set(input_string.lower())
input_string = "The quick brown fox jumps over the lazy dog"
print(is_pangram(input_string))
Output:
Question 5:
Write a Python function that prints out the first n rows of Pascal's triangle.
Note : Pascal's triangle is an arithmetic and geometric figure first imagined by Blaise Pascal.
Sample Pascal's triangle :
Each number is the two numbers above it added together
Sample Output: [1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
Explanation: The above code should define Pascal's Triangle up to a specified row 'n'. It uses a list
'trow' to represent each row and y as an auxiliary list for calculations. The function iterates through
the rows of the triangle, printing each row and updating it based on the previous row's values using
list comprehension. Finally, it generates Pascal's Triangle up to row 6 by calling the "pascal_triangle()"
function.
Code:
def pascal_triangle(n):
row = [1]
for i in range(n):
print(row)
row = [1] + [row[j] + row[j+1] for j in range(len(row)-1)] + [1]
pascal_triangle(6)
Output:
Question 6:
Write a Python function to check whether a number is "Perfect" or not.
According to Wikipedia : In number theory, a perfect number is a positive integer
that is equal to the sum of its proper positive divisors, that is, the sum of its positive
divisors excluding the number itself (also known as its aliquot sum). Equivalently, a
perfect number is a number that is half the sum of all of its positive divisors
(including itself).
Example : The first perfect number is 6, because 1, 2, and 3 are its proper positive
divisors, and 1 + 2 + 3 = 6. Equivalently, the number 6 is equal to half
its positive divisors: ( 1 + 2 + 3 + 6 ) / 2 = 6. The next perfect number is 28 = 1 + 2 +
4 + 7 + 14. This is followed by the perfect numbers 496 and 8128.
Sample Output: true
Explanation:
The above code should define determines whether a given number 'n' is a perfect number. It iterates
through numbers from 1 to 'n-1' to find factors of 'n' and calculates their sum. Finally, it
checks if the sum of factors is equal to the original number 'n' and prints the result (True if 'n' is a
perfect number, False otherwise) for the number 6.
Code:
def is_perfect_number(n):
if n <= 1:
return False
sum_of_divisors = 0
return sum_of_divisors == n
number = 6
print(is_perfect_number(number))
Output:
Question 7:
Write a Python program to detect the number of local variables declared in a function. Sample
Output: 3 Explanation: The above code should defines a function "abc()" that contains three local
variables: 'x', 'y', and 'str1'. However, it only defines these variables and doesn't utilize them further.
Then, it accesses the number of local variables defined in the "abc()" function using the
abc.__code__.co_nlocals attribute and prints the result.
Code:
def abc():
x = 10
y = 20
str1 = "Hello"
print(abc.__code__.co_nlocals)
Output: