Cpu(中央处理器):相当于人的大脑,用于计算。
内存:储存数据,4\8\16\32G,成本高,断电即消失。
硬盘:1T,固态(速度快)\机械硬盘,储存数据,长久保持的重要文件,小电影等等。
操作系统:
应用程序:
宏观上:Python2与Python3的区别:
1. Python2源码不标准,混乱,重复码太多,
2. Python3统一标准,取出重复码。
编译型:一次性将所有程序编译成二进制文件。
优点:运行速度快。
缺点:开发效率低,不能跨平台。
使用语言:c,c++等。
解释型:当程序执行时,一行一行的解释。
优点:开发效率高,可以跨平台。
缺点:运行速度慢。
使用语言:python,php等。
运行第一个py文件:
Python3x:Python 文件路径 回车。
Python2x:Python2 文件路径 回车。
宏观区别:Python2x源码,重复率高,不规范,默认编码方式是ASCII码。
解决方式:在文件的首行输入:#-*- encoding:utf-8 -*-解决python2中文报错。
Python3默认编码方式是utf-8。
1 #-*- encoding:utf-8 -*- 2 #print(‘我爱中国‘)
定义:就是将一些运算的中间结果暂存到内存中,以便后续代码调用。
1 x=1+2+3+4 2 print(x) 3 print(x*5) 4 y=x*5 5 print(y+100-45+2) 6 7 print(‘泰哥泰哥,我是小弟‘) 8 print(‘泰哥泰哥,我是三弟小妹‘)
规范: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=1234 2 12 #10r=3 3 w*e=4 4 #print=3 5 6 t-t = 2 7 3t_t = 23 8 *r = 4 9 _ = ‘fdsa‘ 10 ___ = 4 11 %- = ‘fdsa‘ 12 2w = 5 13 qwe-r = ‘wer‘ 14 kfdsdlafhsdakfhdsakdfjkhsdakf = ‘太白‘ 15 print(名字) 16 AgeOfOldboy = 56 17 NumberOfStudents = 80 18 19 #下划线 20 age_of_oldboy = 56 21 number_of_students = 80
定义:一直不变的量,全部为大写字母。(BIR_OF_CHINA = 1949)
1 ge1 = 12 2 age2 = age1 3 age3 = age2 4 age2 = 100 5 age3 = 5 6 print(age1,age2,age3) #12, 100 ,100 7 #12,12,12, 8 #12,100,12 9 #100,100,100,
方便自己,方便他人理解代码。
单行注释:#
多行注释:’’’被注释内容’’’/””” 被注释内容”””。
1.等待输入。
2.将你输入的内容赋值给前面变量。
3.Input出来的数据类型全部是str。
1 name = input(‘请输入你的名字:‘) 2 age = input(‘请输入你的年龄:‘) 3 print(‘我的名字是‘+name,‘我的年龄‘+age+‘岁‘)
数字:int 12,3,45
+ - * / **(幂次方) %(取余)
Ps:type( )
字符串转化成数字:int(str) 条件:str必须是数字组成的。
数字转化成字符串:str(int)
1 print(100,type(100)) 2 print(‘100‘,type(‘100‘))
字符串:stràpython当中凡是使用引号引起的都是字符串。
可相加:字符串的拼接。
可相乘(与数字):str * int
1 print(1) 2 print("jsdfdsfsadl;fjdsal;j") 3 print("I‘m a teacher") 4 5 a = ‘泰哥‘ 6 b = ‘小二‘ 7 c = a + b 8 print(c) 9 print(‘泰哥‘ + ‘小二‘ +‘货‘) 10 print(‘坚强‘*8) 11 12 msg = """ 13 今天我想写首小诗, 14 歌颂我的同桌, 15 你看他那乌黑的短发, 16 好像一只炸毛鸡。 17 """ 18 #print(msg)
Bool:布尔值。 True/False
1 print(True,type(True)) 2 print(‘True‘,type(‘True‘))
1.If 条件:
结果
2.if 条件:
结果
else:
结果
3.if 条件:
结果
elif 条件:
结果
.....
else:
结果
1 #第一种: 2 if 4 > 5 : 3 print(‘我请你喝酒‘) 4 print(‘喝什么酒‘) 5 6 #第二种: 7 if 4 > 5: 8 print(‘我请你喝酒‘) 9 else: 10 print(‘喝什么酒‘) 11 12 #多选: 13 num = input(‘请输入您猜的数字:‘) 14 if num == ‘1‘: 15 print(‘一起抽烟‘) 16 elif num == ‘2‘: 17 print(‘一起喝酒‘) 18 elif num == ‘3‘: 19 print(‘新开了一家,走看看‘) 20 else: 21 print(‘你猜错了.....‘) 22 23 score = int(input("输入分数:")) 24 if score > 100: 25 print("我擦,最高分才100...") 26 elif score >= 90: 27 print("A") 28 elif score >= 60: 29 print("C") 30 elif score >= 80: 31 print("B") 32 elif score >= 40: 33 print("D") 34 else: 35 print("太笨了...E") 36 37 name = input(‘请输入名字:‘) 38 age = input(‘请输入年龄:‘) 39 if name == ‘小二‘: 40 if age == ‘18‘: 41 print(666) 42 else: 43 print(333) 44 else: 45 print(‘错了....‘)
While 条件:
循环体
1 print(‘111‘) 2 while True: 3 print(‘我们不一样‘) 4 print(‘在人间‘) 5 print(‘痒‘) 6 print(‘222‘) 7 8 #从1--100 9 count = 1 10 flag = True 11 #标志位 12 while flag: 13 print(count) 14 count = count + 1 15 if count > 100 : 16 flag = False 17 18 count = 1 19 while count <= 100: 20 print(count) 21 count = count + 1 22 23 count = 1 24 sum = 0 25 while count <= 100: 26 sum = sum + count 27 count = count + 1 28 print(sum)
无限循环:
终止循环:1.改变条件,使其不成立。
2.break。
1 #break 2 print(‘11‘) 3 while True: 4 print(‘222‘) 5 print(333) 6 break 7 print(444) 8 print(‘abc‘) 9 10 count = 1 11 while True: 12 print(count) 13 count = count + 1 14 if count > 100:break 15 16 print(111) 17 count = 1 18 while count < 20 : 19 print(count) 20 continue 21 count = count + 1 22 23 count = 0 24 while count <= 100 : 25 count += 1 26 if count > 5 and count < 95: 27 continue 28 print("loop ", count) 29 print("-----out of while loop ------")
while else:
当while循环被break打断,就不会执行else的结果。
1 eg:1 2 count = 0 3 while count <= 5; 4 count += 1 5 if count == 3:break 6 print("Loop",count) 7 else: 8 print("循环正常执行完啦") 9 print("----out of while loop----") 10 11 eg:2 12 count = 0 13 while count <= 5; 14 count += 1 15 if count == 3:pass 16 print("Loop",count) 17 else: 18 print("循环正常执行完啦") 19 print("----out of while loop----")
continue:结束本次循环,继续下次循环。
%占位符,s:字符串,d:digit 数字.
%%只是单纯的显示%.
1 #格式化输出: 2 # % s d 3 eg;1 4 name = input(‘请输入姓名‘) 5 age = input(‘请输入年龄‘) 6 height = input(‘请输入身高‘) 7 msg = "我叫%s,今年%s,身高%s" %(name,age,height) 8 print(msg) 9 10 eg:2 11 name = input(‘请输入姓名‘) 12 age = input(‘请输入年龄‘) 13 job = input(‘请输入工作‘) 14 hobbie = input(‘请输入爱好‘) 15 16 msg = ‘‘‘----------info of %s----------- 17 Name : %s 18 Age : %d 19 Job : %s 20 Hobbie : %s 21 -------------end--------------‘‘‘ %(name,name,int (age),job,hobbie) 22 print(msg)
1 name = input(‘请输入名字‘) 2 age = input(‘请输入年龄‘) 3 height = input(‘请输入身高‘) 4 msg = "我叫%s,今年%s,身高%s,学习进度3%%" %(name,age,height) 5 print(msg) 6 (想要早格式化输出中单纯的%,就再加一个%)
原文:https://www.cnblogs.com/AmazingYGY/p/10526377.html