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基础教程之第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 ...
- Python基础教程之第5章 条件, 循环和其它语句
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 #Chapter 5 条件, 循环 ...
- Python基础教程之第3章 使用字符串
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "copyri ...
- Python基础教程之List对象 转
Python基础教程之List对象 时间:2014-01-19 来源:服务器之家 投稿:root 1.PyListObject对象typedef struct { PyObjec ...
- Python基础教程之udp和tcp协议介绍
Python基础教程之udp和tcp协议介绍 UDP介绍 UDP --- 用户数据报协议,是一个无连接的简单的面向数据报的运输层协议.UDP不提供可靠性,它只是把应用程序传给IP层的数据报发送出去,但 ...
- OpenVAS漏洞扫描基础教程之OpenVAS概述及安装及配置OpenVAS服务
OpenVAS漏洞扫描基础教程之OpenVAS概述及安装及配置OpenVAS服务 1. OpenVAS基础知识 OpenVAS(Open Vulnerability Assessment Sys ...
- Linux入门基础教程之Linux下软件安装
Linux入门基础教程之Linux下软件安装 一.在线安装: sudo apt-get install 即可安装 如果在安装完后无法用Tab键补全命令,可以执行: source ~/.zshrc AP ...
- RabbitMQ基础教程之Spring&JavaConfig使用篇
RabbitMQ基础教程之Spring使用篇 相关博文,推荐查看: RabbitMq基础教程之安装与测试 RabbitMq基础教程之基本概念 RabbitMQ基础教程之基本使用篇 RabbitMQ基础 ...
- python基础教程之pymongo库
1. 引入 在这里我们来看一下Python3下MongoDB的存储操作,在本节开始之前请确保你已经安装好了MongoDB并启动了其服务,另外安装好了Python的PyMongo库. 1. 安装 pi ...
随机推荐
- 基于Java的开源3D游戏引擎jMonkeyEngine
jMonkeyEngine简介 jMonkeyEngine是一款纯Java语言编写的游戏引擎,继承了Java应用跨平台的特性,而且是开放源代码的,遵循BSD开源协议,BSD开源协议用一句简单的话概括就 ...
- 携手互联网企业10巨头设VC基金
包括小米科技.盛大集团.人人网.掌趣科技.游族网络.龙图游戏.蓝港互动.37游戏.星辉互动娱乐.博雅互动等10家知名互联网企业作为出资人(LP)的优格创投基金近日正式成立. 众所周知,伴随着移动互联网 ...
- UESTC 1599 wtmsb
这天,AutSky_JadeK看到了n张图片,他忍不住说道:“我TM社保!”. 每张图片有一个社保值,他可以合并两张图片,合并所得的图片的社保值是原来两张图片的社保值之和. 每次合并需要消耗的体力也是 ...
- Atcoder Grand Contest 107 A Biscuits
A - Biscuits Time limit : 2sec / Memory limit : 256MB Score : 200 points Problem Statement There are ...
- How Chromium Displays Web Pages: Bottom-to-top overview of how WebKit is embedded in Chromium
How Chromium Displays Web Pages This document describes how web pages are displayed in Chromium from ...
- CodeChef November Challenge 2013 部分题解
http://www.codechef.com/NOV13 还在比...我先放一部分题解吧... Uncle Johny 排序一遍 struct node{ int val; int pos; }a[ ...
- react-native flatlist setState修改数据视图不刷新解决方案
给flatlist添加属性:handleMethod = {({viewableItems}) => this.handleViewableItemsChanged(viewableItems) ...
- scrapy框架中间件配置代理
scrapy框架中间件配置代理import random#代理池PROXY_http = [ '106.240.254.138:80', '211.24.102.168:80',]PROXY_http ...
- PHP解析XML格式文档
<?php// 首先要建一个DOMDocument对象$xml = new DOMDocument();// 加载Xml文件$xml->load("3.xml");// ...
- Safe and efficient allocation of memory
Aspects of the present invention are directed at centrally managing the allocation of memory to exec ...