设为首页 收藏本站
查看: 717|回复: 0

[经验分享] python学习笔记2——第一章 语法入门

[复制链接]

尚未签到

发表于 2017-5-4 09:58:18 | 显示全部楼层 |阅读模式
  为了方便学习,从今天开始只专注于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 name
Hello 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 " + temp
Traceback (most recent call last):
File "<pyshell#35>", line 1, in <module>
print "test " + temp
TypeError: 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 " + name
Hello 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 " + name
Hello world
>>> input("Enter a number: ")
Enter a number: 3    #数字3是合法的python表达式,并显示python表达式数字3
3
>>> raw_input("Enter a number: ")
Enter a number: 3     #虽然输入的是数字,但是显示字符串,因为把输入3作为原始数据放入字符串中
'3'
  8. 使用"\"来对换行进行转义
  使用三个单引号或双引号来表示长字符串,中间可以使用单引号及双引号

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

  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"  #解决方法,使用原始字符r
C:\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)   将值转换为字符串

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-372845-1-1.html 上篇帖子: 也说 支付宝即时到帐接口的python实现 下篇帖子: 正则表达式使用学习(C++、Qt、Python)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表