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

Lecture 3 - Introduction To Computer Data Processing Using Python

Introduction to data processing

Uploaded by

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

Lecture 3 - Introduction To Computer Data Processing Using Python

Introduction to data processing

Uploaded by

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

CS4051 FUNDAMENTALS OF

COMPUTING

Topic 3: Introduction to Computer Data


Processing using Python
LECTURE OUTLINE
1 Programming Languages
2 Data Processing in Python
2.1 Variables declaration
2.3 Input/output statements
2.2 Expressions and operators
2.4 Data types
3 Python Functions
– Functions and libraries
4 Errors in Python Programs
5 An Python Example
PROGRAMING LANGUAGES
• Low level languages: Assembly languages or
assemblers are computer languages specific to the computer
architecture which replaces the binary machine instructions with
shorter, more convenient expressions
– Instructions: LOAD, STORE, COPY and MOVE
– The assembler programs are machine dependent
• Higher level programming
languages are created to
allow humans to describe
precisely algorithms
using a higher level of
abstraction than
Assembler codes
– Example: Java, C, C++,
Python, R
PROGRAMMING DATA PROCESSSING
• High level programing languages shield users from
details of the machine and makes their work more
efficient
+ More productive: a single programming language
statement usually represents more than one machine
instructions.
+ Conceal the hardware detail: programmers need not
know the details of the hardware.

– However, the high level program needs to be translated


into machine language before execution.
GETTING STARTED WITH PYTHON
• Python is a popular programming language for
computation, data analysis and text processing
– We will use Python version 3 to write Python scripts
– We are not going to use the full features of Python, leaving
out the object-oriented features
• Python can be used to develop, test and execute Python
scripts in two modes
– Interactive mode: using command shell
– Scripting mode: using file editor to write the script, and a
Python interpreter to execute the script
PYTHON IN INTERACTIVE MODE
PYTHON IN SCRIPTING MODE
DATA PROCESSING IN PYTHON
• The data has type
• Simple (primitive) data types
– Binary, hexadecimal, decimal integers
– Floating point decimal numbers
– Boolean values (True, False)
– Characters
• Complex (structured) data types
– Character strings
– Lists, Vectors, Tables, Dictionaries, etc.
VARIABLES AND DECLARATION
• Variables: store data
>>> my_greetings = 'Hello!'
>>> my_greetings
'Hello!'

• The variables have different types (implicitly


defined)
>>> my_integer = 5
>>> my_floating_point = 26.2
>>> my_Boolean = True
>>> my_hexadecimal = 0xFF
INPUT/OUTPUT INSTRUCTIONS
• Python data processing is organised in a workflow
– Input data
– Process data to produce results
– Output results

