zoukankan      html  css  js  c++  java
  • Python学习笔记一

    前言:Python系列学习笔记参考的是简明 Python 教程(Swaroop, C. H. 著,沈洁元  译),谢谢^_^

    一、缩进

    这意味着同一层次的语句必须有相同的缩进。每一组这样的语句称为一个块。

    如何缩进
    不要混合使用制表符和空格来缩进,因为这在跨越不同的平台的时候,无法正常工作。我 强烈建议 你在每个缩进层次使用 单个制表符 或 两个或四个空格 
    选择这三种缩进风格之一。更加重要的是,选择一种风格,然后一贯地使用它,即  使用这一种风格。

    二、变量

    变量不需申明类型,即可使用,所有变量的作用域是它们被定义的块,从它们的名称被定义的那点开始。

    1 #!/usr/bin/python
    2 # Filename: expression.py
    3 
    4 length = 5
    5 breadth = 2
    6 area = length * breadth
    7 print 'Area is', area
    8 print 'Perimeter is', 2 * (length + breadth)
    View Code

    type()可以获取变量类型。float()、int()、string() 可以实现变量类型的转换

    type()的应用举例  >>> type(eee)

                         <type 'str'>

    raw_input()可实现用户输入,获取数字串。例子:

      inp = raw_input(‘Europe floor?’)

      usf = int(inp) + 1

      print 'US floor', usf

    输出为: 

      Europe floor? 0

      US floor 1

    三、控制流

    1、if..elif..else

     1 #!/usr/bin/python
     2 # Filename: if.py 
     3 
     4 number = 23
     5 guess = int(raw_input('Enter an integer : '))
     6 
     7 if guess == number:
     8     print 'Congratulations, you guessed it.' # New block starts here
     9     print "(but you do not win any prizes!)" # New block ends here
    10 elif guess < number:
    11     print 'No, it is a little higher than that' # Another block
    12     # You can do whatever you want in a block ...
    13 else:
    14     print 'No, it is a little lower than that' 
    15     # you must have guess > number to reach here
    16 
    17 print 'Done'
    18 # This last statement is always executed, after the if statement is executed
    if..elif..else

     if not... 条件为False时 执行冒号后边的语句块

    2、while...else 

    while循环条件变为False的时候,else块才被执行。else块事实上是多余的,因为你可以把其中的语句放在同一块(与while相同)中,跟在while语句之后,这样可以取得相同的效果。

     1 #!/usr/bin/python
     2 # Filename: while.py
     3 
     4 number = 23
     5 running = True
     6 
     7 while running:
     8     guess = int(raw_input('Enter an integer : '))
     9 
    10     if guess == number:
    11         print 'Congratulations, you guessed it.' 
    12         running = False # this causes the while loop to stop
    13     elif guess < number:
    14         print 'No, it is a little higher than that' 
    15     else:
    16         print 'No, it is a little lower than that' 
    17 else:
    18     print 'The while loop is over.' 
    19     # Do anything else you want to do here
    20 
    21 print 'Done'
    while...else

     3、for...in 

    for..in循环对于任何序列都适用。这里我们使用的是一个由内建range函数生成的数的列表,但是广义说来我们可以使用任何种类的由任何对象组成的序列。else部分是可选的。如果包含else,它总是在for循环结束后执行一次,除非遇到break语句。

    1 #!/usr/bin/python
    2 # Filename: for.py
    3 
    4 for i in range(1, 5):
    5     print i
    6 else:
    7     print 'The for loop is over'
    for...in

    4、break

    break语句用于终止循环,即哪怕循环条件没有称为False或序列还没有被完全递归,也停止执行循环语句。并且,如果你从forwhile循环中 终止 ,任何对应的循环else块将执行。

    5、continue

    continue语句被用来告诉Python跳过当前循环块中的剩余语句,然后 继续 进行下一轮循环。可与if not语句连用

     6、try..except..

    处理异常信息为保证程序不中断,可使用try、except结构。exit()可用来退出程序

  • 相关阅读:
    2020寒假简记
    感知神经网络模型与学习算法
    信息检索模型与评估
    Diffie-Hellman密钥交换
    RSA密码体制
    MySQL基准测试(benchmark)
    MySQL数据引擎
    MySQL 多版本并发控制(MVCC)
    MySQL事务管理
    利用dotnet restore 导入本地 .nupkg 包
  • 原文地址:https://www.cnblogs.com/autumnvivi/p/3594539.html
Copyright © 2011-2022 走看看