Python基础知识2——变量和数据类型

本章内容来自书籍,记录下来仅方便复习,如有侵权,请联系作者删除。

一、 变量

命名规则
(1)变量名包含字母、数字、下划线,不能以数字开头
(2)变量名不能包含空格
(3)变量名不要包含python关键字和函数名
(4)变量名应该简短,有描述性
(5)谨慎使用 “l” 和 “O”

如果被调用的变量名错误,Python解释器会提供一个traceback,提示变量名错误

age = 23
print(aag)
运行结果:
NameError: name 'aag' is not defined

二、 字符串

在Python中,用引号括起的都是字符串。单引号,双引号都可以
1. 修改字符串的大小写

name = 'ADA loverlace'
print(name.title())    #Ada Loverlace
print(name.upper())    #ADA LOVERLACE
print(name.lower())    #ada loverlace

title(): 以首字母为大小的方式显示每个单词
upper(): 将字符串改为全部大写
lower(): 将字符串改为全部小写

2. 合并(拼接)字符串
Python使用加号(+)来合并字符串
3. 使用制表符或换行符来添加空白
这里的空白泛指任何非打印字符,如空格、制表符、换行符
制表符:\t
换行符:\n

print("Languages:\n\tPython\n\tC\n\tJavaScript")

运行结果:
Languages:
        Python
        C
        JavaScript

4. 删除空白
Python能够找出字符串开头和末尾多余的空白,并删除
rstrip(): 删除字符串末尾的空白
lstrip(): 删除字符串开头的空白
strip(): 删除字符串两端的空白
这三种方法对字符串的处理,是暂时性的,没有改变变量的值。

5. 使用字符串是避免语法错误
如单引号中包含撇号

favorite_language = 'Python, It's my favorite language'
print(favorite_language)
运行结果:
SyntaxError: invalid syntax

三、 数字

数字经常被用于记录游戏得分、表示可视化数据、存储Web应用信息等

1. 整数,运算符号
(1)在Python中,可对整数执行加(+)减(-)乘(*)除(/)运算
(2)两个乘号(**)表示乘方运算
(3)支持使用括号()修改运算次序

2. 浮点数
Python将带小数点的数字都称为浮点数

3. 使用函数str()避免类型错误
Python将非字符串值表示为字符串,暂时的。

age = 23
print("Happy "+age+"rd Birthday!")
运行结果:
TypeError: can only concatenate str (not "int") to str

使用str()函数,可以正确运行:

age = 23
print("Happy "+str(age)+"rd Birthday!")
运行结果:
Happy 23rd Birthday!

四、注释

在Python语言中,注释用#标识,井号后面的内容都会被Python解释器忽略。
编写注释的主要目的是参数代码要做什么,以及如何做的

# 注释内容
"""文本字符串内容"""
'''文本字符串内容'''

三个引号引住的内容是文档字符串,并非注释。

五、Python之禅

只需要在解释器中执行命令:import this

>>> 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. 变量名错误: NameError: name 'aag' is not defined
  2. 类型错误:TypeError: can only concatenate str (not "int") to str
  3. 语法错误:SyntaxError: invalid syntax
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值