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 beforemoving 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 Descriptionabs(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
相关文章推荐
- python基础:语法,数据类型和运算符
- Python基础--常用数据类型(常用内置对象)
- 【Python】04、python基础数据类型与运算符
- Python3学习笔记【基础1--数据类型,运算符】
- Python基础I---常用数据类型
- Python--前端基础之JavaScript(JS的引入方式,JS的变量、常量和标识符,JS的数据类型,运算符,流程控制,JavaScript的对象)
- Python基础(2)-基本数据类型以及运算符介绍
- python 基础2 编码转换 pycharm 配置 运算符 基本数据类型int str list tupple dict for循环 enumerate序列方法 range和xrange
- 【Python】从简单案列中揭示常用内置函数以及数据类型
- python运算符,数据类型及常用方法
- Python学习之路【第二篇】-pyc简介、Python常用的数据类型及其用法和常用运算符
- 小甲鱼:Python学习笔记001_变量_分支_数据类型_运算符等基础
- Python基础 -- 01 Python的基础语法(数据类型、输入输出、运算符、数据类型转换、判断语句、循环语句)
- 黑马程序员_Java基础_常用基础内容(进制转换、基本数据类型、运算符、流程控制语句等)
- python基础-变量、数据类型、input、格式化、运算符、流程控制
- Python基础——运算符、数据类型(0329)
- Chapter 3、Java语法基础(三)--- 运算符、数据类型转换 (22nd,Feb)
- 数据类型,运算符和表达式05 - 零基础入门学习C语言06
- javaSE_8系列博客——Java语言的特性(二)--高级语言的基础知识(2)-- 变量和常用数据类型
- Python基础语法之数据类型和变量