1、if判断
语法1:
if 条件:
code1
code2
code3
if 条件:
code1
code2
code3
if 条件:
code1
code2
code3
语法2:
if 条件:
code1
code2
code3
else:
code1
code2
code3
语法3:
if 条件1:
pass
elif 条件2:
pass
elif 条件3:
pass
else:
pass
强调:
if的多分支或自上而下一次匹配条件,一旦匹配成功就立刻执行该条件
下的自代码块,其余条件即便是成立也再去执行了
语法4:
if 条件1:
if 条件2:
pass
2、while循环(又称条件循环)
语法:
while 条件:
code1
code2
code3
day=1
while day < 11:
默写
听课
写作业
晚自习
day+=1
while+break:终止本层循环
while+continue:终止本次循环,直接进入下一次循环
while+else:else会在循环结束后进行一次判定,如果循环期间没有被break打断,就应该执行else的代码
while循环的嵌套:
while 条件1:
while 条件2:
break
break
tag=True
while tag:
while tag:
if 条件:
tag=False
3、for循环
语法:
l=[1,2,3]
for i in l:
print(i)
for i in range(1,5,2):#1 3
print(i)
l=['a','b','c']
for i in range(len(l)): #range(3)
print(i,l[i])
for+break
for+continue
for+else
今日内容:
数据类型+内置方法
01 数据类型
#一:基本使用
# 1 int用途:年龄 号码 等级...
# 2 定义方式
age=10 #age=int(10)
# x=int('1111') # int只能将纯数字的字符串转成十进制的整型
# print(type(x))
# 3 常用操作+内置的方法
#算数运算,比较运算
#二:该类型总结
# 1 存一个值or存多个值
# 只能存一个值
# age=10
# age
# 2 有序or无序
# 没有有序无序这么一说
# 3 可变or不可变
#可变不可变类型总结:
# 可变指的是改变原值,即在原值基础上进行修改
# 可变类型的底层原理:在id不变的情况下,值可以改变
# 不可变类型的底层原理:值改变,id也跟着变
#int是不可变数据类型
# age=10
# print(id(age))
# age=11
# print(id(age))
# 了解(**):
# 长整型Long,只有在python2中才有长整型号
# 复数complex
# x=1-2j
# print(x.real)
# print(x.imag)
# print(type(x))
#一:基本使用
# 1 用途:薪资 身高 体重
#
# 2 定义方式
salary=3.1 #salary=float(3.1)
n=float('3.1')
# 3 常用操作
#算数运算 比较运算
# #二:该类型总结
# 1 存一个值or存多个值
# 只能存一个值
#
# 2 有序or无序
# 没有有序无序这么一说
# 3 可变or不可变
# float是不可变类型
# x=3.1
# print(id(x))
# x=3.2
# print(id(x))
02 字符串类型
# 1 用途: 描述性质的数据,比如人的名字,单个爱好,地址
#
# 2 定义方式
# name='egon' #name=str('egon')
# x=str(1)
# y=str(1.1)
# z=str([1,2,3])
# n=str({'a':1})
# print(type(x))
# print(type(y))
# print(type(z))
# print(type(n))
# 3 常用操作+内置的方法
#优先掌握的操作(*****):
#1、按索引取值(正向取+反向取) :只能取
msg='hello world'
# print(type(msg[5]))
# print(msg[-1])
# msg[2]='A'
#2、切片(顾头不顾尾,步长)
msg='hello world'
# print(msg[1:5],type(msg[1:5]))
# print(msg[6:-1])
# print(msg[6:11])
# print(msg[6:])
# print(msg[6::2])
# 了解(**)
# print(msg[0:])
# print(msg[::-1])
# msg='hello world'
# print(msg[-3:-6:-1])
# print(msg[6:9:-1])
#3、长度len
# msg='hello world'
# print(len(msg))
#4、成员运算in和not in
# print('SB' in 'my name is alex,alex is SB')
# print('alex' in 'my name is alex,alex is SB')
# print('egon' not in 'my name is alex,alex is SB') # 推荐
# print(not 'egon' in 'my name is alex,alex is SB')
#5、移除空白strip
# name=' e gon '
# print(name.strip(' '))
# print(name.strip())
# name='****A*e*gon****'
# print(name.strip('*'))
# name='****egon****'
# print(name.lstrip('*'))
# print(name.rstrip('*'))
# pwd=input('>>: ').strip() #pwd='123 '
# if pwd == '123':
# print('login successful')
# msg='cccabcdefgccccc'
# 'c'
# print(msg.strip('c'))
# print('*-=egon *&^'.strip('-= *&^'))
#6、切分split
# msg='egon:18:male:180:160'
# l=msg.split(':')
# print(l)
# print(l[3])
#7、循环
# msg='hello world'
# for x in msg:
# print(x)
# 需要掌握的操作(****)
#1、strip,lstrip,rstrip
#2、lower,upper #lower:字母全小写 upper:字母全大写
# name='EoN'
# print(name.lower())
# name='egonN'
# print(name.upper())
#3、startswith,endswith
# print('alex is SB'.startswith('alex'))
# print('alex is SB'.endswith('B'))
#4、format的三种玩法
# print('my name is %s my age is %s' %('egon',18))
# print('my name is {name} my age is {age}'.format(age=18,name='egon')) # 可以打破位置的限制,但仍能指名道姓地为指定的参数传值
# print('my name is {} my age is {}'.format('egon',18))
# print('my name is {0} my age is {1} {1} {1} {1}'.format('egon',18))
#5、split,rsplit
# info='egon:18:male'
# print(info.split(':',1))
# print(info.split(':',1)) #['egon','18:male']
# print(info.rsplit(':',1)) #['egon:18','male']
#6、join:只能将元素全为字符串的列表拼成一个大的字符串
# info='egon:18:male'
# l=info.split(':')
# print(l)
# new_info='-'.join(l)
# print(new_info)
# num=['a','b','c']
# ':'.join(num) #'a'+':'+'b'+':'+'c'
# num=[1,2,'c']
# ':'.join(num) #1+':'+2+':'+'c'
#7、replace
# msg='my name is wupeiqi,wupeiqi is SB'
# print(msg.replace('wupeiqi','Pig',1))
# print(msg)
#8、isdigit
# print('111.1'.isdigit())
# print('1111'.isdigit())
# AGE=73
# age=input('>>: ').strip() #age='asdfasdf'
# if age.isdigit():
# age=int(age)
# if age > AGE:
# print('too big')
# elif age < AGE:
# print('too small')
# else:
# print('you got it')
# else:
# print('必须输入数字啊傻叉')
# 其他操作(了解即可)(**)
#1、find,rfind,index,rindex,count
msg='my name is alex,alex is hahaha'
# print(msg.find('alex'))
# print(msg.find('SB')) #找不到会返回-1
# print(msg.index('alex'))
# print(msg.index('SB')) # 找不到index会报错
# print(msg.find('alex',0,3))
# print(msg.count('alex'))
# print(msg.count('alex',0,15))
#2、center,ljust,rjust,zfill
# print('info egon'.center(50,'-'))
# print('info egon'.ljust(50,'-'))
# print('info egon'.rjust(50,'-'))
# print('info egon'.zfill(50))
#3、expandtabs #将tab符号(\t)转换成空格
# print('a\tb\tc'.expandtabs(1))
#4、captalize,swapcase,title==首字母大写,大小写互换
# print('my name is egon'.capitalize()) #My name is egon
# print('my Name Is egon'.swapcase()) #MY nAME iS EGON
# print('my name is egon'.title()) #My Name Is Egon
#5、is数字系列
num1=b'4' #bytes
num2=u'4' #unicode,python3中无需加u就是unicode
num3='壹' #中文数字
num4='Ⅳ' #罗马数字
#isdigit():bytes,unicode
# print(num1.isdigit())
# print(num2.isdigit())
# print(num3.isdigit())
# print(num4.isdigit())
#isdecimal():unicode
# print(num2.isdecimal())
# print(num3.isdecimal())
# print(num4.isdecimal())
#isnumberic;unicode,中文,罗马
# print(num2.isnumeric())
# print(num3.isnumeric())
# print(num4.isnumeric())
#6、is其他
# print('abasdf123123'.isalnum())
# print('asdfasdf'.isalpha())
# print('egon'.islower())
# print('ABC'.isupper())
# print(' '.isspace())
# print('My Name Is Egon'.istitle())
# #二:该类型总结
# 1 存一个值or存多个值
# 只能存一个值
#
# 2 有序or无序
# 有序
# 3 可变or不可变
# 不可变
name='egon'
print(id(name))
name='alex'
print(id(name))
#作用:多个装备,多个爱好,多门课程,多个女朋友等
#定义:[]内可以有多个任意类型的值,逗号分隔
# my_girl_friends=['alex','wupeiqi','yuanhao',4,5] #本质my_girl_friends=list([...])
#
# l=list('hello') # list内只能跟能够被for循环遍历的数据类型
# print(l)
# l=list({'a':1,'b':2})
# print(l)
#优先掌握的操作:
#1、按索引存取值(正向存取+反向存取):即可存也可以取
# names=['alex','wxx','lxx','egon']
# names[0]='ALEX'
# print(names)
#2、切片(顾头不顾尾,步长)
# names=['alex','wxx','lxx','egon']
# print(names[0:3])
#3、长度
# names=['alex','wxx','lxx','egon']
# print(len(names))
#4、成员运算in和not in
# names=['alex','wxx','lxx','egon',4]
# print(4 in names)
#5、追加
# names=['alex','wxx','lxx','egon']
# names.append('cxx1')
# names.append('cxx2')
# names.append('cxx3')
# print(names)
#6、删除
# names=['alex','wxx','lxx','egon']
# del names[2]
# print(names)
#7、循环
# names=['alex','wxx','lxx','egon']
# for name in names:
# print(name)
# 需要掌握的操作(****)
# names=['alex','wxx','lxx','egon',4,3.1]
# names.insert(1,'SB')
# print(names)
# names=['alex','wxx','lxx','egon',4,3.1]
# res=names.remove('wxx') # 单纯的删掉,是按照元素的值去删除,没有返回值
# print(res)
# print(names)
# names=['alex','wxx','lxx','egon',4,3.1]
# res=names.pop(1) #拿走一个值,是按照索引去删除,有返回值
# print(names)
# print(res)
# names=['alex','wxx','lxx','egon',4,3.1]
# print(names.pop())
# print(names.pop())
names=['alex','wxx','lxx','lxx','egon',4,3.1]
# print(names.count('lxx'))
# print(names.index('lxx'))
# names.clear()
# print(names)
# x=names.copy()
# print(x)
# names.extend([1,2,3])
# print(names)
# names.reverse()
# print(names)
# names=[1,10,-3,11]
# names.sort(reverse=True)
# print(names)
#二:该类型总结
# 1 存一个值or存多个值
# 可以存多个值,值都可以是任意数据类型
#
# 2 有序or无序
# 有序
# 3 可变or不可变
# 可变
# l=['a','b']
# print(id(l))
# l[0]='A'
# print(id(l))