Python学习笔记DAY1
1、关于解释器:
(1)官网下载的python安装包里包含的解释器为CPython;
(2)IPython是基于CPython之上的一个交互式解释器,也就是说,IPython只是在交互方式上有所增强,但是执行Python代码的功能和CPython是完全一样的。
CPython用>>>作为提示符,而IPython用In [序号]:作为提示符。
(3)PyPy,运行速度最快的解释器;
2、在Linux系统上运行的时候,一般在coding的最开始会注明"#!/usr/bin/env python",含义是声明解释器在系统中的位置,可以直接执行代码;
3、变量命名:官方建议使用下划线"_"来对变量名进行标注,以解释变量名的含义,常量一般用大写字母来注明。
如:name_of_president = "Bing"
PI = 3.1415 #圆周率的常量命名
4、关于字符编码:

在Python 2.x的版本中,如包含中文字符,需在coding的最开始注明"-*- coding:utf-8 -*-",代表用utf-8进行处理;
5、注释:
两种方式:#和''' '''
6、%s:字符串;%d:整型;%f:浮点型;
7、input()默认输入的是string,type()用来显示数据类型,需要进行强制转换(如需要);
几种打印方式:
8、判断:
1 _username = "bing" 2 _password = "bing" 3 4 username = input("username: ") 5 password = input("password: ") 6 7 if username == _username and password == _password: 8 print("Welcome {name} login...".format(name = username)) 9 else: 10 print("Invalid username or password!!!")
9、循环
(1)while
1 #!/usr/bin/env python 2 #Author:Bing 3 4 age_of_oldboy = 56 5 6 count = 0 7 8 while count < 3: 9 guess_input = int(input("guess the age of oldboy: ")) 10 if guess_input == age_of_oldboy: 11 print("yes, you got it !") 12 break 13 elif guess_input > age_of_oldboy: 14 print("think smaller !") 15 else: 16 print("think older !") 17 count += 1 18 if count == 3: 19 continue_confirm = input("Do you want to try again?") 20 if continue_confirm != "n": 21 count = 0
(2)for
1 #!/usr/bin/env python 2 #Author:Bing 3 4 age_of_oldboy = 56 5 6 count = 0 7 8 for i in range(3): 9 guess_input = int(input("guess the age of oldboy: ")) 10 if guess_input == age_of_oldboy: 11 print("yes, you got it !") 12 break 13 elif guess_input > age_of_oldboy: 14 print("think smaller !") 15 else: 16 print("think older !")
10、break和continue:
break:跳出本循环;
continue:跳出本次循环,进入下次循环;
浙公网安备 33010602011771号