ICAPRG301A Apply introductory programming techniques

Download Report

Transcript ICAPRG301A Apply introductory programming techniques

ICAPRG301A Apply introductory programming techniques
Charles Babbage 1791 - 1871
Developed the design for the first computer which he
called a difference engine, though he never built it.
This has now been built based upon his design.
He also worked on a more advanced model called the
Analytical Engine with another computing pioneer
Ada Lovelace. However the design was never
completed.
On two occasions I have been asked [by members of
Parliament]: 'Pray, Mr. Babbage, if you put into the machine
wrong figures, will the right answers come out?' I am not able
rightly to apprehend the kind of confusion of ideas that could
provoke such a question - Charles Babbage
ICAPRG301A
Week 2
Strings and things
ICAPRG301A Apply introductory programming techniques
Variables
• Read the notes carefully
• Variable are words used in a program to stand for data
• Useful when we don’t know the exact data or it will change
• Can be various types of numbers (Integer, Float etc), words (called
Strings), booleans (True or False) etc
• Variable have scope, which means they exist in certain places
• In Python variables only exist once they have been given a value
ICAPRG301A
Week 2
Strings and things
ICAPRG301A Apply introductory programming techniques
Variables types
Numbers (Integers, Decimals, Money, Time, Binary)
In Python we have int and float as the main number variables
Text (Strings, characters)
In Python we mainly use Strings
Boolean (True or False)
In Python we use the key words True, False. It also has a key word None
which is used to show a variable is empty
Array
In Python we use List as the major array, but there is also Dictionary, Set
List Comprehension, Generator etc
There are 10 types of people, those who understand binary, and those who don’t
ICAPRG301A
Week 2
Strings and things
ICAPRG301A Apply introductory programming techniques
Variables assignment
a = 5 in Python this sets the variable a to the value of 5
b = a in Python this sets the variable b to the value of 5
b= b + 5 in Python this is worked out by first calculating the right hand
side and then assigning it to the left hand side
c = d + b in Python this will throw an error because the variable d does
not yet have a value
In all these examples a single equal sign means assignment.
A computer is a literal beast: it will do exactly what it thinks you told it to do.
Unfortunately, that is not necessarily what you think you told it to do.
ICAPRG301A
Week 2
Strings and things
ICAPRG301A Apply introductory programming techniques
Variables scope
a= 5
def double(x):
a=x+x
double (a)
Consider this code what will it print?
1.
2.
3.
4.
a
5
10
Throw an error
print (a)
ICAPRG301A
Week 2
Strings and things
ICAPRG301A Apply introductory programming techniques
Lists
• Special type of variable, called Arrays in some languages
• Each list contain elements, these elements can be ordinary variables
or other lists
• Declared like any variable but list is surrounded by [] and elements
separated by , eg list = [1,’hello’, [‘a’,’b’,’c’], True]
• Accessed by using slicing
ICAPRG301A
Week 2
Strings and things
ICAPRG301A Apply introductory programming techniques
Functions
• Sometimes called methods or procedures
• Small bits of code that can be named and used as a single command
• Basic building blocks of programming
• In a well constructed program everything is inside a function
• In Python is defined by the command def
ICAPRG301A
Week 2
Strings and things