L02 - Introduction To Programming
L02 - Introduction To Programming
Semester 1, 2021
Lecture 2,
Introduction to Programming
COMMONWEALTH OF AUSTRALIA
WARNING
2
Lecture 2: Programming Basics
3
How to prepare and attend the zoom lecture for this
Python programming course
4
What is Python System?
Collection of applications
(Python build in library)
5
What is Python library?
Info1110
Collection of applications
6
Textbook library - stdio.py
In this course, you may find that we need other files and code to run
our programs based on the course textbook by Sedgewick. They are a
necessary part of the course for us to build understanding of
programming with Python.
Code stdio.py
7
What is a program?
8
Your First Program
The classic program of all time is “HelloWorld”. In Python it looks like this:
p r i n t ( " H e l l o World!" )
9
Wait, that was just some text.
Ok, true.
When you write a program you really are writing a text file, or a set of
text files, which you might even link with other programs.
The text file you write has to be in a special, very particular format,
such that it obeys the syntax of the programming language you are
using.
Once the text file is written, a compiler is used to process the file and
turn it into something that a computer can run: an executable, a
program, or an application or app.
10
Wait, that was just some text. (cont.)
11
There are many programming languages
12
Languages
1 i n t main ( ) {
2 p r i n t f ( " Hello , World! \ n " ) ;
3 }
13
Languages(cont.)
Javascript:
1 <! DOCTYPE HTML >< html >< body >< script >
2 alert( ' Hello World ' );
3 </ script > </ body > </ html >
14
In general...
What we have for all of these programs is something like the following:
1 < l o a d t h i n g s we n e e d > # "optional"
2 < b e g i n a method > # e x p l i c i t o r i m p l i c i t , " main "
3 < p r i n t the string > # d e f i n i t e l y needed
4 < end t h e p r i n t e d l i n e somehow >
5 < end t h e method > # a s i n l i n e #2
15is / 27
INFO1110 2020 S2 Dr. John Stavrakak
Exampleprogram
1 print("Hi!")
2 print("Five!")
16
Compiling and running
After making the text file, a.k.a. “the.py file”, we can compile and run in
one step
On the command line (or “terminal”) you do this: And run it like this:
~> python Hi.py
Hi!
Five!
When you type python and then the name of a program on the
command-line, you’re invoking the Python interpreter to run
your program. The Python Interpreter is itself a program, which
enables python programs to run on many different computer
platforms.[1]
17
[1] there are multiple versions of Python and this makes the statement untrue
Python haslayers
is similar to
import stdio
stdio.writeln(“Hello World”)
which is similar to
import sys
sys.stdout.write(“Hello World”)
sys.stdout.write('\n’)
sys.stdout.flush()
Which lines are instructing the Python Interpreter to print the right
message when the program is run? 18
Anatomy of the Hello World program
This is called a method. Methods can be in any order, and they can "call"
each other to perform smaller actions. This one is calling a method
called “writeln" to print the data we provide
19
Python syntax
In order for the compiler to turn your code into a working program,
the code has to obey a lot of syntax rules. You will pick up many of
these as you go along, but let’s just list a few now too:
q Some words are reserved — e.g., import, int, float, if, else.
You can’t use these for variable names.
q Variable names can’t begin with numbers.
q Expressions are delineated with parentheses ( )
q Array items are accessed with brackets [ ]
q Control flow statements should end with colons :
q Strings are delimited by double quotes " " or single quotes ’ ’
q The code is only executed if it is indented correctly (ouch!)
It’s very easy to get things wrong. Don’t worry. Most things are fixable.
DIVE IN! 20
The important bits in Hello, World
› The main part that you have to worry about and understand is the line which
does the real work. stdio. is a special variable, an object of a class, that has a
method defined for it called writeln. It’s the writeln method that actually prints
a string of characters to the console.
21
Running it
Too easy...
22
Making a Program Go: Compiling a program
23
Making a Program Go: Compiling a program
This makes a .pyc file for your program, which is where the bytecode is
(all being well).
In this case the class file would be called HelloWorld.class, and would
not be human-readable (nor can I represent it here on a slide).
24
Basic input and output
1 response = input ()
2 print( response)
What happens?
25
Basic input and output
26
Basic input and output
27
TestingCode
So you need to run your program many times with different inputs
and make sure it works. Testing is really important.
28
Syntaxand Logic and Compiler
When you write programs you have to write them in such a way that
they make sense to the compiler – there’s a syntax, that is, a set of
rules, that you must follow in order for this to be true.
However even if we have the correct syntax we may still not make
sense. This is a very famous syntactically correct yet meaningless
sentence:
Colorless green ideas sleep furiously
Noam Chomsky
In most cases your compiler will complain because of syntax errors, but
it will also sometimes give you an error message if a variable is unused,
or used before it’s been initialised, or if there is part of the code that is
unreachable. These are logic errors. The compiler won’t be able to pick
up on every logic error (else there would be no need for debugging!) but
it will help you find simple logical errors. Pay attention to those
compiler messages!
30