Download A Python Tour: Just a Brief Introduction

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
no text concepts found
Transcript
A Python Tour: Just a Brief Introduction
"The only way to learn a new programming language is by writing programs in it."
-- B. Kernighan and D. Ritchie
"Computers are good at following instructions, but not at reading your mind."
-- D. Knuth
Program: n. A magic spell cast over a computer allowing it to turn one's input into error messages.
tr. v. To engage in a pastime similar to banging one's head against a wall, but with fewer
opportunities for reward.
Useful Definitions
• Programming Language: A language that is designed to express instructions
that can be carried out by a computer
• Program: A detailed set of instructions – written in a programming
language – for a computer to perform
• Program Execution ("running" a program): The computer performs the
program's instructions
• Machine language: The low-level language of a computer that consists of
each computation's encoding as a sequence of 0s and 1s. In machine
language, a program could look like this:
0110111110101110
1000010111010111
0000111101010111
...
• A sequence of 0s and 1s (called bits) corresponds to operations such as Add,
Store, Get Instruction, etc. Each computer (CPU) has its own
machine language.
What is Python?
• A high-level language developed by Guido van Rossum in the
Netherlands in the late 1980s. Released in 1991.
• Named after Monty Python
• Twice received recognition as the language with the largest growth in
popularity for the year (2007, 2010)
• Clean, concise syntax
– Easy to learn
– Can create powerful programs quickly
• Design is compact
– Language constructs are easy to learn
– Easy to remember
How Does Python Work?
• Python is interpreted:
– Each command is translated to machine language and
executed "on the fly"
• The other type is compiled:
– All commands are translated into machine language first
– The resulting program is stored in a new file
– Then the machine language program is executed
How Python Works
•
Python uses an interpreter, which is a program that translates each command in your
program into machine language and executes it.
• Here's what you do:
– Type your source code (Python) in a file (that ends in extension .py)
– Invoke the python interpreter (either from the command line or using IDLE) to
translate your instructions into machine language and execute them
– Or you may also use Python interactively, and enter one command at a time:
>> python
Python 3.2.4
Type "help", "copyright", "credits" or "license" for more information
>>>> print("hello, cosc 1301!")
hello, cosc1301!
>>>> for i in range(3):
...
print(i)
...
0
1
2
Comments in a Python Program
•
•
•
•
Explanations of code purpose in English
Indicated by the # character
Ignored by the interpreter
The style guide for this class requires comments:
– At the beginning of a module
– At the beginning of a function
– Before a chunk of related statements that carry out some unified action
# python program example
def main():
# Say hello
print("Hello!")
main() # Execute the main function
Question: What output do you expect from the above Python program?
A. "Hello"
B. Hello
C. Say hello
Repetition in Python
• In programs, we may want the computer to repeat an instruction or
set of instructions more than once
• To do that, we use loops, also known as repetition structures
Examples with a loop:
for i in [1, 2, 3]:
print(i) # indentation is important
for i in range(3):
print(i)
Output:
1
2
3
Output:
0
1
2
Decisions in Python
• We may want the computer to perform certain
actions only under certain conditions.
– Test for the condition (to see if it's true)
• If condition is met, perform the action
• If condition is not met, skip the action
Example:
i = 5
if i < 10:
print(i, "is less than 10")
Expected Output
def main():
for i in [2, 4, 6]:
print(i)
main()
What is the output?
A. 1 B. 2 C. i
2 4 i
3 6 i
Python Program Structure
• A source file may contain function definitions, which are a collection of Python
statements that will be executed together when the function is invoked. Function
definitions start like this:
def myFunctionName() :
• Statements that are part of the function (the loop body) must be indented(say 4 spaces)
underneath the above line – this indentation tells the Python interpreter which
statements are part of your function.
• Functions are not executed until you call them.
• It is common to include a function called main which is executed first
• A Python program can contain statements which are not part of any function.
• A comment is included after a # sign and is ignored by the Python interpreter.
An Example: Some Source Code in Python
def main():
print("hello")
print("world")
print("hello", "world")
myNum = 13
print(myNum * myNum)
main()
# call the main function
Output:
169
hello
world
hello world
Python Tour: Things to Remember
• Whitespace (blank spaces) matters
– Statements inside functions/loops/conditionals must be
indented
• Tells interpreter which statements belong
• Two spaces too few, six probably too many – be consistent
• IDLE indents automatically
– Common to include a main function