pythonsolve
Question 1
Write a Python program to find the smallest and largest of three numbers.
Sample Input : Enter the three numbers : 1,2,3
Sample Output : Largest : 3 Smallest : 1
Question 2
Write a python program that will print the following:
1 3 5 7 9 11
10 8 6 4 2 0
(Hint: After 11 the number gets subtracted by 1 then it gets subtracted by 2)
Question 3
Write a python program that will accept 10 numbers from the user and find the number of odd and even numbers.
Sample Input : 22, 11, 6, 99, 100
Sample Output: There are 2 odd numbers and 3 odd numbers
Question 4
Write a python program that will print the number of days if the user inputs the month name.
Sample Input : March
Sample Output: No of days : 31.
Question 3
Write a program that takes input from the user and when the user press enter to quit the program gives the sum of all even numbers and the sum of all odd numbers.
Sample:
Input
Enter a number 2
Enter a number 4
Enter a number 3
Enter a number 5
Enter a number (enter)
Output
Sum of even number: 6
Sum of Odd number: 8
Question 4
Write a program to take a string as input and your program should:
Print the same string with all lowercase letters. Note: You should not use islower() function.
Print the number of vowels ‘aeiou’ present in the string.
Question 5
Write a program to ask input from the user the number of asterisk to print and how many lines need to be printed.
Sample:
Input
Enter number of asterisk: 7
Enter number of lines: 4
Output
*******
*******
*******
*******
Question 6
Write a program to check whether the user input number is a prime number or not.
Sample:
Input
Enter number: 8
Output
Number 8 is not prime number.
Question 7
Write a program to print the numbers in the following format:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Question 8
Write a program that accepts a sentence and calculate the number of letters and digits.
Suppose the following input is supplied to the program:
hello class! 123
Then, the output should be:
LETTERS 10
DIGITS 3
Question 9
Write a script named copyfile.py. This script should prompt the user for the names of two text files. The contents of the first file should be input and written to the second file.
Questions:
1. Write a loop that accumulates the sum of all of the numbers in a list named data.
2. Define a function named sum. This function expects two numbers, named low and high, as arguments. The function computes and returns the sum of all of the numbers between low and high, inclusive.
3. Assume that the variable data refers to the dictionary {“b”:20, “a”:35}. Write the expressions that perform the following tasks:
4. Replace the value at the key “b” in data with that value’s negation.
5. Add the key/value pair “c”:40 to data.
6. Remove the value at key “b” in data, safely.
7. Print the keys in data in alphabetical order.
8. Write a Python program to get the smallest number from a list
9. Write a Python script to concatenate following dictionaries to create a new one
i. Sample Dictionary : dic1={1:10, 2:20} dic2={3:30, 4:40} dic3={5:50,6:60} Expected Result : {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
10. Write a Python function to check whether a number is in a given range
11. Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters. Sample String : 'The Quick Brow ' Expected Output : No. of Upper case characters : 3 No. of Lower case Characters : 12
12. Write a Python function that takes a list and returns a new list with unique elements of the first list. Sample List : [1,2,3,3,3,3,4,5] Unique List : [1, 2, 3, 4, 5]
13. Try the following code and rectify the mistakes in the code
a. def doSum(alist):
b. suval = 0
c. for l in alst:
i. sumval = sumval + l
d. return sumval
e. mylist = [1,2,3,4,5]
f. print(dSum(mylist))
14. Write a Python class which has two methods get_String and print_String. get_String accept a string from the user and print_String print the string in upper case
15. Write a Python class named Rectangle constructed by a length and width and a method which will compute the area of a rectangle
16. Define a class Person and its two child classes: Male and Female. All classes have a method "getGender" which can print "Male" for Male class and "Female" for Female class.
17. Create a Student class and initialize it with name and roll number. Make methods to : 1. Display - It should display all informations of the student. 2. setAge - It should assign age to student 3. setMarks - It should assign marks to the student.
Question 1 Explain what happens when the following recursive function is called with the value 4 as an argument
def example(n) :
if n > o:
print(n)
example(n-1) (05 Marks)
Question 2 a. What are instance variables, and what role does the name “self” play in the context of a class definition? (2.5 Marks)
b. How is the lifetime of an object determined? What happens to an object when it dies? (2.5 Marks)
Question 3 Define a method for the Bank class that returns the total assets in the bank (the sum of all account balances). (05 Marks)
Question 4 Two playing cards can be compared by rank. For example, an ace is less than a 2. When c1 and c2 are cards, c1.rank < c2.rank expresses this relationship. Explain how a method could be added to the Card class to simplify this expression to c1 < c2. (05 Marks)
Question 5 Write a python program that calculates and prints the number of minutes in a year. (05 Marks)
Question 6 a. Using python, write a loop that accumulates the sum of all of the numbers in a list named data. (2.5 Marks)
b. What is the purpose of the dir function and the help function? (2.5 Marks)
Question 7 Write a function called isprime which takes a single integer argument and returns True if the integer is a prime number, and False otherwise. A prime number is a number with only two distinct divisors, 1 and itself. Note: 1 is therefore not a prime number. (05 Marks)
Question 8 Write a Python program that read a line of input from the user until the user enters a blank. The program should print out the total number of occurrences of each letter in the alphabet. These should be printed in alphabetical order. Your program should work like this:
> ./letters
hello world
d 1
e 1
l 3
o 2
r 1
w 1 (05 Marks)
Question 9 Why does Python code generate fewer types of syntax errors than code in other programming languages? (05 Marks)
Question 10 Write a python program that will perform the operations of simple calculator. The operations are : Addition, subtraction, Multiplication and division.
Sample output :
Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4): 3
Enter first number: 15
Enter second number: 14
15 * 14 = 210 (05 Marks)
Question 11 Identify the error in the following program and write the correct python source code line = raw_input("Type a word")
print "You typed", line
line = line + "h"
num = int(line)
print "You typed the number ", num (05 Marks)
Question 12 a. Describe two fundamental differences between terminal-based user interfaces and GUIs. (2.5 Marks)
b. What roles do the parameters and the return statement play in a function definition? (2.5 Marks)
Question 13 Write a python code to sort the given set of numbers in ascending order. You must not use any built-in functions of python. (05 Marks)