Python学习笔记(一)————基础知识

1.数字和表达式

除法

在3.0之前的版本,一个整数被另一个整数除,计算结果的小数部分会被截除,例如:
>>> 1/2
0

如果需要普通的除法怎么办?这里提供两个有效的方案:

1.要用实数(包含小数点的十进制数)而不是整数进行运算

>>> 1.0/2.0
0.5
>>> 1/2.0
0.5
>>> 1.0/2
0.5
>>> 1/2.
0.5
>>> 1./2
0.5

2.让Python改变除法的执行方式

可以在程序前加上以下语句,或者在解释器里执行
>>> from __future__ import division
>>> 1/2
0.5

当然,单斜线不再用作整除了,此时使用另外的操作符-------双斜线:
>>> 1//2
0

就算是浮点数,双斜线也会执行整除
>>> 1.0//2.0
0.0

另外,取余运算也很有用,如果需要每10分钟执行一次,就可以检查时间%10的结果是否为0
>>> 10%3
1
>>> 2.75%0.5
0.25

幂运算符比取反运算符优先级高
>>> -3**2
-9
>>> (-3)**2
9

2.语句

print语句
>>> print 4
4
>>> print"sdf"
sdf

在Python3.0以后,print是函数,这意味着编写print(4)而不是print 4
>>> print 4
SyntaxError: invalid syntax
>>> print (4)
4

看一下print在3.0之前和之后的区别
3.0之前,print是语句
>>> help(print)
SyntaxError: invalid syntax

3.0之后,print是函数
>>> help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

从3.0版本的Manuals中可以找到 print

Converts the print statement to theprint() function.

3.函数

幂运算函数pow(),round函数会把浮点数四舍五入为最接近的整数值:
>>> pow(3,2)
9
>>> round(4.56)
5.0
>>>

4.模块

可以理解为导入到Python以增强其功能的扩展。需要命令import来导入,按照“模块.函数”的格式来使用模块的函数:
>>> import math
>>> math.floor(97.7)
97.0
>>> 

若想转换成整数,可以使用int函数:
>>> int(math.floor(97.7))
97

int函数/类型在把参数转换成整数时会自动向下取整,所以在转换过程中,math.floor是多余的,可以直接下写成
>>> int(97.7)
97

与floor相对的是ceil,可以将给定的是数转换成大于或等于它的最小整数
>>> math.ceil(97.7)
98.0

事实上,可以用变量来引用函数(或者Python中大多数的对象),比如
>>> foo = math.sqrt
>>> foo(9)
3.0

sqrt只能处理浮点数,如何求负数的平方根,要用到cmath(complex math,复数)的模块来实现
>>> import cmath
>>> cmath.sqrt(-1)
1j
>>> 

如果使用了from...import...语句,就没法使用普通的sqrt函数了。所以,除非真的需要from这个形式的模块导入语句,应该坚持使用普通的import。

5.让脚本像普通程序一样运行

1.UNIX

在脚本首行前面加 #!(叫做pound bang 或者shebang),在其后加上用于解释脚本的程序(这里就是python)的绝对路径:
#!/usr/bin/env/ python


在实际运行脚本之前,必须让脚本文件具有可执行的属性:
$ chmod a+x hello.py

然后就能这样运行代码了(假设路径就是当前目录)
$hello.py

2.windows

在windows系统中,让代码像普通程序一样运行的关键在于扩展名.py。双击py文件,就能运行。例如:
name = raw_input("wha is your name?")
print "Hello,"+ name +"!"

程序运行完就关了,如果想要看结果,可以加上
raw_input("Press <Enter>")

6.字符串

1.单引号和双引号

事实上,单引号和双引号没有区别。只是在某些情况下,需要结合使用:
>>> "say "hi""
SyntaxError: invalid syntax
>>> 'say "hi"'
'say "hi"'

或者:
>>> 'let's go'
SyntaxError: invalid syntax
>>> "let's go"
"let's go"

当然也可以使用转义字符(\):
>>> 'lst\'s go'
"lst's go"

如果字符串中既有单引号,又有双引号,使用反斜线又不太直观,这是就用到了长字符串和原始字符串

2.长字符串、原始字符串和Unicode

1.长字符串

