Introduction to
Programming
Day 1: Fundamentals
Nick Efford
Twitter: @python33r
Email: N.D.Efford@leeds.ac.uk
G+: http://plus.google.com/+NickEfford
Course Structure
Todays Objectives
Mornings:
Me showing you things
...and you asking questions (hopefully)
Short quizzes and small programming exercises
Short break for coffee, etc
Afternoons:
Mainly you, doing more extended exercises
Some presentation from me if needed
Reading Material
Reading Material
http://bit.ly/interactpython
Writing Code
Sublime Text 3
http://www.sublimetext.com/3
Computer Programs
Exercise 1
Translation Tools
Programming Languages
Human language is far too complex, vague and
imprecise to be translated easily or reliably into
CPU instructions...
...so we need an intermediate representation:
this is what a programming language is
Hello World! in C
#include <stdio.h>
int main(void)
{
printf("Hello World!");
return 0;
}
Hello World! in
Intel Assembly Language
program Hello(output);
section .data
begin
writeln('Hello World!')
end.
msg:
len:
db "Hello World!",0xa
equ $-msg
section .text
global main
main:
mov
mov
mov
mov
int
edx,len
ecx,msg
ebx,1
eax,4
0x80
;
;
;
;
;
length of string
pointer to string
file descriptor (stdout)
system call number (sys_write)
interrupt for system calls
mov
mov
int
ebx,0
eax,1
0x80
Exercises 2 & 3
print("Hello World!")
hello.py: Analysis
Exercise 4
hello2.py: Analysis
1.
2.
3.
4.
A Different Approach:
Variables in C & C++
a = 1
assignment attaches a
name tag to a value
int a = 1;
a = 2
reassignment moves
tag to a new value
(old value disappears)
a = 2;
b = a
int b = a;
String Representations
'Hello World!'
"Hello World!"
Quiz
Integers
1: ""
Examples:
2: "Goodbye, Cruel World...'
3: 'I don't know much Python yet.'
45
-2
14327830152
0b101101
0x2d
0x signifies hexadecimal (base 16)
Type Conversions
int(9.7)
Examples:
float(9)
9.0
int('42')
42
str(42)
42
bool(42)
True
bool(0)
False
44.99
-2.0
1.527e-6
3e14
bool('xyx') True
notation for 3.0 1014
(no decimal point but still a float)
bool('')
False
note: no rounding!
(use round for that)
non-zero values
convert to True
non-empty strings
convert to True
Arithmetic Operators
Arithmetic Expressions
**
*
/
//
%
multiplication
division (yielding float result)
division (yielding int result)
remainder
Examples:
+
-
addition
subtraction
Three levels of
precedence here;
** higher than /,
/ higher than +, etc
7**2
2*37.5 + 12
2*(37.5 + 12)
7 / 2
7 // 2
7 % 2
Arithmetic Expressions
Input of Numbers
Examples:
x = 7
y = 2
print(x**y)
total = 1 + 4 + 2
average = total / 3
print(average)
1.
2.
3.
Extra Functionality
Exercises 5 & 6
title for
dialog box
text of
message
Summary
We have
Discussed what programming is and seen examples of
different programming languages