0% found this document useful (0 votes)
6 views24 pages

Tutorial 02

Uploaded by

ffff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views24 pages

Tutorial 02

Uploaded by

ffff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

VARIABLES,

FLOW CHARTS &


PSEUDOCODE
Recitation 01
OPERATORS
Python has many operators. Some examples
are:

+, -, *, /, %
DATA TYPES
 In Python, all data has an associated data “Type”.
 You can find the “Type” of any piece of data by
using the type() function:
type(‘Hi’)
type(True)
int & float

Python supports two different types of numbers,


Integers (int) and Floating point numbers (float).
Floating Point numbers have a fractional part (digits
after the decimal place), while Integers do not!
EFFECT OF DATA TYPES

Math operators work differently on Floats and


Ints:
int + int produces an int

int + float or float + int produces a float


TYPE CONVERSION
(CASTING)
Functions exist which will take data in one type and
return data in another type.
 int() - Converts compatible data into an integer. This
function will truncate floating point numbers
 float() - Converts compatible data into a float.
 str() - Converts compatible data into a string
CASTING
 int(3.3) produces 3 str(3.3) produces “3.3”

 float(3) produces 3.0 float(“3.5”) produces 3.5

 int(“7”) produces 7

 int(“7.1”) Throws an ERROR!

 float(“Test”) Throws an ERROR!


VARIABLES

Variables are names that can point to data.

They are useful for saving intermediate results

and keeping data organized.


VARIABLES

The assignment operator (=) assigns data to

variables.
When a variable is evaluated, it produces the

value of the data that it points to.


PROGRAM EXAMPLE
Find the area of a circle given the radius:
>>> Radius = 10
>>> pi = 3.14159
>>> area = pi * Radius * Radius
>>> print( area )

prints 314.15
USER INPUT
 We can instruct Python to name = input(‘Who are you? ’)
pause and read data from the print('Welcome', name)

user using the input function Who are you? Chuck


Welcome Chuck
 The input function returns a

string
CONVERTING USER INPUT
 If we want to read a number inp = input(‘Europe floor?’)
from the user, we must usf = int(inp) + 1
convert it from a string to a print('US floor', usf)
number using a type
conversion function Europe floor? 0
 Later we will deal with bad US floor 1
input data
FLOW CHARTS &
PSEUDOCODE
PROGRAMMING TOOLS

 Flowchart

 Pseudocode
FLOWCHARTS

Graphically depict the logical steps to carry out a task


and show how the steps relate to each other.
BASIC FLOWCHART SYMBOLS
Terminals
 represented by rounded rectangles
 indicate a starting or ending point

START

END
BASIC FLOWCHART SYMBOLS
 Input/ Output Operations
 represented by parallelograms
 indicate an input or output operation
BASIC FLOWCHART SYMBOLS
 Processes
 represented by rectangles
 indicates a process such as a mathematical computation or
variable assignment
PSEUDOCODE
Uses English-like phrases to outline the task in
an algorithm
COMMENTS
 As programs get bigger and more complicated, they

get more difficult to read.

 Formal languages are dense, and it is often difficult to

look at a piece of code and figure out what it is doing,


or why.
COMMENTS IN PYTHON
Comments start with the # symbol

#compute the area of a rectangle


area = width * height
PSEUDOCODE & ALGORITHM
Example 1:
Write an algorithm to determine a student’s final
grade and indicate whether it is passing or failing.
The final grade is calculated as the average of four
marks.
PSEUDOCODE & ALGORITHM
Pseudocode:
 Input a set of 4 marks
 Calculate their average by summing and dividing by 4
 if average is below 60
Print “FAIL”
else
Print “PASS”
PSEUDOCODE & ALGORITHM
Detailed Algorithm
Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Step 3: if (GRADE < 60) then
Print “FAIL”
else
Print “PASS”
endif

You might also like