`

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基础教程(第2版)》是一本全面介绍Python编程的指南,适合初学者入门。Python作为一种解释型、面向对象、动态数据类型的高级程序设计语言,因其简洁的语法和强大的功能而广受欢迎。该书分为基础篇和高级篇...

    python基础教程第二版

    《Python基础教程第二版》是针对初学者的一本经典教材,尤其适合那些对Python 2.0版本感兴趣的读者。本书全面、深入地介绍了Python编程语言的基础概念和语法,旨在帮助读者掌握这一强大而灵活的编程工具。以下是根据...

    Python基础教程 第三版

    作为初学者入门编程或者专业人士增强技能的工具,Python基础教程第三版是学习这一语言的理想资源。该高清版教程涵盖了Python的最新版本特性,确保读者能够掌握现代Python开发的关键知识。 **Python语言特性** 1. *...

    Python基础教程(第三版).pdf

    《Python基础教程(第三版)》是一本专为初学者设计的Python编程教材,全面覆盖了Python语言的基础知识,旨在帮助读者快速掌握Python编程技能。本教程以清晰易懂的语言和丰富的实例,引领读者深入理解Python的核心...

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

    《Python基础教程(第2版)》是一本全面覆盖Python编程基础知识的经典教程,适用于不同层次的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版 修订版).pdf

    通过阅读《Python基础教程(第2版 修订版)》,读者不仅可以学到Python的基础知识,还能了解到Python的许多高级特性,从而能够熟练地运用Python进行各种类型的软件开发。这本书适合Python初学者,也适合有一定经验的...

    python 基础教程第三版

    《Python基础教程(第3版)》是一本深入浅出的Python编程指南,适用于初学者和有一定经验的程序员。本书全面覆盖了Python语言的核心概念、语法结构以及实用技巧,旨在帮助读者快速掌握Python编程的基本技能。 在...

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

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

    python基础教程源代码-python基础教程第三版源代码.pdf

    总之,《Python基础教程》第三版的源代码为Python初学者提供了一个丰富的实践平台,不仅覆盖了Python的基础知识,还包含了进阶主题,使得学习过程更具互动性和实用性。通过逐步解析和实践这些代码,学习者可以建立起...

    python基础教程至60课基础

    本资源摘要信息涵盖 Python 基础教程的 60 课内容,涵盖 Python 基础知识点的所有方面。 安装和设置 * Python 的安装和设置(Python 第 1 课) + 介绍 Python 的安装过程和设置环境 基本输出 * print 语句的...

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

    《Python基础教程(第2版·修订版)2014》是一本针对初学者的Python编程指南,它深入浅出地介绍了Python语言的核心概念和语法。这本书在2014年进行了修订,以适应Python语言的发展和变化,相较于2010年的版本,它...

    Python基础教程(第二版)源代码.zip

    《Python基础教程(第二版)》是一本深受Python初学者喜爱的经典教材,它以其清晰的讲解和丰富的示例引领读者逐步掌握Python编程的基础知识。源代码.zip文件包含了本书中的所有实例代码,是学习过程中不可或缺的参考...

    python基础教程第二版+源代码

    "Python基础教程第二版"是学习Python语言的经典教材,由专家编写,旨在为初学者提供全面而深入的Python知识。这本书包含了Python的基础概念、语法结构、数据类型、控制结构、函数、模块和包、面向对象编程以及错误...

    python教程答案第三章-Python基础教程(第三章).pdf

    以上就是Python基础教程第三章的主要内容,涵盖了字符串格式化、模板字符串、字符串方法等重要概念,这些都是Python编程中处理文本数据的基本技能。理解并掌握这些知识点,将有助于进一步学习和应用Python。

    python 基础教程 Hetland 第二版 源代码

    《Python基础教程》是Martin Hetland所著的一本经典教材,第二版更是对初学者和有一定经验的开发者提供了深入浅出的Python知识讲解。 在这一版的源代码中,读者可以找到书中各章节实例的实现,这对于理解Python编程...

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

    Python是一种高级编程语言,以其简洁明了的语法和强大的功能...理解这些基础知识是深入学习Python的前提,也是成为熟练Python程序员的第一步。随着对这些概念的熟悉和实践,将能够编写出更复杂、功能丰富的Python程序。

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

    《Python基础教程(第2版·修订版)》是一本为初学者设计的Python编程教材,旨在帮助读者全面掌握Python编程基础知识。这本书详尽地介绍了Python语言的核心概念、语法结构以及编程实践,是学习Python编程的绝佳参考...

    python入门教程廖雪峰-Python基础教程.pdf

    在Python的基础教程中,理解脚本第一行的含义至关重要,特别是对于使用Linux或Unix系统的用户。这一行被称为"shebang"或者"hash-bang",它的作用是告诉操作系统应该使用哪个解释器来运行Python脚本。 通常,我们会...

Global site tag (gtag.js) - Google Analytics