Python-part4-基础

语法1:

if 条件:
code1
code2
code3
....

age=180
height=163
weight=75
sex='female'
is_beautiful=True

if age > 16 and age < 30 and height > 158 and weight < 100 and sex == 'female' and is_beautiful:
print('表白。。。')

print('=====>other code')

语法2:

if 条件:
code1
code2
code3
....
else:
code1
code2
code3
....
 age=180
height=163
weight=75
sex='female'
is_beautiful=True

if age > 16 and age < 30 and height > 158 and weight < 100 and sex == 'female' and is_beautiful:
    print('表白。。。')
else:
    print('阿姨好')

语法3:多分枝

强调:if的多分枝=但凡有一个条件成立,就不会再往下判断其他条件了
if 条件1:
    code1
    code2
    code3
    ....
elif 条件2:
    code1
    code2
    code3
    ....
elif 条件3:
    code1
    code2
    code3
    ....
........
     else:
    code1
    code2
   code3
    ....

练习

如果:成绩>=90,那么:优秀
如果成绩>=80且<90,那么:良好
如果成绩>=70且<80,那么:普通
其他情况:很差

 score = input('>>: ')
score=int(score)
if score >= 90:
    print('优秀')
elif score >= 80:
    print('良好')
elif score >= 70:
    print('普通')
else:
    print('很差')

语法4:if嵌套

age=18
height=163
weight=75
sex='female'
is_beautiful=True

is_successfull=True

if age > 16 and age < 30 and height > 158 and weight < 100 and sex == 'female' and is_beautiful:
    print('表白。。。')
    if is_successfull:
        print('在一起')
    else:
        print('我逗你玩呢')
else:
    print('阿姨好')


print('other code....')

如果:今天是Monday,那么:上班
如果:今天是Tuesday,那么:上班
如果:今天是Wednesday,那么:上班
如果:今天是Thursday,那么:上班
如果:今天是Friday,那么:上班
如果:今天是Saturday,那么:出去浪
如果:今天是Sunday,那么:出去浪

today=input('>>: ')
if today == 'Monday':
    print('上班')
elif today == 'Tuesday':
    print('上班')
    elif today == 'Wednesday':
    print('上班')
elif today == 'Thursday':
    print('上班')
elif today == 'Friday':
    print('上班')
elif today == 'Saturday':
    print('出去浪')
elif today == 'Sunday':
    print('出去浪')
else:
    print('''必须输入其中一种:
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    Sunday
    ''')
today=input('>>: ')
if today == 'Monday' or today == 'Tuesday' or today == 'Wednesday' or today == 'Thursday' or today ==           'Friday':
    print('上班')
elif today == 'Saturday' or today == 'Sunday':
    print('出去浪')
else:
    print('''必须输入其中一种:
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    Sunday
    ''')

1什么是循环

循环就是一个重复的过程

2为何要有循环

人可以重复的去做某一件事
程序中必须有一种机制能够控制计算机像人一样重复地去做某一件事

3如何用循环

语法

while条件:
code1
code2
code3
...


user_from_db='egon'
pwd_from_db='123'

whileTrue:
inp_user=input('please  input  your  username:')
inp_pwd=input('please  input  your  password:')
ifinp_user==user_from_dbandinp_pwd==pwd_from_db:
print('login  successfull')
else:
print('userorpassworderr')


while+break:break代表结束本层循环
user_from_db='egon'
pwd_from_db='123'

whileTrue:
inp_user=input('please  input  your  username:')
inp_pwd=input('please  input  your  password:')
ifinp_user==user_from_dbandinp_pwd==pwd_from_db:
print('login  successfull')
break
else:
print('user  or  password  err')

while+continue:continue代表结束本次循环(本次循环continue之后的代码不在运行),直接进入下一次循环
强调:continue一定不要作为循环体的最后一步代码

start=1
whilestart<8:#5<8
ifstart==4:
start+=1#start=5
continue
print(start)
start+=1


whileTrue:
print(1)
print(2)
print(3)
user_from_db='egon'
pwd_from_db='123'

whileTrue:
inp_user=input('please  input  your  username:')
inp_pwd=input('please  input  your  password:')
ifinp_user==user_from_dbandinp_pwd==pwd_from_db:
print('loginsuccessfull')
break
else:
print('user  or  password  err')
continue

while循环的嵌套

user_from_db='egon'
pwd_from_db='123'

whileTrue:
inp_user=input('please  input  your  username:')
inp_pwd=input('please  input  your  password:')
ifinp_user==user_from_dbandinp_pwd==pwd_from_db:
print('login  successfull')
whileTrue:
cmd=input('>>>:')#cmd='quit'
ifcmd=='quit':
break
print('%srun......'%cmd)
break
else:
print('user  o r  password  err')

