1 快速改造:基础知识(2)

目录

1.10 保存并执行程序 

1.10.1 通过命令提示符运行程序

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

1.10.3 注释

1.11 字符串 

1.11.1 单引号字符串和转义字符

1.11.2 拼接字符串

1.11.3 字符串表示,str和repr

1.11.4 Input和raw_input作比较

1.11.5 长字符串、原始字符串和unicode

1.12 小结

1.12.1 本章的新函数


1.10 保存并执行程序 

IDLE下

1.10.1 通过命令提示符运行程序

(1)直接打印

编辑一个文件hello.py 

# 编辑一个文件hello.py
[root@localhost python]# vim hello.py
print 'Hello World'

linux里面直接运行

[root@localhost python]# python hello.py
Hello world

(2)raw_input输入打印

>>> name = raw_input("what is your name? ")
what is your name? GGGGG
>>> print 'Hello,' + name +'!'
Hello,GGGGG!

换成脚本形式

# 编辑一个文件testHello.py
[root@localhost python]# vim testHello.py   #添加以下内容
name = raw_input("what is your name? ")
print 'Hello,' + name +'!'

[root@localhost python]# python testHello.py  #运行命令
what is your name? hive
Hello, hive!

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

(1)Linux

希望自己的代码能够在unix下顺利执行,那么只要把下面的内容放在脚本首行就行

#!/usr/bin/env python

Ps:如果系统还包括了旧版本的python在,这个时候最好添加python可执行文件的具体位置,如#!/usr/bin/python2或者#!/usr/bin/python2.7,具体路径会因为系统有差异

[root@localhost python]# vim hello.py  #添加以下内容,首行指明这是一个python程序
#!/usr/bin/python2
print 'Hello World'
[root@localhost python]# ./hello.py 
-bash: ./hello.py: Permission denied
[root@localhost python]# chmod a+x hello.py   #赋执行权限
[root@localhost python]# ./hello.py 
Hello World

#如果不加#!/usr/bin/python2,直接./hello.py执行的话,会报错./hello.py: line 1: print: command not found 这是因为默认识别成了linux 的shell脚本,没有print函数

添加了#!/usr/bin/env python之后,hello.py还可以去掉py后缀

(2)Windows IDLE

如果在IDLE下添加一个文件testHello.py

name = raw_input("what is your name? ")

print 'Hello,' + name +'!'

