首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

python学习札记2——第一章 语法入门

2012-10-09 
python学习笔记2——第一章 语法入门为了方便学习,从今天开始只专注于2.51.#获取用户输入name raw_input

python学习笔记2——第一章 语法入门

为了方便学习,从今天开始只专注于2.5

1.

#获取用户输入>>name = raw_input("what's your name?")>>print "Hello" + name + "!"

?

?2. 通过命令提示符运行python脚本? ??有问题,未解决。

?

?3. File-->new window--->弹出窗口,将1中内容编写入程序,保存为.py文件,ctrl+F5运行,在解释器中显示结果。

或直接双击.py文件,不过需要添加一句话press <Enter>,这样才能保证,命令窗口显示结果后不马上关闭,而是需要用户按回车键关闭。在命令行窗口中显示结果。

name = raw_input("what's your name?")print "Hello" + name + "!"raw_input("Press <enter>")
?

4.

>>> name = "Hello world">>> print nameHello world>>> name'Hello world'>>> "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!'    "Let's go!"        #可以用转义字符显示单引号,整个文本由双引号括起来>>> ""Hello. world!" she said"'"Hello. world!" she said'>>> 

?

5. 字符串拼接采用”+“

>>> "Hello. " + "world!"'Hello. world!'>>> x = "Hello. ">>> y = "world!">>> x + y'Hello. world!'

?

6. str()把值转换为合理形式的字符串,以便用户可以理解。

???? repr()创建一个字符串,它以合法的Python表达式的形式来表示值

?

?? 简而言之,str、repr和反引号(怎么算是反引号???)是将Python值转换为字符串的三种方法,str让字符串更易于 阅读,repr和反引号则把结果字符串转换为合法的Python表达式。

>>> print repr("Hello world")'Hello world'>>> print str("Hello world")Hello world>>> temp = 12>>> print "test " + tempTraceback (most recent call last):  File "<pyshell#35>", line 1, in <module>    print "test " + tempTypeError: cannot concatenate 'str' and 'int' objects>>> print "test " + repr(temp)     #也可以用反引号将temp括起来test 12 

?

7. input 和 raw_input

?? input()会假设用户输入的是合法的python表达式

?? raw_input()把所有的输入作为原始数据将其放入字符串中

?? 除非对input特别需要,否则应尽可能使用raw_input

>>> name = raw_input("what's your name? ")what's your name? world   #把输入world作为原始数据放入字符串中>>> print "Hello " + nameHello world>>> name = input("what's your name? ")what's your name? world    #world不是合法的python表达式Traceback (most recent call last):  File "<pyshell#51>", line 1, in <module>    name = input("what's your name? ")  File "<string>", line 1, in <module>NameError: name 'world' is not defined>>> name = input("what's your name? ")what's your name? "world"   #使用input时,这里需要输入字符串"world",才是合法的python表达式>>> print "Hello " + nameHello world>>> input("Enter a number: ")Enter a number: 3    #数字3是合法的python表达式,并显示python表达式数字33>>> raw_input("Enter a number: ")Enter a number: 3     #虽然输入的是数字,但是显示字符串,因为把输入3作为原始数据放入字符串中'3'

?

8. 使用""来对换行进行转义

??? 使用三个单引号或双引号来表示长字符串,中间可以使用单引号及双引号

>>> print "hello \world"hello world   #实现跨行表达一行字符串>>> print """This is avery longstring."Hello World!"over"""   #三个双引号表达长字符串,包括换行This is avery longstring."Hello World!"over>>> print '''This also is avery longstring.'''This also is avery longstring.

?

9.

>>> print "Hello.\n world!"  #\n换行符Hello. world!>>> print "C:\nowhere"  #比如想输出C盘的nowhere文件夹,会把\n误当做换行符C:owhere>>> print "C:\\nowhere"  #可以使用转义字符"",但是若表达式中斜线过多,比较乱C:\nowhere>>> print r"C:\nowhere"  #解决方法,使用原始字符rC:\nowhere>>> print r 'C:\nowhere'   #但r跟字符串之间不应该有空格,否则报错SyntaxError: invalid syntax 

?

总结第一章

主要讲解python语法,涉及到一些函数及普通的输入输出,比较简单但比较琐碎,大体看看即可。

?

用到的函数

?

abs(number)? 返回数字的绝对值

cmath.sqrt(number) 返回数字的平方根,也可应用于负数

float(object)? 将数字或字符串转换为浮点数

help() 提供交互式帮助

input(prompt)? 获取用户输入

int(object) 将字符串和数字转换为int型

long(object)

math.ceil(number) 向上取整

math.floor(number)? 向下取整

math.sqrt(number) 平方根,不适用于负数

pow(x, y[,z]) x的y次幂,所得结果对z取模

raw_input(prompt)? 获取用户输入,返回的输入为字符串

repr(object)? 返回值的字符串表示

round(number[,ndigits])??? 根据给定的精度对数字进行四舍五入

str(object)?? 将值转换为字符串

?

热点排行