Skip to content

Instantly share code, notes, and snippets.

@rswofxd
rswofxd / List序列.py
Created March 23, 2012 13:16
List序列
# -*- coding:utf-8 -*-
s = ['a','b','c','d']
for i in range(len(s)):
print(i,s[i]) #代码缩进:tab键值=4个空格
input()
#生成以下索引序列
#0 a
@rswofxd
rswofxd / for-in遍历.py
Created March 23, 2012 13:18
for-in遍历
# -*- coding:utf-8 -*-
for n in range(2,10)
for x in range(2,n)
if n % x==0:
print(n,'equals',x,'*',n//x)
break #而pass语句不做任何操作
else:
print(n,'is a prime number')
input()
@rswofxd
rswofxd / Fun函数定义.py
Created March 23, 2012 13:19
Fun函数定义
# -*- coding:utf-8 -*-
def fib2(n):
"""Return a list containing the Fibonacci series up to n.""" #docstring(must be """)
result = []
a,b = 0,1
while b<n :
result.append(b)
a,b = b,a+b
return result
@rswofxd
rswofxd / Print打印.py
Created March 23, 2012 13:21
Print打印
#! /usr/bin/python
# -*- coding: utf8 -*-
me=input("input your name:")
print("你好,%s"%me)
@rswofxd
rswofxd / 'str+int'变量.py
Created March 23, 2012 13:22
'str+int'变量
#字符串和数字
a=2 #text
b="test"
#how to put int to str
c=str(a)+b
d="1111" #number
#how to put str to int
@rswofxd
rswofxd / List序列操作.py
Created March 23, 2012 13:25
List序列操作
# -*- coding:utf-8 -*-
args = [3,6]
list(range(*args)) #‘*’用于拆分列表关键字参数操作符
#>>>[3,4,5]
def fun(a,b=3,c,d='hello'):
.......
h = {fr:7,tw:8,th:'world',fo:'hello'}
>>>fun(**h) #‘**’用于拆分字典关键字参数操作符
@rswofxd
rswofxd / Class类操作.py
Created March 23, 2012 13:26
Class类操作
# -*- coding:utf-8 -*-
class MyClass:
'"A simple example class"'
__name__ #.........default property........
__bases__ # when class is named
__dict__ #.........data property...........
def __init__(self,vars1,vars2): #'self' is the default first arg for function in a class
self.m = vars1
self.n = vars2
..... #called when class is exampled,and return 'None'
@rswofxd
rswofxd / Data_Struc数据结构.py
Created March 23, 2012 13:27
Data_Struc数据结构
# -*- coding:utf-8 -*-
list.append(x) #a[len(a):]=[x]
list.extend(L) #a[len(a):]=L
list.insert(i,x) #a.insert(0,x)put x into head
list.remove(x) #remove the first x
list.pop([i]) #remove i and return i ,the '[]' mean 'optionnal',or it may remove and retrun the last one
list.index(x) #to get the index of the first x
list.count(x) #to count the times of x-disapearing
list.sort() #to sort the list
list.reverse() #to reverse the list
@rswofxd
rswofxd / List列表操作.py
Created March 23, 2012 13:30
List列表操作
#列表类似Javascript的数组,方便易用
#定义元组
word=['a','b','c','d','e','f','g']
#如何通过索引访问元组里的元素
a=word[2]
print ("a is: "+a)
b=word[1:3]
print ("b is: ")
@rswofxd
rswofxd / File文件操作.py
Created March 23, 2012 13:31
File文件操作
# -*- coding:utf-8 -*-
#..........file..............
#open(filename,mode),mode is optional,and return a file object
f = open('/xx/xx/file','r/w/r+/a')
f.read(size)
f.read(line)
f.read(lines)
for line in f: #all-list the file content
print(line,end=' ')