L02 - Introduction To Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Introduction to Programming

Semester 1, 2021
Lecture 2,
Introduction to Programming

Slides based on material by John Stavrakakis and that provided for


Sedgewick. All rights reserved
Copyright Warning

COMMONWEALTH OF AUSTRALIA

Copyright Regulations 1969

WARNING

This material has been reproduced and communicated to you by or


on behalf of the University of Sydney pursuant to Part VB of the
Copyright Act 1968 (the Act).

The material in this communication may be subject to copyright


under the Act. Any further reproduction or communication of this
material by you may be the subject of copyright protection under
the Act.

Do not remove this notice.

2
Lecture 2: Programming Basics

3
How to prepare and attend the zoom lecture for this
Python programming course

Lecture2: Programming Basics

4
What is Python System?

Collection of applications
(Python build in library)
5
What is Python library?

Info1110
Collection of applications

Print( ) stdio library:


write ()
writeln()
Welcome to
INFO1110

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.

These can be downloaded here:

Code stdio.py

7
What is a program?

A program is a set of instructions that were written, probably by a


human, in such a way that can be converted to instructions that a
computer can understand and then execute.
Wikipedia has this to say:
A computer program (also software, or just a program) is a
sequence of instructions written to perform a specified task with a
computer.

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.)

Write lots of Convert the text Run the snazzy


code in a text into executable program and
file code make millions!

You can read this, but


the computer can't
You can't read this,
but the computer can

11
There are many programming languages

. . . and no “best one”. We have chosen Python for this course as it is a


major language used in industry, is cross-platform and has some nice
features that make it very suitable for Rapid Application Development.

Here are some other major languages:


Java, Javascript, C, C++, C#, PHP, Go, Ruby, Perl

12
Languages

In old (1978) C it would be more like this:

1 i n t main ( ) {
2 p r i n t f ( " Hello , World! \ n " ) ;
3 }

in ISO-C++ it would be like this,


1 # include < iostream >
2
3 int main ()
4 {
5 std :: cout << " Hello World! " << std :: endl;
6 }

13
Languages(cont.)

in Perl like this:

1 p r i n t " H e l l o World! \ n " ;

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

This program will just print out a couple of messages.


Don’t worry if you don’t know what each line does; that’s why you’re here!

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

1 p r i n t ( " H e l l o World " )

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

Comments begin with a #

# we need to include other code for this program


import stdio

# we use the following line to print to screen


stdio.writeln(“Hello World”)

This is called an object.


Everything is an object! This is our data

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.

writeln also puts a newline at the end of whatever it prints, so you


don’t have to add a newline character, ‘\n’.

You don’t have to remember all this new terminology now

21
Running it

To run the program you type

~> python HelloWorld.py


Hello World

where it’s assumed that HelloWorld.py has no


errors

Too easy...

Go write some code!

22
Making a Program Go: Compiling a program

In order to make a Python (or Java, C, C++) program actually do


anything it has to be compiled. This is a process done by a compiler that
converts your human-readable (Python) code into machine-readable
byte code or machine code.

To compile the source code. You would type

> compiler_program human_readable_source_code

The output is machine readable instructions and data.

To run the program, the computer is fed instructions and


data

23
Making a Program Go: Compiling a program

You don’t need to compile with Python! it happens automatically but


that does not mean it is not happening!
> python -m py_compile HelloWorld.py

(followed by a Return) and that should produce no output, which


means there aren’t any errors detected by the compiler.

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).

> python HelloWorld.pyc


Hello World!

24
Basic input and output

1 response = input ()
2 print( response)

> python HelloInput.py

What happens?

25
Basic input and output

1 print(" Do you like pizza? ")


2 response = input ()
3 print( response)

> python HelloInput.py


Do you like pizza?
I am not sure
I am not sure

26
Basic input and output

1 user_ answer = input(" Do you like pizza? ")


2 print( user_ answer )

> python HelloInput.py


Do you like pizza? I am not sure
I am not sure

27
TestingCode

We will spend more time on this later, but


for now, just try to remember that you
cannot guarantee your code will work!

• Not the first time


• Not for every case
• Even if it compiles beautifully

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.

We have the same thing in English: we can’t just make up a sentence of


random words and expect it to make sense.

randomizing it’s of enough words bad order the just the.


Tguhoh aaltpeprny if you lvaee the fsrit and lsat ltetres in pacle it’s ok
29
Syntax is not Logic

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

You might also like