在学习一门计算机语言的时候,首先接触的入门程序基本上都是"Hello World".最近学习了下Python3.1也将入门级别的程序给以记录下。
[quote]
使用python3.1小技巧:在idle里ctrl+n可以打开一个新窗口,输入源码后ctrl+s可以保存,f5运行程序.
[/quote]
[size=large]
[color=blue][b]Hello World 程序[/b][/color][/size]
[size=large][color=blue][b]你好[/b][/color][/size]
[size=large][color=blue][b] 字符和数字[/b][/color][/size]
[size=large][color=blue][b]列表[/b][/color][/size]
[quote]
知识点:
1. 列表长度是动态的,可任意添加删除元素.
2. 用索引可以很方便访问元素,甚至返回一个子列表
[/quote]
[size=large][color=blue][b]字典[/b][/color][/size]
[quote]
知识点:
1. 将他当Java的Map来用即可.
[/quote]
[size=large][color=blue][b]字符串[/b][/color][/size]
[size=large][color=blue][b]条件和循环语句[/b][/color][/size]
[quote]
知识点:
1. 条件和循环语句
2. 如何得到控制台输入
[/quote]
[size=large][color=blue][b]异常处理[/b][/color][/size]
[size=large][color=blue][b]文件处理[/b][/color][/size]
[quote]
知识点:
1. open的参数:r表示读,w写数据,在写之前先清空文件内容,a打开并附加内容.
2. 打开文件之后记得关闭
[/quote]
[size=large][color=blue][b]类和继承[/b][/color][/size]
[quote]
知识点:
1. self:类似Java的this参数
[/quote]
[size=large][color=blue][b]包机制[/b][/color][/size]
每一个.py文件称为一个module,module之间可以互相导入
module可以定义在包里面.Python定义包的方式稍微有点古怪,假设我们有一个parent文件夹,该文件夹有一个child子文件夹.child中有一个module a.py . 如何让Python知道这个文件层次结构?很简单,每个目录都放一个名为_init_.py 的文件.该文件内容可以为空.这个层次结构如下所示:
[quote]
parent
--__init_.py
--child
-- __init_.py
--a.py
b.py
[/quote]
那么Python如何找到我们定义的module?在标准包sys中,path属性记录了Python的包路径.你可以将之打印出来:
通常我们可以将module的包路径放到环境变量PYTHONPATH中,该环境变量会自动添加到sys.path属性.另一种方便的方法是编程中直接指定我们的module路径到sys.path 中:
[quote]
知识点:
1. 如何定义模块和包
2. 如何将模块路径添加到系统路径,以便python找到它们
3. 如何得到当前路径
[/quote]
[quote]
使用python3.1小技巧:在idle里ctrl+n可以打开一个新窗口,输入源码后ctrl+s可以保存,f5运行程序.
[/quote]
[size=large]
[color=blue][b]Hello World 程序[/b][/color][/size]
print("hello World !")
[size=large][color=blue][b]你好[/b][/color][/size]
s1 = input("Input your name:")
print("你好 %s:" % s1)
[size=large][color=blue][b] 字符和数字[/b][/color][/size]
#用内置函数进行转换
a=2
b="test"
c=str(a)+b
d="1234"
e=a+int(d)
print("c is %s, e is %i" %(c,e))
[size=large][color=blue][b]列表[/b][/color][/size]
#! /usr/bin/python
# -*- coding: utf8 -*-
#列表类似Javascript的数组,方便易用
#定义元组
word=['a','b','c','d','e','f','g']
#如何通过索引访问元组里的元素
a=word[2]
print ("a is: "+a)
b=word[1:3]
print ("b is: ")
print (b) # index 1 and 2 elements of word.
c=word[:2]
print ("c is: ")
print (c) # index 0 and 1 elements of word.
d=word[0:]
print ("d is: ")
print (d) # All elements of word.
#元组可以合并
e=word[:2]+word[2:]
print ("e is: ")
print (e) # All elements of word.
f=word[-1]
print ("f is: ")
print (f) # The last elements of word.
g=word[-4:-2]
print ("g is: ")
print (g) # index 3 and 4 elements of word.
h=word[-2:]
print ("h is: ")
print (h) # The last two elements.
i=word[:-2]
print ("i is: ")
print (i) # Everything except the last two characters
l=len(word)
print ("Length of word is: "+ str(l))
print ("Adds new element")
word.append('h')
print (word)
#删除元素
del word[0]
print (word)
del word[1:3]
print (word)
[quote]
知识点:
1. 列表长度是动态的,可任意添加删除元素.
2. 用索引可以很方便访问元素,甚至返回一个子列表
[/quote]
[size=large][color=blue][b]字典[/b][/color][/size]
x={'a':'aaa','b':'bbb','c':12}
print (x['a'])
print (x['b'])
print (x['c'])
for key in x:
print ("Key is %s and value is %s" % (key,x[key]))
[quote]
知识点:
1. 将他当Java的Map来用即可.
[/quote]
[size=large][color=blue][b]字符串[/b][/color][/size]
word="abcdefg"
a=word[2]
print ("a is: "+a)
b=word[1:3]
print ("b is: "+b) # index 1 and 2 elements of word.
c=word[:2]
print ("c is: "+c) # index 0 and 1 elements of word.
d=word[0:]
print ("d is: "+d) # All elements of word.
e=word[:2]+word[2:]
print ("e is: "+e) # All elements of word.
f=word[-1]
print ("f is: "+f) # The last elements of word.
g=word[-4:-2]
print ("g is: "+g) # index 3 and 4 elements of word.
h=word[-2:]
print ("h is: "+h) # The last two elements.
i=word[:-2]
print ("i is: "+i) # Everything except the last two characters
l=len(word)
print ("Length of word is: "+ str(l))
[size=large][color=blue][b]条件和循环语句[/b][/color][/size]
x=int(input("Please enter an integer:"))
if x<0:
x=0
print ("Negative changed to zero")
elif x==0:
print ("Zero")
else:
print ("More")
# Loops List
a = ['cat', 'window', 'defenestrate']
for x in a:
print (x, len(x))
[quote]
知识点:
1. 条件和循环语句
2. 如何得到控制台输入
[/quote]
[size=large][color=blue][b]异常处理[/b][/color][/size]
#! /usr/bin/python
s=input("Input your age:")
if s =="":
raise Exception("Input must no be empty.")
try:
i=int(s)
except Exception as err:
print(err)
finally: # Clean up action
print("Goodbye!")
[size=large][color=blue][b]文件处理[/b][/color][/size]
#spath="D:/test/1.txt"
f=open(spath,"w") # Opens file for writing.Creates this file doesn't exist.
f.write("First line 1.\n")
f.writelines("First line 2.")
f.close()
f=open(spath,"r") # Opens file for reading
for line in f:
print("每一行的数据是:%s"%line)
f.close()
[quote]
知识点:
1. open的参数:r表示读,w写数据,在写之前先清空文件内容,a打开并附加内容.
2. 打开文件之后记得关闭
[/quote]
[size=large][color=blue][b]类和继承[/b][/color][/size]
class Base:
def __init__(self):
self.data = []
def add(self, x):
self.data.append(x)
def addtwice(self, x):
self.add(x)
self.add(x)
# Child extends Base
class Child(Base):
def plus(self,a,b):
return a+b
oChild =Child()
oChild.add("str1")
print (oChild.data)
print (oChild.plus(2,3))
[quote]
知识点:
1. self:类似Java的this参数
[/quote]
[size=large][color=blue][b]包机制[/b][/color][/size]
每一个.py文件称为一个module,module之间可以互相导入
# a.py
def add_func(a,b):
return a+b
# b.py
from a import add_func # Also can be : import a
print ("Import add_func from module a")
print ("Result of 1 plus 2 is: ")
print (add_func(1,2)) # If using "import a" , then here should be "a.add_func"
module可以定义在包里面.Python定义包的方式稍微有点古怪,假设我们有一个parent文件夹,该文件夹有一个child子文件夹.child中有一个module a.py . 如何让Python知道这个文件层次结构?很简单,每个目录都放一个名为_init_.py 的文件.该文件内容可以为空.这个层次结构如下所示:
[quote]
parent
--__init_.py
--child
-- __init_.py
--a.py
b.py
[/quote]
那么Python如何找到我们定义的module?在标准包sys中,path属性记录了Python的包路径.你可以将之打印出来:
import sys
print(sys.path)
通常我们可以将module的包路径放到环境变量PYTHONPATH中,该环境变量会自动添加到sys.path属性.另一种方便的方法是编程中直接指定我们的module路径到sys.path 中:
import sys
import os
sys.path.append(os.getcwd()+'\\parent\\child')
print(sys.path)
from a import add_func
print (sys.path)
print ("Import add_func from module a")
print ("Result of 1 plus 2 is: ")
print (add_func(1,2))
[quote]
知识点:
1. 如何定义模块和包
2. 如何将模块路径添加到系统路径,以便python找到它们
3. 如何得到当前路径
[/quote]