python basic

数据类型

控制流

if语句:

  #!/usr/bin/python
  number = 23
  guess  = int(raw_input('Enter an integer:'))
  
  if guess == number:
          print 'equals'
          print 'but you do not win any prizes!'
  elif guess > number:
          print 'greater'
  else:
          print 'smaller'
  print 'Done'

while语句:(python 中循环可以和else连用)

  #!/usr/bin/python
  number = 23
  running = True
  
  while running:
          guess = int(raw_input('Enter an integer:'))
  
          if guess ==  number:
                  print 'Congratulations'
                  print 'but you do not win any prizes.'
                  running = False 
          elif guess < number:
                  print 'lower than that'
                  print 'try again'
          else:
                  print 'higher than that'
                  print 'try again'
  else:
          print 'The while loop is over.'
  print 'Done.'

for循环

  #!/usr/bin/python
  
  for i in range(1, 5):
      print i
  else:
      print 'The for loop is over'

break语句:

  #!/usr/bin/python
  
  while True:
          s = raw_input('Enter something:')
          if s == 'quit':
                  break
          print 'Length of the string is', len(s)
  print 'Done'

continue语句:

  #!/usr/bin/python
  while True:
          s = raw_input('Enter something:')
          if s == 'quit':
             continue
          print 'Length of the string is', len(s)
  print 'Done'

from xxx import yyyimport yyy的区别与java静态导入类似,前者引用xxx直接引用,后者需要加yyy前缀yyy.xxx

python的良好的编程习惯

ps:

python -m SimpleHTTPServer 7777 把当前目录当做web服务根目录,端口是7777, 电脑手机发送文件利器

sudo yum install MySQL-python