........................................................................................................

user_from_db='egon'
pwd_from_db='123'

tag=True
whiletag:
inp_user=input('please  inpu  tyour  username:')
inp_pwd=input('please  input  you  rpassword:')
ifinp_user==user_from_dbandinp_pwd==pwd_from_db:
print('login  successfull')
whiletag:
cmd=input('>>>:')#cmd='quit'
ifcmd=='quit':
tag=False
break
print('%srun......'%cmd)
else:
print('userorpassworderr')

while+else
else的代码会在while循环没有break打断的情况下最后运行

n=1
while  n<5:
if  n==4:
break
print(n)
n+=1
else:
print('=====》')





user_from_db='egon'
pwd_from_db='123'

count=0
tag=True
whiletag:
ifcount==3:
print('输错次数过多')
break
inp_user=input('please  input  your  username:')
inp_pwd=input('please  input  your  password:')
ifinp_user==user_from_dbandinp_pwd==pwd_from_db:
print('login  successfull')
whiletag:
cmd=input('>>>:')#cmd='quit'
ifcmd=='quit':
tag=False
break
print('%srun......'%cmd)
else:
print('user  or  passworderr')
count+=1#count=3#输错3次

names=['egon','alex_dsb','lxx_sb','yxx_dsb']
i=0
whilei<len(names):#4<4
print(names[i])
i+=1

for循环:可以不依赖索引而取指

names=['egon','alex_dsb','lxx_sb','yxx_dsb']
foriteminnames:#item='lxx_sb'
print(item)

dic={'x':1,'y':2,'z':3}
forkindic:#k='x'
print(k,dic[k])

for vs while
for可以不依赖于索引取指,是一种通用的循环取指方式
for的循环次数是由被循环对象包含值的个数决定的,而while的循环次数是由条件决定的

names=['egon','alex_dsb','lxx_sb','yxx_dsb']
foriinrange(0,len(names)):
print(names[i])

for+break
for+continue

for+else
foriinrange(10):
#  if  i==4:
#  break
print(i)
else:
print('====>')

1、有序:但凡有索引的数据都是有序的

2、可变不可变

可变类型:在值变了的情况下,id不变,证明在改原值

不可变类型:在值变了的情况下,id也跟着变,证明不是在改原值

一:基本使用:int

1用途:记录年龄、等级、号码

2定义方式

age=18#age=int(18)
print(type(age))
int('abadf')#报错
int('10.1')#报错
int('101')#int只能将字符串中包含纯数字转成整型

进制转换(了解**)

其他进制转成十进制
二进制:0,1
10#1(21)+0(20)
十进制:0-9
371#3
(10
2)+7(101)+1(100)
八进制:0-7
371#3
(82)+7(81)+1(80)
十六进制:0-9A-F
371#3
(16
2)+7(161)+1(8*0)

十进制转成其他进制
print(bin(12))
print(oct(12))#14=>1(81)+4(8**0)
print(hex(16))

二:该类型总结
1存一个值
3不可变
x=10
print(id(x))
x=11
print(id(x))

一:基本使用:float

1用途:记录身高、体重、薪资
2定义方式
salary=3.1#salary=float(3.1)
res=float('3.3')
print(type(res))

3常用操作+内置的方法

二:该类型总结

1存一个值
3不可变
x=10.3
print(id(x))
x=10.4
print(id(x))

2018-7-14 ----船长

每个工作日七点之前准时更新。

留个爱心再走吧!!!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,440评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,814评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,427评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,710评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,625评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,014评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,511评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,162评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,311评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,262评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,278评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,989评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,583评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,664评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,904评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,274评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,856评论 2 339

推荐阅读更多精彩内容

  • 一、快捷键 ctr+b 执行ctr+/ 单行注释ctr+c ...
    o_8319阅读 5,777评论 2 16
  • importUIKit classViewController:UITabBarController{ enumD...
    明哥_Young阅读 3,765评论 1 10
  • 是这样吧 我忘记了你的样子 人潮涌动的街口,时光的洪流 我想起你 忘记了你的样子 我常恨这世界,它不爱说话 像这风...
    千秋_阅读 809评论 0 2
  • 路过 遇见 回首 渐 逝
    言壳阅读 250评论 0 0
  • 又到三月,又见梅花红蕾簇拥,在春风雨露中回想着一年前的三月间,漫步铜川新区植物园情景与心境,再把曾经的短文奉献给春...
    李月芳阅读 301评论 0 3