保存后双击这个文件,这个时候如果代码有问题就会立马弹出程序窗口并瞬间关闭,所以最好先在IDLE下运行一遍试试,目前我采用的是python3.8,此处raw_input就报错:NameError: name 'raw_input' is not defined,百度查看报错原因:raw_input是2.x版本的输入函数,在新版本环境下会报错,该函数未定义。在3.x版本中应该用input()代替raw_input()。(

name = input("what is your name? ")
print 'Hello,' + name +'!'

这个时候代码对了,但是输入名字后还没查看到结果就结束了(关闭了窗口)

末尾添加一行:input("Press <Enter>")

改了之后再双击会发现点不动,什么都没有 

1.10.3 注释

井号(#)注释,右边一切内容被忽视

Ps:编写python语言时,注释尽可能不要写废话(无效注释)

#获得用户名  
user_name=input(“what is your name?”)   显而易见就是用户名
# 打印圆的周长     有效注释
print 2 * pi * radius

1.11 字符串 

如Hello,world就是字符串(一串字符)

1.11.1 单引号字符串和转义字符

>>> "hello,world!"
'hello,world!'

这里的Python打印字符串使用了单引号括起来,但程序中输入的是双引号;但如果使用print就不同(print就不会打印单引号);用单引号时打印结果也是单引号

>>> 'hello,world!'
'hello,world!'

 而对于句子中还有单引号存在时,就会报错python不知道如何处理Let后的s(也就是该行余下的内容),所以报错,所以需要对s前的单引号进行转义(采用反斜杠\)

>>> "Let's go!"
"Let's go!"
>>> 'Let's go!'
  File "<stdin>", line 1
    'Let's go!'
         ^
SyntaxError: invalid syntax
>>> 'Let\'s go!'
"Let's go!"

使得python明白中间的单引号是字符串中的一个字符,而不是字符串的结束标记,同样如下:

>>> "\"Hello,world!\" she said"
'"Hello,world!" she said'

1.11.2 拼接字符串

(1)直接连着书写

>>> "Let's say "  '"Hello,world"'
'Let\'s say "Hello,world"'

  (2)采用类似于加法运算的形式

>>> x="Hello, "
>>> y="world!"
>>> x y   #必须同时书写拼接才可
  File "<stdin>", line 1
    x y
      ^
SyntaxError: invalid syntax
>>> x+y  #其实就是"Hello, " + "world!"
'Hello, world!'

1.11.3 字符串表示,str和repr

所有通过python打印的字符串还是被引号括起来的,这是因为python打印值的时候会保持该值在python代码中的状态,而不是你希望用户看到的状态。如果使用print语句,结果就不一样了

Str和int、long一样是一种类型,而repr是函数

>>> print repr("Hello, world!")
'Hello, world!'
>>> print repr(10000L)
10000L
>>> print str("Hello, world!")
Hello, world!
>>> print str(10000L)
1000

 Python2中:repr(x)也可以写做`x`实现(`是反引号,不是单引号),如果希望打印一个包含数字的句子,反引号就比较有用  ,不加则报错TypeError: cannot concatenate 'str' and 'int' objects

>>> temp=42
>>> print "The temperature is " + temp
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> print "The temperature is " + `temp`
The temperature is 42
>>> print "The temperature is " + repr(temp)    #python2中repr(temp)=`temp`
The temperature is 42

当然也可以用str

>>> temp=42
>>> print ("The temperature is " + str(temp))
The temperature is 42
>>> str(temp)
'42'

python3.0以后,已经不再用反引号了,还是用repr

总结:str、repr和反引号是将python值转换成字符串的3种方法,函数str让字符串更易于阅读,而repr(和反引号)则把结果字符串转换为合法的python表达式。

1.11.4 Input和raw_input作比较

我在百度看到:raw_input是2.x版本的输入函数,在新版本环境下会报错,该函数未定义。在3.x版本中应该用input()代替raw_input()。

但书中讲的有点不同,在一个脚本中输入

#!/usr/bin/python2
name = input("what is your name? ")
print 'Hello,' + name +'!'

赋权限   [root@localhost ~]# chmod a+x testRaw.py 

[root@loongbari23 python]# ./testRaw.py   #输入名字Gumby
what is your name? Gumby
Traceback (most recent call last):
  File "./testRaw.py", line 2, in <module>
    name = input("what is your name? ")
  File "<string>", line 1, in <module>
NameError: name 'Gumby' is not defined

报错了,这是因为input会假设用户输入的是合法的python表达式(或多或少有些与repr函数相反的意思),输入时以字符串形式输入是没问题的what is your name? "Gumby",采用raw_input就会通过,但python3是可以的,无需加双引号(Python2,将input函数改成raw_input后通过)

raw_input将所有的输入当做原始数据(raw data),然后将其放入字符串中

1.11.5 长字符串、原始字符串和unicode

多行字符串或者多种特殊字符的字符串的时候

(1)长字符串

跨多行时,采用三个引号代替普通引号

#!/usr/bin/python2
print('''This is a very long string.
It continues here.
And it's not over yet.
"Hello. world"
Still here''')

[root@localhost python]# chmod a+x testLong.py 
[root@localhost python]# ./testLong.py 
This is a very long string.
It continues here.
And it's not over yet.
"Hello. world"
Still here

IDLE执行

要反斜杠转义。

Ps:普通字符串也可以用 \  跨行,其实就是转义字符 \ 对换行符进行了转义,只是被忽略了

>>> print "Hello,\
... world!"
Hello,world!
>>> 1+2+\
... 4+5
12
>>> print \
... 'Hello world'
Hello world

(2)原始字符串

原始字符串对于反斜线不会特殊对待(普通情况反斜线是转义)比如下面的例子

换行符:\n

Hello world
>>> print 'Hello,\nworld'
Hello,
world

如果要打印的字符串中包含\和n怎么办

>>> path='C:\nowhere'
>>> path
'C:\nowhere'
>>> print path  #使用print就不行
C:
owhere

这个时候就需要使用转义字符\进行转义

>>> print('C:\\nowhere')
C:\nowhere

但对于长路径,那么可能需要很多反斜线

>>> print 'D:\Program Files\fnord\foo\bar\baz\frozz\bozz'
D:\Program Files
                nord
                    oaaz
                        rozozz
>>> print('D:\\Program Files\\fnord\\foo\\bar\\baz\\frozz\\bozz')
D:\Program Files\fnord\foo\bar\baz\frozz\bozz

这种情况下原始字符串就派上用场了,原始字符串不会把反斜线当成特殊字符串

>>> print(r'D:\Program Files\fnord\foo\bar\baz\frozz\bozz')
D:\Program Files\fnord\foo\bar\baz\frozz\bozz

原始字符串以r开头,但也要对引号进行转义,不过最后输出的字符串包含了转移所用的反斜线   

>>> print(r'Let\'s go!')
Let\'s go!
>>> print(r"Let's go!")
Let's go!
>>> print("Let's go!")
Let's go!

 不能在原始字符串结尾输入反斜线,如果最后一个字符是反斜线,则需要转义

>>> print(r"This is example\")
  File "<stdin>", line 1
    print(r"This is example\")
                             ^
SyntaxError: EOL while scanning string literal
>>> print(r"This is example\\")   #会将转义字符反斜线也打印
This is example\\
>>> print(r"This is example" "\\")  #只打印一个反斜线,把反斜线单独处理
This is example\ 

原始字符串可同时用单引号双引号,甚至三引号

(3)Unicode字符串

Python中平普通字符串在内不是以8位的ASCII码形成存储的,而Unicode字符串则存储为16位Unicode字符,这样就能够表示更多字符集了。

>>> u'Hello,world'
u'Hello,world'

Unicode字符串采用u前缀,就像原始字符串使用r一样

1.12 小结

算法:使用计算机能够理解的语言,对如何完成一项任务的详尽描述

表达式:用于表示值,2+2是表达式,表示数值4,表达式也可以包含变量

变量:一个名字表示某个值 x=2是赋值语句,x是变量

语句:告诉计算机做某些事情的指令

函数:python中的函数像数学中的函数,可带参数,可自定义函数

模块:对python功能的扩展,可以被导入python中。如from math import sqrt

程序:编写、保存和运行python程序

字符串:文本片段

1.12.1 本章的新函数

函数

描述

abs(number)

返回数值的绝对值

cmath.sqrt(number)

返回平方根,可用于负数(复数的平方根)

float(object)

将字符串和数字转换为浮点数

help()

提供交互式帮助

input(prompt)

获取用户输入

int(object)

将字符串和数字转换为整数

long(object)

将字符串和数字转换为长整型数

math.ceil(number)

返回数的上入整数,返回值类型是浮点型

math.floor(number)

返回数的下舍整数,返回值类型是浮点型

math.sqrt(number)

返回平方根,不适用于负数

pow(x,y[,z])

返回x的y次幂(所得结果为z取模)

raw_input(promat)

获取用户输入,结果做原始字符串

repr(object)

返回值的字符串表示形式

round(number[, ndigits])

根据给定的精度对数字进行四舍五入

str(object)

将值转换为字符串

接下来学:数据结构

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值