>>> greet=input('Enter greetings


here:') Enter greetings here: Hello,
Python!
>>> greet
'Hello, Python!'
>>> print(greet)
Hello, Python!
PYTHON - BASIC OPERATORS
Operators Meaning and examples
+, -, *, /
** Exponent 10**2 ➔10 to the power of 2
% Modulus 10%3 ➔ remainder 1

>, <, >=, <=


// Floor Division 10/3 ➔ 3
== logical equality If the two values are equal
!=, <> If the two values are not equal

& Binary AND a&b


| Binary OR a|b
^ Binary XOR a^b
~ Binary NOT ~a
<<, >> Binary left or right shift a << 2 0 -11
EXPRESSIONS AND OPERATIONS
• Python interpreter computes expressions:

─ Arithmetic expressions
>>> print(3 + 4) # Prints 7
>>> print(7 * 8) # Prints 56
>>> print(45 / 4) # Prints 11.25
>>> print(2 ** 10) # Prints 1024
─ Logical expressions
>>> print(1==2) # Prints False
>>> print(5>3 and '5'>'3’) # Prints True
─ String expressions
>>> r = 'Hello'
>>> print(r + 'World’) # Prints HelloWorld
>>> print(r * 3) # Prints HelloHelloHello
TYPES AND TYPE CONVERSION
• The operations in Python must match the type of
data in order to make sense
• Sometimes Python automatically converts types
>>> x=0b100 # x is binary data
>>> print(x==4) # Prints True
• We can also convert data from one type to another
using suitable type conversion operations
>>> x='127'
>>> y=127
>>> print(x+x)
127127
>>> print(y+y)
254
>>> print(str(y)+x) # 127127
EXAMPLE: BITWISE OPERATIONS

• The bin() function returns the binary as a string

>>> print(bin(0b10011010 & 0b11001001)) #


AND 0b10001000
>>> print(bin(0b10011010 | 0b11001001)) #
OR 0b11011011
>>> print(bin(0b10011010 ^ 0b11001001)) # XOR
0b1010011
PYTHON FUNCTIONS
• Programming using Python has three
prerequisites: data, algorithms, and program
interpreter
– The data can be prepared in data files, entered using
input operations during the execution.
– The algorithms are coded using Python
– The program interpreter needs to be installed
• Installed python
– A so-called integrated development environment (IDE)
– An editor
– Interpreter
FUNCTIONS
• Function: A name with a series of operations
performed on the given parameter(s)
• Function call: Appearance of a function in a Python
expression or statement

Example: Finding the largest number


>>> x = 1034
>>> y = 1056
>>> z = 2078
>>> w = max(x, y, z) # Finds the biggest
>>> print(w)
2078
FUNCTION PARAMETERS

• A fruitful function: return a value


• A void function or procedure: does not return a
value
Example: Calculating the hypotenuse of a triangle
# Entering the sides of a right triangle
>>> sideA = 3.0
>>> sideB = 4.0
# Calculate third side
>>> hypotenuse = math.sqrt(sideA**2 + sideB**2)
# Output the result
>>> print(hypotenuse) method of a library
5.0
USING LIBRARIES OF PREDEFINED FUNCTIONS

• The function definitions can be grouped into


libraries
– Python has set of libraries for use
– You may develop your own libraries
– Before use, the libraries need to be loaded, e.g.,

import math
# Inputting the side lengths, first try
sideA = int(input('Length of side A? '))
sideB = int(input('Length of side B? '))
# Calculate third side via Pythagorean
Theorem hypotenuse = math.sqrt(sideA**2 +
sideB**2) print(hypotenuse)
0-18
ERRORS IN PYTHON PROGRAMS
• Syntax errors
- Omissions
>>> print(5 +)
SyntaxError: invalid syntax
- Typing errors (typos)
>>> pront(5)
NameError: name 'pront' is not defined
• Semantic errors
– Incorrect expressions or wrong types like
>>> total_pay = 'Jun Li' + extra_hours * rate
• Runtime errors
- Unintentional divide by zero
EXAMPLE: MARATHON TRAINING ASSISTANT
PROGRAM
# Marathon training assistant.
import math
# This function converts a number of minutes
and # seconds into just seconds.
def total_seconds(min, sec):
return min * 60 + sec
# This function calculates a speed in miles per
# hour given a time (in seconds) to run a
single # mile.
def speed(time):
return 3600 / time
MARATHON TRAINING ASSISTANT
# Prompt user for pace and mileage.
pace_minutes = int(input('Minutes per mile? '))
pace_seconds = int(input('Seconds per mile? '))
miles = int(input('Total miles? '))
# Calculate and print speed.
mph = speed(total_seconds(pace_minutes,pace_seconds))
print('Your speed is ' + str(mph) + ' mph')
# Calculate elapsed time for planned workout.
total = miles *
total_seconds(pace_minutes,pace_seconds)
elapsed_minutes = total // 60
elapsed_seconds = total % 60
print('Your elapsed time is ' + str(elapsed_minutes) +
' mins ' + str(elapsed_seconds) + ' secs')
EXAMPLE MARATHON TRAINING DATA

Time Per Mile Total Elapsed Time

Minutes Seconds Miles Speed (mph) Minutes Seconds

9 14 5 6.49819494584 46 10

8 0 3 7.5 24 0
7 45 6 7.74193548387 46 30

7 25 1 8.08988764044 7 25

You might also like