2.1变量
2.1.1变量的命名和使用:(可以包含字母、数字、下划线)
1. message = "xxxxx" (√允许的)
2. _message = "xxxxx" (√)
3. 1_message = "xxxxx" (x不允许的) 不允许数字开头
4. 2message = "xxxxx" (x)
5. 0 = "xxxxx" (x)
6.如果变量名过长我们不允许有空格而是采取下划线的形式进行分割:longlong message(x)
longlong_message(√)
7.不要将python关键字和函数名用作变量(官方文档可看关键字函数名):print = "xxxxx" (x)
2.2 字符串
2.2.1 表示方法
1.双引号表示手法:"this is a string"
2.单引号表示手法:'this is a string'
3.双引号加单引号表示手法:"The language 'Python'is named after Monty Python,not the snake."
2.2.2 使用“方法”来修改字符串的大小写
方法如下:
1. title()——首字母大写 2. upper()——全大写 3. lower()—— 全小写
xxx = "Hello python!!!!..."
print(xxx.lower())
message = "Hello python crash counrse world!!!"
print(message.upper())
xzw = "Hello world!!!"
print(xzw.title())
对应输出如下结果:
hello python!!!!...
HELLO PYTHON CRASH COUNRSE WORLD!!!
Hello World!!!
2.2.3 在字符串中使用变量:(关于f字符串)
first_name = "xrz"
second_name = "and"
last_name = "xxy"
full_name = f"{first_name} {second_name} {last_name}"
print(full_name)
对应输出结果:
xrz and xxy
值得注意一点f字符串在3.6及之后版本才有,之前则需要采用format()
2.2.4 使用制表符或换行符来添加空白
print("python")
print("\tpython")
对应输出:
python
python
print("python")
print("\npython")
对应输出:
python
python
print("Languages:\n\tPython\n\tJava\n\tC++")
对应输出: 一行中我们同时既可以用换行符也可以用制表符
Languages:
Python
Java
C++
2.2.5 使用“方法”删除空白
方法如下:
1. rstrip()——删除右边的空白 2. lstrip()——删除左边的空白 3. strip()—— 删除左右的空白
del_r_languages = "python "
print(del_r_languages.rstrip())
del_l_languages = " python"
print(del_l_languages.lstrip())
del_s_languages = " python "
print(del_s_languages.strip())
对应输出:python(左右都无空格)
2.2.6 使用字符串时避免语法错误(灵活运用单引号双引号的表示手法)
'one of python's languages' (x)
"one of python's languages" (√)
2.3 数
3 + 2 //加
3 - 2 //减
2 * 3 //乘
3 / 2 //除
3 ** 2 //乘方运算,等于9
3 ** 3 //等于27
2 + 3 * 2 //满足数学运算规则,等于14
(2 + 3) * 2 //使用圆括号可以让python按照你指定的次序执行运算,等于12
关于整数和浮点数:无论是那种运算,只要有操作数是浮点数,python默认得到的总是浮点数
2.3.1 同时给多个变量赋值
x,y,z = 0,1,2
print(x)
print(y)
print(z)
2.3.2 常量
常量类似于变量,但其值在程序的整个生命周期内保持不变。Python没有内置的常量类型,但Python程序员会使用全大写来指出应将某个变量视为常量,其值应始终不变:
MAX_CONNECTIONS = 5000
在代码中,要指出应将特定的变量视为常量,可将其字母全部大写。
2.4注释
在python中注释用#
# xxx = "Hello python!!!!..."
# print(xxx.lower())
python中的指导原则(我个人理解为是一种小彩蛋^^!)
import this #直接输入运行
#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!
第二章完结散花...............明天要继续加油的呀!!
—————如有不足或错误望指出