Introduction to Programming - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Introduction to Programming

Description:

In this course we will use at least four languages: Python, PHP, HTML and SQL. 7/6/09 ... We will be using a free computer programming language called. Python ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 33
Provided by: publi4
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Programming


1
Introduction to Programming
2
Computer Languages
Computer Languages are used to manipulate data
stored in memory. There are 100s of different
kinds of computer languages though maybe only
between 10 and 20 are in widespread use. Example
include C/C, Java, Delphi, Basic, Python,
Perl, C, HTML, PHP, SQL, .. In this course we
will use at least four languages Python, PHP,
HTML and SQL.
3
Your First Computer Program
We will be using a free computer programming
language called Python Python is installed on
all the lab computers, if you wish load it onto
your personal computer, please download it from
python.org
4
Python
Two ways to start Python
See chapter 1, page 3 for more details
5
Enter a Program
Start Python Enter the following line at the
python console
print 5 3
6
More about print
The following line
print 5, 3
Will cause 5 and 3 to be displayed followed by a
newline To avoid a new line, put a , at the
end What does the following program do? print
5, 3, print hello
7
Values and Types
Chapter 2
8
Variables (Sec 2.2)
Data in a program is stored at specific locations
in memory. However, it is inconvenient to have
to specify actual memory addresses in a computer
program. Therefore all programming languages
introduce the idea of
VARIABLES
9
Variables (Sec 2.2)
Examples of legal variable names in Python
include
x y interestRate ATPConcentration counter i j k
In Python, upper case and lower mattes, thus ATP
and atp represent differ variables.
10
Variable Names
Use descriptive names as much as possible. For
loops (see later) use the variable names, i, j,
k For other variables try to use names that
reflect their meaning.
interestRate
Variable names will often be composed of more
than one word joined together. As a matter of
style, lower case the first word in the variable,
upper case the first letter in subsequent names
in the variable.
proteinSequenceLength
11
Variable Names
Names must start with a letter or an
underscore Names may only contain letters,
underscore characters or digits Legal names
interestRate F6P _3PG
Illegal names
3PG interestRate
12
Expressions I (sec 2.5)
a 3 b 5 c a b (addition) c a b
(subtraction) c a b (multiplication) c
a / b (division) c math.pow (2,3)
(power)
13
Expressions II (sec 2.5)
a 3 b 5 c 7 c c(a b)
(bracketing) c (ca) b (bracketing) d
math.sin (a) math.sin (b) d math.sqrt (a)
(square root)
14
Strings (sec 2.8)
Examples of strings A banana DNA
GCGGTACTT Pi 3.1415 Operations on
strings A DNA DNA3
15
Strings (sec 3.1)
Converting strings to numbers, numbers to
strings A 3.14 Convert a string to a
number Pi float (A) Convert a number to a
string A str (Pi) N 99 i int (N)
16
To use math functions
LIBRARIES import math x math.sin
(30.0) print x
Import the math library
17
Getting Data from the User
interest input (Enter the interest rate
) money input (Enter the amount of cash
) print interest/100money myName raw_input
(Get my name ) MyName holds a string
18
Loops The FOR Loop
Loops permit a calculation to be repeated many
times
for i in range(4) do stuff
Upper limit of iteration
i will range from 0 to 3
The iteration count MUST be an integer, if you
want to use a float, use int (x), where x is a
floating point number, to convert the float to
an Integer.
19
Loops The FOR Loop
Loops permit a calculation to be repeated many
times
n input(How many times to loop ) for i in
range(n) print i
20
Loops The FOR Loop
  • Write a program to print out the nth times table.
  • User input
  • The length of the table (usually 10)
  • The particular table to generate

21
Loops The FOR Loop
How long is the table ? 10 Which table ? 5 1
times 5 5 2 times 5 10 3 times 5 15 4
times 5 20 5 times 5 25 6 times 5 30 7
times 5 35 8 times 5 40 9 times 5 45 10
times 5 50
22
Loops The FOR Loop
print "Print out the times table" print n
input(How long is the table ) t input(Which
table )) for i in range (n) print i, it
A more elaborate print statement
print i, times , t , it
23
Nested Loops The FOR Loop
It is possible to nest loops, that is to have
loops within loops
for i in range (n) for j in range (m)
print i,j
24
Nested Loops The FOR Loop
Write a program to print out a complete times
table. Let the user specify the size of the table
ie the number of rows and columns.
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20
5 10 15 20 25
25
Nested Loops The FOR Loop
Write a program to print out a complete times
table. Let the user specify the size of the table.
print "Print out the times table" print n input
(How big is the table ) for i in range
(n) for j in range (n) print ij,
print
26
Commenting
Commenting a program is very important, it allow
one to annotate a program with reminders,
information on how the program works who wrote it
etc.
This is a comment
When doing assignments, start all your programs
with the following comment lines.
Program Title Frogger Game Author Joe
Smith Date 18 July 2007
27
Commenting
Program Title Times Table Author Joe
Smith Date 18 July 2007 print Print out the
times table print n input (How big is the
table ) Start a nested loop, work row by
row for i in range (n) for j in range (n)
print ij, print
28
Assignment 1a
Write a program to compute the results of a
savings plan. The user should be able to enter
the Starting deposit, Number of years of
saving, initial deposit, interest rate, how much
additional savings to be added each year. The
program should print out the bank balance at the
end of each year. Marks will be given for a
working program (50), variable naming
(25), program commenting (25). Hand in by next
Friday Cover sheet with student name, class,
date, sheet with program listing, sheet with a
number of runs to illustrate program.
29
Assignment 1a
How many years to save 10 Initial Deposit
1000 Interest rate 5 Additional savings per
year 100 Year 1 Amount 1150 Year 2 Amount
1307.5 Year 3 Amount 1472.875 Year 4 Amount
1646.51875 Year 5 Amount 1828.8446875 Year
6 Amount 2020.286921875 Year 7 Amount
2221.301267969 Year 8 Amount
2432.366331367 Year 9 Amount
2653.984647936 Year 10 Amount 2886.683880332
30
Computer Memory
123
0
3.1415
1
2.7182
2
3
4
5
6
Address
7
8
9
10
11
12
13
31
Using Variables Assignments
3
a
a 3 b 5 print a b
5
b
Address
32
Using Variables Assignments
3
a
a 3 b 5 c a b print c
5
b
c
8
Address
Write a Comment
User Comments (0)
About PowerShow.com