需要跨行的长字符串,使用三个引号(单引号或者双引号都可以):
>>> '''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.\nIt continues here.\nAnd it\'s not over yet.\n"Hello world!"\nStill here.'
>>> 

普通字符串也可以跨行,若一行的最后一个字符是反斜线,那么,换行符本身就被“转义”了,也就是被忽略了:
>>> 'hello\
world'
'helloworld'

2.原始字符串

如果希望在字符串中包含反斜线再加上n怎么办?
1.可以用反斜线对其本身进行转义:
>>> 'c:\\python\\lib\\test\\output'
'c:\\python\\lib\\test\\output'
>>> print 'c:\\python\\lib\\test\\output'
c:\python\lib\test\output

2.使用原始字符串
>>> r'c:\\python\\lib\\test\\output'
'c:\\\\python\\\\lib\\\\test\\\\output'
>>> print r'c:\\python\\lib\\test\\output'
c:\\python\\lib\\test\\output
>>> print r'c:\python\lib\test\output'
c:\python\lib\test\output

在这里有一个疑问:问什么print出来和直接输出字符串得到的结果不同?参照后文的str和repr,可能会得到答案。
如果希望原始字符串以一个反斜线结尾的话,有很多方法,但是本质上是把反斜线单独作为一个字符串来处理:
>>> print r'c:\python\lib\test\output\'
SyntaxError: EOL while scanning single-quoted string
>>> print r'c:\python\lib\test\output''\\'
c:\python\lib\test\output\

注意:可以在原始字符串中同时使用单双引号,即使三引号字符串也可以充当原始字符串。
3.Unicode字符串
Python中的普通字符串在内部是以8位的ASCII码形式存储的,而Unicode字符串则存储为16位Unicode字符,这样就能表示更多的字符集了。
在普Python3.0中,所有的字符串都是Unicode字符串。

3.拼接字符串

一个字符串紧接着另一个字符串,Python会自动拼接它们(合为一个字符串):
>>> "Let's say" '"hello!"'
'Let\'s say"hello!"'

这仅仅是书写字符串的一种特殊方法,拼接字符串的一般方法就像加法运算一样:
>>> x="hello"
>>> y="world"
>>> x
'hello'
>>> y
'world'
>>> x y
SyntaxError: invalid syntax
>>> x+y
'helloworld'

4.str和repr

所有通过Python打印的字符串还是被引号括起来,Python打印值的时候会保持该值在Python代码中的状态。如果使用print语句,结果就不一样了:
>>> 'hello,world'
'hello,world'
>>> 10000L
10000L
>>> print 'hello,world'
hello,world
>>> print 1000L
1000

这里实际上是值被转换为字符串的两种机制,可以通过两个函数来使用这两种机制:
1.str函数
它会把值转换为合理形式的字符串,以便用户理解。

2.repr函数
会去创建一个字符串,他以合法的Python表达式的形式来表示值。(实际上,str和int、long一样,是一种类型。而repr仅仅是函数。)
>>> print repr("Hello,world!")
'Hello,world!'
>>> print repr(1000L)
1000L
>>> print str("Hello,world")
Hello,world
>>> print str(1000L)
1000

repr(x)的功能也可以用`x`来实现(`是反引号)。如果希望打印一个包含数字的句子,那么使用反引号可能更清楚一些:
>>> temp = 42
>>> print "the temprature is "+temp

Traceback (most recent call last):
  File "<pyshell#127>", line 1, in <module>
    print "the temprature is "+temp
TypeError: cannot concatenate 'str' and 'int' objects
>>> print "the temprature is "+`temp`
the temprature is 42

注:在Python3.0中已经不再使用反引号了,应该坚持使用repr。
简而言之,str、repr和反引号是将Python值转换为字符串的3种方法。str让字符串更易于阅读,而repr和反引号则把结果字符串转换为合法的Python表达式。

5.input和raw_input的比较

在脚本文件中输入下面的语句:
name = input("what is your name?")
print "Hello,"+ name +"!"
raw_input("Press <Enter>")

这是一个完全合法的程序。但是,这是不可行的。问题在于input会假设用户输入的是合法的Python表达式,如果以字符串作为输入,是没有问题的。
raw_input函数会把所有的输入当做原始数据(raw data),然后将其放入字符串中:
>>> input('Enter a number: ')
Enter a number: 6
6
>>> raw_input('Enter a number: ')
Enter a number: 6
'6'

除非对input有特殊需求,否则尽可能的使用raw_input。
注:Python3.3.2 manuals中,取消了raw_input:
raw_input

Converts raw_input() to input().

以上是今天学习的一些Python基础知识,坚持学下去!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值