Python基础二(变量&input)

变量

变量 是 为了存储 程序运算过程中的一些中间 结果,为了方便日后调用

官方文档说明如下:

  Variables变量 are used to store保存、储存 information信息 to be referenced被日后调用 and manipulated操作或更改 in a computer program程序. They also并且还 provide提供 a way方式 of labeling标记 data数据 with a descriptive描述性 name, so our programs can be understood理解 more clearly更清晰 by the reader阅读者 and ourselves我们自己. It is helpful to think of variables as containers容器 that hold保持、保存 information(如果我们把变量看作成是一个保存信息的容器是更容易理解的). Their sole主要 purpose目的 is to label标记 and store存储 data in memory内存里. This data数据 can then然后 be used使用它 throughout整个 your program.

变量命名规则:

1. 字母数字下划线组成
2. 不能以数字开头,不能含有特殊字符和空格
3. 不能以保留字命名
4. 不能以中文命名
5. 定义的变量名应该有意义
6. 驼峰式命、 下划线分割单词
7. 变量名区分大小写

什么是常量:

例如:不变的量 π = 3.141592653.... 

在py里面所有的变量都是可变的 ,所以用全部大写的变量名来代表次变量为常量

字符编码:

支持中文的第一张表就叫 GB2312
1980 gb2312 
1995 gbk1.0 
2000 gb18030 
big5 台湾
unicode 万国码 支持所有国家和地区的编码
2**16 = 65535 = 存一个字符 统一占用2个字节

UTF-8 = unicode 的扩展集,可变长的字符编码集

Python2.x == Assic 默认编码
  #!-*- coding:utf-8 -*-
  #coding:utf-8
python3.x == unicode默认编码
unicode 是向下兼容gb2312 , gbk

注释
单行注释 用#
多行注释用三个单引号或三个双引号 '''被注释的内容'''

 

用户输入(input)

#用户交互小练习
death_age = 80 name = input("your name:") age = input("your age:") #input 接受的所有数据都是字符串,即便你输入的是数字,但依然会被当成字符串来处理 print( type(age) ) #int integer =整数 把字符串转成int,用int(被转的数据) #str string =字符串 把数据转成字符串用str(被转的数据) print("Your name:",name) #print("You can still live for ", death_age - int(age)," years ....") print("You can still live for " + str(death_age - int(age)) +" years ....")
#猜成绩
score = int(input("score:"))

if score > 90:
    print("A")
elif score > 80:
    print("B")
elif score > 70:
    print("C")
elif score > 50:
    print("D")
else:
    print("")
#猜年龄
age_of_princal = 56 

guess_age = int( input(">>:") )
'''
if guess_age == age_of_princal then

    print("yes")
else 
    print("no ")
'''

if guess_age == age_of_princal:
    print("Yes,you got it..")
elif guess_age > age_of_princal:
    print("shoud try samller..")
else:
    print("try bigger ...")

 

posted @ 2018-06-20 23:13  乐观的小孩  阅读(957)  评论(0编辑  收藏  举报