FDSc in ICT

gorgeousvassalSoftware and s/w Development

Nov 7, 2013 (3 years and 8 months ago)

92 views

FDSc

in ICT

Module 106


Introduction to
Programming

Lecture 2


Python programming

Previously


Introduction


IDE
-

Idle

Objective


Primitives


First program

Introduction


6 Primitives


Allow you to program in any language


Program is a recipe


Need a language


Python
is
a dynamically
typed language that
includes (but does not require one to use)
object
oriented features
and constructs.


Introduction


The most unusual aspect of Python is that
whitespace is significant; instead of block
delimiters (braces → "{}" in the C family of
languages), indentation is used to indicate
where blocks begin and end.


We will use Python 2.6

Example Program

#Find the square root of a perfect square

x=3

ans
=0

while
ans
*
ans

<= x:


ans
=ans+1

print
ans

This block of white
space equals one TAB.
A TAB is 4 spaces

Language


Every language has a syntax


Syntax needs to be separated from semantics


Syntax


What are the legal expressions


Python gives lots of help here


Static semantics


What programs are
meaningful


Python can give some help here


E.g. “My dog is Susan”

Language


Semantics


What does the program mean?


What happens when we run the program?


You need to develop a style


Simple Parts of Python


Value:


Numbers


3


integer


6.4
-

float


Strings (typed text)



abc



Operations


Numbers


+


-


*


/


Operations


Variables


To store things


myString

= ‘
abc


Basics of Computations


Operands and Operators


Statements


key building blocks for writing
code


Branching


Iterations


Conditionals

Writing a Program


We will deduce new kinds of information


Need 2 things to write a program


Representation for fundamental data


A way to give the computer instructions
(description of recipe)


Data


2 kinds


Numbers


Strings


3 kind is Boolean


True or false


Associated with every kind of primitive is a
type


String


String


Number


integer, float, etc

Expressions


Combine things in expressions


Expression is formed of operands and
operators


Interpreter


evaluates and prints


Inside a script


no print unless we make it
explicit


3 + ‘
ab
’ (will give error


static semantic error)


Str
(3) + ‘
abc



type conversion

Type Checking


Python does type checking


Weak to strong typing


Not that strong:


‘a’ < 3 ....... Gives False


4 < ‘a’ ....... Gives True (Comparing ASCII


probably not what we
want)


Need type discipline


9/5 = 1 ...... Integer division


9%5 = 4 ..... Remainder value


3+4*5 = 23 .... Precedence rules


Think BODMAS


When in doubt, use parenthesis

Variables


Have their own values


Have an assignment


x = 3*5 ......... Creates a binding (link to a value)


y = 15


x + x ....... Let z be bound
to the value

of x

Types of Variables


Gets it from the value


X = 3 ....... Integer


Y = ‘Peter’ ....... String


Can be dynamic


X = ‘
abc
’ ..... Changed from
int

to string


This is not good because we can get operator overload


Best practice: DO NOT CHANGE VALUES
ARBITRARILY

Variables


Where can we use variables?


Any place that it is legal to do so


Statements


legal commands that Python can
interpret


print, assignment, def


Variable names


important to document.


Keywords are commands


28 keywords are excluded


print, if, else

Branching Programs


Can change the order of instructions based on
some text (usually a value of a variable)


1
st

Conditional


Colon is important
-
> defines block of instructions


Colon is the start


Carriage return is the end


Branching Programs

Look at the handout

x =
int
(
raw_input
('Enter an integer: '))

y =
int
(
raw_input
('Enter an integer: '))

z =
int
(
raw_input
('Enter an integer: '))

if x < y:


if x < z:



print 'x is least'


else:



print 'z is least'


else:



print 'y is least'

While y is less than x it will always print out ‘y is least’

While


Iteration


Loops


Also called recursion


While something is true or false, do some
tests

Example

If we don’t change the value of the loop variable, it will loop forever.

For what values of x will this run for? Does it have to be positive values?

Tutorial


Handout for Lecture 2


Play around with the values


Play around
with indentation