定义:方法就是提前内置给各个数据类型的一些功能
# 方法的表现形式
int(res) # 方法名()# 语法格式
数据类型.方法名()
整型和浮点型的内置方法
整型
res = '123'
print(type(res)) # str
print(int(res)) # 123 int
'''int方法只能够转纯数字类型'''
res2 = '123kevin'
print(int(res2))print(int('')) # invalid literal for int() with base 10: ''
res = int(input('请输入内容'))
print(int(res))
十进制转换其他进制
'''二进制、八进制、十进制、十六进制之间的互相转换'''
# 10---------->00001010
print(bin(2000)) # 0b1010 # 0b代表的是二进制
print(oct(2000)) # 0o3720
print(hex(2000)) # 0x64 0x7d0 0-9 11 12 14 14 15"""
ob:二进制
0o:八进制
0x:十六进制
a b c d e f
"""#### 反向转换
print(int('0b1010', 2)) # 10
print(int('0o3720', 8)) # 2000
print(int('0x7d0', 16)) # 2000
浮点型
s = '123.1'
print(float(s))
字符串类型的内置方法
数据类型转换
res = 123
print(str(res), type(str(res))) # '123'
print(str(123.1)) # '123'
print(str('helloworld')) # '123'
print(str([1, 2, 3, 4]), type(str([1, 2, 3, 4]))) # [1, 2, 3, 4]
s_list = str([1, 2, 3, 4]) # '[1, 2, 3, 4]'
print(s_list[0])
print(str({'a':1}), type(str({'a':1}))) # '{'a':1}'
print(str((1, 2, 3)), type(str((1, 2, 3)))) # '{'a':1}'
print(str({1, 2, 3, 4}), type(str({1, 2, 3, 4}))) # '{'a':1}'
字符串不允许改变值
# 2.切片(顾头不顾尾,步长)
# 2.1 顾头不顾尾:取出索引为0到8的所有字符
print(str1[0:3]) # hel
print(str1[0:9]) # hello pyt
print(str1[0:9:3]) # hloptprint(str1[::-1])
print(str1[6:]) # 一直切到字符串的末尾
print(str1[:5]) # 从索引位置为0的开始
print(str1[::-1]) # 这个写法就是把一个字符串翻转了# str1 = 'hello pythonpythonpythonpythonpythonpythonpythonpythonpythonpythonpythonpython'
# str1 = 'hello python'
print(len(str1)) # length 13print('hello1' in str1)
print('hello' not in str1)# .strip 移除字符串首尾指定的字符(默认移除空格)
str1 = ' hello python! '
str1 = '$$hello python!$$'
str1 = '@@@hello p@@@ython!@@@@@@'
print(str1.strip(), len(str1), len(str1.strip()))
print(str1.lstrip(), len(str1), len(str1.lstrip()))
print(str1.rstrip(), len(str1), len(str1.rstrip()))print(str1.strip('@'), len(str1), len(str1.strip()))
print(str1.lstrip('@'), len(str1), len(str1.lstrip()))
print(str1.rstrip('@'), len(str1), len(str1.rstrip()))
username = input('username:').strip()
password = input('password:').strip()
str1 = 'kevin@18@male@200000' # ['kevin', 18, 'male', 2000]
# res = str1.split('|') # ['kevin', '18', 'male', '200000']
res = str1.split('@') # ['kevin', '18', 'male', '200000']
print(res)for i in str1:
print(i)
编写⽤户登录程序
要求:
温馨提示:
⽤户名与密码来源于字符串source_data = 'kevin|123'
想办法从中拆分出⽤户名和密码⽤于后续账户信息⽐对
普通要求:
1.验证失败情况下可⼀直循环验证 成功则直接退出
拔⾼练习:
1.只允许三次失败机会
2.登录成功后进⼊内层循环,⽤户输⼊任何指令利⽤格式化输出
打印正在执⾏该⽤户指令即可,直到⽤户输⼊字⺟q退出内层循环
# 1. 从字符串中拆分出用户名和密码
source_data = 'kevin|123'
# res = source_data.split('|') # ['kevin', '123']
# real_username = res[0]
# real_password = res[1]
real_username, real_password = source_data.split('|') # ['kevin', '123'] # 解压赋值
### 3. 定义一个用来记录输错密码次数的计数器
count = 0
while True:
## 输错次数3次直接结束程序
if count == 3:
break
username = input('username:').strip()
password = input('password:').strip()
# 2. 比较用户名和密码的正确性
if username == real_username and password == real_password:
print('登录成功')
while True:
cmd = input('请输入你的指令:').strip()
if cmd == 'q':
break
print('正在执行你的指令:%s' % cmd)
else:
print('用户名或者密码错误')
count+=1
列表的内置方法
"""所有可以支持for循环的类型都可以转为列表"""
1.1 正向取(从左往右)
my_friends = ['tony', 'jason', 'tom', 4, 5, 'tom', 4, 5]
print(my_friends[0])
print(my_friends[-1])
print(my_friends[-2])
print(my_friends[6])print(my_friends[0:3]) # ['tony', 'jason', 'tom']
print(my_friends[3:]) # ['tony', 'jason', 'tom', 4, 5]
print(my_friends[:3]) # ['tony', 'jason', 'tom']
print(my_friends[0:5:2]) # ['tony', 'tom', 5]
print(my_friends[::-1]) # [5, 4, 'tom', 'jason', 'tony']
print(len(my_friends)) # 5
my_friends = ['tony', 'jason', 'tom', 4, 5] # 可变类型与不可变类型
print(my_friends)
列表添加元素
1. append在末尾追加元素
my_friends.append(666)
my_friends.append(777)
my_friends.append([1, 2, 3, 4]) # ['tony', 'jason', 'tom', 4, 5, [1, 2, 3, 4]]
2. insert
my_friends.insert(2, 666)
'''insert能够指定你插入数据的位置'''
my_friends.insert(2, [1, 2, 3, 4]) # ['tony', 'jason', [1, 2, 3, 4], 'tom', 4, 5]
print(my_friends)
my_friends = ['tony', 'jason', 'tom', 4, 5]
3. extend扩展-----------》合并两个列表的元素值
my_friends.extend([11, 22, 33, 44, 55]) # ['tony', 'jason', 'tom', 4, 5, 11, 22, 33, 44, 55]
print(my_friends)
extend: for + append
new_list = [11, 22, 33, 44, 55]
for i in new_list:
my_friends.append(i)
print(my_friends)
### 修改
my_friends[0] = 'jerry'
my_friends[2] = 'jerry111'
my_friends[666] = 'jerry111'
print(my_friends)
删除
my_friends = ['tony', 'jason', 'tom', 4, 5]
1. del
del my_friends[0]
del my_friends[0]
del my_friends[0]
2. remove
my_friends.remove('tony')
res = my_friends.remove('tom')
print(res) # None
3. pop弹出
res = my_friends.pop(2) # ['tony', 'jason', 'tom', 4]
# my_friends.pop() # ['tony', 'jason', 'tom', 4]
my_friends.pop() # ['tony', 'jason', 'tom', 4]
print(my_friends)
print(res)