Python Assignment
Python Assignment
Verify
our answers by typing the expressions into Python.
a) 9 - 3
b) 8 * 2.5
c) 9 / 2
d) 9 / -2
e) 9 // -2
f) 9 % 2
g) 9.0 % 2
h) 9 % 2.0
i) 9 % -2
j) -9 % 2
k) 9 / -2.0
l) 4 + 3 * 5
m) (4 + 3) * 5
3. For each of the following expressions, in which order are the sub expressions
evaluated?
a. 6 * 3 + 7 * 4
b. 5 + 3 / 4
c. 5 - 2 * 3 ** 4
4. Two of Python’s built-in functions are min and max. In the Python shell,
execute the following function calls:
a. min(2, 3, 4)
b. max(2, -3, 4, 7, -5)
c. max(2, -3, min(4, 7), -5)
5. Following the function design recipe, define a function that has one parameter,
a
number, and returns that number tripled.
example
>>> triple(4)
12
6. Following the function design recipe, define a function that has two parameters,
both
of which are numbers, and returns the absolute value of the difference of the
two.
Hint: Call built-in function abs.
7. Following the function design recipe, define a function that has one parameter,
a
distance in kilometers, and returns the distance in miles. (There are 1.6
kilometers
per mile.)
Following the function design recipe, define a function that has three
parameters, grades between 0 and 100 inclusive, and returns the average
of those grades.
8. What value does each of the following expressions evaluate to? Verify your
answers by typing the expressions into the Python shell.
a. 'Computer' + ' Science'
b. 'Darwin\'s'
c. 'H20' * 3
d. 'C02' * 0
9. Express each of the following phrases as Python strings using the appropriate
type of
quotation marks (single, double, or triple) and, if necessary, escape
sequences. There
is more than one correct answer for each of these phrases.
a. They’ll hibernate during the winter.
b. “Absolutely not,” he said.
c. “He said, ‘Absolutely not,’” recalled Mel.
d. hydrogen sulfide
e. left\right
10. Rewrite the following string using single or double quotes instead of triple
quotes:
'''A
B
C'''
11. Use built-in function len to find the length of the empty string.
12. Given variables x and y, which refer to values 3 and 12.5, respectively, use
function print to print the following messages. When numbers appear in
the messages, variables x and y should be used.
a. The rabbit is 3.
b. The rabbit is 3 years old.
c. 12.5 is average.
d. 12.5 * 3
e. 12.5 * 3 is 37.5.
15. Write a function named different that has two parameters, a and b. The function
should return True if a and b refer to different values and should return False
otherwise.
16. Write a program that makes a backup of a file. Your program should prompt the
user for the name of the file to copy and then write a new file with the same
contents but with .bak as the file extension.
Suppose the file alkaline_metals.txt contains the name, atomic number, and atomic
weight of the alkaline earth metals:
beryllium 4 9.012
magnesium 12 24.305
calcium 20 20.078
strontium 38 87.62
barium 56 137.327
radium 88 226
Write a for loop to read the contents of alkaline_metals.txt and store it in a list
of lists, with each inner list containing the name, atomic number, and atomic
weight for an element. (Hint: Use string.split.)
Question;:
Write a program which will find all such numbers which are divisible by 7 but are
not a multiple of 5,
between 2000 and 3200 (both included).
The numbers obtained should be printed in a comma-separated sequence on a single
line.
Hints:
Consider use range(#begin, #end) method
Question:
Write a program which can compute the factorial of a given numbers.
The results should be printed in a comma-separated sequence on a single line.
Suppose the following input is supplied to the program:
8
Then, the output should be:
40320
Hints:
In case of input data being supplied to the question, it should be assumed to be a
console input.
Question:
With a given integral number n, write a program to generate a dictionary that
contains (i, i*i) such that is an integral number between 1 and n (both included).
and then the program should print the dictionary.
Suppose the following input is supplied to the program:
8
Then, the output should be:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}
Hints:
In case of input data being supplied to the question, it should be assumed to be a
console input.
Consider use dict()
Question:
Write a program which accepts a sequence of comma-separated numbers from console
and generate a list and a tuple which contains every number.
Suppose the following input is supplied to the program:
34,67,55,33,12,98
Then, the output should be:
['34', '67', '55', '33', '12', '98']
('34', '67', '55', '33', '12', '98')
Write a program that calculates and prints the value according to the given
formula:
Q = Square root of [(2 * C * D)/H]
Following are the fixed values of C and H:
C is 50. H is 30.
D is the variable whose values should be input to your program in a comma-separated
sequence.
Example
Let us assume the following comma separated input sequence is given to the program:
100,150,180
The output of the program should be:
18,22,24
Question
Write a program that accepts sequence of lines as input and prints the lines after
making all characters in the sentence capitalized.
Suppose the following input is supplied to the program:
Hello world
Practice makes perfect
Then, the output should be:
HELLO WORLD
PRACTICE MAKES PERFECT
A website requires the users to input username and password to register. Write a
program to check the validity of password input by users.
Following are the criteria for checking the password:
1. At least 1 letter between [a-z]
2. At least 1 number between [0-9]
1. At least 1 letter between [A-Z]
3. At least 1 character from [$#@]
4. Minimum length of transaction password: 6
5. Maximum length of transaction password: 12
Your program should accept a sequence of comma separated passwords and will check
them according to the above criteria. Passwords that match the criteria are to be
printed, each separated by a comma.
Example
If the following passwords are given as input to the program:
ABd1234@1,a F1#,2w3E*,2We3345
Then, the output of the program should be:
ABd1234@1
Hints:
In case of input data being supplied to the question, it should be assumed to be a
console input.
Python Program to Count the Number of Vowels Present in a String using Sets
Python Program to Check Common Letters in Two Input Strings
Python Program that Displays which Letters are in the First String but not in the
Second