Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views9 pages

Sori Befikadu

The document is an assignment for a Computer Programming IV course, detailing various Python programming tasks and questions. It covers topics such as data types, control structures, recursion, and function definitions. The assignment includes coding exercises and theoretical questions related to Python programming concepts.

Uploaded by

meriaddislenovo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Sori Befikadu

The document is an assignment for a Computer Programming IV course, detailing various Python programming tasks and questions. It covers topics such as data types, control structures, recursion, and function definitions. The assignment includes coding exercises and theoretical questions related to Python programming concepts.

Uploaded by

meriaddislenovo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

COMPUTER PROGRAMMING IV

C
ASSIGNMENT

NAME:SORI BEFIKADU…UGR/4926/17

Submission date : 26th May 2025


Submitted to : Instructor, Kinde Mekuria
1.In Python, what is the difference between “False” and False?
difference between “False” and False in Python:
• "False" (with quotes) is a string (str) and is considered True in Boolean context because
it's not empty.
• False (without quotes) is a Boolean (bool) value and represents falsehood.

2. Write a python if else statement that checks whether a given value is even or
odd number.

3. Write a python program to:


a. Find sum of digits of a number:
b. Reverse the given number:

c. Check the number is palindrome or not

4. Write a python program that checks if a given number is prime.


5. What is the value of x after the following statement execution?
x=1
x *= x + 1
Initial value of x is 1.
The expression multiplies x by (x + 1), which means 1 multiplied by 2.
So, the result is 2.
Final value of x is: 2
6. ANSWEAR
Since the temperature is 50, the first condition is true, so it displays:
Output: "too hot"
7.ANSWEAR
After execution of the code, what is the value of y?
Since x is 0, the condition "x > 0" is false.
Therefore, y is assigned the value after "else," which is 200.
Final value of y is: 200
8.ANSWEAR:
The string "Hello World" will be printed infinite times because the variable count
is never incremented, so the condition count < 10 remains true forever.
9.
What will the following Python code fragment display?
The loop starts with num = 7 and decreases it by 2 in each iteration, printing the
value after subtraction. The loop continues while num > 0.
Printed output:
10. What sequence will the function range(20,11,-3) will return?
This range() function starts at 20, ends before 11, and decreases by 3 each step.
Sequence returned:

11. For following python code fragment:


a. What will be printed on the screen?

ANSWEAR:A

B. Number of iterations:
Infinite, but only 5 values are printed before the loop becomes stuck due to the
continue statement.
12. Find sum and average of list of numbers using function composition.
(Function composition is a way of combining functions such that the result of
one function is passed as the argument of the next function)

13. If called the display function bellow is called with arguments as in display(‘Tests’,4), what
will be the output?

Output:
14. Write a python code that find sum of the first n natural numbers using
recursive function.

15. Write a python program that solves x power of y recursively.

16. Write a python program that finds GCD of two numbers.


17. Using while loop, implement countdown by n. def
countdown_by_n(count_from, count_by): """Prints a count down from
count_from by count_by. Stops printing before the result goes negative.”””

18.For the given function definition, if f=fib(3) is called, write the sequence of the lines executed
from the beginning where the fib function is call to the end of the function call. Hint the first to
be executed is line 10 and the last is also line 10, note that there is recursive calls. 1 def fib(n): 2
3 4 5 6 7 8 9 sum = 0 if n == 0 or n == 1: return 1 sum += fib(n-1) sum += fib(n-2) return sum 10
f=fib(3)

ANSWEAR: 10 -> 1 -> 2 -> 3 -> 6 -> 1 -> 2 -> 3 -> 6 -> 1 -> 2 -> 3 -> 4 -> 6 -> 7 -> 1 -> 2 -> 3 -> 4 ->
7 -> 8 -> 6 -> 7 -> 1 -> 2 -> 3 -> 4 -> 7 -> 8 -> 10

19. The following near_and_far() function accepts three ints a, b, and c as arguments and Return True if
one of b or c is "close" (differing from a by at most 1), while the other is "far", differing from both other
values by 2 or more. Fill all of the 5 spaces with the correct comparison or logical operators. (Hint:
near_far(2,3,4) returns true while near_far(2,3,4) returns false) def near_and_far(a, b, c): cond1 = abs(a-
b) ___ 1 and abs(b-c) ____ 2 and abs(a-c) ____ 2 cond2 = abs(a-c) ____1 and abs(a-b) ____ 2 and abs(c-
b) ____ 2 return cond1 ____ cond2

ANSWEAR: in completed form


def near_and_far(a, b, c):

cond1 = abs(a-b) <= 1 and abs(b-c) >= 2 and abs(a-c) >= 2

cond2 = abs(a-c) <= 1 and abs(a-b) >= 2 and abs(c-b) >= 2

return cond1 or cond2


20. For the sequence 2, 3, 6, 18, 108, 1944..., write a recursive function next_produc (n) that calculates the nth
number of the sequence.

21. Write a recursive function reverse() that returns the value of its string argument, except
reversed. For example, reverse("python") should return "nohtyp".

You might also like