设为首页 收藏本站
查看: 1302|回复: 0

[经验分享] Python基础教程之第1章 基础知识

[复制链接]

尚未签到

发表于 2017-5-5 12:13:10 | 显示全部楼层 |阅读模式
#1.1 安装Python
#1.1.1 Windows
#1.1.2 Linux和UNIX
#1.1.3 Macintosh
#1.1.4 其他发布版
#1.1.5 时常关注,保持更新
#1.2 交互式解释器
D:\>python
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print "Hello, World!"
Hello, World!
>>> The Spanish Inquisition
File "<stdin>", line 1
The Spanish Inquisition
^
SyntaxError: invalid syntax
#1.3 算法是什么
#1.4 数字和表达式
>>> 2 + 2
4
>>> 53673 + 235253
288926
>>> 1/2
0
>>> 1.0/2.0
0.5
>>> from __future__ import division
>>> 1 / 2
0.5
>>> 1 // 2
0
>>> 1.0 // 2.0
0.0
>>> 1 % 2
1
>>> 10 / 3
3.3333333333333335
>>> 10 % 3
1
>>> 9 / 3
3.0
>>> 9 % 3
0
>>> 2.75 % 0.5
0.25
>>> 2 ** 3
8
>>> 3 ** 2
9
>>> (-3) ** 2
9
#1.4.1 长整数
>>> 1000000000000000000
1000000000000000000L
>>> 1000000000000000000L
1000000000000000000L
#1.4.2 十六进制和八进制
>>> 0xAF
175
>>> 010
8
#1.5 变量
>>> x = 3
>>> x * 2
6
#1.6 语句
>>> 2 * 2
4
>>> print 2*2
4
>>> x = 3
#1.7 获取用户输入
>>> input("The meaning of life: ")
The meaning of life: 42
42
>>> x = input("x: ")
x: 34
>>> y = input("y: ")
y: 42
>>> print x*y
1428
>>> if 1 == 2: print 'One equals two'
...
>>> if 1 == 1: print 'One equals one'
...
One equals one
>>> time = 120
>>> if time % 60 == 0: print 'On the hour!'
...
On the hour!
#1.8 函数
>>> 2**3
8
>>> pow(2,3)
8
>>> 10 + pow(2, 3.5)/3.0
13.771236166328254
>>> 10 + pow(2, 3*5)/3.0
10932.666666666666
>>> abs(-10)
10
>>> 1/2
0
>>> round(1.0/2.0)
1.0
#1.9 模块
>>> import math
>>> math.floor(32.9)
32.0
>>> int(math.floor(32.9))
32
>>> from math import sqrt
>>> sqrt(9)
3.0
>>> foo=math.sqrt
>>> foo(4)
2.0
#1.9.1 cmath和复数
>>> sqrt(-1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
>>> import cmath
>>> cmath.sqrt(-1)
1j
>>> (1+3j) * (9+4j)
(-3+31j)
>>> print "Hello, World!"
Hello, World!
>>> "Hello, world!"
'Hello, world!'
>>> "Let's go!"
"Let's go!"
>>> '"Hello, world!" she said'
'"Hello, world!" she said'
>>> 'Let's go!'
File "<stdin>", line 1
'Let's go!'
^
SyntaxError: invalid syntax
>>> 'Let's go!'
#1.9.2 回到__future__
#1.10 保存并执行程序
name=raw_input("What is your name? ")
print "Hello, " + name + "!"
raw_input("Press <enter>")
#1.10.1 通过命令提示符运行Python脚本
#1.10.2 让脚本像普通程序一样运行
D:\>python
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
#1.10.3 注释
>>> import math
>>> pi = 3.14
>>> radius = 1.5
>>> print 2 * pi * radius
9.42
>>> # print the girth/perimeter of the circle
...
>>> user_name = raw_input("What is your name?")
What is your name?Jonathan
>>> print user_name
Jonathan
#1.11 字符串
>>> print "Hello, world!"
Hello, world!
#1.11.1 单引号字符串和使引号转义
>>> "Hello, world!"
'Hello, world!'
>>> "Let's go!"
"Let's go!"
>>> '"Hello, world!" she said'
'"Hello, world!" she said'
>>> 'Let's go!'
File "<stdin>", line 1
'Let's go!'
^
SyntaxError: invalid syntax
>>> 'Let\'s go!'
"Let's go!"
>>> "\"Hello, world!\" she said"
'"Hello, world!" she said'
#1.11.2 拼接字符串
>>> "Let's say " '"Hello, world!"'
'Let\'s say "Hello, world!"'
>>> x="Hello,"
>>> y="wrold!"
>>> x+y
'Hello,wrold!'
>>> xy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'xy' is not defined
>>> "Hello, " + "world!"
'Hello, world!'
#1.11.3 字符串表示,str和repr和反引号
#I guess repr means to reverse an expression
>>> "Hello, world!"
'Hello, world!'
>>> 10000L
10000L
>>> print "Hello, world!"
Hello, world!
>>> print 10000L
10000
>>> print repr("Hello, world!")
'Hello, world!'
>>> print repr(10000L)
10000L
>>> print str("Hello, world!")
Hello, world!
>>> print str(10000L)
10000
>>> print `"Hello, world!"`
'Hello, world!'
>>> print `10000L`
10000L
>>> temp = 42
>>> print "The temperature is " + temp
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> print "The temperature is " + `temp`
The temperature is 42
#1.11.4 input和raw_input的比较
>>> name = input("What is your name?")
What is your name?Jon
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'Jon' is not defined
>>> name = input("What is your name?")
What is your name?"Jon"
>>> print "Hello, " + name + "!"
Hello, Jon!
>>> input("Enter a number" ")
File "<stdin>", line 1
input("Enter a number" ")
^
SyntaxError: EOL while scanning string literal
>>> input("Enter a number ")
Enter a number 3
3
>>> raw_input("Enter a number ")
Enter a number 3
'3'
#1.11.5 长字符串、原始字符串和Unicode
>>> print '''This is a very long string
... It continues here.
... And it's not over yet.
... "Hello, world!"
... Still here.'''
This is a very long string
It continues here.
And it's not over yet.
"Hello, world!"
Still here.
>>> """You can also use double quotes like this."""
'You can also use double quotes like this.'
>>> print "Hello, \
... world!"
Hello, world!
>>> 1+2+\
... 4+5
12
print \
'Hello, world!'
Hello, world!
>>> print 'Hello, \nworld!'
Hello,
world!
>>> path = 'C:\nowhere'
>>> path
'C:\nowhere'
>>> print path
C:
owhere
>>> print 'C:\\nowhere'
C:\nowhere
>>> print path 'C:\\Program Files\\fnord\\foo\\bar\\frozz\\bozz'
File "<stdin>", line 1
print path 'C:\\Program Files\\fnord\\foo\\bar\\frozz\\bozz'
^
SyntaxError: invalid syntax
>>> print 'C:\\Program Files\\fnord\\foo\\bar\\frozz\\bozz'
C:\Program Files\fnord\foo\bar\frozz\bozz
>>> print r'C:\nowhere'
C:\nowhere
>>> print r'C:\Program Files\fnord\foo\bar\frozz\bozz'
C:\Program Files\fnord\foo\bar\frozz\bozz
>>> print r'Let\'s go!'
Let\'s go!
>>> print r'This is illegal\'
File "<stdin>", line 1
print r'This is illegal\'
^
SyntaxError: EOL while scanning string literal
>>> print r"This is illegal\"
File "<stdin>", line 1
print r"This is illegal\"
^
SyntaxError: EOL while scanning string literal
>>> print r'C:\Program Files\foo\bar' '\\'
C:\Program Files\foo\bar\
>>> u'Hello, world!'
u'Hello, world!'
>>> u'你好,世界!'
u'\u4f60\u597d\uff0c\u4e16\u754c\uff01'
>>>


本章的新函数
abs(number)返回数字的绝对值
cmath.sqrt(number)返回平方根,也适用于负数
float(object)将字符串和数字转换为浮点数
help()提供交互式帮助
input(prompt)获取用户输入
int(object)将字符串和数字转换为整数
long(object)将字符串和数字转换为长整数
math.cell(number)返回数的上入整数,返回值的类型为浮点数
math.floor(number)返回数的下舍整数,返回值的类型为浮点数
math.sqrt(number)返回平方根,不适用于负数
pow(x,y[,z])返回x的y次幂(所得结果对z取模)
raw_input(prompt)获取用户输入,返回的类型为字符串
repr(object)返回该值的字符串表示形式
round(number[,ndidits)根据给定的精度对数字进行四舍五入
str(object)将值转换为字符串

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-373467-1-1.html 上篇帖子: 方法类Python Tutorial(九):类 方法类 下篇帖子: Python基础教程之第8章 异常
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表