python学习笔记(字符串与数字 )(*1)

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!
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值