变量和简单数据类型

变量:

  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 aren't 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 you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
原文地址:https://www.cnblogs.com/leisurelyRD/p/10251617.html