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

[经验分享] python学习笔记(九)之语句1

[复制链接]

尚未签到

发表于 2018-8-13 09:03:14 | 显示全部楼层 |阅读模式
  python学习笔记(九)之语句1
  print
  python2中,print是一个语句,python3中它是一个函数。
  实例1:
  >> print "hello,world!"
  hello,world!
  >> print "hello","world!"
  hello world!
  说明:print语句中,字符串后面会接一个\n符号,即换行!但是,如果要在一个字符串后面跟着逗号,那么换行就取消了,如下:
  实例2:
  >> for i in [1,2,3,4]:
  ...     print i
  ...
  1
  2
  3
  4
  >> for i in [1,2,3,4]:
  ...     print i,
  ...
  1 2 3 4
  import
  实例3:
  >> import math
  >> math.pow(3,2)
  9.0
  或者是:
  >> from math import pow  ##适合引入模块较少的情况。如果引入模块多了,可读性就下降了,会不知道那个函数来自那个模块,如实例4
  或>>> from math import *
  >> pow(3,2)
  9.0
  实例4:
  >> from math import pow as pingfang  ##对pow重命名,使用pingfang()就相当于在使用pow()
  >> pingfang(3,2)
  9.0
  引入多个函数,标准做法:
  实例5:
  >> from math import pow,e,pi
  >> pow(e,pi)   ##e是欧拉数;pi是π(pai)
  23.140692632779263
  赋值语句
  a = 3 就是一个赋值语句
  实例6:
  >> x,y,z = "wtf",1,["love","you"]
  >> x
  'wtf'
  >> y
  1
  >> z
  ['love', 'you']
  实例7:
  >> a
  ('love', 'datagrand')
  >> type(a)
  <type 'tuple'>
  if--条件语句
  实例8:
  >> a = 4
  >> if a != 5:   ##冒号是必须的,只有返回的是True,才会执行下面的语句
  ...     print a  ##要缩进四个空格
  ...
  4
  if...elif...else
  基本样式结构:
  if 条件1:
  语句块1
  elif 条件2:
  语句块2
  elif 条件3:
  语句块3
  。。。
  else:
  语句块4
  实例9:
cat shiyan.py
  #!/usr/bin/env python
  #coding:utf-8
  print &quot;Please enter any integer number:&quot;
  ##raw_input() The number entered is a string
  ##int() Convert the string to an integer
  number = int(raw_input())
  if number == 10:
  print &quot;The number you entered is:%d&quot; %number
  print &quot;you are great!&quot;
  elif number > 10:
  print &quot;The number you entered is:{}&quot;.format(number)
  print &quot;The number is more than 10.&quot;
  elif number < 10:
  print &quot;The number you entered is:{}&quot;.format(number)
  print &quot;The number is less than 10.&quot;
  else:
  print &quot;are you a human?&quot;
  三元操作符
  语法:a = b if c else d
  (1)如果c为真,那么执行a = b
  (2)如果c为假,那么执行a = d
  实例10:
  >> name = &quot;wtf&quot; if &quot;didi&quot; else &quot;zhao&quot;  ##if &quot;didi&quot;相当于if bool(&quot;didi&quot;)
  >> name
  'wtf'
  >> name = &quot;wtf&quot; if &quot;&quot; else &quot;zhao&quot;  ## if &quot;&quot;相当于if bool(&quot;&quot;) ps:两个引号之间没有空格
  >> name
  'zhao'
  for循环
  语法:
  for 循环规则:
  操作语句
  实例11:
  for循环--字符串
  >> hello = &quot;world&quot;
  >> for i in hello:
  ...     print i
  ...
  w
  o
  r
  l
  d
  实例12:
  for循环--列表
  >> wtf_python = [&quot;data&quot;,&quot;grand&quot;,&quot;welcome you&quot;,&quot;&quot;]
  >> wtf_python
  ['data', 'grand', 'welcome you', '']
  >> for world in wtf_python:
  ...     print world
  ...
  data
  grand
  welcome you
  >>
  实例13:
  for循环--元组
  >> c = (&quot;wtf&quot;,&quot;datagrand&quot;)
  >> type(c)
  <type 'tuple'>
  >> for i in c:
  ...     print i
  ...
  wtf
  datagrand
  实例14:
  for循环--字典
  >> d = {&quot;wtf&quot;:&quot;python&quot;,&quot;didi&quot;:&quot;data&quot;}
  >> type(d)
  <type 'dict'>
  >> for i in d:
  ...     print i
  ...
  didi
  wtf
  数字是不能进行for循环的
  因为int对象是不可迭代的,即for循环所应用的对象,应该是可迭代的。
  实例15:
  for循环--数字(不可for循环)
  >> for i in 123:
  ...     print i
  ...
  Traceback (most recent call last):
  File &quot;<stdin>&quot;, line 1, in <module>
  TypeError: 'int' object is not iterable
  判断一个对象是否可迭代的方法:
  实例16:
  >> import collections
  >> isinstance(123,collections.Iterable)
  False   ##不可迭代
  >> isinstance([1,2,3],collections.Iterable)
  True    ##可迭代
  range
  语法:
  range(start,stop[,step])
  语法说明:
  start:开始数值,默认为0,也就是如果不写这项,就是认为start=0
  stop:结束的数值,必须要写的。
  step:变化的步长,默认是1,也就是不写,就是认为步长为1.坚决不能为0.
  range()函数特点:
  (1)这个函数可以创建一个数字元素组成的列表;
  (2)常用于for循环
  (3)函数的参数必须是整数,默认从0开始。
  (4)step默认是1.如果不写,就是按照此值;
  (5)step不能为零,如果等于零就报错;
  (6)如果step是正数,返回列表的最后的值不包含stop值,即start+istep这个值小于stop;如果step是负数,start+istep的值大于stop。
  实例17:
  >> range(5)
  [0, 1, 2, 3, 4]
  >> range(0,5)
  [0, 1, 2, 3, 4]
  >> range(0,5,1)
  [0, 1, 2, 3, 4]
  >> range(1,5)
  [1, 2, 3, 4]
  >> range(1,5,2)
  [1, 3]
  >> range(0,-9,-1)
  [0, -1, -2, -3, -4, -5, -6, -7, -8]
  实例18:
  找出小于100的能够被3整除的正整数
cat range.py
  #!/usr/bin/env python
  #coding:utf-8
  wtf = []
  for n in range(1,100):
  if n % 3 == 0:
  wtf.append(n)
  print wtf
  执行:
python range.py
  [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]

运维网声明 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.iyunv.com/thread-550958-1-1.html 上篇帖子: 第五篇 Python常用的模块 下篇帖子: 图解Python变量与赋值
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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