Python——变量和简单数据类型

一.字符串——用引号括起的都是字符串

1.单引号与双引号

  1. 显示双(单)引号
    双(单)引号位于单(双)引号中
  2. 撇号可以位于双引号之间;撇号不可以位于单引号之间
#√
"Python's strengthhs"             

'Python's strengthhs'  

2.大小写字母

  1. title()
    以首字母大写的方式显示每个单词
  2. upper()
    全部大写
  3. lower()
    全部小写
  4. eg:
name="ada lovelace"
print(name.title())

3.连接两个字符串:+

first_name="ada"
last_name="lovelace"
full_name=first_name+" "+last_name
message="Hello,"+full_name.title()+"!"
print(message)

4.\t制表符 \n换行符

print("\tPython")

5.删除空格

  1. 右侧rstrip() 左侧lstrip() 两端strip()
  2. 暂时删除
favorite_language='python '
print(favorite_language)
print(favorite_language.rstrip())
  1. 永久删除
favorite_language='python '
print(favorite_language)
 #将结果存回原来的变量中
favorite_language=favorite_language.rstrip() 
print(favorite_language)

6.单引号、双引号定义的字符串必须在一行;三引号可以分布在多行

二.数字

1.整数(integer)

  1. 四则运算
  2. 3**2表示3的2次方
  3. 空格不影响计算表达式
  4. 二进制0b开头、八进制0o开头、十六进制0x开头

2.浮点数

  1. 正常计算
  2. 存储不精确,比如1.1+2.2=3.30000…03,怎么解决?
 from decimal import Decimal
 print(Decimal(1.1)+Decimal(2.2))

3.str()将非字符串值→字符串

eg:

age=23
message=“happy”+str(age)+"rd birthday!"

三.布尔类型(boolean)

四.注释:#

不可以写中文

五.几条原则

The Zen of Python, by Tim Peters

  1. Beautiful is better than ugly.漂亮
  2. Explicit is better than implicit.优雅
  3. Simple is better than complex.简单
  4. Complex is better than complicated.
  5. Flat is better than nested.
  6. Sparse is better than dense.
  7. Readability counts.易理解
  8. Special cases aren’t special enough to break the rules.
  9. Although practicality beats purity.
  10. Errors should never pass silently.
  11. Unless explicitly silenced.
  12. In the face of ambiguity, refuse the temptation to guess.
  13. There should be one-- and preferably only one --obvious way to do it.
  14. Although that way may not be obvious at first unless you’re Dutch.
  15. Now is better than never.
  16. Although never is often better than right now.
  17. If the implementation is hard to explain, it’s a bad idea.
  18. If the implementation is easy to explain, it may be a good idea.
  19. Namespaces are one honking great idea – let’s do more of those!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

说说其实

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值