Uploaded by cunhamarisa73

class 01 211019

advertisement
Programming
António Carvalho Brito (acbrito@fe.up.pt)
Carlos Bragança (braganca@fe.up.pt)
Telmo Matos (tpm@fe.up.pt)
Thiago Sobral (thiago@fe.up.pt)
Assessment components
1st test (40%) – 27/11
2nd test (40%) – 24/01
Moodle quizzes (10%) – 22/01
Web site (10%) – 17/01
António Carvalho Brito / Carlos Bragança de Oliveira
Programming
Pág. 2
Algorithm
ALGORITHM -
EXAMPLES
A series of separate steps that following
a certain sequence lead to the solution of
the problem
- Cooking recipe
- Route directions
- Equipment setup
António Carvalho Brito / Carlos Bragança de Oliveira
Programming
Pág. 3
Solving a problem
PROBLEM
ALGORITHM
FLOWCHART
OR
PSEUDOLANGUAGE
CONVERT
TO
CODE
COMPUTER
António Carvalho Brito / Carlos Bragança de Oliveira
Programming
Pág. 4
Algorithm description
FLOWCHART
INSTRUCTIONS
START / END
READ
ASSIGNMENT
PSEUDOLANGUAGE
START / END
INPUT( )
VARIABLE <- EXPRESSION
IF ... THEN
DECISION
ELSE
END IF
WRITE
LOOP
PRINT( )
FOR ... TO
END FOR
FUNCTION
FLOW DIRECTION
António Carvalho Brito / Carlos Bragança de Oliveira
Programming
Pág. 5
Creating Algorithms - example
Given the base and the height calculate the area of a triangle
PSEUDOLANGUAGE
FLOWCHART
START
Base, Height
START
INPUT(Base, Height);
Area <- Base * Height / 2;
PRINT(Area);
END
Area = Base * Height / 2
PYTHON
Area
END
António Carvalho Brito / Carlos Bragança de Oliveira
# Calculate the area of a triangle
base = float(input ("Triangle base:"))
height = float(input ("Triangle height:"))
area = base * height / 2
print("Area of the triangle = ", area)
Programming
Pág. 6
Spyder IDE
António Carvalho Brito / Carlos Bragança de Oliveira
Programming
Pág. 7
Program physical structure

A Phyton program is a set of logical lines

A logical line is constructed from one or more physical lines

A physical line is a sequence of characters terminated by an end-of-line sequence

Two or more physical lines may be joined into logical lines using backslash
characters (\)

A logical line can be blank (contains only spaces, tabs, formfeeds)

