Python之编译安装及简单编写规则

Python之编译安装及交互式简单使用

  • 什么是python

Python是一种计算机程序设计语言。是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越来越多被用于独立的、大型项目的开发。

优点
简单、易学、速度快、免费、开源、可移植性、解释性、丰富的库、规范的代码
高层语言:用Python语言编写程序的时候无需考虑诸如如何管理你的程序使用的内存一类的底层细节。
面向对象:Python既支持面向过程的编程也支持面向对象的编程。
可扩展性:如果需要一段关键代码运行得更快或者希望某些算法不公开,可以部分程序用C或C++编写,然后在Python程序中使用它们。
可嵌入性:可以把Python嵌入C/C++程序,从而向程序用户提供脚本功能。

缺点:
单行语句和命令行输出问题:很多时候不能将程序连写成一行,如import sys;for i in sys.path:print i。而perl和awk就无此限制,可以较为方便的在shell下完成简单程序,不需要如Python一样,必须将程序写入一个.py文件。
独特的语法:这也许不应该被称为局限,但是它用缩进来区分语句关系的方式还是给很多初学者带来了困惑。即便是很有经验的Python程序员,也可能陷入陷阱当中。
运行速度慢:这里是指与C和C++相比。

  • python3的编译安装

目前python最新的版本为python3.8,还在处于测试阶段,而当前使用的系统所装的python版本为python2.7
python2和python3目前都在企业中被使用
因此,为了方便比较二者的一些差别,在此对python3进行编译安装

编译安装准备:python3.7的源码包,可自行在python官网www.python.org下载

编译安装过程:

tar zxf Python-3.6.4.tgz -C /opt ##解压源码包
yum install gcc zlib zlib-devel openssl-devel -y #解决依赖性
cd Python-3.6.4/ ##进入解压目录编译和安装
./configure --prefix=/usr/local/python3 --with-ssl  ##--prefix :安装路径 	--with-ssl:添加ssl加密
make && make install ##安装

测试:cd /usr/local/python3/bin ./python3
在这里插入图片描述
添加python3的命令到环境变量中
临时添加:

export PATH="/usr/local/python3/bin:$PATH"

永久添加:

echo export PATH="/usr/local/python3/bin:$PATH" >> ~/.bashrc
vim ~/.bashrc 
source ~/.bashrc 
  • python交互式测试

打印:(类似shell的"echo")

>>> print(1)
1
>>> print('hello')
hello
>>> print('hello world')
hello world
  • python的简单程序编写
vim test.py
# _*_coding:utf-8_*_

# 1.没有分号(编码规范PEP8)
# 2.严格按照缩进的语言
print('hello python')
print('你好 python')
print('hello linux')

python test.sh
hello python
你好 python
hello linux
  • python中的变量
>>> qq = 12345
>>> a = qq
>>> print(a)
12345
>>> print(qq)
12345
  • python中的数据类型

整型:int

>>> a = 1
>>> print(a)	
1
>>> type(a)  ##查看类型
<class 'int'>

浮点型:float

>>> b=1.2
>>> print(b)    
1.2
>>> type(b)
<class 'float'>

字符串:str

>>> c = 'westos'
>>> print(c)
westos
>>> type(c)
<class 'str'>

booll型(只有两个值:True False 非0即真)

>>> a = 1
>>> bool(a)
True
>>> bool(0)
False
>>> bool('')
False      ##没有内容也为false
>>> bool(' ')
True
>>> 
>>> bool('redhat')
True
  • python中数据类型的转换
>>> a = 1
>>> type(a)
<class 'int'>
>>> float(a)  ##整型转换浮点型
1.0
>>> type(a)
<class 'int'>
>>> b = float(a)
>>> b
1.0
>>> type(b)
<class 'float'>
>>> a
1
>>> b = 2.0
>>> int(b)  ##浮点型转换成整型
2
>>> b = 2.3
>>> int(b)
2
>>> c = 'redhat'  ##str不能转换成int
>>> int(c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'redhat'
>>> b = 123  ##整型转换成字符串
>>> str=(b)
>>> b
123
  • 在内存中删除一个变量
>>> del a
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> del b
>>> b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined
  • python的输入输出

python3.x与python2.x的区别在于:
python3.x的input():接收任意数据类型
python2.x的input():只支持正确的数值类型 raw_input():支持数值类型和字符串类型

python3.x

>>> input('Num:')
Num:2
'2'
>>> input('Num:')
Num:abc
'abc'
>>> import getpass
>>> num = getpass.getpass('请输入密码:')
请输入密码:
>>> num
'123'

python2.x

>>> input('Num:')
Num:2
2
>>> input('Num:')
Num:'redhat'
'redhat'
>>> input('Num:')
Num:True
True
>>> input('Num:')
Num:redhat
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'redhat' is not defined

>>> raw_input('Num')
Numredhat
'redhat'
>>> raw_input('Num:')
Num:redhat
'redhat'

如果接收到的数值要进行比较的时候,一定要转换为同一种类型

>>> age = input('age:')
age:19
>>> age
'19'
>>> age > 18
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'str' and 'int'
>>> age = int(input('age:'))
age:19
>>> age
19
>>> age > 18
True
  • python的格式化输出

%s :代表字符串的占位 %d:整型

>>> name = 'westos'
>>> name
'westos'
>>> age = 11
>>> print('%s的年龄是%d' %(name,age))
westos的年龄是11
>>> age = '123'
>>> print('%s的年龄是%d' %(name,age))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: %d format: a number is required, not str

%f:浮点型
%.xf(x:1,2,3,4,5)保留小数点后多少位

>>> money = 323232.424112
>>> name = 'tom'
>>> money = 60000
>>> print('%s的工资为%f' %(name,money))
tom的工资为60000.000000  ##默认情况为保留小数点后六位小数
>>> money = 6000.99
>>> print('%s的工资为%f' %(name,money))
tom的工资为6000.990000
>>> print('%s的工资为%.2f' %(name,money))
tom的工资为60000.00
>>> print('%s的工资为%.3f' %(name,money))
tom的工资为60000.000
>>> print('%s的工资为%.1f' %(name,money))
tom的工资为60000.0

整数的占位:不够的位数 前面补0

>>> sid = 1
>>> name = 'lily'
>>> print('%s的学号是%d' %(name,sid))
lily的学号是1
>>> print('%s的学号是103%d' %(name,sid))
lily的学号是1031
>>> print('%s的学号是0000%d' %(name,sid))
lily的学号是00001
>>> print('%s的学号是%.5d' %(name,sid))
lily的学号是00001
>>> print('%s的学号是%.6d' %(name,sid))
lily的学号是000001
>>> print('%s的学号是%.4d' %(name,sid))
lily的学号是0001

百分数的实现

>>> scale = 0.1
>>> print('数据的比例是:%.2f' %(scale))
数据的比例是:0.10
>>> print('数据的比例是:%.2f' %(scale * 100))
数据的比例是:10.00
>>> print('数据的比例是:%.2f%' %(scale * 100))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: incomplete format
>>> print('数据的比例是:%.2f%%' %(scale * 100))
数据的比例是:10.00%
  • python中的算术运算符

python2.x

>>> 5/2
2
>>> 100/300
0
>>> 5.0/2
2.5
>>> 100/300.0
0.3333333333333333

python3.x

>>> 5/2
2.5
>>> 100/300
0.3333333333333333
>>> 1+2
3
>>> 1* 2
2
>>> 1/2
0.5
>>> 5%2 #取余
1
>>> 5//2 #取整
2
>>> a = 1
>>> a = a+1
>>> a +=1

END

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值