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
# -*- coding:utf-8 -*- | |
s = ['a','b','c','d'] | |
for i in range(len(s)): | |
print(i,s[i]) #代码缩进:tab键值=4个空格 | |
input() | |
#生成以下索引序列 | |
#0 a |
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
# -*- 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() |
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
# -*- 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 |
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 | |
# -*- coding: utf8 -*- | |
me=input("input your name:") | |
print("你好,%s"%me) |
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
#字符串和数字 | |
a=2 #text | |
b="test" | |
#how to put int to str | |
c=str(a)+b | |
d="1111" #number | |
#how to put str to int |
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
# -*- 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) #‘**’用于拆分字典关键字参数操作符 |
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
# -*- 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' |
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
# -*- 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 |
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
#列表类似Javascript的数组,方便易用 | |
#定义元组 | |
word=['a','b','c','d','e','f','g'] | |
#如何通过索引访问元组里的元素 | |
a=word[2] | |
print ("a is: "+a) | |
b=word[1:3] | |
print ("b is: ") |
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
# -*- 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=' ') |
OlderNewer