icaprg301a week 2 strings and things charles babbage 1791 - 1871 developed the design for the first...

7
ICAPRG301A Week 2 Strings and things 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 Apply introductory programming techniques

Upload: sibyl-bell

Post on 04-Jan-2016

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: ICAPRG301A Week 2 Strings and things Charles Babbage 1791 - 1871 Developed the design for the first computer which he called a difference engine, though

ICAPRG301A Week 2 Strings and things

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 Apply introductory programming techniques

Page 2: ICAPRG301A Week 2 Strings and things Charles Babbage 1791 - 1871 Developed the design for the first computer which he called a difference engine, though

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

Page 3: ICAPRG301A Week 2 Strings and things Charles Babbage 1791 - 1871 Developed the design for the first computer which he called a difference engine, though

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 Nonewhich is used to show a variable is empty

Array In Python we use List as the major array, but there is also Dictionary, SetList Comprehension, Generator etc

There are 10 types of people, those who understand binary, and those who don’t

Page 4: ICAPRG301A Week 2 Strings and things Charles Babbage 1791 - 1871 Developed the design for the first computer which he called a difference engine, though

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 5b = 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 handside 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.

Page 5: ICAPRG301A Week 2 Strings and things Charles Babbage 1791 - 1871 Developed the design for the first computer which he called a difference engine, though

ICAPRG301A Week 2 Strings and things

ICAPRG301A Apply introductory programming techniques

Variables scopea= 5

def double(x): a = x + x

double (a)

print (a)

Consider this code what will it print?

1. a2. 53. 104. Throw an error

Page 6: ICAPRG301A Week 2 Strings and things Charles Babbage 1791 - 1871 Developed the design for the first computer which he called a difference engine, though

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

Page 7: ICAPRG301A Week 2 Strings and things Charles Babbage 1791 - 1871 Developed the design for the first computer which he called a difference engine, though

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