15天快速入门Python:Day1-初识Python和基础知识
该系列为Python的快速入门教程,共15章(天)。以代码实战为主的方式来介绍, 希望能带你快速入门!
01 - 初识Python
Python 简介:
Python是由荷兰人吉多·范罗苏姆(Guido von Rossum)发明的一种编程语言,是目前世界上最受欢迎和拥有最多用户群体的编程语言。
Python 历史:
1989年圣诞节:Guido开始写Python语言的编译器。
1991年2月:第一个Python解释器诞生,它是用C语言实现的,可以调用C语言的库函数。
1994年1月:Python 1.0正式发布。
2000年10月:Python 2.0发布,Python的整个开发过程更加透明,生态圈开始慢慢形成。
2008年12月:Python 3.0发布,引入了诸多现代编程语言的新特性,但并不完全兼容之前的Python代码。
2020年1月:在Python 2和Python 3共存了11年之后,官方停止了对Python 2的更新和维护,希望用户尽快过渡到Python 3。
前面已经讲了很多Python的前世今生,那Python的优势和缺点是什么呢?
Python 优缺点:
Python的优点很多,简单为大家列出几点。
1. 简单明确,跟其他很多语言相比,Python更容易上手。
2. 能用更少的代码做更多的事情,提升开发效率。
3. 开放源代码,拥有强大的社区和生态圈。
4. 能够做的事情非常多,有极强的适应性。
5. 能够在Windows、macOS、Linux等各种系统上运行。
Python最主要的缺点是执行效率低,但是当我们更看重产品的开发效率而不是执行效率的时候,Python就是很好的选择。
Python 应用领域:
目前Python在Web服务器应用开发、云基础设施开发、网络数据采集(爬虫)、数据分析、量化交易、机器学习、深度学习、自动化测试、自动化运维等领域都有用武之地。
02 - Python基础知识
Python 编码:
默认情况下,Python 3 源码文件以 UTF-8 编码,所有字符串都是 unicode 字符串。当然你也可以为源码文件指定不同的编码:
1 | # -*- coding: cp-1252 -*- |
正常我们平时写代码的话都是指定UTF-8编码,如图:
1 | # -*- coding: utf-8 -*- |
Python 标识符:
什么是标识符?
标识符(IDentifier)是指用来标识某个实体的一个符号。
标识符是程序中某一元素(变量、关键字、函数、类、模块、对象)的名字。
除了关键字的名字是固定的,其它元素都可以根据标识符的命名规则进行命名。
标识符命名规则:
1. 第一个字符必须是字母表中字母或下划线 _ 。
2. 标识符的其他的部分由字母、数字和下划线组成。
3. 标识符对大小写敏感。
在 Python 3 中,可以用中文作为变量名,非 ASCII 标识符也是允许的了。
这里需要注意,标识符遵循PEP8原则,但也可以这样:蛇形/小驼峰/大驼峰命名法。
蛇形:在两个词中间用_下划线分割开来就为蛇形
小驼峰:在两个词一个词开头为大写,第二个词为小写就是小驼峰
大驼峰:两个词开头都是大写字母就为大驼峰
如下:
1 2 3 4 5 6 7 8 9 | # 知名见意 name = '小明' age = 12 # 蛇形 family_name = '小明' # 小驼峰 familyName = '小明' # 大驼峰 FamilyName = '小明 |
一般我习惯使用蛇形和小驼峰。一些标识符命名最好是能一眼读懂,做到见名知意。
上面介绍的是变量,那什么是变量?
量:是衡量/记录现实世界中的某种特征/状态
变:指的是记录的状态是可以发生变化的
那什么是常量?
常量指的程序运行过程中不会改变的量。
常量和变量命名规范:
常量通常是大写,变量是小写。
不以关键字作为标识符:
什么为关键词?就比如python自带的模块,关键词,函数,类,有了的名字大家尽量不要再去用。如何自己查看内置的关键词命令行输入:
1 2 3 | import keyword keyword.kwlist [ 'False' , 'None' , 'True' , 'and' , 'as' , 'assert' , 'break' , 'class' , 'continue' , 'def' , 'del' , 'elif' , 'else' , 'except' , 'finally' , 'for' , 'from' , 'global' , 'if' , 'import' , 'in' , 'is' , 'lambda' , 'nonlocal' , 'not' , 'or' , 'pass' , 'raise' , 'return' , 'try' , 'while' , 'with' , 'yield' ] |
这样python内置的关键字(也叫保留字)是不能作为标识符来用,以免冲突。
Python 注释:
认识注释:
注释就是就是对代码的解释说明,注释的内容不会被当作代码运行
注释使用方式:
代码注释分单行和多行注释
1、单行注释用#号
2、多行注释可以用三对双引号''''''
格式:
1 2 3 4 5 6 | # 单行注释 '' ' 多行注释可以用三对双引号 多行注释可以用三对双引号 多行注释可以用三对双引号 '' ' |
Python 行与缩进:
大家都知道Java是使用花括号{}来表示代码块的,而python是使用缩进。这也是python最具特色的地方。缩进的空格数是可变的,但是同一个代码块的语句必须包含相同的缩进空格数。实例如下:
1 2 3 4 | if name == '小明' : print( "true" ) else : print( "false" ) |
如果写成这样:
1 2 3 4 5 | if name == '小明' : print( "true" ) else : print( "false" ) print( "false" ) |
就会报错:
1 2 3 4 5 | print( "false" ) ^ IndentationError: unindent does not match any outer indentation level Process finished with exit code 1 |
Python 代码换行显示:
Python 通常是一行写完一条语句,但如果语句很长,我们可以使用反斜杠 \ 来实现多行语句,例如:
1 2 3 4 | name = '小明' +\ '小明' +\ '小明' print(name) |
也可以用在 [], {}, 或 () 中的多行语句,不需要使用反斜杠 \,例如:
1 2 3 4 5 6 | name = '小明' , '小明' , '小明' name2 = [ '小明' , '小明' , '小明' ] name3 = { '小明' , '小明' , '小明' } print(name) print(name2) print(name3) |
运行结果:
1 2 3 | ( '小明' , '小明' , '小明' ) [ '小明' , '小明' , '小明' ] { '小明' } |
这里需要注意的是,用逗号连接,相当于元组,而字典会自动去重。
Python 打印函数print:
在py2中print是关键字,而在py3是函数。可以看一下定义:
1 2 3 4 5 6 7 8 9 10 11 12 | def print(self, *args, sep= ' ' , end= '\n' , file=None): # known special case of print "" " print(value, ..., sep= ' ' , end= '\n' , file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default . Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream. "" " pass |
打印的话,print 默认输出是换行的,如果要实现不换行需要在变量末尾加上 end="":
1 2 3 4 5 6 | name3 = { '小明' , '小明' , '小明' } # 换行输出 print(name3) # 不换行输出 print(name3, end= '' ) print(name3, end= '' ) |
输出结果:
1 2 3 | { '小明' } { '小明' }{ '小明' } Process finished with exit code 0 |
Python 导入模块:
在平时开发中,很多时间需要使用其他模块或自定义模块,而引入方式是:import 与 from...import
可以看一下示例:
1 2 3 4 5 6 7 8 | import sys print( '================Python import mode==========================' ) print ( '命令行参数为:' ) for i in sys.argv: print (i) print ( '\n python 路径为' ,sys.path) from time import sleep sleep(0.5) |
Python 模拟用户输入:
执行下面的程序在按回车键后就会等待用户输入:
1 | input( "\n\n按下 enter 键后退出。" ) |
Python 空行:
函数之间或类的方法之间用空行分隔,表示一段新的代码的开始。类和函数入口之间也用一行空行分隔,以突出函数入口的开始。
空行与代码缩进不同,空行并不是 Python 语法的一部分。书写时不插入空行,Python 解释器运行也不会出错。但是空行的作用在于分隔两段不同功能或含义的代码,便于日后代码的维护或重构。
记住:空行也是程序代码的一部分。
Python 命令行参数:
很多程序可以执行一些操作来查看一些基本信息,Python可以使用-h参数查看各参数帮助信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ... Options and arguments (and corresponding environment variables): -b : issue warnings about str(bytes_instance), str(bytearray_instance) and comparing bytes/bytearray with str. (-bb: issue errors) -B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x -c cmd : program passed in as string (terminates option list) -d : debug output from parser; also PYTHONDEBUG=x -E : ignore PYTHON* environment variables (such as PYTHONPATH) -h : print this help message and exit (also --help) -i : inspect interactively after running script; forces a prompt even if stdin does not appear to be a terminal; also PYTHONINSPECT=x -I : isolate Python from the user's environment (implies -E and -s) -m mod : run library module as a script (terminates option list) -O : remove assert and __debug__-dependent statements; add .opt-1 before .pyc extension; also PYTHONOPTIMIZE=x -OO : do -O changes and also discard docstrings; add .opt-2 before .pyc extension -q : don't print version and copyright messages on interactive startup -s : don't add user site directory to sys.path; also PYTHONNOUSERSITE -S : don 't imply ' import site' on initialization -u : force the binary I/O layers of stdout and stderr to be unbuffered; stdin is always buffered; text I/O layer will be line-buffered; also PYTHONUNBUFFERED=x -v : verbose (trace import statements); also PYTHONVERBOSE=x can be supplied multiple times to increase verbosity -V : print the Python version number and exit (also --version) when given twice, print more information about the build -W arg : warning control; arg is action:message:category:module:lineno also PYTHONWARNINGS=arg -x : skip first line of source, allowing use of non-Unix forms of #!cmd -X opt : set implementation-specific option file : program read from script file - : program read from stdin ( default ; interactive mode if a tty) arg ...: arguments passed to program in sys.argv[1:] |
知识点回顾:
以上主要介绍Python的简介和基础知识,了解Python基本知识和掌握Python使用规范,以便后续学习。
工欲善其事,必先利其器!
作者:全栈测试开发日记
出处:https://www.cnblogs.com/liudinglong/
csdn:https://blog.csdn.net/liudinglong1989/
微信公众号:全栈测试开发日记
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架