首页 > 编程语言 > 详细

Python学习基础:变量

时间:2018-09-23 17:06:09      阅读:172      评论:0      收藏:0      [点我收藏+]

变量(Variables):

是为了存储程序在运算过程中的一些中间结果,为了方便日后调用储存在计算的内存中
官方介绍:
Variables are used to storeinformation to be referenced and manipulated in a computer program.
They alsoprovidea way of labeling data with a descriptive name, so our programs can be understoodmore 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.
 

常量 :

不变的量 

eg.  π = 3.141592653...(在 Python 中 π 用 PI 表示)

注意:在Python中所有的变量都是可变的 ,所以用全部大写的变量名来代表次变量为常量

 

变量的命名规则:
 1.  要具有描述性
 2.  变量名只能_,数字,字母组成,不可以是空格或特殊字符(#?<.,¥$*!~)
 3.  不能以中文为变量名
 4.  不能以数字开头
 5.  保留字符是不能被使用
 6.  当需要用两个或以上的单词来进行命名时:
   Eg.  使用下划线区分单词 :student_number =
           驼峰式命名(首字母不能大写):studentNumber =
 7.  不可以用关键字作为变量名
      #关键字:指令或有特殊含义的词汇 (eg.print )

 

格式化方法:format()

format(变量1,变量2)
变量1 对应{0}  变量2 对应{1}
注意:Python从0开始计数,意味着索引中的第一位是0,第二位是1
age = 20
name = ALEX
print({1} : {0} .format(age,name))

 

如果上述内容有错漏之处或者有疑问的地方,欢迎大家一起指出,一起学习!!!

Python学习基础:变量

原文:https://www.cnblogs.com/ArticleYeung/p/9692792.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!