Python概述和基础知识

一、什么是python
1.python是一种计算机程序设计语言,具有简洁性、易读性以及可扩展性,相较于其他语言学习起来更加容易。目前python有四大主要应用,一是网络爬虫,二是Web开发,三是人工智能设计,四是自动化运维。
二、python当中的控制语句
Python的设计目标之一是让代码具备高度的可阅读性。它设计时尽量使用其它语言经常使用的标点符号和英文单字,让代码看起来整洁美观。它不像其他的静态语言如C、Pascal那样需要重复书写声明语句,也不像它们的语法那样经常有特殊情况和意外

if语句,    当条件成立时运行语句块。经常与else, elif(相当于else if) 配合使用。
for语句,   遍历列表、字符串、字典、集合等迭代器,依次处理迭代器中的每个元素。
while语句, 当条件为真时,循环运行语句块。
try语句,   与except,finally配合使用处理在程序运行中出现的异常情况。
class语句, 用于定义类型。
def语句,   用于定义函数和类型的方法。
pass语句,  表示此行为空,不运行任何操作。
assert语句, 用于程序调试阶段时测试运行条件是否满足。
with语句,    Python2.6以后定义的语法,在一个场景中运行语句块。比如,运行语句块前加密,然后在语句块运行退出后解密。
yield语句,   在迭代器函数内使用,用于返回一个元素。自从Python 2.5版本以后。这个语句变成一个运算符。
raise语句,   制造一个错误。
import语句,  导入一个模块或包。
from import语句,  从包导入模块或从模块导入某个对象。
import as语句,    将导入的对象赋值给一个变量。
in语句,           判断一个对象是否在一个字符串/列表/元组里。

优点:
简单:Python是一种代表简单主义思想的语言。阅读一个良好的Python程序就感觉像是在读英语一样。它使你能够专注于解决问题而不是去搞明白语言本身。
易学:Python极其容易上手,因为Python有极其简单的说明文档 。
免费:开源:Python是FLOSS(自由/开放源码软件)之一。使用者可以自由地发布这个软件的拷贝、阅读它的源代码、对它做改动、把它的一部分用于新的自由软件中。FLOSS是基于一个团体分享知识的概念。
高层语言:用Python语言编写程序的时候无需考虑诸如如何管理你的程序使用的内存一类的底层细节。
可移植性:由于它的开源本质,Python已经被移植在许多平台上(经过改动使它能够工作在不同平台上)。这些平台包括Linux、Windows、FreeBSD、Macintosh、Solaris、OS/2、Amiga、AROS、AS/400、BeOS、OS/390、z/OS、Palm OS、QNX、VMS、Psion、Acom RISC OS、VxWorks、PlayStation、Sharp Zaurus、Windows CE、PocketPC、Symbian以及Google基于linux开发的android平台。
缺点
运行速度慢、中文资料少

三、解释器(英语:Interpreter)
又译为直译器,是一种电脑程序,能够把高级编程语言一行一行直接转译运行。解释器不会一次把整个程序转译出来,只像一位"中间人",每次运行程序时都要先转成另一种语言再作运行,因此解释器的程序运行速度比较缓慢。它每转译一行程序叙述就立刻运行,然后再转译下一行,再运行,如此不停地进行下去。
四、Python的安装和运行
1.进入官网www.python.org
2.点击Download(程序)下的Source code(源码)进入后选择python最新版本下载
3.按照压缩包格式解压压缩包
4.k8s自带python3.6
企业7中源码编异安装python3.8演示


[root@rhle7_node1 Desktop]# ls  查看桌面的压缩包
  Python-3.8.2.tgz
