Download Lecture 1: Getting Started With Python

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Logic programming wikipedia , lookup

String literal wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Compiler wikipedia , lookup

Go (programming language) wikipedia , lookup

Corecursion wikipedia , lookup

Reactive programming wikipedia , lookup

Functional programming wikipedia , lookup

Programming language wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Sieve of Eratosthenes wikipedia , lookup

Object-oriented programming wikipedia , lookup

Interpreter (computing) wikipedia , lookup

Control flow wikipedia , lookup

Structured programming wikipedia , lookup

Python syntax and semantics wikipedia , lookup

Python (programming language) wikipedia , lookup

Transcript
Lecture 1: Getting Started With Python
CS1068+– Introductory Programming in Python
Summary
Programming languages
Python. Interpreters.
Dr Kieran T. Herley
and
programming.
Department of Computer Science
University College Cork
2014/15
KH (16/09/14)
Lecture 1: Getting Started With Python
2014/15
1 / 15
KH (16/09/14)
Computers and Programming
2 / 15
Why do we need programming languages?
Quandry Modern computers are computationally powerful and capable of
carrying out different tasks, but how to we get them to do what we want?
Resolution
Program Detailed and precise set of instructions specifying how a
computational task should be completed
Programmming language Notation in which a computer program is
expressed
Lecture 1: Getting Started With Python
2014/15
Computers and Programming
Computers and programming
KH (16/09/14)
Lecture 1: Getting Started With Python
2014/15
3 / 15
Natural languages such as English are too verbose, imprecise and
ambiguous
Ponder: write down instructions in English for performing long
division on two large integer values (must be complete, unmabiguous
and always produce correct answer)
Computers can’t (really) cope with English anyway
KH (16/09/14)
Lecture 1: Getting Started With Python
2014/15
4 / 15
Computers and Programming
Python
Some programming languages
Many, many different programming languages (hundreds) 1 :
A+, A++, A-0 System, ABAP, ABC, . . . C, . . . Fortran, . . . Java, . . .
JavaScript, . . . Pascal, Python Python. .. Scratch, . . . Yorick, YQL,
Zeno, Z notation, ZOPL, ZPL
Developed around 1990 by Guido van Rossum (right)
Our language is Python:
Widely adopted as intro. programming language
Many well-known systems developed using Python
(e.g. YouTube)
Modern
Powerful and expressive
Widely used
(Relatively) beginner-friendly
1
Source:
Wikipedia
Source: Wikipedia
KH (16/09/14)
Lecture 1: Getting Started With Python
2014/15
5 / 15
KH (16/09/14)
Lecture 1: Getting Started With Python
Python
LIMIT = 1000
MAXPERLINE = 10
num primes = 0
for n in range(2, LIMIT+1):
possible prime = True
# set possible prime to false if n composite
for c in range(2, n):
if n % c == 0:
possible prime = False
break
What does Python look like cont’d?
Print every prime
number in range 2 to
1000
# Program to print prime numbers up to 1000
# Kieran Herley, Sept 2014
Don’t worry about
notational detail for
now
num primes = 0
for n in range(2, LIMIT+1):
possible prime = True
LIMIT = 1000
MAXPERLINE = 10
# set possible prime to false if n composite
for c in range(2, n):
if n % c == 0:
possible prime = False
break
# print n if it is prime
if possible prime :
print (”%6d” % n, end=””)
num primes = num primes + 1
if num primes % MAXPERLINE == 0:
print ()
print ()
# print n if it is prime
if possible prime :
print (”%6d” % n, end=””)
num primes = num primes + 1
if num primes % MAXPERLINE == 0:
print ()
print ()
KH (16/09/14)
Lecture 1: Getting Started With Python
6 / 15
Python
What does Python look like?
# Program to print prime numbers up to 1000
# Kieran Herley, Sept 2014
2014/15
2014/15
7 / 15
KH (16/09/14)
Method embodied in
program must be
Complete, detailed and
unambiguous
Be guaranteed to
complete the assigned
task correctly
Program must conform to
Python’s rules:
“Finicky”: every dot,
comma and (some) space
matters
Even minor slips may
invalidate program
Lecture 1: Getting Started With Python
2014/15
8 / 15
Translations
Translations
Machine Language
Translations
We use special “translating” software known as an interpreter to
“bridge” language gap (Python to machine language)
Computers don’t understand Python either (not directly anyway)!
Source:
Intel
For the processor to execute it a program must be expressed in
machine language:
Interpreter:
typically intricate and lengthy combination of
very primative individual steps (e.g. add two numbers)
designed to be machine- rather than human-friendly
KH (16/09/14)
Lecture 1: Getting Started With Python
Analyzes Python program
Converts it into equivalent machine-language prograem
Executes it
2014/15
9 / 15
KH (16/09/14)
Using Python
Lecture 1: Getting Started With Python
2014/15
10 / 15
Using Python
Introducing IDLE
IDLE’s Command Shell
We will use the IDLE interactive development environment to
compose and run Python programs
Incorporates
Python interpreter to translate and execute programs
Interactive command shell to enter simple programs and execute them
Program editor to compose more complex programs
User types a “command”
(here a one-line python program) at the >>> prompt
and hits enter
KH (16/09/14)
Lecture 1: Getting Started With Python
2014/15
11 / 15
KH (16/09/14)
Idle takes command, interprets and executes it; any output produced is displayed
Lecture 1: Getting Started With Python
2014/15
12 / 15
Using Python
Using Python
IDLE’s Program editor
Technical Note on Python 3
Warning
Some Python 3 code not backwards compatible with older versions of the
language
Python 3 outlaws some features of older versions, so some programs
written in say Python 2 will not work
Need to be careful consulting materials (books, webpages etc)
describing older versions
To compose more complex programs can use IDLE’s program editor
(right)
Can execute progs.; output displayed in shell output (left)
KH (16/09/14)
Lecture 1: Getting Started With Python
2014/15
13 / 15
2014/15
15 / 15
Back Material
Notes and Acknowledgements
Reading
Code
Acknowledgements
KH (16/09/14)
Lecture 1: Getting Started With Python
KH (16/09/14)
Lecture 1: Getting Started With Python
2014/15
14 / 15