by austin laudenslager an introduction to python

9
By Austin Laudenslager AN INTRODUCTION TO PYTHON

Upload: brent-simmons

Post on 21-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: By Austin Laudenslager AN INTRODUCTION TO PYTHON

By Austin Laudenslager

AN INTRODUCTION TO PYTHON

Page 2: By Austin Laudenslager AN INTRODUCTION TO PYTHON

Created in 1989 by Guido van RossumHighly extensible Aims to create beautiful, readable codeVariables are assigned a type automaticallyBlocks of code are defined by whitespaceArguments from the console are stored in the argv

variable, which can be accessed using sys.argv[n]_ variable can be used in interactive mode to refer to

the previous expression – it can not be assigned a value manually

PYTHON

Page 3: By Austin Laudenslager AN INTRODUCTION TO PYTHON

+, -, *, % work the same as C++/Java

/ returns a float, // performs integer division

** can be used to calculate powers

Use parenthesis for grouping ()

MATH

TIP: Integers and floating point numbers may both be used in the same equation without typecasting, computes to floating point number.

>>>5+5 >>>5*5 >>>7%2 10 25 1

>>>5/2 >>>5//22.5 2

>>>5**2>>>10**1025 100

>>>5*2+2>>>5*(2+2)12 20

Page 4: By Austin Laudenslager AN INTRODUCTION TO PYTHON

Can be enclosed in either single or double quotes

\ can be used to escape quotes, \n is new line

Use print() to display strings, r ignores special characters

Triple quotes allows strings to span multiple lines

STRINGS

>>> ‘string’ >>> “string”

>>>’I can\’t’ >>>’Line one\nLine two’‘I can’t” Line one

Line two

>>>Print(‘some\name’) >>>print(r’some\name’)some some\nameame

>>>print(“””Line oneLine two“””)

Line oneLine two

Page 5: By Austin Laudenslager AN INTRODUCTION TO PYTHON

+ allows for concatenation, * performs repetition

Strings are indexed from the left AND right

Use [n:m] to slice strings

Use len() to return the length of a string

STRING OPERATIONS

>>> ‘string’ + ‘one’ >>> ‘string’ * 3‘stringone’‘stringstringstring’

>>>word=‘Python’ >>>word[0]>>>word[-2]

‘P’ ‘0’

>>>word[2:4] >>>word[:2]‘th’ ‘Py’

>>>len(word)6

TIP: Strings are immutable:Word[3] = ‘z’ will NOT work.

Page 6: By Austin Laudenslager AN INTRODUCTION TO PYTHON

Declared as a list of comma seperated values

Can be indexed, sliced, and concatenated the same as strings

ARE mutable, can be changed by index OR slice

Can use function append() to add items to the end of a list

Funtion len() returns length of list

Can nest lists:

LISTS

>>>numbers = [1, 2, 3] >>>strings = [‘a’, ‘b’, ‘c’]

>>>number.append(1) >>>len(number)[4, 8, 7, 3, 1] 5

>>>nest = [numbers, words]>>>nest[0] >>>nest[0][3][4, 8, 7, 3, 1] 3

>>>numbers[1] = 5>>>numbers[0:2] = [4, 8, 7][1, 5, 3] [4, 8, 7, 3]

Page 7: By Austin Laudenslager AN INTRODUCTION TO PYTHON

While, if, elif, else work like in C++Unlike C++, for loops iterate over items of a

sequencenumbers = [6, 2, 4]

Use range(n) to iterate over a sequence of number

Break statement ends current loopElse can also be used with loopsContinue statement skips to the next iteration of the

loopPass can be used to represent a statement where no

action is needed

CONTROL FLOW STATEMENTS

>>>for n in numbers:

TIP: Control flow statements do NOT use parenthesis in Python.

>>>for i in range(10)

IMPORTANT: Python uses whitespace indentation, levels of code are determined by indentation, not grouped by curly braces!

Page 8: By Austin Laudenslager AN INTRODUCTION TO PYTHON

All functions are of the def, or definition typeCan return any type of objectReturns none by defaultExample function:

n can be of any type and will be returned as the same type

Can also include optional arguments with default values:

Can be called with multiply(n) or multiply(n,m)

FUNCTIONS

>>> def returnVal(n):print(n)return n

>>> def multiply(n, count=2):return n*count

Page 9: By Austin Laudenslager AN INTRODUCTION TO PYTHON

SOURCES

https://docs.python.org/3/tutorial/index.htmlhttp://en.wikipedia.org/wiki/Python_%28programming_language%29