Python Leaning-变量和简单数据类型

变量

message = 'Hello Python world!'  # message是自定义的一个变量名,将值'Hello Python world!'存储到该变量中
print(message)

输出:Hello Python world!

message = 'Hello Python Crash Course world!'  # 允许对变量的值进行修改
print(message)

输出:Hello Python Crash Course world!

变量的命名与使用

  • 变量名只能包含字母、数字、下划线。但是不能以数字开头。
  • 变量名不能包含空格。
  • 不要将Python关键字和函数名用作变量名,例如Print.
  • 变量名应既简短又具有描述性。
  • 慎用小写字母l和大写字母O,它因它们可能被人错看成数字1和0.

    就目前而言,应使用小写的Python变量名。在变量名中使用大写字母虽然不会导致错误,但避免使用大写字母是个不错的主意。

了解错误

当运行有问题的代码时,解释器会报错,它会提供一个Traceback。这是一条记录,指出了解释器尝试运行代码时,在什么地方陷入了困境。

例如,将变量message名拼写错

message = 'Hello Python Crash Course world!'  
print(mesage) 

报错内容如下

Traceback (most recent call last):
  File "C:/Users/Administrator/Desktop/a.py", line 3, in <module>
    print(mesage)  
NameError: name 'mesage' is not defined

说明

File “C:/Users/Administrator/Desktop/a.py”, line 3, in 显示在哪个文件的第几行存在问题

print(mesage) 将有问题的语句列出来

NameError: name ‘mesage’ is not defined 告诉编程人员这个错误是因为变量没有被定义而引起的

字符串

字符串就是一系列字符;在Python中,用引号括起来的都是字符串,其中的引号可以是单引号,也可以是双引号

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

利用这种便利性,可以在字符串中包含引用号和撇号

print('I told my friend, "Python is my favorite language!"')

输出:I told my friend, “Python is my favorite language!”

print("The language 'Python' is named after Monty Python, not the snake.")

输出:The language ‘Python’ is named after Monty Python, not the snake.

注:在使用时注意单引号与双引号之间的嵌套关系,避免语法错误

使用方法修改字符串大小

name = "ada lovelace"
print(name.title())  # title()方法表示将每个单词的首字母都改为大写

输出:Ada Lovelace

方法是Python可对数据执行的操作;通过’.’来调用方法;方法后面都跟着一对括号

print(name.upper())  # upper() 将字符串全部改为大写ADA LOVELACE
print(name.lower())  # lower() 将字符串全部改为小写ada lovelace

合并(拼接)字符串

first_name = 'ada'
last_name = 'lovelace'
full_name = first_name+" "+last_name
print(full_name)   

输出:ada lovelace

Python使用加号(+)来合并字符串

使用制表符或换行符来添加空白

制表符用\t表示

print("Hello\tPython")

输出:Hello Python

换行符用\n表示

print("Hello\nPython")

输出:
Hello
Python

删除空白

favorite_language = 'python '  # 在末尾添加了一个空格
print(favorite_language.rstrip())  # rstrip()方法可将字符串开头与结尾的空格去掉

注:rstrip()只是将去掉空格后的字符串返回,并没有直接在原来的变量中操作,所以favorite_language中的值并没有发生改变;可以采用下面方式改变变量去掉空格后的值

favorite_language.lstrip()  # lstrip()删除开头的空格
favorite_language.strip()  # strip()删除两端空格  

数字

整数

举例:对整数进行加、减、乘、除运算

2+3
2-3
2*3
2/3

注:2**3表示乘方运算

6**(1+3)  # 用括号可以改变运算的顺序

浮点数

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

0.1 + 0.1
0.2 - 0.1
2 * 0.1
3 / 0.2

注:结果包含的小数位数可能是不确定的,这是计算机表示数字的方式,可忽略

>>> 0.2 + 0.1
0.30000000000000004

 使用str()将数字转换成字符串

age = 23
message = "Happy "+str(age)+"rd Birthday!"
print(message)

输出:Happy 23rd Birthday!

注释

注释让你能够使用自然语言在程序中添加说明

在Python中,注释用#号标识。#号后面的内容都会被Python解释器忽略

# 向大家问好
print("Hello Python people!")

第一行将被忽略,第二行才会被执行

#表示单行的注释,用成对的三个引号可表示多行的注释,引号可以是单引号也可以是双引号

'''
大家好
这是多行注释
'''
print("Hello Python!")

Python 之禅

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!

以下是翻译

优美胜于丑陋(Python 以编写优美的代码为目标)
明了胜于晦涩(优美的代码应当是明了的,命名规范,风格相似)
简洁胜于复杂(优美的代码应当是简洁的,不要有复杂的内部实现)
复杂胜于凌乱(如果复杂不可避免,那代码间也不能有难懂的关系,要保持接口简洁)
扁平胜于嵌套(优美的代码应当是扁平的,不能有太多的嵌套)
间隔胜于紧凑(优美的代码有适当的间隔,不要奢望一行代码解决问题)
可读性很重要(优美的代码是可读的)
即便假借特例的实用性之名,也不可违背这些规则(这些规则至高无上)
不要包容所有错误,除非你确定需要这样做(精准地捕获异常,不写 except:pass 风格的代码)
当存在多种可能,不要尝试去猜测
而是尽量找一种,最好是唯一一种明显的解决方案(如果不确定,就用穷举法)
虽然这并不容易,因为你不是 Python 之父(这里的 Dutch 是指 Guido )
做也许好过不做,但不假思索就动手还不如不做(动手之前要细思量)
如果你无法向人描述你的方案,那肯定不是一个好方案;反之亦然(方案测评标准)
命名空间是一种绝妙的理念,我们应当多加利用(倡导与号召)

目录
上一章 Python Learning-Python环境安装
下一章 Python Learning-列表

猜你喜欢

转载自blog.csdn.net/weixin_38486884/article/details/81839871