Python官网有一个基础入门教程,后面还有一个类似考试的题目。这个教程语言很生动,学习起来也比较轻松,感觉很好,这里把学习到的一些基础知识点做一个总结,希望作为一个备忘,感兴趣的同学可以转载。
1.输出
>>> print("This is the first python program.") This is the first python program. >>> print "Hello Python" Hello Python >>>注:这两种方式都支持,但3.0以上版本的python只支持第一种方式。
2.注释
>>> print("not commnet") not commnet >>> #print("comment") >>> ''' print("We are in a comment") print ("We are still in a comment") ''' print("We are out of the comment")注:单行注释是#(与powershell一致),多行注释是三个单引号
3.变量
>>> a = 1 >>> print(a) 1 >>> str = "hello" >>> print str hello >>> c = 2 >>> print(a+c) 3 >>> print(type(a)) <type 'int'> >>> print(type(str)) <type 'str'> >>> d = "3" >>> print(a+int(d)) 4 >>>注:python变量的类型是弱类型的,即声明变量时不需要指定类型,这个跟powershell类似。type函数是获取变量类型的,在变量前面加int,str是进行类型强制转换。
4.运算符
>>> print (3 + 4) 7 >>> print (3 - 4) -1 >>> print (3 / 4) 0 >>> print (3 * 4) 12 >>> print (3 ** 4) 81 >>> print(81//4) 20 >>> a = 0 >>> a += 2 >>> print(a) 2注:3**4 是3的4次方,//表示求最大商,+=操作和其他语言相同
5.if...else
a = 20 if a >= 22: print("if") elif a >= 21: print("elif") else: print("else")注:if后面的:不要忘记,主要elif的写法有点特殊
6.函数定义及使用
def someFunction(): print("foo") someFunction() </span><span style="font-family:Microsoft YaHei;"> def someFunction2(a, b): print(a+b) someFunction2(12,451)注:不要忘记:,传参数方式跟其他函数相同。
7.循环
for a in range(1,3): print (a) a = 1 while a < 10: print (a) a+=1注:第一种是for循环,跟常见的形式不太一样,第二种是while循环,比较常见的形式。
8.字符串操作
>>> strVar = "hello" >>> print(strVar.count('l')) 2 >>> print(strVar.find('h')) 0 >>> print(strVar.upper()) HELLO >>> print(strVar.lower()) hello >>> print(strVar.replace('l','m')) hemmo >>> print(strVar.strip()) hello >>>注:strip()是去除首尾空格
9.列表
>>> sampleList = [1,2,3,4,5,6,7,8] >>> print(sampleList) [1, 2, 3, 4, 5, 6, 7, 8] >>> sampleTuple = ("a","b","c") >>> print(sampleTuple) ('a', 'b', 'c') >>> sampleList.append(9) >>> print(sampleList) [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> sampleTuple.append("d") Traceback (most recent call last): File "<pyshell#44>", line 1, in <module> sampleTuple.append("d") AttributeError: 'tuple' object has no attribute 'append' >>>注: Python的列表有两种,list 是允许append的,但是tupe是不允许append的。但是如果确实需要append tuple,先将tuple转换成list再调用append即可;
10.字典
>>> myDic = {'username':"Austin",'age':20} >>> print(myDic["username"]) Austin >>> print(myDic["age"]) 20 >>> for d in myDic: print(d,myDic[d]) ('username', 'Austin') ('age', 20) >>>注:字典属于key/value类型的数据结构,用法比较常见。
11.异常处理
var1 = '1' try: var1 = var1 + 1 # since var1 is a string, it cannot be added to the number 1 except: print(var1, " is not a number") #so we execute this print(var1) var1 = '1' try: var2 = var1 + 1 # since var1 is a string, it cannot be added to the number 1 except: var2 = int(var1) + 1 print(var2)注:这里不是catch,而是except,注意书写形式
12.读文件
f = open("test.txt","r") #opens file with name of "test.txt" print(f.readline()) print(f.readline()) f.close() I am a test file. Maybe someday, he will promote me to a real file.</span> f = open("test.txt","r") #opens file with name of "test.txt" myList = [] for line in f: myList.append(line) print(myList) f.close() ['I am a test file.\n', 'Maybe someday, he will promote me to a real file.\n', 'Man, I long to be a real file\n', 'and hang out with all my new real file friends.']
注:r 是读的意思,w是写的意思。不要忘记关闭管道!!
13.写文件
f = open("test.txt","w") #opens file with name of "test.txt" f.close() #the file content will be cleared!!! f = open("test.txt","w") #opens file with name of "test.txt" f.write("I am a test file.") f.write("Maybe someday, he will promote me to a real file.") f.write("Man, I long to be a real file") f.write("and hang out with all my new real file friends.") f.close() f = open("test.txt","a") #opens file with name of "test.txt" f.write("and can I get some pickles on that") f.close()注意: 使用w时,open的时候会将文件已有的内容清空!!!
14.类
#ClassOne.py class Calculator(object): #define class to simulate a simple calculator def __init__ (self): #start with zero self.current = 0 def add(self, amount): #add number to current self.current += amount def getCurrent(self): return self.current calc = Calculator() print(calc.add(2)) print(calc.getCurrent())
注:类的定义形式跟常见的形式比较不同