python day 3

昨日内容回顾   
in not in 在不在里面
格式化输出 % s d
msg = '我叫%s,今年%d岁' % ('老男孩',51)
msg = '我叫%(name)s,今年%(age)d岁' % {'age':12,'name':'alex'}
msg = '我叫%s,我的学习进度1%%' % ('老男孩')
msg = "我叫%s,我看着像%r" % ('太白','郭德纲')
print(msg)
while else
如果while循环被break打断,则不走else语句。
运算符
前后比较运算
() > not > and > or
同一优先级,从左至右依次计算。
前后是数值
x or y,if x True,return x,else return y.
1 or 3 1 1 and 3 3
o or 2 2
1 > 2 or 3 and 4 < 2
数据转换:
int str
int bool 非零True,零为False。
bool int True 1 ,False 0

编码
ascii, 8位,英文字母,数字,特殊字符。
万国码,unicode
python2x,unicode 默认是两个字节表示一个字符,可以编译安装时调整。
python3x,unicode 统一是四个字节表示一个字符。
unicode 创建初期:16位,两个字节表示一个字符。
A: 0000 0001 0001 0010
国: 0000 0001 0001 0010
32位四个字节表示一个字符。
A: 0000 0001 0001 0010 0000 0001 0001 0010
国: 0000 0001 0001 0010 0000 0001 0001 0010
utf-8:最少用8位表示一个字符。
A: 0000 0001
欧洲: 0000 0001 0000 0001
中:0000 00010000 00010000 0001
utf-16
gbk :国标。
A: 0000 0001
中:0000 0001 0000 0001

基础数据类型初始
int 运算。+ - * / ** %...
bool :判断,真假,作为条件。
str.存储少量的数据。'太白','password'...操作简单,便于传输。
list 列表 [1,2,'alex',{'name':'老男孩'}] 存放大量的数据,大量的数据放在列表中便于操作。
tuple 元组 只读列表。(1,2,{'name':'老男孩'})
dict 字典 {'name_list':[nana...]},储存关系型的数据。查询速度非常快,二分查找。
set 集合。交集,并集,差集....



int
bool
str

for 循环
课堂训练:

 

 
  1 s = 'Old_Boy_are_the_best_training_institutions'
  2 # 索引
  3 '''
  4 # s1 = s[6]
  5 # print(s1)
  6 '''
  7 # 切片
  8 '''
  9 # s1 = s[0:4]
 10 # s2 = s[:4]
 11 # s3 = s[:]
 12 # s4 = s[:-1]
 13 # print(s1)
 14 # print(s2)
 15 # print(s3)
 16 # print(s4)
 17 '''
 18 # 步长
 19 '''
 20 s1 = s[0:6:2]
 21 s2 = s[:4:3]
 22 s3 = s[::-1]
 23 s4 = s[:-15:-2]
 24 # print(s1)
 25 # print(s2)
 26 # print(s3)
 27 print(s4)
 28 '''
 29 # 常规操作方法
 30 s = 'old_Boy_are_the_best_training_institutions'
 31 # 首字母大写,其他字母小写 capitalize  全部大写 upper  全部小写  lower
 32 '''
 33 s1 = s.capitalize()
 34 s2 = s.upper()
 35 s3 = s.lower()
 36 print(s1)
 37 print(s2)
 38 print(s3)
 39 '''
 40 '''
 41 code = 'FaXg'.upper()
 42 your_code = input('请输入验证码:').upper()
 43 if your_code == code:
 44     print('验证成功')
 45 '''
 46 #居中center
 47 '''
 48 s1 = s.center(100)
 49 s2 = s.center(100,'-')
 50 print(s1)
 51 print(s2)
 52  '''
 53 #大小写翻转swapcase
 54 '''
 55 s1 = s.swapcase()
 56 print(s1)
 57 '''
 58 # 每个单词首字母大写 (非字母隔开)title
 59 '''
 60 s = 'old boy are the best training institutions '
 61 s1 = s.title()
 62 print(s1)
 63 '''
 64 #判断以什么开头,以什么结尾,startswith,endswith
 65 '''
 66 s1 = s.startswith('_',7)
 67 s2 = s.endswith('s')
 68 print(s1)
 69 print(s2)
 70 '''
 71 '''
 72 s = ('abc\ncdcdsd\r')
 73 print(s)
 74 '''
 75 #去除首尾的空格 换行
 76 '''
 77 s = (' \tabc\tcdcdsd\t')
 78 s1 = s.lstrip()
 79 s2 = s.rstrip()
 80 print(s1,s2)
 81 '''
 82 # '''
 83 # name = input('请输入你的名字:').strip()
 84 # if name == 'bob':
 85 #     print('333')
 86 # s = '  ,bobisbai  '
 87 # s1 = s.strip('i  ')
 88 # print(s1)
 89 # '''
 90 #通过元素找索引 find index
 91 '''
 92 s = 'mynameisbob'
 93 print(s.find('n'))
 94 print(s.find('m', 2, 6))
 95 print(s.index('y', 1, 6))
 96 '''
 97 #寻找元素的个数,可切片 count
 98 '''
 99 s = 'mynameisbobbai'
100 s1 = s.count('b' ,7 ,13)
101 print(s1)
102 '''
103 # 替换 replace
104 '''
105 s = '我是一只小小小鸟,想要飞却怎么飞也飞不高'
106 s1 = s.replace('飞' ,'走' ,2)
107 print(s1)
108 '''
109 #分割split
110 '''
111 s = 'ni shuo ni ai bu ai wo '
112 print(s.split())
113 s1 = 'ni,shuo,ni,ai,bu,ai,wo'
114 print(s1.split(','))
115 s2 = 'niashuoaniaiabuaiawo'
116 print(s2.split('a',1))
117 '''
118 # format 格式化输出
119 
120 '''
121 s = '我是{},我今天{}岁,我明天还是{}岁,我的兴趣是{}。'.format('白恒富','27','27','学习')
122 s1 = '我是{0},我今天{1}岁,我明天还是{1}岁,我的兴趣是{2}。'.format('白恒富','27','学习')
123 s2 = '我是{name},我今天{age}岁,我明天还是{age}岁,我的兴趣是{hobby}。'.format(\
124     name='白恒富',hobby='学习',age='27')
125 print(s)
126 print(s1)
127 print(s2)
128 '''
129 # is 系列 isalnum  isalpha isdigit
130 '''
131 name = '123a'
132 if name.isdigit():
133     name = int(name)
134     print(name,type(name))
135 else:
136     print('您输入的元素非全部数字')
137 '''
138 #指定索引输出元素[],for 循环,len 位数
139 s = 'mynameisbob'
140 print(s[5])
141 print(len(s))
142 count = 0
143 # while count < len(s):
144 #     print(s[count])
145 #     count += 1
146 for i in s:
147     print(i)
148 
149 
150 
151 # count = 0
152 # s = 'fdsafdsag'
153 # print(s[0])
154 # print(s[1])
155 # print(s[2])
156 # while count < len(s):
157 #     print(s[count])
158 #     count += 1
159 # for i in s:
160 #     print(i)
课堂训练str
1 i = 13309349731
2 print(i.bit_length())
int,2进制最少多少位
 1 s = '1'
 2 '''if s:
 3     print('666')
 4 else:
 5     print('222')
 6 print(bool(s),type(bool(s)))
 7 '''
 8 i=0
 9 print(i,type(i))
10 print(type(i))
11 print(type(bool(i)))
bool转换

 

posted @ 2018-03-20 20:19  大白1#  阅读(47)  评论(0)    收藏  举报