1. Unix Shell Scripts
#! /usr/bin/env python
...script goes here...
2. Pause the script
raw_input()
3. Python built in types
Numbers 1234, 3.14, 999L, 3+4j, Decimal
Strings 'spam', "guido's"
Lists [1,[2,'three'],4]
Dictionaries {'food':'spam', 'taste':'yum'}
Tuples (1,'spam',r,'U')
Files myfile=open('eggs','r')
Other types Sets, types,None,Booleans
4. math module contains more advanced numeric functions
import math
math.pi
math.sqrt(85)
5. random module performs random number generation and random selections
import random
random.random()
random.choice([1,2,3,4])
6. String in python
len(S) #length
S[0] #the first character of string S
S[-1] #the last item of string S
S[1:3] #substring of S, includes S[1] and S[2], without S[3]
S[0:]
S[:3]
S[:-1]
S[:]
s1+s2 # concatenation
s*8 # repetition
Notice: string objects in python are immutable, we can never change the content of the string, only can we create new strings.
s[0]='z' will cause error
s='z'+s[1:] is the correct way
s.find('pa') # find substring
s.replace('pa','XYZ') #replace
These functions don't change the original string.
s.split(',')
s.upper()
s.isalpha() #content tests: isalpha, isdigit, etc
s.rstrip() #remove whitespace on the right side
7. use dir(object) function to see what methods it has.
8. to ask about what each function does, use help(object.function)
9. show the value of character, ord('\n')
10. re module for regular expression
import re
match=re.match('/(.*)/(.*)/(.*)','/usr/home/jack')
match.groups() #output is ('usr','home','jack')
11. List is mutable, i.e. changed in place unlike string.
l[0]='123'
l.append('NI')
l.pop(2)
l.sort()
l.reverse()
12. arbitrary nesting is supported
M=[[1,2],[3,4]]
M[1] # result is [3,4]
M[0][1] # result is 2
13. List comprehension - accessing columns
c=[row[1] for row in M] # result is [2,4]
#! /usr/bin/env python
...script goes here...
2. Pause the script
raw_input()
3. Python built in types
Numbers 1234, 3.14, 999L, 3+4j, Decimal
Strings 'spam', "guido's"
Lists [1,[2,'three'],4]
Dictionaries {'food':'spam', 'taste':'yum'}
Tuples (1,'spam',r,'U')
Files myfile=open('eggs','r')
Other types Sets, types,None,Booleans
4. math module contains more advanced numeric functions
import math
math.pi
math.sqrt(85)
5. random module performs random number generation and random selections
import random
random.random()
random.choice([1,2,3,4])
6. String in python
len(S) #length
S[0] #the first character of string S
S[-1] #the last item of string S
S[1:3] #substring of S, includes S[1] and S[2], without S[3]
S[0:]
S[:3]
S[:-1]
S[:]
s1+s2 # concatenation
s*8 # repetition
Notice: string objects in python are immutable, we can never change the content of the string, only can we create new strings.
s[0]='z' will cause error
s='z'+s[1:] is the correct way
s.find('pa') # find substring
s.replace('pa','XYZ') #replace
These functions don't change the original string.
s.split(',')
s.upper()
s.isalpha() #content tests: isalpha, isdigit, etc
s.rstrip() #remove whitespace on the right side
7. use dir(object) function to see what methods it has.
8. to ask about what each function does, use help(object.function)
9. show the value of character, ord('\n')
10. re module for regular expression
import re
match=re.match('/(.*)/(.*)/(.*)','/usr/home/jack')
match.groups() #output is ('usr','home','jack')
11. List is mutable, i.e. changed in place unlike string.
l[0]='123'
l.append('NI')
l.pop(2)
l.sort()
l.reverse()
12. arbitrary nesting is supported
M=[[1,2],[3,4]]
M[1] # result is [3,4]
M[0][1] # result is 2
13. List comprehension - accessing columns
c=[row[1] for row in M] # result is [2,4]
No comments:
Post a Comment