首页 技术 正文
技术 2022年11月23日
0 收藏 339 点赞 4,082 浏览 1350 个字

Python版本:Python 2.7.5

Python是一种面向对象、解释型计算机程序设计语言

1.基本操作符
python的除法的结果会随着数值类型的变化而变化
整数相除,结果会取整
实数相除,结果会取实数,如果不是整除,会有小数点
当然,也有整除的操作符 就是// 无论数值类型是什么,都是整除。

>>> 1.0//3.0
0.0
>>> 1/3
0
>>> 1.0/3.0
0.3333333333333333
>>> 1/3.0
0.3333333333333333
>>> 1.0//3.0
0.0

幂 也就是乘方的操作符是**,也可以用函数pow()代替操作符

>>> 2**3
8
>>> pow(2,3)
8

其他函数
abs 绝对值 round 四舍五入
floor 向下取整 ceil 向上取整
int() float() long()等 类型对象

#需要引入math包
>>> import math
>>> int(math.floor(abs(-100.3)))
100

str()函数会把值转换为合理格式的字符串,以便用户可以理解。
repr()函数会创建一个字符串,以合法的python表达式的形式来表示值。

>>> print repr("Hello,world!")
'Hello,world!'
>>> print str("Hello,world!")
Hello,world!

input()函数和raw_input()函数的区别:
input()会假设用户输入的是合法的Python表达式
raw_input()会把所有的输入当成原始数据,放入字符串中。

>>> input("Enter a str:")
Enter a str:hello
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'hello' is not defined
>>> raw_input("Enter a str:")
Enter a str:hello
'hello'

”’或者””” 适用于多行连续输入 
两行的话,也可以用\

>>> print 1+2\
... +3+4
10
>>> print '''come on
... move ahead
... for your future'''
come on
move ahead
for your future
>>>

原始字符串
原始字符串不会把反斜杠当作特殊字符,在原始字符串中输入的每个字符都会与书写的方式一致。

>>> print 'c:\nwww'
c:
www
>>> print 'c:\\nwww'
c:\nwww
>>> print r'c:\nwww'
c:\nwww

注意:(1)原始字符串中,单引号,双引号,以及三引号都会当作字符串处理
(2)原始字符串中,以\结尾会出错,可以用组合的方式构成

>>> print r'c:\nwww\'
File "<stdin>", line 1
print r'c:\nwww\'
^
SyntaxError: EOL while scanning string literal
>>> print r'c:\nwww' "\\"
c:\nwww\
>>>

Python入门(一)

部分内容来源于书籍 《Beginning.Python.From.Novice.to.Professional,2nd.Edition》

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