Python初识-day1

时间:2024-01-17 12:59:26

1.第一个python程序(在pycharm中运行的,感觉还不错)

Python初识-day1

  注释:

    当行注释:#被注释内容

    多行注释:'''  被注释内容 '''

2.变量

(1) 自己理解的定义(仅供参考): 为了存储数据以便反复调用的自定义合法标识符(存于内存中)。

比如:name = “congcong”   #name即为一个变量。

 (2)    变量定义的规则:

    <1>变量名只能是字母、数字、或下划线的任意组合。

    <2>变量名的第一个字符不能是数字。

    <3>关键字不能声明为变量名。

      包括['and’,'as','assert'(宣称),'break','class','continue','def','if','elif','else','for','except','finaly',

'exec'(执行程序),'form','global','import','in','is','lambda'(匿名程序),'not','or','pass','print','raise'(增加),

'return','try','while','with','yield'(收率)]。

3.常量的表示

  字母必须大写,且不能修改,否则会出错。

  例如:  PHR = 3

4.字符编码

   (1) python解释器在加载 .py文件中的代码时,会对内容进行编码(默认是ASCII码)

  (2) ASCII码(美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言,

其最多只能用8位来表示(即一个字节,2**8=256),所以,ASCII码最多只能表示255个字符。如下图(素材来源于网上):

Python初识-day1

  (3)字符编码发展史:

    ASCII码 ——1980年,GB2312,支持7千多汉字——1995年,GBK1.0,支持2万多汉字——2000年,GB18030,支持2万7千多汉字

—— Unicode,统一为2bytes(字节),由于有争议,接着另开发了utf-8——utf-8,支持中英文,英文1个字母为1byte,而中文一个汉字为3bytes。

5.用户交互程序(如无特殊说明,以下python代码均是在pycharm中运行过的,可用的) 

 #congcong for huangYuTing
import getpass #导入getpass模块,将密码设为不可见
_usename = 'congcong' #预存用户名
_password = 'huangyuting' #预存用户名密码
usename = input("usename:") #用户输入用户名
password = input("password:") #用户输入密码
if _usename == usename and _password == password: #表判断,两个条件均成立时继续
print('Welcome user {name} login...'.format(name = usename)) #.format()获取用户名
else:
print('Invalid usename or password')

6.if...else判断结构(含while) 

 #congcong for huangYuTing
age_of_erha = 21 count = 0
while count < 3:
guess_age = int(input('Guess age:'))
if guess_age == age_of_erha:
print('You are right!')
break
elif guess_age > age_of_erha:
print('Think smaller.')
else :
print('Think bigger...')
count += 1
if count == 3:
print('Do you want to continue....')
Is_continue = input('continue:')
if Is_continue != 'n':
count = 0
#else:

7.while and for 循环

 #congcong for huangYuTing
'''
for i in range(0,15,2):#从 0 开始,15结束,每次相差 2
print('number',i)
''' '''
age_of_erha = 21
for i in range(4):
guess_age = int(input('Guess age:'))
if guess_age == age_of_erha:
print('You are right!')
break
elif guess_age > age_of_erha:
print('Think smaller.')
else :
print('Think bigger...')
else:
print('You have tried too many times....')
''' # continue的使用
'''
for i in range(0,20,2):
if(i > 7):
print('congcong',i)
else:
continue #跳出本次循环,继续下次循环,而break是结束循环
print('Hello')
'''
#多重for循环
for i in range(0,8,1):#小于8,不包含8
print('hello...........',i)
for n in range(0,6,1):
print('world',n)
if n > 3:
break #结束最近的循环
#多重循环打印结果如下:
 hello........... 0
world 0
world 1
world 2
world 3
world 4
hello........... 1
world 0
world 1
world 2
world 3
world 4
hello........... 2
world 0
world 1
world 2
world 3
world 4
hello........... 3
world 0
world 1
world 2
world 3
world 4
hello........... 4
world 0
world 1
world 2
world 3
world 4
hello........... 5
world 0
world 1
world 2
world 3
world 4
hello........... 6
world 0
world 1
world 2
world 3
world 4
hello........... 7
world 0
world 1
world 2
world 3
world 4