Description
1. Write pseudocode for the following problems.
(a) Compute and output the perimeter of a rectangle.
(b) Check if an input number is either 5 or 6.
(c) Display the following rectangular pattern:
* * * * *
* *
* *
* *
* * * * *
2. Predict the output of the following Python codes.
(a) print(2**2**3)
(b) print(30 – 3**2 + 8/3**2*10)
(c) print(7.0 % 5)
(d) x = 2 y = 3
print(((x != 1) or (y >= 5)) and ((x < 2) and (y > 1)))
(e) x = 5 x //= 2 x **= 3
print(x)
3. (a) Write Python code that prompts for a complex number from the user and uses the cmath module to compute the exponential of the complex number as well as returns the representation of the complex number in polar coordinates.
Current time is 12:22:29
4. (a) Write Python code that reads in the length of a square from the user as a real number and then compute the area of the square.
(b) Write Python code that prompts for the principal amount p, number of years n, and annual percentage rate r from the user and then computes the simple interest which is equal to the product of these three quantities divided by 100. Here are the sample input and output
Enter the principal amount: 15000 Enter the number of years: 6 Enter the annual interest rate (%): 7.5
The simple interest is 675000.0
5. (a) Write a Python program that accepts a positive integer as input and prints the sum of the cubes of all the positive integers smaller than the input number using a while statement.
(b) Write a Python program that accepts a positive integer n no greater than 10 as input and prints the multiplication table showing all multiples from 1 × n to n × n using a for statement.
(c) Write a Python program that accepts an integer as input and prints a list of all the divisors of that number using if and for statements.
(d) A palindrome is a number or a text phrase that reads the same backwards as forwards. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554, and 11611. Write a Python program that prompts for a five-digit integer from the user and determines whether it is a palindrome using if and while statements.
(e) Write a Python program that prints the numbers from 1 to 100. But it prints “Fizz” for multiples of three, prints “Buzz” for the multiples of five, and prints “FizzBuzz” for multiples of both three and five.
6. (a) Write Python code that removes all the bs from the string
abstring = ’abababababababab’
and creates the new string a string = ’aaaaaaaa’
without using the string.replace method.
(b) Write Python code that will print ’jihgfedcba’, ’adgj’, and ’igeca’ by slicing the string ’abcdefghij’.
(c) Write a Python program that accepts two strings as input and prints the string with a longer length. If the two strings have the same length, then the program prints these strings line by line.
(d) Write a Python program that prompts for a string and then prints the string in lower case without using the string.lower method.
(e) Write a Python program that prompts for a long string containing multiple words and then prints back the same string with the words in reverse order. Here are the sample input and output
Enter a long string: My name is Michele
The string with word order reversed: Michele is name My
7. (a) Write a Python program that prints all even numbers from the number list:
[386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328,
615, 953, 345, 399, 162, 758, 219, 918, 237, 412, 566, 826, 248,
866, 950, 626, 949, 687, 217, 815, 67, 104, 58, 512, 24, 892,
894, 767, 553, 81, 742, 717, 379, 843, 831, 445, 958, 743, 527] in the same order and stops printing any numbers that come after 237 in the list. (b) Write a Python program that prompts for a positive integer n and generates a list of lists of the form [[n], [n – 1, n], [n – 2, n – 1, n], …, [1, 2, …, n – 1, n]].
(c) Write Python code that accepts a sequence of comma-separated integers and uses list comprehension to generate a list of the squares of each odd integer in the sequence. Here are the sample input and output:
Enter a sequence of comma-separated numbers: 1,2,3,4,5,6,7,8,9 Squares of the odd numbers in the sequence: 1,9,25,49,81
(d) Write a Python program that generates and prints another tuple whose values are even numbers in the tuple (1, 2, 3, 4, 5, 6, 7, 8, 9, 10).
(e) Write a Python program to print the list after removing the 0th, 2nd, 4th, 6th elements from the list [12, 24, 35, 70, 88, 120, 155] by using list comprehension.
(Hints: Use enumerate() to get (index, value) tuple.)
8. Predict the output of the following Python codes.
(a) print(“{c} times {a} equals {b}.”.format(a=”six”, b=30, c=”five”))
(b) print(“Sum: {:4d}, Average: {:5.2f}”.format(88, 18.66666667))
(c) print(“**** {0:#<15} ****”.format(“Surprise!”))
(d) a, b = 128, -8
print(“{0:+7d} x{1:+6d} ——- ={2:+6d}”.format(a, b, a*b))
(e) a = 1.234567e-8 print(“{:8.3E}”.format(a)) print(“{:12.9f}”.format(a))
9. (a) Write a Python function that accepts two integers in string form and prints their product.
(b) Write a Python function that accepts a string and prints the number of upper case letters and lower case letters in the string.
(c) Write a Python function that returns the minimum of four numbers.
(d) Write a Python function that accepts a number and returns True if the number is prime and otherwise returns False.
(e) Write a recursive Python function that returns the sum of squares of the first n integers.
10. (a) Write a Python program that counts the number of lines in a text file.
(b) Write a Python program that prints the combinations of each line from the first file with the corresponding line from the second file as individual lines using the with statement. Assume the two files have the same number of lines. (Hint: Use the zip function to iterate over the lines from each file at the same time.)
(c) Write a Python program that creates a file in which all uppercase letters in the English alphabet are listed in order by specified number of letters on each line.
11. (a) Write a Python program that takes two digits m and n as input and generates a m×nnumpy array where the element in the i-th row and j-th column of the array is i ∗ j with i = 1, 2, …, m and j = 1, 2, …, n.
(b) Write Python code that generates the numpy arrays array([3, 7, 11, 15]), array([[5, 6, 7], [13, 14, 15]]), and array([[1, 3], [13, 15]]) by slicing the array a = np.arange(1, 17).reshape(4, 4).
(c) Write a Python program that sorts a given 2×2 numpy array along the first axis, the last axis, and on the corresponding flattened array.
(d) Predict the output of the following Python code: import numpy as np a = np.arange(1, 5).reshape(2, 2) b = np.arange(2, 9, 2).reshape(2, 2) np.vstack((a, b)) np.hstack((a, b)) np.dstack((a, b)) c = np.arange(24).reshape(3, 2, 4) np.vsplit(c, 3) np.dsplit(c, 2)
(e) Write a Python program that converts Celsius temperatures stored in a numpy array to Fahrenheit.
(f) Write a Python program that uses vectorization to create a numpy array that stores the values of the function z = xye−(x2+y2)/2 for the dataset x and y which are both 100 uniformly spaced points over the interval [−3,3].
12. (a) Write a Python program that creates a dictionary of names and birthdays, then asks the user to enter a person’s name, and prints the birthday of that person. Here are the sample input and output:
Welcome to the birthday dictionary. We know the birthdays of:
Niels Bohr
Albert Einstein
Werner Heisenberg
Max Planck
Erwin Schrodinger
Who’s birthday do you want to look up? Max Planck Max Planck’s birthday is 23 Apr 1858.
Welcome to the birthday dictionary. We know the birthdays of:
…
Who’s birthday do you want to look up? Peter Chan Sorry, we don’t have Peter Chan’s birthday.
(b) Write a Python program that counts and prints the numbers of each character in a string input by console.
Reviews
There are no reviews yet.