Created
February 17, 2013 09:33
-
-
Save zhenyi2697/4970773 to your computer and use it in GitHub Desktop.
Python: start up
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# This is a start up list for python | |
# It will be used to quickly review the basic syntaxes and operations of python | |
# Code summerized from "简明 Python 教程" : http://woodpecker.org.cn/abyteofpython_cn/chinese/index.html | |
# Usually, python command options begin with two -- , e.g. --version --help | |
# Python Use True & False for boolean values | |
# None for null | |
# Control block | |
if a == b: | |
#do somethings… | |
elif a > b: | |
#do somethings else … | |
else: | |
#end. | |
while a != b: | |
#do something | |
#functions | |
def sayHello(message, time = 1): | |
# instructions here | |
print message * times | |
#doc | |
def max(a,b): | |
'''Find the maximum of two numbers: function title | |
The two numbers must be integer : function description''' | |
# do somethings... | |
pass | |
#module: a file that contains all the variables and functions | |
import sys | |
for i in sys.argv: | |
print i # print input params | |
#data structure: | |
#list: [1, 2, 3, 4, 5] #can store data with any type | |
l = [1, 2, 3, 4, 5] | |
len(list) # length of list | |
list.append("last one") # add item in the end | |
del list[0] = list.remove(0) #remove item 0 | |
#tuple : unmutable list, usually used for print | |
t = ('cat', 'dog', 'wolf') | |
print t[0] | |
print "The age for %s is %s" % (name, age) | |
# dictionary | |
ab = {} | |
ab['Guido'] = 'guido@python.org' | |
for name, address in ab.items(): | |
print 'Contact %s at %s' % (name, address) | |
if 'Guido' in ab: # OR ab.has_key('Guido') | |
print "\nGuido's address is %s" % ab['guido'] | |
# string functions: | |
name = 'Swaroop' # This is a string object | |
if name.startswith('Swa'): | |
print 'Yes, the string starts with "Swa"' | |
if 'a' in name: | |
print 'Yes, it contains the string "a"' | |
if name.find('war') != -1: | |
print 'Yes, it contains the string "war"' | |
delimiter = '_*_' | |
mylist = ['Brazil', 'Russia', 'India', 'China'] | |
print delimiter.join(mylist) | |
#oop: | |
#类的方法必须有一个额外的参数名称self, 调用时候python会自动赋值!! | |
class Person: | |
def __init__(self, name): | |
self.name = name | |
def sayHi(self): | |
print 'Hello, my name is', self.name | |
p = Person('Swaroop') | |
p.sayHi() | |
# inheritance: | |
class Teacher(SchoolMember): '''Represents a teacher.''' | |
def __init__(self, name, age, salary): | |
SchoolMember.__init__(self, name, age) | |
self.salary = salary | |
print '(Initialized Teacher: %s)' % self.name | |
# exception handling: try.. except.. | |
import sys | |
try: | |
s = raw_input('Enter something --> ') | |
except EOFError: | |
print '\nWhy did you do an EOF on me?' | |
sys.exit() # exit the program | |
#在函数中接收元组和列表 | |
def powersum(power, *args): | |
'''Return the sum of each argument raised to specified power.''' | |
total = 0 | |
for i in args: | |
total += pow(i, power) | |
return total | |
powersum(2, 3, 4) # will return 25 | |
#execute shell command: | |
os.system(zip_command) | |
#determine if file exists | |
if not os.path.exists(file_dir): | |
os.mkdir(file_dir) # make directory | |
# basic file handing: | |
poem = '''\ | |
Programming is fun | |
When the work is done | |
if you wanna make your work also fun: | |
use Python!''' | |
f = file('poem.txt', 'w') # open for 'w'riting | |
f.write(poem) # write text to file | |
f.close() # close the file | |
f = file('poem.txt') | |
# if no mode is specified, 'r'ead mode is assumed by default | |
while True: | |
line = f.readline() | |
if len(line) == 0: # Zero length indicates EOF | |
break | |
print line, | |
# Notice comma to avoid automatic newline added by Python | |
f.close() # close the file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment