Python Laboratory Excersices
L1_2.2 Python Programs
Step 3: Modify the above program to display the source code.
L1_2.3 Syntax Errors and Bugs
L1_2.4 Comparing Python and C
Compile the code as follows. This will produce an executable file a.out.$ gcc test2.c -lm
Run the executable as follows.$ ./a.out -lm
L3 Conditional Control Structures
Develop a program to input a temperature reading in either Celsius (C) or Fahrenheit (F) scale and convert it to the other scale. The temperature reading consists of a decimal integer followed by letter "F" or "f" if the temperature is in Fahrenheit scale or letter "C" or "c" if it is in Celsius scale. You may use a similar format for the output of the program. You must draw a flowchart to express the algorithm of your program.Note : 9C = 5(F - 32)
Given a date as a triplet of numbers (y, m, d), with y indicating the year, m the month (m = 1 for January, m = 2 for February, etc..) and d the day of the month, the corresponding day of the week f (f = 0 for Sunday, f = 1 for Monday, etc..) can be found as follows:
- If m < 3, let m = m + 12 and let y = y - 1
- Let a = 2m + 6(m + 1) / 10
- let b = y + y/4 - y/100 + y/400
- let f1 = d + a + b + 1
- Let f = f1 mod 7
Develop a program that will read a date and print the corresponding day of the week. All divisions indicated above are integer divisions. You must draw a flowchart to express the algorithm of your program.
Develop a program to implement a simple arithmetic calculator. First display a menu of 4 operations to the user to choose from (e.g., 1 - addition, 2 - subtraction, 3 - multiplication, 4 - division). After the operation is selected, ask the user to enter two numbers. Then perform the selected operation between the two numbers and display the result. After implementing the basic calculator, try to enhance it by adding features (e.g., add more operations to the menu such as integer division and modulo division, add exception handling, handle division by zero, formatting of results,...)
L5 Loop Control Structures
Develop a program that takes as input a series of positive integers and outputs whether each is a prime. The program should terminate if a negative integer is given as the input. A prime number is a number that is divisible by only one and itself. However one is not considered a prime number.
Develop a program that takes as input a series of positive integers and outputs whether each is a perfect number. The program should terminate if a negative integer is given as the input. A perfect number is a number whose factors other than itself add up to itself. Examples: 6 = 1 + 2 + 3, 28 = 1 + 2 + 4 + 7 + 14
Develop a program to implement the "Guess the Number" game. In this game, the program thinks of a random integer in the range[1, 20] and asks the user to guess it. The user gets only four attempts, and for each wrong guess, the program will tell if the guess is too high or too low. To win the game, the user has to correctly guess the number within four attempts.
Develop a program to compute and display the total and the average of 10 integer inputs from the keyboard. You must use a list to store the 10 integers.
Develop a program to find and display the minimum and the maximum among 10 numbers entered from the keyboard. You must use a list to store the numbers entered. The numbers can be non-integers.
Suppose there are 4 students each having marks of 3 subjects. Develop a program to read the marks from the keyboard and calculate and display the total marks of each student. Use a 2D list to store the marks.
Develop a program to read names of two sports that you and your friends love to play and watch. Then generate all sentences where the subject is in ["I", "We"], the verb is in ["play", "watch"] and the object is in the two sports. Use lists to store the words and generate the sentences by iterating through the lists using deeply nested loops.
Develop a program to read in two matrices A and B with dimensions 3 x 4 and 4 x 3 respectively, and compute and display their product AB (of dimensions 3 x 3), Assume that the elements of the matrices are integers. You must use functions while implementing this program.
Hint: Define global variables of two-dimensional arrays to store the value of the matrices A and B. Break down the program into thre functions as follows.
- Read in the elements of matrices A and B
- Compute the product of the two matrices
- Display the resultant matrix AB