Introduction to Python. What is Python? Interpreted object oriented high level programming language...

26
Introduction to Python
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    257
  • download

    4

Transcript of Introduction to Python. What is Python? Interpreted object oriented high level programming language...

Page 1: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

Introduction to Python

Page 2: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

What is Python?

• Interpreted object oriented high level programming language– No compiling or linking neccesary

• Extensible: add new functions or modules to the interpreter

Page 3: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

Benefits

• Offers more structure and support for large programs than shell and batch files

• Modules – can be reused• No compilation saves development time• Easy to read syntax

Page 4: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

The Interpreter• Similar to UNIX– Reads and executes commands interactively– When called with a file name argument, it reads

and executes a script from that file.

• Invoking is simple:– python

or– python -c command [arg] ...

Page 5: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

• Script name and additional arguments are passed to sys.argv

• When commands are read from a tty, the interpreter is said to be in interactive mode.– Primary Prompt: >>>– Secondary Prompt: ...• Continuation lines are needed when entering a multi-

line construct. As an example, take a look at this if statement:

Page 6: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

Example:>>> the_world_is_flat = 1

>>> if the_world_is_flat:

... print "Be careful not to fall off!"

...

Be careful not to fall off!

Page 7: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

Using Python as a Calculator• >>> 2+2• 4• >>> # This is a comment• ... 2+2• 4• >>> 2+2 # and a comment on the same line as code

• 4• >>> (50-5*6)/4• 5.0• >>> 8/5 # Fractions aren’t lost when dividing integers

• 1.6

Page 8: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

Using Variables

>>> width = 20>>> height = 5*9>>> width * height900

>>> x = y = z = 0 # Zero x, y and z>>> x0>>> y0>>> z0

Page 9: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

Using Variables>>> # try to access an undefined variable

... n

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name ’n’ is not defined

>>> # last printed expression is stored as _

>>> price * tax

12.5625

>>> price + _

113.0625

Page 10: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

Strings>>> ’spam eggs’

’spam eggs’

>>> ’doesn\’t’

"doesn’t"

>>> "doesn’t“

"doesn’t“

>>> ’"Yes," he said.’

’"Yes," he said.’

>>> "\"Yes,\" he said."

’"Yes," he said.’

>>> ’"Isn\’t," she said.’

’"Isn\’t," she said.’

Page 11: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

New lineshello = "This is a rather long string containing\n\several lines of text just as you would do in C.\n\Note that whitespace at the beginning of the line is\significant."

print(hello)This is a rather long string containingseveral lines of text just as you would do in C.Note that whitespace at the beginning of the line is significant.

Page 12: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

Manipulating Strings>>> word = ’Help’ + ’A’

>>> word

’HelpA’

>>> ’<’ + word*5 + ’>’

’<HelpAHelpAHelpAHelpAHelpA>’

>>> ’str’ ’ing’

’string’

>>> ’str’.strip() + ’ing’

’string’

Page 13: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

Subscripts>>> word[4]

’A’

>>> word[0:2]

’He’

>>> word[2:4]

’lp’

>>> word[:2] # The first two characters

’He’

>>> word[2:] # Everything except the first two characters

’lpA’

Page 14: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

More Slice Notation>>> word[0] = ’x’Traceback (most recent call last):File "<stdin>", line 1, in ?

>>> ’x’ + word[1:]’xelpA’>>> ’Splat’ + word[4]’SplatA’

>>> word[1:100]’elpA’>>> word[10:]’’>>> word[2:1]’’

Page 15: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

More Slice Notation>>> word[-1] # The last character

’A’

>>> word[-2] # The last-but-one character

’p’

>>> word[-2:] # The last two characters

’pA’

>>> word[:-2] # Everything except the last two characters

’Hel’

Page 16: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

Lists

• Most versatile compound data type.• List items may be different types.

>>> a = [’spam’, ’eggs’, 100, 1234]

>>> a

[’spam’, ’eggs’, 100, 1234]

Page 17: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

Lists Indices>>> a[0]’spam’>>> a[3]1234>>> a[-2]100>>> a[1:-1][’eggs’, 100]>>> a[:2] + [’bacon’, 2*2][’spam’, ’eggs’, ’bacon’, 4]>>> 3*a[:3] + [’Boo!’][’spam’, ’eggs’, 100, ’spam’, ’eggs’, 100, ’spam’, ’eggs’, 100, ’Boo!’]

Page 18: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

Programming>>> # Fibonacci series:... # the sum of two elements defines the next

... a, b = 0, 1>>> while b < 10:... print(b)... a, b = b, a+b...112358

Page 19: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

if Statements>>> x = int(input("Please enter an integer: "))

Please enter an integer: 42>>> if x < 0:... x = 0... print(’Negative changed to zero’)... elif x == 0:... print(’Zero’)... elif x == 1:... print(’Single’)... else:... print(’More’)...More

Page 20: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

for Statements>>> # Measure some strings:

... a = [’cat’, ’window’, ’for’]

>>> for x in a:

... print(x, len(x))

...

cat 3

window 6

for 3

Page 21: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

Using range() function

>>> for i in range(5):

... print(i)

...

0

1

2

3

4

Page 22: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

Using break and else for loops>>> for n in range(2, 10):

... for x in range(2, n):

... if n % x == 0:

... print(n, ’equals’, x, ’*’, n//x)

... break

... else:

... # loop fell through without finding a factor

... print(n, ’is a prime number’)

...

Page 23: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

Using passThe pass statement is used for when a statement is required but no action.

>>> while True:

... pass # Busy-wait for keyboard interrupt (Ctrl+C)

...

>>> class MyEmptyClass:

... pass

...

Page 24: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

Defining Functions>>> def fib(n): # write Fibonacci series up to n... """Print a Fibonacci series up to n."""... a, b = 0, 1... while b < n:... print(b, end=’ ’)... a, b = b, a+b... print()...>>> # Now call the function we just defined:... fib(2000)1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597

Page 25: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

Keyword Arguments-May use keyword arguments of the form:

keyword = valuedef parrot(voltage, state=’a stiff’,

action=’voom’):

print("-- This parrot wouldn’t", action, end=’

’)

print("if you put", voltage, "volts through it.")

Page 26: Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.

http://docs.python.org/tutorial/index.html