#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基础教程之第1章 基础知识的更多相关文章

  1. Python基础教程之第2章 列表和元组

    D:\>python Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Typ ...

  2. Python基础教程之第5章 条件, 循环和其它语句

    Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 #Chapter 5 条件, 循环 ...

  3. Python基础教程之第3章 使用字符串

    Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "copyri ...

  4. Python基础教程之List对象 转

    Python基础教程之List对象 时间:2014-01-19    来源:服务器之家    投稿:root   1.PyListObject对象typedef struct {    PyObjec ...

  5. Python基础教程之udp和tcp协议介绍

    Python基础教程之udp和tcp协议介绍 UDP介绍 UDP --- 用户数据报协议,是一个无连接的简单的面向数据报的运输层协议.UDP不提供可靠性,它只是把应用程序传给IP层的数据报发送出去,但 ...

  6. OpenVAS漏洞扫描基础教程之OpenVAS概述及安装及配置OpenVAS服务

    OpenVAS漏洞扫描基础教程之OpenVAS概述及安装及配置OpenVAS服务   1.  OpenVAS基础知识 OpenVAS(Open Vulnerability Assessment Sys ...

  7. Linux入门基础教程之Linux下软件安装

    Linux入门基础教程之Linux下软件安装 一.在线安装: sudo apt-get install 即可安装 如果在安装完后无法用Tab键补全命令,可以执行: source ~/.zshrc AP ...

  8. RabbitMQ基础教程之Spring&JavaConfig使用篇

    RabbitMQ基础教程之Spring使用篇 相关博文,推荐查看: RabbitMq基础教程之安装与测试 RabbitMq基础教程之基本概念 RabbitMQ基础教程之基本使用篇 RabbitMQ基础 ...

  9. python基础教程之pymongo库

    1. 引入 在这里我们来看一下Python3下MongoDB的存储操作,在本节开始之前请确保你已经安装好了MongoDB并启动了其服务,另外安装好了Python的PyMongo库. 1.  安装 pi ...

随机推荐

  1. Mysql基础第二部分,针对以后python使用

    #外键 表与表相连 列名 MUL 外键 参照完整性 不能添加另一个表没有的字段create table study_record( id int auto_increment primary key, ...

  2. 版本控制Git(1)——理解暂存区

    一.svn和Git的比较 我们都知道传统的源代码管理都是以服务器为中心的,每个开发者都直接连在中间服务器上, 本地修改,然后commit到svn服务器上.这种做法看似完美,但是有致命的缺陷. 1. 开 ...

  3. Linux企业应用--RHAS 2.1 下安装中文 Lotus Domino R 6.5 图解

    原文请到ftp.jms165.com下载,是用上传用户                                                 (RHAS3+ksnapshot+OperOff ...

  4. css3 边框阴影效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Codefroces 849 A,B

    A. Odds and Ends time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  6. ORA-12012 Error on auto execute of job "SYS"."ORA$AT_OS_OPT_SY_<NN> in 12.2.0 Database

    报错如下:ORA-12012: error on auto execute of job "SYS"."ORA$AT_OS_OPT_SY_88"ORA-2000 ...

  7. XFCE 桌面环境美化,fedora27系统

    一.添加RPM Fusion源,安装方法这里就不说了以前的文章里写过. 二.安装XFCE 主题管理器 xfce-theme-manager [root@Fedora ~]# dnf install x ...

  8. 客户端运行gpupdate /force,后提示原因是缺少到域控制器的网络连接

    问题: C:\Users\wb>gpupdate /force正在更新策略... 用户策略更新成功完成.无法成功更新计算机策略.遇到下列错误: 处理组策略失败,原因是缺少到域控制器的网络连接.这 ...

  9. 如何批量telnet查看否开放指定端口

    由于机房搬迁,需要重新telnet看看网络是否联通 [weblogic@pays03pre_BankVerify /]$ telnet 172.29.1.159 22Trying 172.29.1.1 ...

  10. qt qlineedit只输入数字

    lineEdit->setValidator(new QRegExpValidator(QRegExp("[0-9]+$")));