Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor...

18
Python Basics

Transcript of Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor...

Page 1: Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands Audience included.

Python Basics

Page 2: Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands Audience included.

2

Python History

•Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands

•Audience included physicists, social scientists, and linguists

•1983: Guido van Rossum joined the ABC team

•Late 1980s: Guido needed a language for a different project; based it on ABC, removed warts

•Python, after Monty Python

Page 3: Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands Audience included.

3

Python History

•Guido: Benevolent Dictator for Life (BDFL)

•Neat set of interviews: http://www.artima.com/intv/

•Search for “Guido”

Page 4: Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands Audience included.

4

How Python is Managed

•Guido collects and writes Python Enhancement Proposals (PEPs)

•http://www.python.org/dev/peps/

•Interested parties comment

•Guido makes final decisions

•Team of programmers (many of them volunteers!) implement the features

Page 5: Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands Audience included.

5

Python Types

•Every Python value has a type that describes what sort of value it is

•Built-in function type will tell you the type of an expression

English Pythoninteger int

“real” number floatpicture Picturepixel Pixel

colour Colorstring of letters str

Page 6: Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands Audience included.

6

Python Numeric Types

•Mathematical types: int float long bool

•English names:

•integer, floating-point number, long integer, boolean

•int range: -2147483648 … 2147483647

•float values: about -10308 … 10308

Page 7: Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands Audience included.

7

Python Numeric Types

long values: unlimited (any number of available locations in memory)

•int values that grow too large are automatically converted to long

•One more: bool (from “Boolean”): True, False

Page 8: Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands Audience included.

8

Sizes in Memory

•Integers have a fixed size

•-1 and 983471 use the same amount of memory

•Floating-point number have a fixed size

•Consequence: can’t represent every possible value

•Long integers can take up an arbitrarily large amount of memory

Page 9: Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands Audience included.

9

Python Operators

•Usual meaning: * + > < <= >= - ( )

•New operators

•power: 2 ** 5

•testing for equality: 3 == x

•remainder: 8 % 3

•division: 8 / 3

•assignment: num_bananas = 0

Page 10: Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands Audience included.

10

You’re Not My Type

•Operations involving different types use this hierarchy for the type of the result:

•float > long > int > bool

•45.3 * 400000000L # result: float

•400000000L - 45 # result: long

•3 + True # result int (more on combining ints and bools later)

Page 11: Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands Audience included.

11

Mathematical Operator Precedence

•Operators in same box group left to right

•Override using parentheses

** Exponentiation

+x -xPositive, negative

* / % //

Multiplication, division,

remainder, integer division

+ -Addition,

subtractionOperator precedence, highest to

lowest

Page 12: Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands Audience included.

12

Names

•Variable names, function names, and every other name you’ll see:

•Begin with a letter or underscore (a-z, A-Z, _)

•Are made up of letters, underscores, and numbers

•Valid: _moo_cow, cep3, I_LIKE_TRASH

•Invalid: 49ers, @home

Page 13: Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands Audience included.

13

Naming Conventions

•thEre’S a GoOD rEasON wHy WorDs haVE A StaNDaRd caPITaLizAtIon sCHemE

•Python convention for function names and variables: pothole_case

•CamelCase is sometimes seen, but not for functions and variables

•When in doubt, use lowercase_pothole

Page 14: Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands Audience included.

14

Function Definition

def function_name(parameters): body

def keyword

Zero or more, comma-separated

One or more statements

Page 15: Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands Audience included.

15

Return Statement

•Form:

•return expression

•This must appear in a function body

•How it’s executed

1.Evaluate the expression

2.Exit the function, using the result of the expression as the value of the function call

Page 16: Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands Audience included.

16

Useful __builtins__

•dir: produce a list of available functions and variables

•help: display documentation

•type: produce the type of an expression

•int, str, float, bool: type conversion

•min, max, abs

•raw_input

Page 17: Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands Audience included.

17

Statements

•You’ve seen these statements

•assignment: variable = value

•print: print expression

•function definition: def function(…): …

•function call: function(…)

•return: return expression

•for loop: for variable in list: …

•If statement: if expression: …

Page 18: Python Basics. 2 Python History Late 1970s: programming language called ABC at the Centrum voor Wiskunde en Informatica in the Netherlands Audience included.

18

What’s Next

Functions