python 学习笔记 (变量和简单变量)(*1)
第一篇(写于2020/1/3/19:00寝室内)
所用教材:python编程从入门到实践
*变量和简单变量**p15
重点*:字符串**
字符串
-
1.字符串表达方式:
1.“this is a way” 双引号
2.‘another way’ 单引号 -
2.字符串的拼接:
python 用’+'做拼接
first='one'#或"one"
second='two'
print(first+' '+second)
3.删除空白(空白也有意义,如’python '与’python’就不一样)
作用就是删去不必要的空白,这章只学习去除两旁的空白。
exp=' python '
>>>exp.strip()
>'python'#这的.strip()就起到一个跳过的作用
>>>exp
>' python '
注意这里只是获取去掉空格后的值,如果输出exp,仍然是原值
另外,strip()还有其他衍生物(俩)
rstirp() | 就是rightstrip()去掉右边的空格 |
---|---|
lstrip() | 就是leftstrip()去掉左边的空格 |
数字
python有整数和浮点数
>>>2+3
>5
>>>2.0+3.0
>5.0#大概是这样的
常见错误数据类型混乱
如下例子,乍一看会以为是对的
>>> age=19
>>> message='i am'+age+'years old'
TypeError: can only concatenate str (not "int") to str
TypeError: can only concatenate str (not “int”) to str就指出数据类型不匹配
str与int不匹配
则使用str() 来转换类型
>>> age=19
>>> message='i am '+str(age)+' years old'
>>> print(message)
i am 19 years old
运行成功
最后 python之禅!
试试向python命令行中输入import this吧,介绍了Tim peters写的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!