0% found this document useful (0 votes)
21 views

Section1 Programming 2-Python

The document contains solutions to 7 Python programming exercises involving arrays and loops. The exercises include calculating sums of ranges of numbers, using break and continue in for loops, and different array methods like append, reverse, and remove.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Section1 Programming 2-Python

The document contains solutions to 7 Python programming exercises involving arrays and loops. The exercises include calculating sums of ranges of numbers, using break and continue in for loops, and different array methods like append, reverse, and remove.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

PYTHON GEHAD ELSHARKAWY

PROGRAMMING
EXERCISE 1:
Write a python program that accepts two integer numbers and an
operator, like (+, -,*,/), and perform the operation indicated by the
operator, and then print the result on screen
SOLUTION
EXERCISE 2:
Design a program to calculate the sum of integers from 1 to 1000 and
then calculate the sum of odd numbers from 1 to 1000 and then
calculate the sum of even numbers from 2 to 1000 and print each sum
separately with printing the final sum which is equal to the sum of
integers, odd and even.
SOLUTION
sum=0
sum_even=0
sum_odd=0
for x in range(1,1001):
sum+=x
if x%2==0:
sum_even+=x
else:
sum_odd+=x
print(‘the summation of the integer numbers from 1 to 1000 is”,sum,”\nthe
summation of the even numbers from 1 to 1000 is”, sum_even,”\nthe summation of
the odd numbers from 1 to 1000 is”,sum_odd)
EXERCISE 3: WHAT ARE
THE OUTPUT?
for i in range(5):
if i == 3:
break
print(i)

Output?

6
EXERCISE 4:WHAT ARE
THE OUTPUT?
for i in range(5):
if i == 3:
continue
print(i)

Output?

Presentation title 7
EXERCISE 5

Write a Python program to append a new item to the end


of the array. Sample Output: Original array: array(‘i', [1, 3,
5, 7, 9])Append 11 at the end of the array: New array:
array(‘i’, [1, 3, 5, 7, 9, 11])
SOLUTION
from array import *
array_num = array('i', [1, 3, 5, 7, 9]) Don’t forget
the insert
print(“the original array: "+str(array_num)) method as we
discussed
array_num.append(11)
print(“the new array: "+str(array_num))
EXERCISE 6:

Write a Python program to reverse the order of the items


in the array. Sample Output Original array: array(‘i’, [1, 3,
5, 3, 7, 1, 9, 3]) Reverse the order of the items: array('i',
[3, 9, 1, 7, 3, 5, 3, 1])
SOLUTION
from array import *
array_num = array('i', [1, 3, 5,3,7,1,9,3])
print(“the original array: "+str(array_num))
array_num.reverse()
print(“the new array: "+str(array_num))
EXERCISE 7:

Write a Python program to remove a specified item using


the index from an array. Sample Output: Original array:
array(‘i’, (1, 3, 5, 7, 9])Remove the third item from the
array:New array: array(i, [1, 3, 7, 9])
SOLUTION
from array import *
array_num = array('i', [1, 3, 5,7,9])
print("Original array: "+str(array_num))
array_num.pop(2)
print("new : "+str(array_num)) Don’t forget
remove
method as we
discussed
Thank You

You might also like