5 字符串
比起C/C++,Python处理字符串的方式实在太让人感动了.把字符串当列表来用吧.
#! /usr/bin/python
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.
![d8b7aa76f29329e9d1c3df212f67564e.png](https://i-blog.csdnimg.cn/blog_migrate/1ef295071b5c53926a970fd48347e051.jpeg)
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))
![645b226b2727a62236a245a3e260c118.png](https://i-blog.csdnimg.cn/blog_migrate/a4b73bca3eefca6f89e77a00317e4528.jpeg)
中文和英文的字符串长度是否一样?
#! /usr/bin/python
# -*- coding: utf8 -*-
s=input("输入你的中文名,按回车继续");
print ("你的名字是 : " +s)
l=len(s)
print ("你中文名字的长度是:"+str(l
'''知识点:
· 类似Java,在python3里所有字符串都是unicode,所以长度一致.
'''
![0a4d9becaf9d191e0df5e9dde81aecf1.png](https://i-blog.csdnimg.cn/blog_migrate/3b2109645bc10d9af6d466e865da2c90.jpeg)
6 条件和循环语句
#! /usr/bin/python
#条件和循环语句
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))
![3e963869932e5dd8b27b57293488ef9a.png](https://i-blog.csdnimg.cn/blog_migrate/7306289b214edc40b1a78f4137735697.jpeg)
#知识点:
# * 条件和循环语句
# * 如何得到控制台输入
7 函数
#! /usr/bin/python
# -*- coding: utf8 -*-
def sum(a,b):
return a+b
func = sum
r = func(5,6)
print (r)
![c083ce37405b26f4e404a066f4489b94.png](https://i-blog.csdnimg.cn/blog_migrate/5568429ccf2e3605c8a1289a996687c2.jpeg)
# 提供默认值
def add(a,b=2):
return a+b
r=add(1)
print (r)
r=add(1,5)
print (r)
![8fa0e06c51831b24c1c5ceb95be85653.png](https://i-blog.csdnimg.cn/blog_migrate/49cd6ff05b6a4d753b22009579e70f45.jpeg)
#一个好用的函数
#! /usr/bin/python
# -*- coding: utf8 -*-
# The range() function
a =range (1,10)
for i in a:
print (i)
a = range(-2,-11,-3) # The 3rd parameter stands for step
for i in a:
print (i)
![3a67b30e0ed51c5017f2c9aac839e645.png](https://i-blog.csdnimg.cn/blog_migrate/31a7962865bde819417616574ab7b584.jpeg)
知识点:
· Python 不用{}来控制程序结构,他强迫你用缩进来写程序,使代码清晰.
· 定义函数方便简单
· 方便好用的range函数
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!")
![b9c0fa5f61b72820f0c86d41f669d170.png](https://i-blog.csdnimg.cn/blog_migrate/30ae86e5d308eb189f2b2d6c92d5131a.jpeg)
9 文件处理
对比Java,python的文本处理再次让人感动
#! /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.")
f.writelines("First line 2.")
f.close()
f=open(spath,"r") # Opens file for reading
for line in f:
print("每一行的数据是:%s"%line)
f.close()
![3d7ae96cf2148364b553f4a387102ba6.png](https://i-blog.csdnimg.cn/blog_migrate/b07a251b1bb68a8cbe98890f8889c240.jpeg)
知识点:
· open的参数:r表示读,w写数据,在写之前先清空文件内容,a打开并附加内容.
· 打开文件之后记得关闭