Heba Programming1 Book-First Part
Heba Programming1 Book-First Part
3
Prepared by: Dr. Heba El Hadidi
2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
• Introduction
What is a computer?
Program
Today computer programs are being used in almost every field, household,
agriculture, medical, entertainment, defense, communication, etc.
- Computer programs are used in our mobile phones for SMS, Chat, and voice
communication.
Program Statements
Program statement is an instruction written in high level PL to do a specific task
in a program.
Page 2 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Although many languages share similarities, each has its own syntax. Once a
programmer learns the languages rules, syntax, and structure, they write the
source code in a text editor or IDE. Then, the programmer often compiles the
code into machine language that can be understood by the computer.
Page 3 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
What is a Compiler?
Page 4 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
When executing programs, errors (bugs) may occur. The process of locating and
correcting bugs is called debugging.
➢ Syntax Errors
➢ Semantic Errors
Errors which appear during the program execution. ex., division by zero, or
variable assigned a large value beyond its capacity.
Page 5 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Exercise:
a) The Programmer.
b) The Computer
c) The Compiler/Interpreter
d) The Instructor
Page 6 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
• Introduction to Python
Python is a high-level programming language, commonly used for general
purposes. It was originally developed by Guido van Rossum in 1980s.
- It has efficient high-level data structures and a simple but effective approach to
object-oriented programming.
- Open Source: Python is "open source”, which means that the developer
community can seamlessly make updates to the code, which are always available
to anyone using Python for their software programming needs.
- Free
Page 7 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
It provides libraries to handle internet protocols such as HTML and XML, JSON,
Email processing, request, beautiful Soup, Feed parser etc. It also provides
Frameworks such as Django, Pyramid, Flask etc to design and develop web
based applications.
Some important developments are: Python Wiki Engines, Pocoo, Python Blog
Software etc.
3) Software Development
Python is popular and widely used in scientific and numeric computing. Some
useful library and package are SciPy, Pandas, IPython etc. SciPy is group of
packages of engineering, science and mathematics.
5) Business Applications
Page 8 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
8) 3D CAD Applications
To create CAD application Fandango is a real application which provides full
features of CAD.
9) Enterprise Applications
Python can be used to create applications which can be used within an Enterprise
or an Organization. Some real time applications are: OpenErp, Tryton, Picalo etc.
EX:
x=5
y = 10
z=x+y
print(z)
3- Repetition (Looping)
Repeat a block of code (action) while a condition is true. There is no limit to the
number of times that the block can be executed.
Ex:
n=1
Page 10 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
print(n)
n = n +1
Indentation in Python
Python doesn't allow the use of parentheses for the block. Instead, indentation is
used to declare a block. If two statements are at the same indentation level, then they
are the part of the same block.
# nothing to print
Page 11 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Python as calculator
Python can do simple arithmetic. For example, 2+2 is a simple arithmetic
expression.
Not all expressions are numerical, for example, 5 > 7 is an expression that
evaluates to the Boolean value False.
Page 12 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Note:
12 .*3 =36.0
5**0.5 =2.23606797749979
Page 13 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
+= Adds the right operand to the left operand and x+=2 x=7
-= subtracts the right operand to the left operand and x-=3 x=4
*= multiplies the right operand by the left operand and x*=2 x=8
/= divides the left operand by the right operand and x/=2 x=4
%= divides the left operand by the right operand and x%=3 x=1
**= Determines the exponential of left operand raised to the power of the right
operand and places the result in the left operand (2*x)**=2 x=4
//= Divides the left operand by the right operand and places the integer result in
the left operand x//=3 x=1
Page 15 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Page 16 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
not in operator: Determines whether the value in the left operand is missing
from the sequence found in the right operand.
Is not operator: Gives True if the type of the value or expression in the right
operand points to a different type in the left operand.
Commenting
- In computer programming, a comment is a programmer readable explanation
or annotation in the source code of a computer program. They are added with the
purpose of making the source code easier for humans to understand, and are
generally ignored by compilers and interpreters.
Page 17 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Variable in Python
- The problem with previous examples is the value calculated is not stored in the
memory. If you are trying to work out some math problem without a computer,
then you need a paper. Same in programming.
So if you want to use it again, then you need to type the formula again.
- For example:
X=10
Y=”Heba”
Page 18 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Height = 142
Width = 34
- The variable is Height with value 142, second variable is Width with value 34
a= 23
p, q= 83.4, 2**0.5
The equal sign “=” is the assignment operator. In the first statement,
it creates an integer a (object) and assigns it a value of 23. In the second
statement it creates two floating point numbers p and q and assigns them
the values of 83.4 and 1.4142135623730951, respectively.
Note:
Case sensitive
Example:
firstName=”Heba”
SurName=”Hamed”
Example:
FirstName=”Heba”
SurName=”Hamed”
# Is there error???
Page 19 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Multiple assignments: a = b = c = 1
The order of evaluation is very important. Having the right side evaluated first
means that assignments like x = x + 1 make sense, because the value of x
doesn’t change until after x + 1
is evaluated. Incidentally, there is a shorthand for this kind of update: x += 1 .
There are similar notations for -= , *= , and /= .
Page 20 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Page 21 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
- For example:
Area = Height * Width
print(Area)
variables Height and Width is used to calculate Area. i.e., the value is stored in
variable Area.
Variable is a name which is used to refer memory location. Variable also known
as identifier and used to hold value.
In Python, we don't need to specify the type of variable because Python is a type
infer language and smart enough to get variable type.
Variable names can be a group of both letters and digits, but they have to begin
with a letter or an underscore.
In python:
Also,
Page 22 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Importing Modules:
import numpy as np
import math #import all the module
from numpy import log #import part of the module
from numpy import log, sin, cos
from numpy import *
print('Hello, world!')
Output
Hello, world!
Note:
num1 = 1.5
num2 = 6.3
Page 23 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
You can re-declare Python variables even after you have declared once.
Identifier Naming
o All the characters except the first character may be an alphabet of lower-
case(a-z), upper-case (A-Z), underscore or digit (0-9).
o Identifier name must not contain any white-space, or special character (!, @,
#, %, ^, &, *).
o Identifier name must not be similar to any keyword defined in the language
(not reserved words).
Page 24 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
o Identifier names are case sensitive for example myname, and MyName is not
the same.
Spaces are not allowed in variable names. No other character that is not a
letter or a number is permitted.
Although variable names can start with the underscore character, you should
avoid doing so in most cases. Variables beginning with an underscore are
generally reserved for special cases.
Remember:
Python is case sensitive, so the variable b is distinct from the variable B.
In [14]: distance = 34.
In [15]: time_traveled = 0.59
In [16]: velocity = distance/time_traveled
In [17]: velocity
Out[17]: 57.6271186440678
x="hi"
print (x)
print(x+str(99)) # hi99
# delete variable
x="hi"
print (x)
del(x)
print(x) # Error
xx=input("Press Key")
------------------
# variable
a=3
Page 25 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
print (a)
x= 3+4
print(x)
xx=input("Press Key")
# ends the output with a <space>
print("Welcome to" , end = ' ')
print("Egypt", end = ' ')
Welcome to Egypt PS C:\Users\HP>
Page 26 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
# input numbers
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')
Output
Enter first number: 1.5
Enter second number: 6.3
The sum of 1.5 and 6.3 is 7.8
Page 27 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
print()
Example:
# To Use Time and date library
from datetime import *
dob=int(input(“Enter your birth date:” ))
today= datetime.today()
year= today.year
age= int(year) – int(dob)
print (“Your age= {age}”)
Page 28 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Write a program that allows the user to specify the number of iterations used in
this approximation and displays the resulting value.
6. Write Python code to calculate the circumstance and area of many circles.
11. Consider the quadratic equation of the form: ax2+bx+c=0 and has two
solutions given with the formula:
−𝑏 ± √𝑏 2 − 4𝑎𝑐
𝑥=
2𝑎
Where a,b and c are inputs, print the numerical value of the two solutions x.
Page 29 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Python supports integers, floating-point numbers and complex numbers. They are
defined as int, float, and complex classes in Python.
Integers and floating points are separated by the presence or absence of a decimal
point. For instance, 5 is an integer whereas 5.0 is a floating-point number.
Complex numbers are written in the form, x + yj, where x is the real part and y is the
imaginary part.
type() function is used to know which class a variable or a value belongs to and
isinstance() function to check if it belongs to a particular class.
a=5
print(type(a))
print(type(5.0))
c = 5 + 3j
print(c + 3)
print(isinstance(c, complex))
output:
<class 'int'>
<class 'float'>
(8+3j)
True
Exercise
Page 30 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Notes:
Type Conversion
print(1 + 2.0) #3.0
print(int(2.3)) #2
print(int(-2.8)) #-2
print(float(5)) #5.0
print(complex('3+5j')) #(3+5j)
print((1.1 + 2.2) == 3.3) #False
Page 31 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
num = 1+2j
num_sqrt = cmath.sqrt(num)
print('The square root of {0}is
{1:0.3f}+{2:0.3f}j'.format(num,num_sqrt.real,num_sqrt.imag))
Output
The square root of (1+2j) is 1.272+0.786j
If we want to take complex number as input directly, like 3+4j, we have to use the
eval() function instead of float().
Notes:
Page 32 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
>>> print(True)
True
True
False
>>>
The assignment operator can be used to increment or change the value of a variable.
In [3]: a = a+1
In [4]: a
Out[4]: 24
The statement, a = a+1 makes no sense in algebra, but in Python (and most computer
languages), it makes perfect sense: it means “add 1 to the current value of a and assign
the result to a.”
In [7]: c
Out[7]: 6
In [8]: c *= 3
In [9]: c
Out[9]: 18
In [10]: d /= -2
Page 33 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
In [11]: d
Out[11]: -3.96
In [12]: d -= 4
In [13]: d
Out[13]: -7.96
Example:
# Calculates time, gallons of gas used, and cost of
# gasoline for a trip
distance = 400. # miles
mpg = 30. # car mileage
speed = 60. # average speed
costPerGallon = 2.85 # price of gas
time = distance/speed
gallons = distance/mpg
cost = gallons*costPerGallon
The code:
a = 5
b = 6
c = 7
# OR
# a = float(input('Enter first side: '))
# b = float(input('Enter second side: '))
# c = float(input('Enter third side: '))
Output
Page 34 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
(-b ± (b ** 2 - 4 * a * c) ** 0.5) / (2 * a)
a = 1
b = 5
c = 6
# conversion factor
conv_fac = 0.621371
# calculate miles
miles = kilometers * conv_fac
print('%0.2f kilometers is equal to %0.2f miles' %(kilometers,miles))
Output
Page 35 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Exercise: Modify the example 8 to convert miles to kilometers using the following
formula.
print(random.randint(0,9))
Output 5
Example 10:
import math
print(math.pi)
print(math.cos(math.pi))
print(math.exp(10))
print(math.log10(1000))
print(math.sinh(1))
print(math.factorial(6))
Output
3.141592653589793
-1.0
22026.465794806718
3.0
1.1752011936438014
720
Page 36 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Example 11:
import random
print(random.randrange(10, 20))
# Shuffle x
random.shuffle(x)
Note:
Expression x+y+z/3
Or
x+y+(z/3)
Or
(x+y+z)/3???
Page 37 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Strings in Python
- Strings is enclosed in quotes and special character are escaped with
backslashes.
- For example: print(“Is it true?‟)
Output: Is it true?
print(“Isn\‟t it true?‟) # it has backslash before the quotes, It means that the quote is a special character.
Page 38 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
b = "Bingo"
c=a+""+b
print(c)
# Out: "My dog's name is Bingo"
d = "927"
e = 927
- Previous example just show how to print the output in one line. How to about to
print an output that has multiple lines?
Page 39 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Exercise
Create variables first name and last name2, then combine it to full name3
Print the name4
Calculate the length of characters for full name, taking input from user
Note:
- Sometimes you need to make a program that need to take an input from the
user.
- For that purpose, you cannot simply assign the value, since it depends on the
user to key in.
- Example of taking an input using python:
Name = input(“Please input your name: ‟)
- We use input function to take the input. You can write any strings between the
bracket and quotes.
- But if you want to take an input integer or float, then we need to casting
(transform) it into integer or float.
- By default, input function will take the input as a String.
- Example:
Height = float(input(„Height: „))
Number_of_car =
int(input(„Number of car: „))
Exercise for Input
- Make a program that taking input for:
- Name
- Age
- Height
- Weight
- Display all the information
-----------------------------------------------------------------------------------------------------
Write a Python program which accepts the radius of a circle from the user and
compute the area.
Page 40 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Example:
pi = 3.1415926535897931
r= 6.0
V= 4.0/3.0*pi* r**3
print('The volume of the sphere is: ',V)
Example:
Statement(s)
Page 41 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Example:
TestMe = 6
if TestMe == 6:
print("TestMe does equal 6!")
print("All done!")
Example:
A=3
B=20
If B> A:
Example:
a= 3
b= 20
if b> a:
print(“b “)
Page 42 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
elif b==a:
print(“b=a”)
else:
print(“A”)
Example:
- For example, grade A will be given to the student IF the student‟s mark get
more than 80%
- Example in Python:
if mark >= 80:
grade = “A‟
elif mark >= 60:
grade = “B‟
elif mark >= 50:
grade = “C‟
else:
grade = “D‟
Page 43 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Page 44 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
if <test expression>:
statements1 (body of if)
else:
statements2 (body of else)
If …..else
Page 45 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
print("1. Red")
print("2. Orange")
print("3. Yellow")
print("4. Green")
print("5. Blue")
print("6. Purple")
Choice = int(input("Select your favorite color: "))
if (Choice == 1):
print("You chose Red!")
elif (Choice == 2):
print("You chose Orange!")
elif (Choice == 3):
print("You chose Yellow!")
elif (Choice == 4):
print("You chose Green!")
elif (Choice == 5):
print("You chose Blue!")
elif (Choice == 6):
print("You chose Purple!")
else:
print("You made an invalid choice!")
Example:
X = int(input("Type a number between 1 and 10: "))
Y = int(input("Type a number between 1 and 10: "))
if (X >= 1) and (X <= 10):
if (Y >= 1) and (Y <= 10):
print("Your secret number is: ", One * Two)
else:
print("Incorrect second value!")
else:
print("Incorrect first value!")
Page 46 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else
If …elif..else
Page 47 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Page 48 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Exercise:
Guess name
Example:
#This code to enter a number then test if it is positive or negative
num=int(input('Enter a number '))
if num>0:
if num %3==0:
print('The number is positive and divisble by 3 ')
else:
print('The number is positive and Non-divisble by 3 ')
elif num==0:
print('Zero number')
else:
print('negative')
Exercise:
Write a Python code to check if a number is divisible by 3 and 5.
Page 49 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Example:
Mark=int(input(“Enter your mark: “))
if Mark >=60:
print(‘Pass- Good)
Mark=int(input(“Enter your mark: “))
if Mark >=60:
print(‘Pass- Good’)
else:
print(‘Fail- Good Luck next time’)
Mark=int(input(“Enter your mark: “))
if Mark <=100 and Mark >=60:
print(‘Pass- Good’)
else:
print(‘Fail- Good Luck next time’)
Page 50 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
print(‘Pass-B’)
elif Mark<80 and Mark>=70:
print(‘Pass-C’)
elif Mark <70 and Mark >=60:
print(‘Pass-D’)
else:
print(‘Fail- Good Luck next time’)
Home work
1- Write python code to check a number, positive or zero or negative. If the
number is positive: check if it is even or not.
Even: divisible by 2
Hint: use nested if statement
2- Write a Python program to check if a given number is even using user defined
function named Is_Even
3- Write Python program to asks the user for his name and greets him with his name
only for the users Alice and Bob .
4- Write a Python program to Write a program that asks the user for a number n and
prints the sum of the numbers 1 to n
5- Ask the user to enter today’s temperature (T). Then print “very hot” if T>30, print
“hot” if 25<T<=30, print “warm” if 20<=T<=25, print “cold” if 10<T<20, print “very
cold” if 0<T<=10, print “ice” if T<=0.
6- Write Python program to ask the user for a number n and prints the sum of the
numbers 1 to n such that only multiplies of three or five are considered in the sum,
e.g. 3,5,6,9,10,12,15 for n=17
7- Write a Python program to check if a given number is prime.
Page 51 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Looping in Python
- Looping is a situation where we need to repeat the same task for specific times
or infinity
Example:
print “Hi” 10 times
for k in range(10):
print(‘Hi’)
Page 52 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
for fruit in ["Apple", "Banana", "water mellon", "Peach", "Orange", "Durian", "Papaya"]:
print(fruit)
5
6
7
8
Page 53 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Example:
sum = 0
for number in range(1,11):
sum = sum + number
print(sum)
Page 54 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Example:
Program to ask the user for 20 numbers and prints their squares using loop,
then prints that the loop is done.
for k in range(20):
num=eval(input(‘Enter number: ‘))
print(‘The square of your number is: ‘, num*num)
print(‘The loop is done’)
Page 55 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Print 0 …99:
for j in range(100):
print(j)
Lists:
Examples:
# Python lists
list1=[]
list2=[1, 2, 3.0, True, 'Hi'] #Different types
# Python lists
colors = ['red', 'blue', 'green']
print(colors[0]) # red
print(colors[2]) #green
print(len(colors)) # 3
# sum list elements
s = [1, 4, 9, 16]
sum = 0
for num in s:
sum += num
print(sum) #30
list=[1,2,3]
list.append(4)
print(list) #[1, 2, 3, 4]
a=[]
a.append('H')
a.append('N') #Nothing
B=[]
B.append(1)
B.append(5)
print(B) #[1, 5]
Page 56 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
#List Sclices
list = ['a', 'b', 'c', 'd']
print(list[1:-1]) ## ['b', 'c']
list[0:2] = 'z' ## replace ['a', 'b'] with ['z']
print(list) ## ['z', 'c', 'd']
# first item
print(my_list[0]) # p
# third item
print(my_list[2]) # o
# fifth item
print(my_list[4]) # e
# Nested List
n_list = ["Happy", [2, 0, 1, 5]]
# Nested indexing
print(n_list[0][1])
# p
# o
# e
# a
Page 57 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
# last item
print(my_list[-1])
my_list = [3, 8, 1, 6, 8, 8, 4]
Page 58 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Tuples:
Examples:
Page 59 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Page 60 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Page 61 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
range( ) function
• generate a sequence of numbers using range() function.
• range(10) will generate numbers from 0 to 9 (10 numbers)
• also define the start, stop and step size as range(start, stop,step_size).
• step_size defaults to 1.
• range(n) the n value determines how many times we will loop.
• range(5) produces a list of 0,1,2,3,4
Page 62 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
• If u want the list start at a value other than zero, specify the starting value.
• range(1,5) produces 1,2,3,4
• If we want 5 to be included put range(1,6) gives 1,2,3,4,5
• range(1,10,2) –third argument- this function gives 1,3,5,7,9.
• range(10) 0,1,2,3,4,5,6,7,8,9
• range(1,10) 1,2,3,4,56,7,8,9
• range(3,7) 3,4,5,6
• range(2,15,3) 2,5,8,11,14
• range(9,2,-1) 9,8,7,6,5,4,3
Page 63 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Example:
Write Python code to add the prime numbers between 1 and 20 in a list.
# prime numbers less 20 in a list
n = 20
primes = []
for i in range(2, n + 1):
for j in range(2, int(i ** 0.5) + 1):
if i%j == 0:
break
else:
primes.append(i)
print(primes)
## out: [2, 3, 5, 7, 11, 13, 17, 19]
Page 64 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
width = "23"
height = "100"
area = width * height
print(area)
# Rectangle
width = 23
height = 17
area = width * height
print("The area is ", area) # 391
circumference = 2 * (width + height)
print("The circumference is ", circumference) # 80
# Circle
r=7
pi = 3.14
print("The area is ", r * r * pi) # 153.86
print("The circumference is ", 2 * r * pi) # 43.96
Page 65 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
import math
r=7
print("The area is ", r * r * math.pi) # 153.9380400258998
print("The circumference is ", 2 * r * math.pi) # 43.982297150257104
if length <= 0:
print("length is not positive")
if width <= 0:
print("width is not positive")
Page 66 2022-2023
Structure Programming 1 with Python 3 Dr Heba El Hadidi
Exercises:
Page 67 2022-2023