python 循环 覆盖之前print内容_Python入门经典实例(二)

本文介绍了Python中字符串的常用操作,包括通过索引和切片获取子串,以及字符串连接。此外,还涉及了条件和循环语句,如if-elif-else及for循环,展示了如何处理用户输入和打印列表元素。文章还提到了函数的定义与使用,强调了Python中缩进的重要性。在异常处理部分,通过示例展示了如何捕获和处理异常。最后,讲解了Python文件的读写操作,包括创建、写入和读取文件的基本方法。
摘要由CSDN通过智能技术生成

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

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

中文和英文的字符串长度是否一样?

#! /usr/bin/python

# -*- coding: utf8 -*-

s=input("输入你的中文名,按回车继续");

print ("你的名字是 : " +s)

l=len(s)

print ("你中文名字的长度是:"+str(l

'''知识点:

· 类似Java,在python3里所有字符串都是unicode,所以长度一致.

'''

0a4d9becaf9d191e0df5e9dde81aecf1.png

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

#知识点:

# * 条件和循环语句

# * 如何得到控制台输入

7 函数

#! /usr/bin/python

# -*- coding: utf8 -*-

def sum(a,b):

return a+b

func = sum

r = func(5,6)

print (r)

c083ce37405b26f4e404a066f4489b94.png

# 提供默认值

def add(a,b=2):

return a+b

r=add(1)

print (r)

r=add(1,5)

print (r)

8fa0e06c51831b24c1c5ceb95be85653.png

#一个好用的函数

#! /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

知识点:

· 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

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

知识点:

· open的参数:r表示读,w写数据,在写之前先清空文件内容,a打开并附加内容.

· 打开文件之后记得关闭

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值