首页 技术 正文
技术 2022年11月15日
0 收藏 437 点赞 5,074 浏览 1405 个字

查看Python版本

# python -V

Python2.7.5是centos7中默认安装的Python

[root@localhost ~]# python -V
Python 2.7.
[root@localhost ~]# python
Python 2.7. (default, Oct , ::)
[GCC 4.8. (Red Hat 4.8.-)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> #输入exit()或者ctrl+d退出Python的交互式编程模式

查看Python在Linux中的安装位置

# whereis python

[root@localhost ~]# whereis python
python: /usr/bin/python2. /usr/bin/python /usr/lib/python2. /usr/lib64/python2. /etc/python /usr/include/python2. /usr/share/man/man1/python..gz
[root@localhost ~]#

升级Python2.x到Python3.x请参照另一篇博客

https://www.cnblogs.com/djlsunshine/p/10689591.html

编写第一个Python实例“hello Word”

# vi hello.py

[root@localhost ~]# vi hello.py
#!/usr/bin/python
print("Hello, World!");

使用Python命令执行该脚本文件

# python hello.py

[root@localhost ~]# python hello.py
Hello, World!

除法运算

Python中的除法有两个运算符

“/”和“//”

在Python2.x中,整数相除的结果是一个整数,把小数部分完全忽略掉

浮点数除法会保留小数点的部分得到一个浮点数的结果

[root@localhost ~]# python2
Python 2.7. (default, Nov , ::)
[GCC 4.8. (Red Hat 4.8.-)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> / >>> 1.0 / 2.0
0.5
>>>

在Python3.x中,“/”除法不再这么做了,对于整数之间的相除,结果也会是浮点数

[root@localhost ~]# python3
Python 3.6. (default, Apr , ::)
[GCC 4.8. (Red Hat 4.8.-)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> /
0.5
>>>

“//”除法叫做floor除法,会对除法的结果自动进行一个floor操作,在python2.x和python3.x中是一致的。

python2:
>>> - // 2
-
>>>
python3:
>>> - // 2
-
>>>

注意:并不是舍弃小数部分,而是执行floor操作,如果要截取小数部分,那么需要使用math模块的trunc函数

python3:
>>> import math
>>> math.trunc(/) >>> math.trunc(-/) >>>

end

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905