今天上班,没有什么实际的任务,闲着无聊抽空在网上搜了搜python3的内容。这篇就是copy+总结。自己动手尝试了一下
1,print
s1=input("Input your name:")
print("hello,%s" % s1)
* input("某字符串")函数:显示"某字符串",并等待用户输入.
* print()函数:如何打印.
2,字符串和数字
a = 2
b = "test"
c = str(a)+ b
print("c is %s" % (c))
d = "1111"
e = a + int(d)
print("e is %i" % (e) )
用int和str函数将字符串和数字进行转换
3,列表
#定义元组
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)
列表长度是动态的,可任意添加删除元素.
注意 []左边包括,右边不包括。eg:b=word[1:3] #word[1],word[2]没有3。
4,字典(前些天编了一个vba真是用够这个数据类型了,大学期间都没有用过几次)
x = {1:'可爱多',2:'妙脆角',3:'果粒橙'} #这三个好像被没有什么联系
print(x[1])
print(x[2])
print(x[3])
for i in x :
print ( "key is %i and item is %s" %(i,x[i]))
这个没什么好说的 字典类型和for循环的调用
5,字符串
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))
python的字符串有没有发现和元组的操作很像~
类似Java,在python3里所有字符串都是unicode,所以长度一致.
6,条件与循环
#条件和循环语句
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")
a = ['cat', 'window', 'defenestrate']
for x in a:
print (x, len(x))
7,函数
def sum(a,b,c,d)
return a+b+c-d
func = sum
r = func(5,6,7,8)
print(r)
答案是10
还有一个python好用的内置函数:range
a =range (1,10)
for i in a:
print(i)
range函数:
函数原型:range(start, end, scan):
参数含义:start:计数从start开始。默认是从0开始。例如range(5)等价于range(0, 5);
end:技术到end结束,但不包括end.例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5
scan:每次跳跃的间距,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)
8,异常处理
#! /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!")
9,文件处理
#! /usr/bin/python
spath="D:/download/baa.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()
open的参数:r表示读,w写数据,在写之前先清空文件内容,a打开并附加内容.