)جميع اسئلة المقالي للسنين اللي فاتت (الجزء المحلول#
Q1
Given the following python code. What is the output?
#produce a mystery table
Size=3
for row in range(1, size+1):
for column in range(1, size+1):
print(row*column,end=’’)
print()
123
246
369
Q2
Write a python program that counts and prints the number of even and odd numbers from a series of
numbers.
N.B: You should declare at list at the beginning of your program for the bellow sample numbers.
Sample numbers:1 , 2, 3, 4, 5, 6, 7, 8, 9
Expected Output:
Number of even numbers: 4 #االرقام الزوجيه
Number of odd numbers : 5 # االرقام الفرديه
nums= [1,2,3,4,5,6,7,8,9]
odd = 0
even = 0
for i in range(len(nums)):
if(nums[i] % 2 == 0):
even += 1
else:
odd +=1
print(f"Numbers of even numbers: {even}")
print(f"Numbers of odd numbers: {odd}")
Q3
Given the following python codes. What is the output of each code?
A)
#produce a shape
N=5
for i in range(n):
for j in range(i):
print(‘* ’, end=””)
print(“”)
for I in range(n,0,-1):
for j in range(i):
print(“* ”, end=””)
print(“”)
B)
word = “STAR”
for char in range(len(word) – 1, -1 , -1):
print(word[char], end=”*”)
print(“\n”)
A)
*
**
***
****
*****
****
***
**
*
B)
R*A*T*S*
Q4
Write a Python program which iterates the integers from 0 to 10 (both included). For multiples of
three print “M3” instead of the number and for the multiples of five print “M5” .
For numbers which are multiples of both three and five “M35”
lst=[0,1,2,3,4,5,6,7,8,9,10]
for i in range(len(lst)):
if(lst[i] % 3 == 0 and lst[i] % 5 == 0):
print("M35")
elif(lst[i] % 3 == 0):
print("M3")
elif(lst[i] % 5 == 0):
print("M5")
else:
print(lst[i])
Q5
Write a python program that reads two integers representing a month and day and prints the season
for that month and day
Sample input:
Input the month (e.g. January, February etc.): july
Input the day: 31
Output:
Season is autumn
month = input("Input the month (e.g. January,February etc.) : ")
day = eval(input("Input the day : "))
if(month in ["january","february","march"]):
season = "spring"
elif(month in ["april","may","june"]):
season = "summer"
elif(month in ["july","august","september"]):
season = "autumn"
elif(month in ["octobr","november","december"]):
season = "winter"
if(month=="march") and (day > 19):
season = "spring"
elif(month=="june") and (day > 20):
season = "summer"
elif(month=="september") and (day > 21):
season = "autumn"
elif(month=="december") and (day > 20):
season = "winter"
print(f"Season is {season}")
#اعملي منشن لو لقيت حل صح, ^^ اسوا كود حرفيا و ممكن يكون فيه اخطاء
Q6
Design and implement a program to draw a triangle, using a loop.
Draw a triangle
• for sides from 1 to 3
• draw a line of length 60 units
• turn left by 120 degree
IMPLEMENTED
from turtle import *
for sides in range(1,4):
forward(60)
left(120)
Q7
Write an algorithm to tell somebody how to walk 500 steps, jumping on every fifth step, and shouting
“I like this” every 20 steps. Call it’walk and have fun’
Walk and have fun
. for step-counter in range(1,500)
. walk a step
. if step-counter multiple of 5
. jump
. if step-counter multiple of 20
. shout “I like this”
Q8
Given the following python code. What is the output?
size = 5
for row in range(0, size):
for column in range(0 ,size):
print(row+column, end="")
print()
012345
12345
23456
34567
45678
Q9
Write a python program to print these lists:- L2 , L7 and Lrest for values beetwen 1 and 30(both
included)
-The three lists L2 L7 Lrest should be declared and initiated as empty lists.
- The program should iterate the whole values beetwen 1 and 30 and check if the current number is a
multiple of 2 and add it to L2.
-if not , it should check if the number is a multiple of 7 and add it to L7
-otherwise, the value should be added to Lrest.
- at the end the 3 lists should be printed as in the fashion found below:
Sample output:
The multiples of 2 are : [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28]
The multiples of 7 are: [7, 21]
The remaining elements are :[1, 3, 5, 9, 11, 13, 15, 17, 19, 23, 25, 27, 29]
N.B: Values that are found in any list should not be found in any other list
nums = []
for n in range(1,30): #to saving time, you can add it manually
nums.append(n)
L2=[]
L7=[]
Lrest=[]
for i in range(len(nums)):
if(nums[i] % 2 == 0 ):
L2.append(nums[i])
elif(nums[i] % 7 == 0):
L7.append(nums[i])
else:
Lrest.append(nums[i])
print(f"The multiples of 2 are : {L2}")
print(f"The multiples of 7 are : {L7}")
print(f"The remainning elements are : {Lrest}")
Q10
A) Write a Python code function fill_list that prompts the user to enter non-negative integers.
The function should keep on entered. Make sure that this list can be used in other parts of a
program
B) Suppose that we want to call the above function and print only the numbers that are multiple
of 3 on one line. Write down the statements that implement the task.
Expected Output:
Enter a non-negative integer, any negative integer to stop 0
Enter a non-negative integer, any negative integer to stop 2
Enter a non-negative integer, any negative integer to stop 3
Enter a non-negative integer, any negative integer to stop 5
Enter a non-negative integer, any negative integer to stop 6
Enter a non-negative integer, any negative integer to stop 9
Enter a non-negative integer, any negative integer to stop -1
0369
fill_list=[]
num = eval(input("Enter a non-negative integer, any negative integer to stop : "))
while(num != -1):
fill_list.append(num)
num = eval(input("Enter a non-negative integer, any negative integer to stop : "))
else:
for i in range(len(fill_list)):
if(fill_list[i] % 3 == 0):
print(fill_list[i], end=" ")