Lecture2
Lecture2
Osama El-Ghonimy
INTRODUCTION TO PROGRAMMING
o What is programming?
o What is programming language?
ADD r0 r2 r5
o Changing any bit changes the meaning.
LOW LEVEL PROGRAMMING
o Example of a program written in machine code:
01100000000000000000000010000000
10100100000000000000000000000000
01100000000000010000000010000100
10100100000000010000000100000000
01100000000000100000000000000000
01100000000000110000000000000100
…
o This is machine code: low level programming.
HIGH LEVEL PROGRAMMING
o You just type:
a = b + c
HIGH LEVEL PROGRAMMING
o You just type:
a = b + c
o The compiler convert this command into machine code sequence.
HIGH LEVEL PROGRAMMING
o You just type:
a = b + c
o The compiler convert this command into machine code sequence.
o A program written in a high-level language is called a source
program.
PYTHON IDE
o Integrated Development Environment (IDE).
o Linux and Mac OS X users probably already have a usable
Python preinstalled on their computers
o Spyder: close to MATLAB GUI.
o Jupyter: Web interface.
o PyCharm.
PYTHON IDE
o Visual Studio Code.
o Another very popular Python distribution, particularly for math,
science, engineering, and data science applications, is the
Anaconda distribution.
o Anaconda includes all the most popular packages for engineering
and data science type workloads in one single installer.
SETUP ANACONDA
o Download Anaconda:
o Go to link:
https://www.anaconda.com/products/distribution .
o Download for windows x64.
o Double click the downloaded file >> Next >> I agree
>> Next >> Next >> Select “Add Anaconda3 to my
PATH environment variable” >> Install >> Finish.
o Open Anaconda: Start menu >> Anaconda3 (64-bit)
>> Anaconda Navigator (anaconda3).
SETUP VISUAL STUDIO CODE
o Download VS Code:
o Go to link: https://code.visualstudio.com/download .
o Download for windows x64.
o Double click the downloaded file >> Next >> Next >> Next
>> Install >> Finish.
o Open VS Code:
o Start menu >> Visual Studio Code.
SETUP VISUAL STUDIO CODE
o Install Python extension:
o Type “Python” in the search box.
o Select the first search result (Python), Developer:
Microsoft.
o Click on install.
o Open New File:
o File menu >> New File >> Jupyter Notebook.
o If VS code recommended installing any other extension,
accept and install.
ARITHMETIC OPERATORS
o Addition: >>> 5+3
8
5+3
o Subtraction: >>> 5-3
2
5−3
>>> 5*3
o Multiplication: 15
5×3
ARITHMETIC OPERATORS
o Division:
>>> 5/3
2 1.6666666666666667
5÷3=1
3
o Normal division (True division). >>> 5//3
1
o Integer division (Floor division).
>>> 5%3
2
o Reminder (modulus):
>>> 6%3
0 0
6÷3=2
3
ARITHMETIC OPERATORS
o Reminder (modulus): >>> 6%4
2
1 2
6÷4=1 =1
2 4
ORDER OF PRECEDENCE
o What is the output of this expression evaluation?
19
ORDER OF PRECEDENCE
o What is the output of this expression evaluation?
o Operators contained within pairs of parentheses are evaluated
first.
20
ORDER OF PRECEDENCE
o What is the output of this expression evaluation?
o Multiplication, division, and remainder operators are applied
next.
21
ORDER OF PRECEDENCE
o What is the output of this expression evaluation?
o Several multiplication, division, and remainder are applied in order.
22
ORDER OF PRECEDENCE
o What is the output of this expression evaluation?
o Addition and subtraction operators are applied last.
23
ORDER OF PRECEDENCE
o What is the output of this expression evaluation?
o Several additions and subtractions are applied from right to left.
CELLS
o Only the last output is displayed. 10+4
10-6
o How to solve this?
4
14
10-6
4
PRINT
o Only the last output is displayed. 10+4
10-6
o How to solve this?
4
14
4
CREATING VARIABLES
o Any application consists of code and …
>>> x=5 …
data.
…
o Create variable from: …
o Instant value. …
x 5
o An expression. x 5
y 10
…
CREATING VARIABLES
o Create variable from: …
>>> x=5 …
o The expression may include
…
another variable (or more). >>> y=5*20
…
o Note that the new value >>> y=5+x …
overwrites the old one. x 5
o The expression may include the >>> y=y*2
same variable (old value). …
y 20
…
CREATING VARIABLES
o Create variable from:
>>> x=5
o The expression may include
another variable (or more). >>> y=5*20
o Note that the new value >>> y=5+x
overwrites the old one.
o The expression may include the >>> y=y*2
same variable (old value). >>> y=2y
o Note that there is no implicit ^ SyntaxError: invalid
syntax
multiplication.
EXAMPLE
o Write a program to calculate the
area and circumference of a circle
based on the following equations:
r = 10
𝑎𝑟𝑒𝑎 = 𝜋𝑟 2
𝑐𝑖𝑟𝑐𝑢𝑚𝑓𝑒𝑟𝑒𝑛𝑐𝑒 = 2𝜋𝑟 3.14159*r*r
o Where: 2*3.14159*r
𝜋 = 3.14159, 𝑟 = 10𝑚 Output:
o Only the last output is displayed. 62.8318
Solution??
EXAMPLE
o Write a program to calculate the
area and circumference of a circle
based on the following equations:
r = 10
𝑎𝑟𝑒𝑎 = 𝜋𝑟 2
𝑐𝑖𝑟𝑐𝑢𝑚𝑓𝑒𝑟𝑒𝑛𝑐𝑒 = 2𝜋𝑟 print(3.14159*r*r)
o Where: 2*3.14159*r
𝜋 = 3.14159, 𝑟 = 10𝑚 Output:
o The user does not know which 314.159
62.8318
output is area or circumference.
EXAMPLE
o Write a program to calculate the
area and circumference of a circle
based on the following equations:
r = 10
𝑎𝑟𝑒𝑎 = 𝜋𝑟 2
print("Area =")
𝑐𝑖𝑟𝑐𝑢𝑚𝑓𝑒𝑟𝑒𝑛𝑐𝑒 = 2𝜋𝑟 print(3.14159*r*r)
o Where: 2*3.14159*r
𝜋 = 3.14159, 𝑟 = 10𝑚 Output:
o The user does not know which Area =
314.159
output is area or circumference. 62.8318
EXAMPLE
o Write a program to calculate the
area and circumference of a circle
based on the following equations:
r = 10
𝑎𝑟𝑒𝑎 = 𝜋𝑟 2
print("Area =", 3.14159*r*r)
𝑐𝑖𝑟𝑐𝑢𝑚𝑓𝑒𝑟𝑒𝑛𝑐𝑒 = 2𝜋𝑟
o Where: 2*3.14159*r
𝜋 = 3.14159, 𝑟 = 10𝑚 Output:
o Print the two values on the same Area = 314.159
62.8318
line using single print statement.
EXAMPLE
o Write a program to calculate the
area and circumference of a circle
based on the following equations:
r = 10
𝑎𝑟𝑒𝑎 = 𝜋𝑟 2
print("Area =", 3.14159*r*r)
𝑐𝑖𝑟𝑐𝑢𝑚𝑓𝑒𝑟𝑒𝑛𝑐𝑒 = 2𝜋𝑟
print("Circumference =",
o Where: 2*3.14159*r)
𝜋 = 3.14159, 𝑟 = 10𝑚 Output:
o Same for circumference. Area = 314.159
Circumference = 62.8318
o How to add units?
EXAMPLE
o Write a program to calculate the
area and circumference of a circle
based on the following equations:
r = 10
𝑎𝑟𝑒𝑎 = 𝜋𝑟 2
print("Area =", 3.14159*r*r,
𝑐𝑖𝑟𝑐𝑢𝑚𝑓𝑒𝑟𝑒𝑛𝑐𝑒 = 2𝜋𝑟 "m2")
print("Circumference =",
o Where: 2*3.14159*r, "m")
𝜋 = 3.14159, 𝑟 = 10𝑚 Output:
o Same for circumference. Area = 314.159 m2
Circumference = 62.8318 m
o How to add units?
EXAMPLE
o Write a program to calculate the
area and circumference of a circle
based on the following equations:
r = 10
𝑎𝑟𝑒𝑎 = 𝜋𝑟 2
print("Area =", 3.14159*r**2,
𝑐𝑖𝑟𝑐𝑢𝑚𝑓𝑒𝑟𝑒𝑛𝑐𝑒 = 2𝜋𝑟 "m2")
print("Circumference =",
o Where: 2*3.14159*r, "m")
𝜋 = 3.14159, 𝑟 = 10𝑚 Output:
o One last note: using the power Area = 314.159 m2
Circumference = 62.8318 m
operator.
COMMENTS
o Provide some information about # A program to find area and
# circumference of a circle
code. # By Dr. Osama El-Ghonimy
o Syntax: Any # inside the code starts
r = 10 # radius
a comment till the end of line. print("Area =", 3.14159*r**2,
"m2")
o The compiler skips compiling these print("Circumference =",
lines. No syntax error. 2*3.14159*r, "m")
o Without # it is a command then Output:
there will be an error. Area = 314.159 m2
Circumference = 62.8318 m
o It is also used for debugging.