namedhao 发表于 2018-8-13 09:03:14

python学习笔记(九)之语句1

  python学习笔记(九)之语句1
  print
  python2中,print是一个语句,python3中它是一个函数。
  实例1:
  >> print "hello,world!"
  hello,world!
  >> print "hello","world!"
  hello world!
  说明:print语句中,字符串后面会接一个\n符号,即换行!但是,如果要在一个字符串后面跟着逗号,那么换行就取消了,如下:
  实例2:
  >> for i in :
  ...   print i
  ...
  1
  2
  3
  4
  >> for i in :
  ...   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(,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)
  
  >> range(0,5)
  
  >> range(0,5,1)
  
  >> range(1,5)
  
  >> range(1,5,2)
  
  >> range(0,-9,-1)
  
  实例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
  
页: [1]
查看完整版本: python学习笔记(九)之语句1