欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python 2018/8/25

程序员文章站 2022-06-22 20:49:08
# 含多空格字符串的分割 hello = "hello python hello"print(a.split(" ")) # ['hello', 'python', '', 'hello'] print(hello.split()) # ['hello', 'python', 'hello']pri ......

# 含多空格字符串的分割

hello = "hello python hello"
print(a.split(" ")) # ['hello', 'python', '', 'hello']

print(hello.split())  # ['hello', 'python', 'hello']
print(len(a.split(" ")[2])) # ''也是一个字符串类型数据,只是什么都没有 # 0
print(a.split(" ")[2]) # 没有任何内容
print(type(a.split(" ")[2])) # <class 'str'> 字符串类型

# 字符串定义

hello = """hello python""" # hello python
hell0 = ""hello python"" # syntaxerror: invalid syntax

list1 = [10,3.13,'hello',true]
list1.append(3)
print(list1)

list1[2] = '你好'
print(list1)
list1.remove('你好')
print(list1)
# list1.remove('nihao') # valueerror: list.remove(x): x not in list
list1.pop(-2)
print(list1)

# 关于del的两种删除方式
del list1[0]
print(list1)
del(list1[0])
print(list1)

#关于while的break

内循环里执行 break 不会导致外循环一起结束
j = 0
while j < 3:
print("外层运行中")
i = 0
while i < 6:
if i ==4:
break
print("内层运行中")
i += 1
if j ==2:
break
j += 1

名片管理系统

python 2018/8/25
 1 while true:
 2     print("欢迎使用itv1.0")
 3     active = true
 4     while true:
 5         if active:
 6             name = input("请输入姓名:(按q退出)")
 7             if name == 'q':
 8                 break
 9             length_name = len(name)
10             if (length_name < 6) or (length_name > 20):
11                 print("长度错误")
12                 continue
13             else:
14                 pass
15             while true:
16                 if active:
17                     tel = input("请输入手机号:(按q返回上一级)")
18                     if tel == 'q':
19                         break
20                     length_tel = len(tel)
21                     if length_tel != 11:
22                         print("长度不合法")
23                         continue
24                     else:
25                         pass
26                     while true:
27                         sex = input("请输入性别:(按q返回上一级)")
28                         if sex == 'q':
29                             break
30                         if sex == '男' or sex == '女':
31                             print("录入成功")
32                             active = false
33                         else:
34                             print("性别错误")
35                             continue
36                         print("*" * 5 + "名片信息" + "*" * 5)
37                         print("姓名:%s" % name)
38                         print("手机号: %s" % tel)
39                         print("性别:%s" % sex)
40                         break
41                 else:
42                     break
43         else:
44             break
45     ask = input("是否继续录入信息?y/n")
46     if ask == 'n':
47         print("谢谢使用v1.0")
48         break
view code