day03 课程大纲
-
基础数类型总览
- 10203 123 3340 int +- * / 等等
- '今天吃了没?' str 存储少量的数据,+ *int 切片, 其他操作方法
- True False bool 判断真假
- [12, True, 'alex', [1,2,3 ]] list 存储大量的数据。
- (12, True, 'alex', [1,2,3 ]) tuple 存储大量的数据,不可改变里面的元素。
- {'name': '太白金星'} dict 存储大量的关联型的数据,查询速度非常快。
- set 交集,并集差集。。。
-
int
-
十进制二进制转换
-
''' 二进制转换成十进制 0001 1010 ------> ? 26 ''' b = 1 * 2**4 + 1 * 2**3 + 0 * 2**2 + 1 * 2**1 + 0 * 2**0 # print(b) # 26 ''' 42 -----> 0010 1010 '''
-
bit_lenth 十进制转化成二进制的有效长度
# bit_lenth 有效的二进制的长度 i = 4 print(i.bit_length()) # 3 i = 5 print(i.bit_length()) # 3 i = 42 print(i.bit_length()) # 4
-
-
bool
-
bool str int 三者之间的转换
# bool str int # bool <---> int ''' True 1 False 0 非零即True 0 是 False ''' # str <---> int *** ''' s1 = 10 int(s1) : 必须是数字组成 i = 100 str(i) ''' # str bool *** # 非空即True s1 = ' ' print(bool(s1)) s1 = '' # 空字符串 print(bool(s1)) # bool ---> str 无意义 print(str(True))
-
应用:
s = input('输入内容') if s: print('有内容') else: print('没有输入任何内容')
-
-
str
-
索引切片步长
s1 = 'python全栈22期' # 对字符串进行索引,切片出来的数据都是字符串类型。 # 按照索引取值 # 从左至右有顺序,下标,索引。 s2 = s1[0] print(s2,type(s2)) s3 = s1[2] print(s3) s4 = s1[-1] print(s4) # 按照切片取值。 # 顾头不顾腚 s5 = s1[0:6] s5 = s1[:6] print(s5) s6 = s1[6:] print(s6) # 切片步长 s7 = s1[:5:2] print(s7) print(s1[:]) # 倒序: s8 = s1[-1:-6:-1] print(s8) # 按索引:s1[index] # 按照切片: s1[start_index: end_index+1] # 按照切片步长: s1[start_index: end_index+1:2] # 反向按照切片步长: s1[start_index: end_index后延一位:2] # 思考题:倒序全部取出来?
-
练习题
2.有字符串s = "123a4b5c" 通过对s切片形成新的字符串s1,s1 = "123" 通过对s切片形成新的字符串s2,s2 = "a4b" 通过对s切片形成新的字符串s3,s3 = "1345" 通过对s切片形成字符串s4,s4 = "2ab" 通过对s切片形成字符串s5,s5 = "c" 通过对s切片形成字符串s6,s6 = "ba2"
-
常用操作方法
# upper lower # s1 = s.upper() # # s1 = s.lower() # print(s1,type(s1)) # 应用: # username = input('用户名') # password = input('密码') # code = 'QweA' # print(code) # your_code = input('请输入验证码:不区分大小写') # if your_code.upper() == code.upper(): # if username == '太白' and password == '123': # print('登录成功') # else: # print('用户名密码错误') # else: # print('验证码错误') # # startswith endswith # print(s.startswith('t')) *** # print(s.startswith('taiBAi')) *** # 了解 # print(s.startswith('B',3,6)) # replace msg = 'alex 很nb,alex是老男孩教育的创始人之一,alex长得很帅' # msg1 = msg.replace('alex','太白') # 默认全部替换 # msg1 = msg.replace('alex','太白',2) # print(msg) # print(msg1) # strip:空白:空格,\t \n # s4 = ' \n太白\t' # # print(s4) # s5 = s4.strip() # print(s5) # 了解 # 可以去除指定的字符 # s4 = 'rre太r白qsd' # s5 = s4.strip('qrsed') # print(s5) # split 非常重要 # 默认按照空格分隔,返回一个列表 # 指定分隔符 # str ---> list # s6 = '太白 女神 吴超' # s6 = '太白:女神:吴超' # l = s6.split(':') # print(l) # 了解: # s6 = ':barry:nvshen:wu' # # print(s6.split(':')) # print(s6.split(":",2)) # join 非常好用 # s1 = 'alex' # s2 = '+'.join(s1) # print(s2,type(s2)) # l1 = ['太白', '女神', '吴超'] # # 前提:列表里面的元素必须都是str类型 # s3 = ':'.join(l1) # print(s3) # count # s8 = 'sdfsdagsfdagfdhgfhgfhfghfdagsaa' # print(s8.count('a')) # format: 格式化输出 # # 第一种用法: # msg = '我叫{}今年{}性别{}'.format('大壮',25,'男') # 第二种用法: # msg = '我叫{0}今年{1}性别{2}我依然叫{0}'.format('大壮', 25,'男') # print(msg) # 第三种用法: # a = 100 # msg = '我叫{name}今年{age}性别{sex}'.format(age=a,sex='男',name='大壮') # print(msg) # is 系列: # name = 'taibai123' # name = '100①' # # print(name.isalnum()) #字符串由字母或数字组成 # # print(name.isalpha()) #字符串只由字母组成 # print(name.isdecimal()) #字符串只由十进制组成 # s1 = input('请输入您的金额:') # if s1.isdecimal(): # print(int(s1)) # else: # print('输入有误')
-
-
for循环
''' 有限循环 for 变量 in iterable: pass ''' s1 = '老男孩教育最好的讲师:太白' # for i in s1: # print(i) for i in s1: print(i) if i == '好': break # break continue # for else: while else:用法一样。