您的位置:首页 > 编程语言 > Python开发

chapter1:python 基础(数据类型,运算符,常用内置函数,模型,strings等)

2012-09-12 14:51 1111 查看

第一章:python基础

 

1 运算符

 

>>1+2*3

>>7

 

整除

>>1/2

>>0

 

浮点数除,任何一方浮点即可

>>1/2.0

>>0.5

 

取余

>>7%5

>>2

 

求幂

>>2**3

>>8

 

注意:幂结合度比负号大

>>-2**2

>>-4

 

扩充:__future__用于导入python未来支持的语言特征

>>> from __future__ import division

>>> 1/2

0.5

>>> 1//2

0

一些future的特性:

feature
optional in
mandatory in
effect
nested_scopes

2.1.0b1

2.2

PEP 227: Statically Nested Scopes

generators

2.2.0a1

2.3

PEP 255: Simple Generators

division

2.2.0a2

3.0

PEP 238: Changing the Division Operator

absolute_import

2.5.0a1

2.7

PEP 328: Imports: Multi-Line and Absolute/Relative

with_statement

2.5.0a1

2.6

PEP 343: The “with” Statement

print_function

2.6.0a2

3.0

PEP 3105: Make print a function

unicode_literals

2.6.0a2

3.0

PEP 3112: Bytes literals in Python 3000

 

2 长整型

1)     2.2版本后,会自动检测长整型

>>100000000000000000

    100000000000000000l

2)     log和int结合会自动转换为long,python中会自动转换

3)     各数据类型的范围

3  十六进制 & 八进制

>>0xAF  >>175

>>010    >>8

备注:A= 10     F = 15  ->  10*16^1+ 15*16^0 ->175

4 表达式和语句

表达式是运算的过程,语句如赋值,print,不过过于纠结二者的差别

3.0中print为函数,print(42)即可打印出42

5 输入input()

>>input()

‘will print out’

>>will print out

>>x = input(‘this is a prompt : ’)

This is a prompt : 40

>>print x

>>40

5 build-in functions

说明:更多build-infunctions 可查看参考文档

ads(-10) 10 ,求模

pow(2,3) 8,求幂

round(1.2)  float型的四舍五入

6 modules - > math

>>import math

>>math.floor(3.8) 3,取靠近小的整数

>>math.ceil(3,1) 4,去靠近大的整数

>>from math import sqrt   from … import …方式引入,不用加前缀

>>sqrt(9) 3.0 

>>fun1 = math.sqrt

>>fun1(9) 相等于 sqrt(9)

 

7 modules - > cmath ,可用于处理负数

注意:cmath不能用from …import …

>>import cmath

>> cmath .sqrt(-9)

8 #!让py直接执行

#!/usr/bin/python, Linux下在py文件首行加#!python执行文件的绝对路径;

chmod a+x hello.py ,给所有用户加可执行权限

hello.py 或者./hello.py即可,不用pythonhello.py

9 windows 下双击

一闪而过,看不清楚执行结果,在最后加上raw_input(“Press<enter>”),可以停留方便看到显示结果

10 Strings-> 单引号’’,双引号””,转移字符\

>>> "hello"

'hello'

>>> 'hello'

'hello'

>>> "let's go"

"let's go"

>>> '"hello"'

'"hello"'

>>> 'lett\'s go'

"lett's go"

11 str & repr & `` - >把python value 转为string

>>> print 'hello'

hello

>>> print '1000l'

1000l

>>> print str('hello')

hello

>>> print repr('hello')

'hello'  展示python原有的格式

>>> print 'string & int ' + `4`

string & int 4     :sting和数字

>>> print `'hello'`

'hello'

12 input & raw_input

说明:尽量使用raw_input

>>> raw_input()

hello

'hello'

>>> input()

hello

Traceback (most recent call last):

  File"<pyshell#17>", line 1, in <module>

    input()

  File"<string>", line 1, in <module>

NameError: name 'hello' is not defined

13 long Strings ,raw Strings, Unicode

Long Strings -> 三重引号

>>> print '''

'line1'

"line2"

line3

'''

---

'line1'

"line2"

line3

Long Strings -> 引号+\

>>print "line1\

    line2"

    line1line2

>>> 1+2\

     +3

6

Raw Strings->\不为转义字符

>>> print 'c:\nowhere'

c:

owhere

>>> print r'c:\nowhere'

c:\nowhere

>>> print r'c:\nowhere\'

SyntaxError: EOL while scanning string literal     #不可\放到最后

Unicode Strings

一般strings存放8-bitASCII,unicodestrings 16-bit Unicode

>>> print u'hello,unicode'

hello,unicode

A Quick Summary from Beginning Python

This chapter covered quite a bit of material. Let’s take a look at what you’ve learned before

moving on.
Algorithms: An algorithm is a recipe telling you exactly how to perform a task. When you

program a computer, you are essentially describing an algorithm in a language the computer

can understand, such as Python. Such a machine-friendly description is called a

program, and it mainly consists of expressions and statements.
Expressions: An expression is a part of a computer program that represents a value. For

example, 2+2 is an expression, representing the value 4. Simple expressions are built from

literal values (such as 2 or "Hello") by using operators (such as + or %) and functions (such

as pow). More complicated expressions can be created by combining simpler expressions

(e.g., (2+2)*(3-1)). Expressions may also contain variables.
Variables: A variable is a name that represents a value. New values may be assigned to

variables through assignments such as x = 2. An assignment is a kind of statement.

Statements: A statement is an instruction that tells the computer to do something. That

may involve changing variables (through assignments), printing things to the screen (such

as print "Hello, world!"), importing modules, or a host of other stuff.
Functions: Functions in Python work just like functions in mathematics: they may take

some arguments, and they return a result. (They may actually do lots of interesting stuff

before returning, as you will find out when you learn to write your own functions in

Chapter 6.)
Modules: Modules are extensions that can be imported into Python to extend its capabilities.

For example, several useful mathematical functions are available in the math module.

Programs: You have looked at the practicalities of writing, saving, and running Python
programs.

Strings: Strings are really simple—they are just pieces of text. And yet there is a lot to know

about them. In this chapter, you’ve seen many ways to write them, and in Chapter 3 you

learn many ways of using them.

New Functions in This Chapter

Function Description

abs(number) Returns the absolute value of a number

cmath.sqrt(number) Returns the square root; works with negative numbers

float(object) Converts a string or number to a floating-point number

help() Offers interactive help

input(prompt) Gets input from the user

int(object) Converts a string or number to an integer

long(object) Converts a string or number to a long integer

math.ceil(number) Returns the ceiling of a number as a float

math.floor(number) Returns the floor of a number as a float

math.sqrt(number) Returns the square root; doesn’t work with negative numbers

pow(x, y[, z]) Returns x to the power of y (modulo z)

raw_input(prompt) Gets input from the user, as a string

repr(object) Returns a string representation of a value

round(number[, ndigits]) Rounds a number to a given precision

str(object) Converts a value to a string
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