A comment starts with a hash character (#) and ends at the end of the physical line.

Leading whitespaces (spaces and tabs) at the beginning of a logical line are used to
compute the indentation level of the line, which in turn is used to determine the
grouping of statements
António Carvalho Brito / Carlos Bragança de Oliveira
Programming
Pág. 8
Numerical and string values

Integer (without decimal point - int)
ex: -20
+18400 525
(unlimited)

Real (with decimal point - float)
ex: 85.9 -1.26e10 22.75
(< 1.8e308, 8 bytes)

Complex (complex)
ex: 1+2j

String (str)
ex: "OLA" "123.45" 'JOÃO ANTONIO'
António Carvalho Brito / Carlos Bragança de Oliveira
Programming
"ABC123.4 "
Pág. 9
Variables

Variables are containers for storing values in computer memory

Variable names can contain alpha-numeric characters and underscores
(A-z, 0-9, and _ )

Variable names must start with a letter or the underscore character

Variable names are case-sensitive

Variables do not need to be declared and are created when values are
assigned to it

The following keywords are reserved words and cannot be used as identifiers
António Carvalho Brito / Carlos Bragança de Oliveira
False
await
else
import
pass
None
break
except
in
raise
True
class
finally
is
return
and
continue
for
lambda
try
as
def
from
nonlocal
while
assert
del
global
not
with
async
elif
if
or
yield
Programming
Pág. 10
Operators and arithmetic expressions


Operators:
- Arithmetic
- Relational
- Logical
- Functions
(**, *, /, //, %, +, -)
(==, !=, <=, <, >, >=)
(not, and, or)
(min(), max(), abs(), ...)
Arithmetic operators:
P
R
I
O
R
I
T
Y
+
António Carvalho Brito / Carlos Bragança de Oliveira
OPERATOR
OPERATION
EXPRESSION
**
Exponentiation
X ** Y
-
Unary Minus
-X
*
/
Multiplication
Division
X*Y
X/Y
//
Floor division
X // Y
%
Remainder of
the division
X % Y
+
-
Addition
Subtraction
X+Y
X-Y
Programming
Pág. 11
Operators and arithmetic expressions

Arithmetic expressions:
- Operands
- Numerical variables
- Numerical constants
- Arithmetic operators
- Functions
- Parentesis
P
R
I
O
R
I
T
I
E
S
António Carvalho Brito / Carlos Bragança de Oliveira
ex: A + B * C / abs(E) * (A - C * (D + F / E ** 3))
1º
6º
7º
2º
8º
3º
4º
5º
9º
10º
Programming
Pág. 12
Assignment operator
VARIABLE = EXPRESSION

Calculates de value of EXPRESSION and stores the result in VARIABLE

The VARIABLE type will be the same as the type of EXPRESSION

EXPRESSION can be a constant, variable or expression
FLOWCHART
C=A+B
PSEUDOLANGUAGE
PYTHON
C <- A + B
c=a+b
Ex:
firstname = "Antonio"
lastname = "Gomes"
name = firstname + lastname
h=5
area = 2.33
volume = area * h
António Carvalho Brito / Carlos Bragança de Oliveira
Programming
Pág. 13
Read / Input
VARIABLE = input ("Message")

Shows the "Message" and waits for the user input

The input value is of type string

If VARIABLE should be numeric the input values must be converted to a
numerical type using the functions int(), float() or complex()
FLOWCHART
PSEUDOLANGUAGE
A, B
INPUT (A, B)
Ex: name = input ("Name: ")
c = int(input ("c = "))
base = float(input ("base = "))
base = float(input (“altura = "))
António Carvalho Brito / Carlos Bragança de Oliveira
Programming
PYTHON
a = input("a = ")
b = input("b = ")
Name: Antonio
c = 20
base = 30.2
altura = 78.3
Pág. 14
Write / Print
print (string expression,…)

Prints the string expression

The string expression can be a combination of string constants, string
variables and numerical constants or variables (converted with the str()
function) concatenated with the + operator
FLOWCHART
A, B
Ex:
António Carvalho Brito / Carlos Bragança de Oliveira
PSEUDOLANGUAGE
PRINT (A, B)
print (a)
print ("a = " + str(a))
print (a, 2*a, 3*a)
print ("good", "morning")
print ("good-" + "morning")
Programming
PYTHON
print("a = " + str(a) + ", b = " + str(b))
5
a=5
5 10 15
good morning
good-morning
Pág. 15
Control structures

Control structures are used to control the logical flow of instructions during
the execution of a program

The following are the basic types of control structures:
Sequential
- by default the instructions are executed one after another
Selection
- based on logical decisions enable the execution of 2 or more
alternative branches
Repetition
- based on logical decisions or iterating through values allow
the repetition of a block of code multiple times (loop)
António Carvalho Brito / Carlos Bragança de Oliveira
Programming
Pág. 16
Logical variables and expressions

Python has a Boolean data type that is either True or False

A variable is of type Boolean if it has assigned a value of True or False

Selection and Repetition control structures use the value, True or False,
of logical expressions to decide what instructions to execute next
António Carvalho Brito / Carlos Bragança de Oliveira
Programming
Pág. 17
Relational operators

Relational operators are used to compare two values that should be of the same
data type (both numerical or both string, …)
António Carvalho Brito / Carlos Bragança de Oliveira
OPERATOR
MEANING
OPERATION
==
EQUAL
X == Y
!=
NOT EQUAL
X != Y
<
LESS THAN
X<Y
>
GREATER THAN
X>Y
<=
LESS THAN OR EQUAL TO
X <= Y
>=
GREATER THAN OR EQUAL TO
X >= Y
Programming
Pág. 18
Relational operators
String comparison

Strings are compared character by character until a decision can be made

The characters are compared using their Unicode value.
Ex:
António Carvalho Brito / Carlos Bragança de Oliveira
Expression
Value
Expression
Value
"AA" < "AB"
True
"AB " > "AB"
True
"14" > "200"
False
"2Z" > "AZ"
False
"a" < "A"
False
"AB" = "AB"
True
Programming
Pág. 19
Logical Operators

Logic operators perform logical operations on logical values (True or False)
OPERATOR
NOT
AND
OR
António Carvalho Brito / Carlos Bragança de Oliveira
OPERATION
NEGATION
CONJUNCTION
DISJUNCTION
X
Y
not X
X and Y
X or Y
False
False
True
False
False
False
True
--
False
True
True
False
False
False
True
True
True
--
True
True
Programming
Pág. 20
Computing expressions - priorities

Expressions can be formed by:
–
–
–
–
ARITHMETIC EXPRESSIONS – The result is a numerical value
STRING EXPRESSIONS – The result is a string value
RELATIONAL OPERATORS – A pair of values (numerical, string, …) can be
compared resulting in a logical value
LOGICAL OPERATORS – A logical operation can be performed on a pair of
logical values (True or False) resulting in a logical value
PRIORITIES:
1º - FUNCTIONS
2º - ARITHMETIC OPERATIONS
3º - RELATIONAL OPERATIONS
4º - LOGICAL OPERATIONS
Ex:
4º
A > B or D < C and A * B / sin(D) <= 10
5º
2º
7º
António Carvalho Brito / Carlos Bragança de Oliveira
The order of calculation can be changed
by the use of parenthesis. In the same
circumstances operations are carried out
from left to right
1º
3º
6º
8º
Programming
Pág. 21
Selection structure

Executes a block of statements only if the logical expression is True
FLOWCHART
X – Logical expression
PSEUDOLANGUAGE
IF X THEN
INSTRUCTIONS 1
X
Yes
PYTHON
if X:
INSTRUCTIONS 1
END IF
No
INSTRUCTIONS 1
António Carvalho Brito / Carlos Bragança de Oliveira
Programming
Pág. 22
Selection structure

Executes a block of statements (INSTRUCTIONS 1) if the logical expression is
True and another block of statements (INSTRUCTIONS 2) if the logical
expression is False
FLOWCHART
PSEUDOLANGUAGE
X – Logical expression
IF X THEN
INSTRUCTIONS 1
No
X
Yes
ELSE
INSTRUCTIONS 2
INSTRUCTIONS 2
António Carvalho Brito / Carlos Bragança de Oliveira
INSTRUCTIONS 1
PYTHON
if X:
INSTRUCTIONS 1
else:
INSTRUCTIONS 2
END IF
Programming
Pág. 23
Selection structure

If the conditition (X) is True, executes the block of statements
(INSTRUCTIONS 1). If the conditition (X) is False it checks the condition of the
next elif block and so on. If the if condition and all the elif conditions are False
the block else is executed.
PSEUDOLANGUAGE
FLOWCHART
X, Y – Logical expressions
No
No
Y
X
Yes
IF X THE
INSTRUCTIONS 1
Yes
ELSEIF Y THEN
INSTRUCTIONS 2
INSTRUCTIONS 1
ELSE
INSTRUCTIONS 3
INSTRUCTIONS 3
António Carvalho Brito / Carlos Bragança de Oliveira
INSTRUCTIONS 2
PYTHON
if X:
INSTRUCTIONS 1
elif Y:
INSTRUCTIONS 2
else:
INSTRUCTIONS 3
END IF
Programming
Pág. 24
Download