CSE 1310 - Assignments

Not Graded Practice for Assignment 1


Exercise 1.

Given the Python expression:

2*3-5+4%2-1/4**2+6/2

(a) What is the output of the expression?
(b) Based on the precedence and associativity of the operators in Python, correctly parenthesize the expression such that you get the same output as above.


Exercise 2.

Predict the output of each of the following:

(a) 3**4/2. 
(b) 3**(4/2).
(c) (3**4)/2.

Exercise 3.

X1 = 5
X2 = 7
X3 = 11

average = X1+X2+X3/3
print(average)

The above code is an incorrect attempt to compute the average of three give numbers. The correct average of 5, 7 and 11 is 7.666. Find the error and rewrite the program so that it gets the desired output.

Also, explain exactly (only a few words are needed) why the code, as given above, is incorrect.


Exercise 4.

The code below is an incorrect attempt to compute the sum of two numerical values (int or float) prompted to the user.

X1 = input('Set X1:')
X2 = input('Set X2:')

sum = X1+X2;
print sum

Determine what was wrong with the code. Rewrite the program so that it gets the correct answer.


Exercise 5.

Write a program that prompts the user for 2 numbers and outputs the resulting sum, product and division between them, following the specified format.

Input: 5 and 2.

Output: (follow the same printing format as shown below)

Sum: 5 + 2 = 7
Product: 5 * 2 = 10
Division: 5 / 2 = 2.5

Exercise 6.

Write a Python code that computes the area of a triangle for given values a, b and c of its sides.

Suggestion: use Heron’s formula:


 


Exercise 7.

Write a program that prompts the user for a number (assume this number is always 3 digits long) and determines the product between its digits.

For example:

Input:  236

Output:

2*3*6 = 36

Suggestion: