标签:风格 else global font 字母 def 基础 core cep
#变量:就是将一些运算的中间结果暂存到内存中,以便后续代码调用
1 print(1+2+3+4)
2 print((1+2+3+4)*5)
3 print((1+2+3+4)*5+100-45+8)
4
5 x=1+2+3+4
6 print(x)
7 print(x*5)
8 y=x*5
9 print(y+100-45+8)
1.只能以字母、数字、下划线组成,且不能以数字开头
2.不能是python中的关键字:[‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘exec‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘not‘, ‘or‘, ‘pass‘, ‘print‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘with‘, ‘yield‘]
3.变量具有可描述性
4.不能是中文(俗成,使用不会报错)
1 s=123
2 #10r=12
3 #w*e=4
4 #print=3
5 _ =34
6 #__ _ =3
7 ___ =4
8
9 a1 = 12
10 a2 = a1
11 a3 = a2
12 a2 = 100
13 print(a1,a2,a3) #12,100,12 按行执行
#常量:一直不变的量
约定俗成,不可更改,全部是大写字母
#注释:方便别人方便自己理解代码
单行注释 #
多行注释 ‘‘‘代码内容‘‘‘ """代码内容"""
#用户交互input
1.等待输入
2.将你输入的内容赋值给前面的变量
3.input出来的类型全部是str
1 name = input(‘请输入你的名字:‘)
2 age = input(‘请输入你的年龄:‘)
3 print(name,age,type(age))
4 #print(‘我的名字是‘+name ,‘我的年龄是‘+age+ ‘岁‘)
5 print(‘我的名字‘+name,‘我的年龄‘+age+‘岁‘)
6 #结果
7 请输入你的名字:wu
8 请输入你的年龄:18
9 wu 18 <class ‘str‘>
10 我的名字wu 我的年龄18岁
type() 查看类型
1 print(100)
2 print(‘100‘)
3 print(100,type(100))
4 print(‘100‘,type(‘100‘))
5 #结果
6 100
7 100
8 100 <class ‘int‘>
9 100 <class ‘str‘>
#数字:int
+ - * / ** 幂 % 取余数 // 取商
#布尔值:bool True False 不能加引号 用于判断
#字符串: str python中凡是用引号引起来的全是字符串
要换行用 """ """
可相加:字符串的拼接
可相乘:str*int
字符串转化成数字:int(str) 条件:str必须是数字组成的
数字转化成字符串:str(int)
1 print(‘I am xx‘)
2 print("I‘m xx")
3 a = ‘但是‘
4 b = ‘反对‘
5 c = a + b
6 print(c)
7 print(c + ‘风格反对和‘)
8 print(True,type(True))
9 print(‘True‘,type(‘True‘))
10 #结果
11 I am xx
12 I‘m xx
13 但是反对
14 但是反对风格反对和
15 True <class ‘bool‘>
16 True <class ‘str‘>
if 条件:
结果
1 #第一种
2 print(555)
3 if 5 > 4 :
4 print(666)
5 print(777)
6 print(777)
1 #第二种
2 if 4 == 3 :
3 print(666)
4 else:
5 print(‘和什么‘)
1 #多选
2 score = int(input(‘输入分数:‘))
3
4 if score > 100:
5 print(‘我擦,最高分才100。。。。‘)
6 elif score >= 90:
7 print(‘A‘)
8 elif score >= 80:
9 print(‘B‘)
10 elif score >= 70:
11 print(‘C‘)
12 elif score >= 60:
13 print(‘D‘)
14 else:
15 print(‘呵呵,没及格。。。‘)
1 #嵌套
2 name = input(‘请输入名字:‘)
3 age = input(‘请输入年龄:‘)
4 if name == ‘小二‘:
5 if age == ‘18‘:
6 print(666)
7 else:
8 print(777)
9 else:
10 print(‘错了。。‘)
while 条件:
循环体
#中止循环:改变条件,使其不成立
break
#continue:结束本次循环,继续下一次循环
1 #第一种
2 print(‘111‘)
3 while True:
4 print(‘我们不一样‘)
5 print(‘在人间‘)
6 print(‘痒‘)
7 print(‘222‘)
8 #无限循环
1 #从1到100
2 count = 1
3 flag = True
4 #标志位
5 while flag:
6 print(count)
7 count = count + 1
8 if count > 100:
9 flag = False
10 #‘‘‘第二种
11 count = 1
12 while count <= 100:
13 print(count)
14 count = count + 1
1 #1+2+。。。+100
2 count = 1
3 sum = 0
4 while count <= 100:
5 sum = sum + count
6 count = count + 1
7 print(sum)
1 #break
2 print(‘11‘)
3 while True:
4 print(‘222‘)
5 print(333)
6 break
7 print(444)
8 print(‘aaa‘)
9 #
10 count = 1
11 while True:
12 print(count)
13 count = count + 1
14 if count > 100:
15 break
1 #无限循环1
2 count = 1
3 while count < 20:
4 print(count)
5 continue
6 count = count + 1
7 break
1 #输出1 2 3 4 5 6 8 9 10
2 count = 1
3 while count < 11:
4 if count != 7:
5 print(count)
6 count = count + 1
1 #输出1 2 3 4 5 6 8 9 10
2 count = 1
3 while count < 11:
4 if count != 7:
5 print(count)
6 else:
7 print(‘‘)
8 count = count + 1
1 #continue
2 count = 0
3 while count <= 100:
4 count += 1 #count = count + 1
5 if count > 5 and count < 95:
6 continue
7 print(‘loop‘,count)
8
9 print(‘----out of while loop----‘)
10 #结果
11 loop 1
12 loop 2
13 loop 3
14 loop 4
15 loop 5
16 loop 95
17 loop 96
18 loop 97
19 loop 98
20 loop 99
21 loop 100
22 loop 101
23 ----out of while loop----
1 #偶数 2~100
2 count = 1
3 while count < 101:
4 s = count % 2
5 # count = count + 1
6 if s == 0:
7 print(count)
8 count = count + 1
9 #奇数 1~101
10 count = 1
11 while count < 101:
12 s = count % 2
13 count = count + 1
14 if s == 0:
15 print(count)
16 # count = count + 1
1 # 1-2+3....-100
2 #法1
3 count = 1
4 num = 0
5 while count < 101:
6 if count % 2 == 1:
7 num += count
8 else:
9 num -= count
10 count += 1
11 print(num)
12 #法2
13 i = 1
14 j = -1
15 sum = 0
16 while i < 101:
17 j = -j
18 sum = sum + i * j
19 i = i + 1
20 print(sum)
1 # 1-2+3....-100 去掉88
2 i = 1
3 j = 1
4 sum = 0
5 while i < 101:
6 if i != 88:
7 sum = sum + i * j
8 j = -j
9 i = i + 1
10 print(sum)
11 # 1-2+3....+100 去掉88
12 i = 1
13 j = 1
14 sum = 0
15 while i < 101:
16 if i != 88:
17 sum = sum + i * j
18 j = -j
19 i = i + 1
20 print(sum)
#while else
当while循环被break打断,就不会执行else的结果
1 #用户登录
2 user = ‘asd‘
3 passwd = ‘123‘
4 zh = input(‘请输入账号:‘)
5 mm = input(‘请输入密码:‘)
6 if zh == user and mm == passwd:
7 print(‘登陆成功‘)
8 else:
9 print(‘账号或密码错误,请重新输入‘)
1 # 用户登录,三次机会,显示还有几次机会
2 user = ‘asd‘
3 passwd = ‘123‘
4 i = 3
5 while i > 0:
6 zh = input(‘请输入账号:‘)
7 mm = input(‘请输入密码:‘)
8 if zh == user and mm == passwd:
9 print(‘登陆成功‘)
10 break
11 else:
12 print(‘账号或密码错误,你还有%d次机会‘ % (i-1))
13 i -= 1
1 #用户登录,三次机会,显示还有几次机会,当剩余0时询问是否再试
2 user = ‘asd‘
3 passwd = ‘123‘
4 i = 3
5 while i > 0:
6 zh = input(‘请输入账号:‘)
7 mm = input(‘请输入密码:‘)
8 if zh == user and mm == passwd:
9 print(‘登陆成功‘)
10 break
11 else:
12 print(‘账号或密码错误,你还有%d次机会‘ % (i-1))
13 i -= 1
14 if i == 0:
15 answer = input(‘还想试试?Y‘)
16 if answer == ‘Y‘:
17 i = 3
18 else:print(‘走好。。。‘)
1 #用户登录,三次机会,显示还有几次机会,当剩余0时询问是否再试,账号、密码分开
2 user = ‘asd‘
3 passwd = ‘123‘
4 i = 3
5 while i > 0:
6 zh = input(‘请输入账号:‘)
7 if zh == user:
8 i = 3
9 while i > 0:
10 mm = input(‘请输入密码:‘)
11 if mm == passwd:
12 print(‘登陆成功‘)
13 print(‘‘‘账号:%s,密码:%s‘‘‘ % (zh,mm))
14 break
15 else:
16 print(‘密码错误,你还有%d次机会‘ % (i - 1))
17 i -= 1
18 if i == 0:
19 answer = input(‘还想试试?Y‘)
20 if answer == ‘Y‘:
21 i = 3
22 else:print(‘真是你账号?‘)
23 break
24 else:
25 print(‘账号错误,你还有%d次机会‘ % (i-1))
26 i -= 1
27 if i == 0:
28 answer = input(‘还想试试?Y‘)
29 if answer == ‘Y‘:
30 i = 3
31 else:print(‘走好。。。‘)
%占位符,s字符串,d digit 数字
%% 只是单纯的显示%
1 #格式
2 msg =‘‘‘--------info of Alex Li---------
3 Name : ALex Li
4 Age : 19
5 Job : Teacher
6 Hobbie: girl
7 ------------end------------‘‘‘
8 print(msg)
9 #一种
10 name = input(‘请输入名字:‘)
11 age = input(‘请输入年龄:‘)
12 job = input(‘请输入工作:‘)
13 hobbie = input(‘请输入爱好:‘)
14
15 msg =‘‘‘--------info of %s---------
16 Name : %s
17 Age : %d
18 Job : %s
19 Hobbie: %s
20 ------------end------------‘‘‘ %(name,name,int(age),job,hobbie)
21 print(msg)
22 #二种
23 name = input(‘请输入名字:‘)
24 age = int(input(‘请输入年龄:‘))
25 job = input(‘请输入工作:‘)
26 hobbie = input(‘请输入爱好:‘)
27
28 msg =‘‘‘--------info of %s---------
29 Name : %s
30 Age : %d
31 Job : %s
32 Hobbie: %s
33 ------------end------------‘‘‘ %(name,name,age,job,hobbie)
34 print(msg)
标签:风格 else global font 字母 def 基础 core cep
原文地址:https://www.cnblogs.com/wuyinglianjianjue/p/10046519.html