python学习笔记之基础知识_<<Python基础教程>>学习笔记之|第01章|基础知识 - 好库文摘...

本学习笔记主要用要记录下学习<>过程中的一些Key Point,或自己没怎么搞明白的内容,可能有点杂乱,但比较实用,查找起来也方便。

第01章:基础知识

------

Jython:      Python的Java实现,运行在JVM中,相对稳定,但落后于Python,当前版本2.5,在TA(Python+Robot)会用到

IronPython:  Python的C#实现,运行在Common Language Runtime,速度比Python要快

>>> from __future__ import division

>>> 1/2

0.5

如果直接运算的话,值是0. 除非输入1/2.0,那么Python就当成否点数来运算

------

变量名:包括字母、数字、下划线,不能以数字开头。所以下面正确的表述

正确:   file_1; dict2; _del

错误:   9jin;   hello world; g-z

------

input与raw_input的区别:

1. 如果输入是数字的话,input后,数据类型为int行,raw_input,则只能字符型,必须用int(str1)进行转化,这在程序编程中要注意的。

>>> i = input('Enter here:')

Enter here:32

>>> type(i)

>>> j = raw_input('Enter here:')

Enter here:43

>>> type(j)

>>> raw_input('Enter a number:')

Enter a number:4

'4'

>>> input('Enter a number:')

Enter a number:4

4

2.  input不能包括特殊字符,转译字符,否则会报错,raw_input则可以。

>>> a = input('Enter here:')

Enter here:\n\t

也要注意如果是字符串输入的话,必须加单引号,或双引号

>>> a = input("Enter string:")

Enter string:'hello'

Traceback (most recent call last):

File "", line 1, in

a = input('Enter here:')

File "", line 1

\n\t

^

SyntaxError: unexpected character after line continuation character

>>> b = raw_input('Enter here:')

Enter here:*\n\c\t

>>>

3. 在Win下面双击运行程序,如果想看到交互式的界面,要避免程序一闪而过,就在最底下加raw_input('Press Enter to Exit<>')

------

>>> round(1/2)        #遵循四舍五入

1.0

>>> math.floor(1.1),math.floor(-1.1)   #floor始终是取小的,从名字也可以知道,地板,当然是最底层

(1.0, -2.0)

------

求负数的平方根报错

>>> import math

>>> math.sqrt(-1)

Traceback (most recent call last):

File "", line 1, in

math.sqrt(-1)

ValueError: math domain error

必须用cmath

>>> import cmath

>>> cmath.sqrt

>>> cmath.sqrt(-1)

1j

>>> (1+2j)*(3-4j)           #复数的运算

(11+2j)

------

shebang

如果希望Python脚本像普通脚本一样运行,可以在行首加上#!,再用chmod增加可执行权限

比如下面的:

#!/usr/bin/env python

or

#!/usr/bin/python

$ chmod +x ./hello.py

$ hello.py    #如果不加的话,必须 ./hello.py     或者   python  hello.py

------

单引号如果还涵单引号的号,必须加转译

''' ''' 三引号,可以按自己意愿输出字符串联,所见即所得的模式,还可以包含单引号,双引号

'Let\'s go'

''Let's go''

>>> print """This is a first programing...

print "Hello,World!"

print "Let's Go"

"""

输出结果:

This is a first programing...

print "Hello,World!"

print "Let's Go"

>>> print str("Hello,World!")

Hello,World!

>>> print repr("Hello,World!")

'Hello,World!'

str(), repr和反引号是转换值为字符型变量的三中方法

用``反引号来计算算术值,或引用带数字型的变量

>>> print `2+3`

5

>>> temp = 2

>>> print "Temp's value is:", temp

Temp's value is: 2

>>> print "Temp's value is:" + str(temp)

Temp's value is:2

>>> print "Temp's value is:" + `temp`

Temp's value is:2

>>> print "Temp's value is:" + repr(temp)

Temp's value is:2

------

\ 在行中用于转义,如\n代表换行,\t制表符等,在行尾,可忽略,主要用于多行输入。如:

>>> 1 + 2 + 3 \

- 4 / 2 \

* 2 % 3

5.0

>>> print "C:\\python27"

C:\python27

也就是说不能在字符串末尾输入\

>>> print "This is illegal.\"

SyntaxError: EOL while scanning string literal

如果真想表示的话,则需要这样:

print r'C:\python27\lib\test\pack1\pack2\pack3' '\\'

C:\python27\lib\test\pack1\pack2\pack3\

如果是长路径的话,这么输入的话就比较麻烦,如果嫌麻烦就在前面加r, raw,原始字符串.

表示保持原样,尤其是正则表达式匹配中,原始字符串会非常有用。

>>> print 'C:\\python27\\lib\\test\\pack1\\pack2\\pack3'

C:\python27\lib\test\pack1\pack2\pack3

>>> print r'C:\python27\lib\test\pack1\pack2\pack3'

C:\python27\lib\test\pack1\pack2\pack3

一般python用的8位ASCII来存储数据的,如果想用Unicode字符来存储的话,则可以存储16位,可以存储更多的字符。用户类似于r

>>> u'Hello,World!'

u'Hello,World!'

本章节主要的内置函数

函数名              作用                  例子

========================================================================

abs(number)         返回数字的绝对值      >>> abs(-2)             #2

cmath.sqrt(number)  返回数字的平方根      >>> cmath.sqrt(-4)      #2j

float(object)       将字符串和数字转换为否点型   >>> float('123') #123.0

help()              提供帮助的交互式界面     >>>help(math.cmath)  #或help()

input(prompt)       获取用户输入      >>>input("Enter nuber:")    #见上文

raw_input(prompt)   获取用户输入,字符型>>>raw_input("Enter:")    #见上文

long(object)        将字符串和数字转换为长整数型 >>> long('12345')#12345L

math.ceil(number)   返回数的上入部分,否点型  >>> math.ceil(1.4)  #2.0

math.floor(number)  返回数的下舍部分,否点型  >>> math.floor(-1.6)#-2.0

pow(x,y[,z])        返回x的y次幂,再对z取模   >>> pow(3,2,4)      #1

repr(object)        返回值的字符串方式        >>> repr(1234)      #'1234'

str(object)         将值转换为字符串          >>> str(12345)      #'12345'

round(number[,ndigit])根据指定精度的数字进行四舍五入操作 >>>round(3.14) #3.0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值