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
no text concepts found
Transcript
Intro to Python
Paul Martin
History
• Designed by Guido van Rossum
• Goal: “Combine remarkable power with very
clear syntax”
• Very popular in science labs
– "scientists often need to improvise when trying to
interpret results, so they are drawn to dynamic
languages which allow them to work very quickly
and see results almost immediately.“
• Guido van Rossum
Compiling and Comments
• Start Python by typing Python
– or Python myFileName
• # is symbol to comment line
– Can be placed on it’s own line or after code
Variable Assignment
• ‘=‘ is the assignment operator
• Can apply = operator to many vars at one time
– ex. x = y = z = 0
Variable Assignment Cont.
• No identifier required
– height = 5
• To declare a floating point variable
– width = 3.3
• Can declare a variable with an equation
– ex. area = width*height #width,height def above
Complex Numbers
• CmpNum = real + imagj
– ex. Cmpnum = 2.0 + 1j
• Access real numbers via Cmpnum.real
• Access imaginary numbers via Cmpnum.imag
• Magnitude can be found by abs(Cmpnum)
Strings
• Can be declare with single or double quote
– “Hello World” or ‘Hello World’
• Can use quote marker inside string when with
the escape character ‘\’
– ex. ‘This isn\’t the end’
• Can also use the other type of quote marker
– ex. “This isn’t the end”
String Concatenation and Length
• Word = ‘str’ + ‘ing’
• Word = ‘str’ ‘ing’
• Length = len(Word)
– For the above word, length will be equal to 6
String Indexing
• Start from index 0
• Word = ‘HelpA’
–
–
–
–
–
–
–
Word[4] = ‘A’ –letter at index 4
Word[0:2] = ‘He’ –letters index 0 <= x < 2
Word[0:100] = ‘HelpA’
Word[:2] = ‘He’ –letters index x < 2
Word[2:] = ‘lpA’ –letters index x > 2
Word[3:1] = ‘’
Accessing in reverse order via the negative sign
• Word[-2] = ‘p’
Lists
• List = [‘a’,’b’,2,3]
• Index Starts from 0
• Accessing Elements
– List[1] = ‘b’
– List[2:4] = [2, 3]
• Lists can be nested
– List2 = [1,List,2]
• List length accessed from len(List)
Mutating Lists
• Lists can be changed
– List[3] = List[3] + 40
• Elements can be removed/inserted
– Insert
• List[1:1] = [‘aa’,’ab’]
– Remove
• List[0:2] = []
If Statements
• If x == 0:
Do Something
elif x == 1:
Do Something Else
else:
Do Anything Else
• To end statement press enter on an empty line
While and Pass Statements
• while statement same function as C
• pass statement does nothing
– Used when program requires no action
• while x == 5:
pass
#this program will continue as long as x = 5 and
#user does not interrupt
Range Statements
• Useful for performing a loop x amount of
times
• range(10)
– creates list [0,1,2,3,4,5,6,7,8,9]
• range(5,10)
– creates list [5,6,7,8,9]
• range(0,20,5)
– Creates list [0,5,10,15]
For Statements
• Iterates over items of a sequence
• for x in a:
print x
• break and continue statements same as C
– break – exit nearest loop
– continue – continue to next iteration of loop
For Else Statements
• Executed if loop not terminated by break
• for n in range(2, 10):
if n == 11
print ’11 found’
break
else:
print ‘no 11 found’
Function Definitions
• def fun(m):
if m == 1:
print ‘m = 1’
• fun(1)
– prints ‘m = 1’
• fun(2)
– prints nothing
References
• http://python.org/
• http://www.cs.drexel.edu/~knowak/cs265_fall
_2010/cs_265_links.htm
• http://www.infoworld.com/d/developerworld/7-programming-languages-the-rise-620