Sunteți pe pagina 1din 42

Starting Python

Chapter 1.1-1.6
Photo from RCReptiles.com
Learning Outcomes

To understand the different stages of writing


and running a program.

To understand the difference between compiled


and interpreted languages.

To begin using the Python programming


language.
Program Development Cycle

Computers only understand machine code.

High-level programs need to be turned into


machine code.

Two ways to do this: compilation and


interpretation.
Compilation

Programmer writes program in high-level source code.

Need: Compiler software that can “translate” code


into machine language for the CPU.

Compiler compiles source code program into


executable program.

User runs executable on the computer.

Examples: C, C++, Microsoft Windows, etc


Compilation

Machine
Source Code Machine Code
runs
(program) (executable)
Compiler

Machine
Inputs runs Outputs
Executable
C++ Example

#import <iostream.h>

int main(int argc, char *argv[])


{
cout << "Hello, world!" << endl;
}
Interpretation
Programmer writes program in source code.

Need: Interpreter software (which is an executable) that


understands high-level language.

User runs interpreter and gives it the source code program as


input.

Interpreter software acts as a virtual machine — i.e. It pretends


to be a computer that “understands” high-level languages.

Examples: Perl, LISP, Python, etc


Interpretation

Source Code
(program)
Computer
Program
runs
Outputs
Interpreter
User Inputs
Perl Example

print "Hello, World!\n";


Compiling vs. Interpreting

Questions to discuss:

Suppose you write and compile a program in a compiled language.


Then you make some changes to your program source code. What do
you need to do to make sure that your executable is updated?

How about for interpreted languages?

Given the same program in a compiled and an interpreted language:


which do you think executes faster?

Different computers (e.g. Mac, PC) understand different machine


languages. What implications does this have for programs?
Platform Dependence

Platform (n): basic technology of a computer system's


hardware and software

Different computers understand different machine


languages.

Therefore, executable programs can only run on the


platform that they were compiled for.

E.g. Your copy of Firefox won’t run on a Mac!


Platform Dependence

Executable programs have to be recompiled by a


compiler that “understands” the new platform before
they can be executed on another platform.

Interpreted programs can be run anywhere, as long as an


interpreter exists for the new platform.

Question: What circumstances are more appropriate for


compiled languages? Interpreted languages?
The Python Interpreter
Python is an interpreted language.

Interpreter software is called python.

Includes an interactive version called IDLE.


Our First Python Command
Terminology

Shell: Software that serves as an interface between the


user and the computer.

Prompt: Signal from interpreter that it is ready to


receive a new command or instruction from the user.

Statement: A complete command to the interpreter.

Expression: a statement (or part of a statement) that


generates a value.
More Python Statements
Try the following yourself:

print "Hello, world!"

print(2 + 3)

print 2 + 3

print (2 + 3)

print(2+3)

print("2 + 3")

print("2+3")

print('2+3')

print("2 + 3 =", 2+3)


Dissecting our Statements

Computer programs are made from programming


statements.

Statements may take in some input, or process


some data, or produce some output.

print(): command to Python to produce some


output by displaying something on the monitor.

“Hello world!”, ‘Hello world!’, "2+3":


strings (sequences of characters)
More Dissection

print ("Hello world!"): Python


statement to output the given string.

2+3: Python expression that generates the


value of 5.

print(2+3): Python statement to output the


result of the expression 2+3.

print(“2 + 3 =”, 2+3):


Python Functions
Python allows us to create our own commands
from sequences of statements.

Commands are also called functions.

Try typing the following into IDLE:


>>> def hello():
print("Hello!")
print("Nice to meet you!")

>>> hello() Hit “Enter” twice to


get this blank line
What is the output?
What did we just do?
Python lets us put a sequence of statements together to make new commands.

New commands are also called functions.

When the function is called, the sequence of statements in its definition are
executed.
Dissecting the function
Functions Recap
Python allows us to make our own commands, called
functions.

Functions have a name and a body.

Functions are defined by putting a sequence of statements in


its body.

Statements in Python functions are indented to show that


they are part of the function body.

When the function is called or invoked, the statements inside


its definition are executed sequentially (one by one from top
to bottom).
Program Flow

Flow (n): the order in which operations are executed.

Our function just demonstrated the concept of sequential flow of


a program.

Programming statements are executed one after another, in


the order that we typed them in, from top to bottom.

Each statement is finished before the next one begins.

Later, we will learn other ways to control program flow.


Try your own function

Take the hello() function that we have


defined earlier, and add your own function to
get the following result:
CUstomizing Functions

Consider the function below:

What do we have to change to greet Mary instead?


Customizing Functions

Python allows us to customize our functions by


putting in changeable parts called parameters.
Customizing Functions

Another Example:
A slight variant
What did we change? What can we learn about the
print() function?
Customizing Functions: Recap

A parameter indicates the “customizable” part of a function.

Parameters are placed within the parentheses that go after


the function name.

Even if the function does not need parameters, we still need


the parentheses!

When we use the function, we supply the customized value


of the parameter to the function.

A function can have zero, one or more than one parameter.


Try this for Yourself

Write a function that would provide the


following output:
Try another One

If you finished the last one, try this one:

Hint: the function definition should be:


def greeting(person, year):
Python Programs
Python provides an interactive shell with a
prompt for us to type in our statements and
function definitions.

But if we log off the machine, or quit the shell,


then our function definitions go away.

We can save our function definitions into a file


called a program so that we can use it over and
over again.
A Python Program
Python programs are just regular text files with Python
statements and functions inside them.

They usually have the file extension .py

For example, my program is called “program1.py”


Dissecting Your Program

# program1.py
# Author: Grace Ngai

Lines that start with a “#” are called comments.

Comments are for the human to read.

They are ignored by the computer.

If a “#” appears in the middle of a line, the computer ignores


everything starting from the “#” through the end of the line.

print("Hello,", person) # prints out a greeting


The Importance of Comments

Question: What’s the use of comments if they


are ignored by the computer?

Answer: Comments explain to you and to other


people what your code is doing, in English (or
in Chinese if you want).

Comments are very important in a program!


Dissecting Your Program
def greet(person):
print("Hello,", person) # prints out a greeting
print("How are you today?")

The rest of the program is the function that we


typed into the interactive shell.

The program stores our function so that we can


reuse it.
Using Your Program

To use the functions inside our program, we need to


tell IDLE to import the file that contains our program.

First, locate the directory that contains the program file.

This is the
directory
(folder)
where my This is my program
program
lives.
Using Your Program

Next, we tell the Python interpreter to move to


the directory where the program is in.
chdir(): Python command
to move to a given directory
>>> chdir("C:/My Documents/Teaching/
COMP201/2009-10/Lectures/02 Intro to
Python")

Put your own


directory name here.
Using Your Program
Now we import the file that contains our
program.
Only the program name
>>> import program1
is needed, not the .py.
And we can call any of the functions defined
inside the program:
>>> greet("Jenny")
Hello, Jenny
How are you today?
Exercise

Try putting your sing() and greeting()


functions into a program, save and import it,
and use the functions.
Python Programs: Recap

Python programs are text files that contain


statements and function definitions (and lots more).

We use program to save our function definitions so


they can be reused later.

To use a program, we need to import it.

Once a program has been imported, we can use any


of the functions defined inside it.
What we’ve Learned
We know what needs to be done before a high-level program can
be executed.

We know the difference between compiled and interpreted


languages.

We know how to invoke the Python interpreter and run simple


Python commands.

We know how to make our own Python commands by defining


our own functions.

We know how to use Python programs to store our functions so


that they can be reused.

S-ar putea să vă placă și