`

Python基础教程之第1章 基础知识

阅读更多
#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) 将值转换为字符串

分享到:
评论

相关推荐

    python基础教程第二版答案-Python基础教程(第2版).pdf

    python基础教程第⼆版答案-Python基础教程(第2版) Python是⼀种解释型、⾯向对象、动态数据类型的⾼级程序设计语⾔,是*受欢迎的程序设计语⾔之⼀。Python语⾔简洁,语法简单,很 适合作为学习编程的⼊门语⾔。 ...

    Python基础入门教程 由浅入深讲解清晰 第1章 基础知识 (共44页).ppt

    Python基础入门教程 由浅入深讲解清晰 第1章 基础知识 (共44页).ppt Python基础入门教程 由浅入深讲解清晰 第2章 Python序列 (共68页).ppt Python基础入门教程 由浅入深讲解清晰 第3章 选择与循环 (共44页)....

    python基础教程 第三版 中文 高清 pdf

    第1 章 快速上手:基础知识 1.1 交互式解释器 1.2 算法是什么 1.3 数和表达式 1.4 变量 1.5 语句 1.6 获取用户输入 1.7 函数 1.8 模块 1.8.1 cmath和复数 1.8.2 回到未来 1.9 保存并执行程序 1.9.1 从...

    Python基础教程(第2版.修订版)

    第1章 基础知识 第2章 列表和元组 第3章 使用字符串 第4章 字典:当索引不好用时 第5章 条件、循环和其他语句 第6章 抽象 第7章 更加抽象 第8章 异常 第9章 魔法方法、属性和迭代器 第10章 充电时刻 第11...

    快速入门Python基础教程-Python基础知识大全PPT模板.pptx

    快速入门python基础教程_python基础知识大全 演讲人 202x-11-11 快速入门Python基础教程-Python基础知识大全PPT模板全文共33页,当前为第1页。 one 01 第一章:python基础语法 快速入门Python基础教程-Python基础...

    Python基础教程第2章.pptx

    Python基础教程第2章全文共133页,当前为第1页。 第2章 Python语言基础 课程描述 本章将介绍Python语言的基本语法和编码规范,并重点讲解Python语言的数据类型、运算符、常量、变量、表达式和常用语句等基础知识,...

    python 基础教程第三版

    经典的python 教程书籍,通过它可以很好的了解几乎所有关于python的基础知识内容,是一本很好的工具书

    Python基础教程第3章.pptx

    Python基础教程第3章全文共61页,当前为第1页。 第3章 Python函数 课程描述 函数(function)由若干条语句组成,用于实现特定的功能。函数包含函数名、若干参数和返回值。一旦定义了函数,就可以在程序中需要实现该...

    python基础教程txt免费-Python基础教程(第2版).pdf

    ⽬录 第1章 基础知识 1.1 安装Python 1.1.1 Windows 1.1.2 Linux和UNIX 1.1.3 苹果机(Macintosh) 1.1.4 其他发布版 1.1.5 时常关注,保持更新 1.2 交互式解释器 1.3 算法是什么 1.4 数字和表达式 1.4.1 长整型数 ...

    Python基础案例教程-第1章-Python编程基础.pptx

    《Python基础案例教程》 第1章 Python编程基础 Python基础案例教程-第1章-Python编程基础全文共56页,当前为第1页。 第1章 Python编程基础 Python是一门非常优秀的计算机编程语言,因使用界面简洁,编写程序过程简便...

    python基础教程(第三版)学习笔记.pdf

    python基础教程(第三版)学习笔记 ''' 第⼀章 基础知识 1.1 安装Python(Windows安装) 下载-&gt;安装(最好勾选Add Python X.XX to Path)-&gt;启动cmd输⼊python显⽰如下信息: Microsoft Windows [版本 6.1.7601] 版权...

    Python基础教程第2章(共133张).pptx

    Python基础教程第2章(共133张)全文共133页,当前为第1页。 第2章 Python语言(yǔyán)基础 课程描述 本章将介绍Python语言的基本语法和编码规范,并重点讲解Python语言的数据类型、运算符、常量、变量、表达式和...

    python基础教程课后答案-Python基础教程(第2版).pdf

    python基础教程课后答案-Python基础教程(第2版) 第1章 基础知识 1 1.1 安装Python 1 1.1.1 Windows 1 1.1.2 Linux和UNIX 3 1.1.3 苹果机(Macintosh) 4 1.1.4 其他发布版 5 1.1.5 时常关注,保持更新 6 1.2 交互...

    Python基础入门教程 由浅入深讲解清晰 第5章 函数的设计和使用 (共49页).ppt

    Python基础入门教程 由浅入深讲解清晰 第1章 基础知识 (共44页).ppt Python基础入门教程 由浅入深讲解清晰 第2章 Python序列 (共68页).ppt Python基础入门教程 由浅入深讲解清晰 第3章 选择与循环 (共44页)....

    Python基础教程.pptx

    Python基础教程 读书笔记模板 Python基础教程全文共30页,当前为第1页。 01 思维导图 读书笔记 作者介绍 内容摘要 目录分析 精彩摘录 目录 03 05 02 04 06 Python基础教程全文共30页,当前为第2页。 思维导图 Python...

    python基础教程视频教程百度云-python视频教程免费下载,百度云网盘资源,全套!....pdf

    python基础教程视频教程百度云-python视频教程免费下载, 百度云⽹盘资源,全套!... 废话不说 92天的Python视频教程 送上! 总计52G! 从基础到前端、从web框架篇到项⽬实战,92天的视频,让你从⼊门到熟练!(精通...

    精品课件 Python从入门到精通 第2章 Python语言基础(共32页).ppt

    Python从入门到精通 第1章 走进Python.ppt Python从入门到精通 第2章 Python语言基础.ppt Python从入门到精通 第3章 运算符与表达式.ppt Python从入门到精通 第4章 流程控制语句.ppt Python从入门到精通 第5章 列表...

    python入门基础知识.pdf

    python入门基础知识.pdf

    Python程序设计教程-第02章-Python的基础知识.pptx

    第2章 Python的基础知识 Python程序设计教程-第02章-Python的基础知识全文共22页,当前为第1页。 内容简介 本章教学目标: 了解Python语言的书写规范; 明白Python标识符的命名规则; 掌握Python的基本数据类型与...

    python基础教程 第三版 PDF文档+源码(高清 + 目录)珍藏版

    第 1 章 快速上手:基础知识........................ 1 1.1 交互式解释器 ........................................... 1 1.2 算法是什么 ............................................... 2 1.3 数和表达式 .......

Global site tag (gtag.js) - Google Analytics