[[D[root@rhle7_node1 Desktop]# tar zxf  Python-3.8.2.tgz -C /opt/  解压到指定目录
[root@rhle7_node1 Desktop]# cd /opt/
[root@rhle7_node1 opt]# ls    查看解压好的软件
Python-3.8.2 
[root@rhle7_node1 opt]# cd Python-3.8.2/  
[root@rhle7_node1 Python-3.8.2]# ls    进入目录查看文件
aclocal.m4          Doc         m4               Parser         README.rst
CODE_OF_CONDUCT.md  Grammar     Mac              PC             setup.py
config.guess        Include     Makefile.pre.in  PCbuild        Tools
config.sub          install-sh  Misc             Programs
{configure 直接在此目录下运行此文件安装}  Lib         Modules          pyconfig.h.in
configure.ac        LICENSE     Objects          Python
**安装步骤:**
(1)./configure --prefix=/usr/local/python3 --with-ssl	 检测安装python3需要的东西,--prefix安装时指定路径/usr/local/python3,检测过程中可能会遇到依赖关系等报错,--with-ssl支持加密
解决依懒性报错:
(2)yum install gcc -y	  安装gcc(c语言编译器)解决一些问题
(3)yum install zlib zlib-devel openssl-devel -y	安装这些安装包,解决剩下的一些依赖关系问题
(4)./configure --prefix=/usr/local/python3 --with-ssl	再次检测安装python3时需要的东西
(5)make && make install	make是用来编译的,它从Makefile中读取命令然后编译,make install是用来安装的,也是从Makefile中读取命令,然后安装到指定路径
(6)cd /usr/local/python3/bin	进入目录
(7)./python3	运行python3看是否能进入python3
(8) 进入目录
[root@rhle7_node1 Python-3.8.2]# cd /usr/localpython3/  
[root@rhle7_node1 python3]# ls
bin  include  lib  share
[root@rhle7_node1 python3]# cd bin/
[root@rhle7_node1 bin]# ls
2to3      easy_install-3.8  idle3.8  pip3.8  pydoc3.8  python3.8         python3-config
2to3-3.8  idle3             pip3     pydoc3  python3   python3.8-config
[root@rhle7_node1 bin]# ./python3  调用python3
Python 3.8.2 (default, Apr 15 2020, 07:54:51) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello')   打印hello
hello   成功

(9.1)设置环境变量直接调用python解释器
[root@rhle7_node1 ~]# ln -s /usr/local/python3/bin/  /usr/local/bin/  使用连接方式

(9.2)设置环境变量追加到此文件后,系统级别设置
 echo export PATH="/usr/local/python3/bin:$PATH" >> /etc/profile  
vim /etc/profile         查看确认。也可以直接编辑文件
source  /etc/profile/     立即生效
设定完成后可以直接调用python,直接输入python就可以使用(保证可靠运行python重新打开shell)

5.python2和pyton3的区别

####python2:
[root@rhle7_node1 ~]# vim test.py 
[root@rhle7_node1 ~]# cat test.py 
print'hello world'   不用加()
print'你好'
[root@rhle7_node1 ~]# vim test.py 
# _*_ coding:utf-8_*_    对于中文打印需要在.py文件的首行加入# _*_ coding:utf-8_*_ 该编码是世界通用的语言编码
print'hello world'         
print'你好'
[root@rhle7_node1 ~]# python2 test.py 
hello world
你好
####python3:
[root@rhel8_node1 ~]# cat test.py    需要括号括起来
print('hello world')
print('你好')
[root@rhel8_node1 ~]# python3 test.py 
hello world
你好
[root@rhel8_node1 ~]# vim test.py   去掉括号
print'hello world'  
print'你好'
[root@rhel8_node1 ~]# python3 test.py   不使用括号会报错
  File "test.py", line 1
    print'hello world'
                     ^
SyntaxError: invalid syntax

6.python中的注释
注释必须以#开头,很明确的代码不需注释说明,复杂代码需在开始注释说明功能,尾行也需说明注释功能。

#(注释) This is a comment  首行注释
print('hello word')      #This also a commen   行尾注释

"""   注释开始
They are all  多行注释
comment
"""   注释结束
print('hello hahaha')
[root@workstation day01]# python3 01_first.py   运行
hello word
hello hahaha

7.python中的输入输出

python3
其会把所有的输出变成字符串
[root@workstation ~]# python3 
Python 3.6.8 (default, Jan 11 2019, 02:17:16) 
[GCC 8.2.1 20180905 (Red Hat 8.2.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> num = input()
12
>>> num
'12'   字符串str型
>>> num = input()
westos
>>> num
'westos'  字符串str型
>>> type(num)   查看num的类型
<class 'str'>   字符串str
python2
在python2中input()输入时,需要在提供数据时输入正确的数据类型,不会将数据转化为字符串型
[root@rhle7_node1 ~]# python2
Python 2.7.5 (default, Sep 12 2018, 05:31:16) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> input('Mum:')
Mum:1
1
>>> input('Num:')   a是字符串,无法识别
Num:a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'a' is not defined     类型报错
>>> raw_input('Num:')   使用raw_input才可也正确输入a
Num:a
'a'
>>> age = raw_input('Input your age:')
Input your age:15
>>> age
'15'    数值也会被当做字符串输入
>>> type(age)   查看类型
<type 'str'>   字符串类型
>>> age > 20    显然是不对的,因为是字符串无法比较大小,比较大小数值必须为同类型
True
>>> int(age) > 20   int()转换成整形才可以比较
False

    

>>> mulu = '目录'
>>> mulu.center(40)  变量赋值居中
'                   目录                   '
>>> mulu.center(40,'*')
'*******************目录*******************'  添加符号居中显示
>>> mulu.center(40,'#')
'###################目录###################'
>>> print('To Beijing'.center(20,'!'))  打印输出居中
!!!!!To Beijing!!!!!

8.python中的变量
1.变量:使一个变量指向一个地址空间

[root@workstation ~]# python3
Python 3.6.8 (default, Jan 11 2019, 02:17:16) 
[GCC 8.2.1 20180905 (Red Hat 8.2.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a=2  使a=2
>>> a
2
>>> id(a)  查看a的指向空间
140688623318272
>>> b = a   使b=a
>>> b
2
>>> id(b)     查看b的指向空间
140688623318272
>>> id(2)     查看2的指向空间
140688623318272

2.整型int和浮点型float

>>> a = 5
>>> a
5
>>> type(a)
<class 'int'>     整型int
>>> b = float(a)    转换a的类型为浮点型float
>>> b
5.0       
>>> type(b)
<class 'float'>  b=5.0 为浮点型
>>> a
5   
>>> 
将a的数据类型int转换成float浮点型并赋值给b

3.字符串str和bool型

[root@workstation ~]# python3
Python 3.6.8 (default, Jan 11 2019, 02:17:16) 
[GCC 8.2.1 20180905 (Red Hat 8.2.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 7   a赋值
>>> a
7
>>> type(a)  查看类型
<class 'int'>  字符串str
 bool型:非0为真,"":意为0,其余都是真(True),0为假(False))
>>> c = 0   
>>> bool(c)
False  为假
>>> c = "0"
>>> bool(c)
True
>>> c = ""  为0
>>> bool(c)
False
>>> c = " "  空格也是值所有输出为真true
>>> bool(c)
True   为真
%s    :表示为str型数据的占位符(str:字符串类型)
%d    :为int型数据的占位符(int:整型)
%f    :为float型数据的占位符(float:浮点型,默认保留6位小数)
%%    :表示输出一个% 
%0.xf :%0表示百分比    .x表示n位小数     f浮点型
%.xd	: int型站位,此位置一共x位,d不够的话前面补0
%.2f%%	:2个%表示百分号要输出%必须有两个%%——百分数小数点后两位

演示:

[root@workstation ~]# python3
Python 3.6.8 (default, Jan 11 2019, 02:17:16) 
[GCC 8.2.1 20180905 (Red Hat 8.2.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> name = 刘强东  将刘强东赋值给name 没有单引号报错  
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '刘强东' is not defined
>>> name = '刘强东'  正确
>>> age = 18   将18赋值给age 
>>> name
'刘强东'
>>> age
18
>>> print('%s age is %d' %(name,age))    %s:为(str)字符串型站位符,%d:(int)整型站位符,%(name,age)为指定占位符的变量:(str型字符串)name今年已经(int整型)18岁了
刘强东 age is 18
>>> name = '奶茶'
>>> print('%s age is %d' %(name,age))
奶茶 age is 18
>>> print('%s age is %d' %(age,name))  注意%(  )括号内的变量名称必须和前面指定的类型顺序一致,不一致会报错
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: %d format: a number is required, not str
>>> money = 8888.778899
>>> print('%s salary is %f' %(name,money)) 
奶茶 salary is 8888.778899
>>> money = 1314
>>> print('%s salary is %f' %(name,money))   float型默认小数点后六位
奶茶 salary is 1314.000000
>>> print('%s salary is %.1f' %(name,money))   %.1f:表示一位小数点
奶茶 salary is 1314.0
>>> print('%s salary is %.3f' %(name,money))   %0.f表示三位小数点
奶茶 salary is 1314.000
>>> name = 'root'
>>> studentid = 1
>>> print('%s studentid is 521%d' %(name,studentid))   521%d:表示int整型站位为0个,521为自定义添加的数值
root studentid is 5211
>>> print('%s studentid is 521%.2d' %(name,studentid))  521%.2d表示int整型站位为2个,因student=1,所以只有一位空缺,0补齐,依次类推如下
root studentid is 52101
>>> print('%s studentid is 521%.4d' %(name,studentid))  521%.4d表示int整型站位为4个
root studentid is 5210001
>>> print('%s studentid is 521%.6d' %(name,studentid)) 521%.6d表示int整型站位为6个
root studentid is 521000001
>>> scale = 0.5
>>> print('The num is %.2f' %(scale * 100))   %2.f表示显示小数点后两位,%(scale * 100 )就是显示scale的值即0.5x100=50
The num is 50.00
>>> print('The num is %.2f%%' %(scale * 100))  加上两个%%就是显示百分比
The num is 50.00%
>>> print('The num is %.3f%%' %(scale * 100))   %3.f表示小数点后三位,以此类推
The num is 50.000%
>>> print('The num is %.4f%%' %(scale * 100))
The num is 50.0000%
>>> print('The num is %.0f%%' %(scale * 100))
The num is 50%
注:安装ipython(保证网速快速)
pip3  install  ipython
[root@workstation ~]# ipython
Python 3.6.8 (default, Jan 11 2019, 02:17:16) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]:                                                                           

9.python当中的运算符号

运算符号     描述				演示
=	    简单的赋值运算符 	c = a + b 将 a + b 的运算结果赋值为 c
+=	    加法赋值运算符	 	c += a 等效于 c = c + a
-=		减法赋值运算符		c -= a 等效于 c = c - a
*		乘法赋值运算符		c *= a 等效于 c = c * a
/		除法赋值运算符		c /= a 等效于 c = c / a
%=		取模赋值运算符		 c = c % a
**		幂赋值运算符	     c = c ** a
//		取整除赋值运算符		 c = c // a

例题

  • 输入学生姓名;
  • 依次输入学生的三门科目成绩;
  • 计算该学生的平均成绩, 并打印;
  • 平均成绩保留一位小数点;
  • 计算该学生语文成绩占总成绩的百分之多少?并打印。eg: 78%;
    思路:
    1.姓名成绩提示语变量赋值——>求和——>求平均——>求语文占比百分比——>打印输出显示的内容
自定义一个.py文件
姓名 = input('请输入姓名:')    给姓名赋值变量
语文 = float(input('请输入语文成绩:'))    因为python3默认所有输出为字符串(str)类型无法实施运算所以要转型成float(浮点型)或整型(int)才可以加减乘除
数学 = float(input('请输入数学成绩:'))
物理 = float(input('请输入物理成绩:'))
总成绩 = 语文 + 数学 + 物理   求和
平均成绩 = 总成绩 / 3   求平均
语文成绩占总成绩比重 = (语文 /  总成绩) * 100   求语文占总成绩的百分比
print('%s 平均成绩 是 %.1f' %(姓名,平均成绩) )      打印输出平均成绩
print('%s 语文成绩占总成绩比重是 %.f%%' %(姓名,语文成绩占总成绩比重))   打印输出语文成绩百分比
运行脚本如下所示
[root@workstation ~]# python3 greade.py   运行
请输入姓名:小黑
请输入语文成绩:80
请输入数学成绩:90
请输入物理成绩:70
小黑 平均成绩 是 80.0
小黑 语文成绩占总成绩比重是 33%
发布了46 篇原创文章 · 获赞 6 · 访问量 1340

猜你喜欢

转载自blog.csdn.net/qq_46089299/article/details/105521128