变量和简单数据类型

Posted leisurelyrd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了变量和简单数据类型相关的知识,希望对你有一定的参考价值。

变量:

  1.只能包含字母、数字和下划线。字母和下划线开头;

  2.不能包含空格;

  3.不要将关键字和函数名做变量名;

  4.简短有描述性;

  5.慎用小写字母l(L)和大写O

错误代码:
message = "Hello,Python World!" print(mesage)
Traceback (most recent call last):
  File "hello_world.py", line 2, in <module>  #文件hello_world.py的第2行存在错误
    print(mesage)
NameError: name mesage is not defined  #名称错误:mesage未定义
name = "ada lovelace"
print(name.title()) #title()是方法,大写单词首字母,方法是python可对数据执行的操作,每个方法后都有括号
Ada Lovelace

name = "ada lovelace"
print(name.upper()) #将字符串改为大写
ADA LOVELACE

name = "Ada LOVElace"
print(name.lower()) #将字符串改为小写
ada lovelace
rstrip() #临时删除右边空白,需改变,要保存
lstrip() #临时删除左边空白,需改变,要保存
strip() #临时删除两边空白,需改变,要保存
 

 

>>> 0.1+0.1
0.2
>>> 0.2+0.2
0.4
>>> 2*0.1
0.2
>>> 2*.02
0.04
>>> 0.2+0.1
0.30000000000000004  #需要注意,结果包含的小数可能是不确定的;
>>> 3*0.1
0.30000000000000004  #需要注意,结果包含的小数可能是不确定的;

注释符号 #

python之禅

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases arent special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless youre Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, its a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- lets do more of those!

 














以上是关于变量和简单数据类型的主要内容,如果未能解决你的问题,请参考以下文章

如何使用Android片段管理器传递变量[重复]

使用 ViewModel 和 LiveData 递增变量的简单片段示例 - 变量始终为空

在一些片段之间填充对象变量的最佳方法

变量和简单数据类型

python变量和简单的数据类型

变量和简单数据类型--注释