十二12 发表于 2018-8-7 07:30:21

Python之路(一)

  1.linux下必须在代码之前加 #!/usr/bin/env python表示调取环境变量,保证python可以正常运行
  2.python下的注释方面
  单选注释 # print("hello world")
  多行注释 """ print("hello world")"""
  3.python输入 输出
  python2.X  Variables = raw_input("anyting:") 输入
  python3.X   Variables = input("anyting:") 输入
  print("anyting") 输出
  格式化输出的几种方法
  example :
  name = 'chen'
  age = '18'
  job = 'IT'
  info = '''
  -----inf of ''' + name + '''------''' + '''
  Name:''' + name + '''
  Age:''' + age + '''
  Job''' + job
  print (info)
  example 2 :
  info = '''
  -----inf of %s------
  Name:%s
  Age:%s
  Job:%s
  '''%(name,name,age,job)
  example 3:
  info = '''
  -----inf of {_name} ------
  Name: {_name}
  Age:{_age}
  Job:{_job}
  '''.format(
  _name = name,
  _age = age,
  _job = job,
  )
  example 4:
  info = '''
  -----inf of {0} ------
  Name: {0}
  Age:{1}
  Job:{2}
  '''.format(name,name,age,job)
  第一种方法不建议使用,对内存占用比较大,建议选用3,4两种方法


[*]if,while,for 语句
  标准方法if expression:
  

    if_suite  

  if esle方法:
  if expression:
  

   if_suite  

  else:
  

    if_suite  

  if else-if方法
  if expression:
  

   if_suite  

  elif expression:
  

    elif_suite  

  else:
  else_suite
  while expression:
  

         while_suite  

  example:
  counter = 0
  while counter < 3:
  print ('loop #%d' %(counter))
  counter += 1
  for eachNum in :
  

   print eachNum  

  0

  1
  2
页: [1]
查看完整版本: Python之路(一)