Python学习之路1-变量和简单数据类型

发布时间:2019-06-10 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Python学习之路1-变量和简单数据类型脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
《Python编程:从入门到实践》笔记。
本章主要介绍Python的基本数据类型以及对这些数据的简单操作。

1. 入门仪式

作为一个合格的程序员,必须精通各种语言的Hello, World!,以下是学习Python的第一段代码:

PRint("Hello, World!")

2. 变量

变量就是数据的别称,和数学上的变量类似。
例如上述代码用变量表示:

message = "Hello, World!"
print(message)

变量有一定的命名规则:

  • 变量名只能包含字母、数字和下划线,且不能以数字开头
  • 变量名不能包含空格,一般用下划线分隔变量中的单词,也可以用驼峰命名法,但Python提倡用下划线
  • Python中的关键字和自带函数不能用于变量名
  • 变量名应该简短明了
  • 慎用小写字母 l 和大写字母O,因为这两个字母容易被看成数字1和0

同时也请注意,python解释器不对代码进行拼写检查,应尽量避免命名错误,比如变量名中少写个字母之类的,否则会出现NameError

3. 字符串

字符串就是一系列被引号括起来的字符,在Python中,引号可以是单引号,也可以是双引号,还可以是三引号。单双引增加了Python字符串的灵活性,减少了转义字符的使用,比如字符串中有且只有单引号时,最外层可以用双引号,反之亦然。三引号主要用于字符串是多行的情况,同时它也常用于注释。例子如下:

"This is a string."
'This is also a string.'

'I told my friend, "Python is my favorITe language!"'
"The language 'Python' is named after Monty Python, not the snake."
"One of Python's strengths is its diverse and supportive commUnity."

"""
this is line 1;
this is line 2.
"""

'''
this is line 1;
this is line 2.
'''

注意,若字符串中出现了和最外层引号相同的引号时,会出现SyntaxError

字符串首字母大写
字符串中每个单词首字母大写:

# 代码:
name = "ada lovelace"
print(name.title())

# 结果:
Ada Lovelace

字符串全部大写和小写

# 代码:
name = "Ada Lovelace"
print(name.upPEr())
print(name.lower())

# 结果:
ADA LOVELACE
ada lovelace

字符串拼接
Python中用+号进行字符串拼接:

# 代码:
First_name = "ada"
last_name = "lovelace"
full_name = first_name + " " + last_name
print(full_name)
print("Hello, " + full_name.title() + "!")

# 结果:
ada lovelace
Hello, Ada Lovelace!

删除字符串首尾的空白:删左空白,删有空白,删两端空白

>>> temp = ' python '
>>> temp
' python '
>>> temp.lstrip()
'python '
>>> temp.rstrip()
' python'
>>> temp.strip()
'python'

4. 数字

特别注意Python中的默认除法:两个整数相除,如果除不尽,会有小数,而不是只保留整数(如C/C++, Java, python2

#整数
>>> 2 + 3
5
>>> 3 - 2
1
>>> 2 * 3
6
>>> 3 / 2
1.5 #不是1
>>> 3 ** 2 #乘方运算
9
>>> (2 + 3) * 4
20

#浮点数(结果包含的小数位数可能不确定)
>>> 0.1 + 0.1
0.2
>>> 0.2 + 0.1  #和计算机内部数字的表示方法有关
0.30000000000000004
>>> 3 * 0.1
0.30000000000000004

数字与字符串的拼接
使用str()函数,否则会报TypeError

# 代码:
age = 18
message = "Happy " + age + "rd Birthday!"

# 结果:
Traceback (most recent call last):
  File "E:/Code/Python/Study/day1/chapter1.py", line 38, in <;module>
    message = "Happy " + age + "rd Birthday!"
TypeError: must be str, not int

正确语法:

# 代码:
age = 18
message = "Happy " + str(age) + "rd Birthday!"
print(message)

# 结果:
Happy 18rd Birthday!

5. 注释

Python中的注释为#号,从#开始到本行结束的中间这部分均为注释内容,不会被执行。

# 代码:
#Say hello to everyone
print("Hello Python people!")   # test

# 结果:
Hello Python people!

6. Python之禅

python命令行中运行如下代码,即可查看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!
迎大家关注我的微信公众号"代码港" &amp; 个人网站 www.vpointer.net ~

Python学习之路1-变量和简单数据类型

脚本宝典总结

以上是脚本宝典为你收集整理的Python学习之路1-变量和简单数据类型全部内容,希望文章能够帮你解决Python学习之路1-变量和简单数据类型所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。