Download An Introduction to Python

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

Large numbers wikipedia , lookup

Addition wikipedia , lookup

Elementary mathematics wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Transcript
AN INTRODUCTION TO
PYTHON
By Austin
Laudenslager
PY THON
Created in 1989 by Guido van Rossum
Highly extensible
Aims to create beautiful, readable code
Variables are assigned a type automatically
Blocks of code are defined by whitespace
Arguments 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






MATH
 +, -, *, % work the same as C++/Java
>>>5+5
10
>>>5*5
25
>>>7%2
1
 / returns a float, // performs integer division
>>>5/2
2.5
>>>5//2
2
 ** can be used to calculate powers
>>>5**2
25
>>>10**10
100
 Use parenthesis for grouping ()
>>>5*2+2
12
>>>5*(2+2)
20
TIP: Integers and floating
point numbers may both
be used in the same
equation without
typecasting, computes to
floating point number.
STRINGS
 Can be enclosed in either single or double quotes
>>> ‘string’ 
>>> “string” 
 \ can be used to escape quotes, \n is new line
>>>’I can\’t’
‘I can’t”
>>>’Line one\nLine two’
Line one
Line two
 Use print() to display strings, r ignores special characters
>>>Print(‘some\name’)
some
ame
>>>print(r’some\name’)
some\name
 Triple quotes allows strings to span multiple lines
>>>print(“””
Line one
Line two
“””)
Line one
Line two
STRING OPERATIONS
 + allows for concatenation, * performs repetition
>>> ‘string’ + ‘one’ >>> ‘string’ * 3
‘stringone’
‘stringstringstring’
 Strings are indexed from the left AND right
>>>word=‘Python’ >>>word[0]
‘P’
>>>word[-2]
‘0’
 Use [n:m] to slice strings
>>>word[2:4]
‘th’
>>>word[:2]
‘Py’
 Use len() to return the length of a string
>>>len(word)
6
TIP: Strings are
immutable:
Word[3] = ‘z’ will NOT
work.
LISTS
 Declared as a list of comma seperated values
>>>numbers = [1, 2, 3]
>>>strings = [‘a’, ‘b’, ‘c’]
 Can be indexed, sliced, and concatenated the same as strings
 ARE mutable, can be changed by index OR slice
>>>numbers[1] = 5
[1, 5, 3]
>>>numbers[0:2] = [4, 8, 7]
[4, 8, 7, 3]
 Can use function append() to add items to the end of a list
 Funtion len() returns length of list
>>>number.append(1)
[4, 8, 7, 3, 1]
>>>len(number)
5
 Can nest lists:
>>>nest = [numbers, words]
>>>nest[0]
>>>nest[0][3]
[4, 8, 7, 3, 1]
3
CONTROL FLOW STATEMENTS
 While, if, elif, else work like in C++
 Unlike C++, for loops iterate over items of a sequence
>>>for n in numbers:
numbers = [6, 2, 4]
 Use range(n) to iterate over a sequence of number
>>>for i in range(10)




TIP: Control flow
statements do NOT use
parenthesis in Python.
Break statement ends current loop
Else can also be used with loops
Continue statement skips to the next iteration of the loop
Pass can be used to represent a statement where no action is
needed
IMPORTANT: Python uses whitespace indentation, levels of code are
determined by indentation, not grouped by curly braces!
FUNCTIONS




All functions are of the def, or definition type
Can return any type of object
Returns none by default
Example function:
>>> def returnVal(n):
print(n)
return n
 n can be of any type and will be returned as the same type
 Can also include optional arguments with default values:
>>> def multiply(n, count=2):
return n*count

Can be called with multiply(n) or multiply(n,m)
SOURCES
https://docs.python.org/3/tutorial/index.html
http://en.wikipedia.org/wiki/Python_%28programmi
ng_language%29