参考书目:《python编程:从入门到实践》 埃里克·玛瑟斯 人民邮电出版社
参考视频:python零基础入门教程 李立超 尚硅谷
Add Pythonxx toPATH
;python
检查安装是否成功。在终端页面输入python
查看系统中python版本号。
若环境中同时安装了python2和python3,
python
表示查询python2的版本信息,python3
表示查询python3的版本信息。
Windows中使用微软的VS code进行配置,具体参考VS code配置python环境。
linux中可以使用Vim等经典工具。
无论在哪种环境,在py文件所在的目录,通过python
或者python3
命令即可运行该脚本。
_
代替;l
和大写字母O
的使用。字符串被引号包围,可以单引号,也可以双引号,不过需要注意前后对应。
变量.title():将每个单词首字母大写
变量.upper():将每个字母大写
变量.lower():将每个字母小写
name = 'this is a name' print(name.title()) print(name.upper()) print(name.lower()) #结果 This Is A Name THIS IS A NAME this is a name
可以使用+
将多个字符串变量进行合并拼接。
first_name = 'xu' last_name = 'peace' full_name = first_name+' '+last_name print('hello, '+full_name.title()) #结果 hello, Xu Peace
\t:制表符,强制缩进4字符
\n:换行符,强制换行
print("code languages includs: \n\tpython\n\tC\n\tJava\n\tC#") #结果 code languages includs: python C Java C#
变量.rstrip():删除右侧空白
变量.lstrip():删除左侧空白
变量.strip():删除左右侧空白
A = '123 ' B = ' 234' C = ' 3 4 5 ' print(A.rstrip()) print(A) print(B.lstrip()) print(B) print(C.strip()) print(C) # 结果 123 123 234 234 3 4 5 3 4 5
空格不影响计算结果,主要是为了方便阅读
乘方计算通过**
表示,如2^4
在程序中可以使用2 ** 4
表示。
可能会出现浮点数计算不准确的问题——是由于计算机内部进制转换导致。
在将数字当作字符使用时,需要加上字符的类函数str()
age = str(23) print("happy " + age + "rd birthday") # 结果 happy 23rd birthday
在一个py文件中,输入import this
会出现龟叔提倡的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!
要求: