zoukankan      html  css  js  c++  java
  • (python learn) 3 operator && expression

    Basically, there are four kinds of operator.

    assignment operator -- like =, a=1 asing a with the value 1

    arithmetic operator  -- like +,-,*,/   8/4=4

    relational operator  -- like >,<,==.  The 2>1 will return true

    logical operator  --  like "and" "or" " not "

    assignment operator

    nothing more to talk , see an example

    >>> a=1+1
    >>> a
    2
    >>> a=8
    >>> a
    8
    

    That is all. The "=" is the assignment operator.

    arithmetic operator

    arithmetic including “+” “-” “*” “/” and some more. Let`s see them one by one.

    These four should be very easy to understand. See below example you will know how to use.

    >>> 1+8
    9
    >>> 9-5
    4
    >>> 2*4
    8
    >>> 8/1
    8
    

    See, it`s very easy. But see below example again.

    >>> 3/2
    1
    >>> 3.0/2
    1.5
    >>> 1+1
    2
    >>> 1.0+1
    2.0
    

    As you can see, the 3/2 is 1 not 1.5. But 3.0/2 will get 1.5. That is because 3 and 2 are all int numbers. So if you want the result is float number, you need to inlcude one float number in the expression.

    Some other arithmetic operators

    //   the operator performing truncating division

    >>> 8//5
    1
    >>> 8.0//5
    1.0
    

    This operator will ignore the reminder.

    % the operator returning the reminder only.

    >>> 5%4
    1
    >>> 9%5
    4
    

    **  the exponentiation operator

    >>> 2**4
    16
    >>> 2**2
    4
    

    a**b return the b-th power of a

    And in python, we can combine the arithmetic operator and assignment operator togather. For example

    a-=100  means a=a-100

    a+=100 means a=a+100

    a**=2 means a=a**2

    a//=5 means a=a//5

    relational operator

    it includes "<,>,<=,>=,!=,==". The relation operator will return a boolen value.

    >>> 1>2
    False
    >>> 1<3
    True
    >>> 1<=1
    True
    >>> 1>=2
    False
    >>> 1!=5
    True
    >>> 1==1
    True
    >>> 1==1.0
    True
    

    Be attentiion to the last one. 1==1.0 will return true.

    logical operator

    it include "and , or , not". 

    Write an calculator

    #!/usr/bin/python
    
    a=int(raw_input("please input number a"))
    b=int(raw_input("please input number b"))
    
    print(a+b)
    print(a-b)
    print(a*b)
    print(a/b)
    

    raw_input will read from the standard input and assignment the value to a. The text in the "" will be show in the screen.

    int will turn the value from raw_input into int type.

    [root@racnode1 test]# python calculator.py
    please input number a 8
    please input number b 4
    12
    4
    32
    2
    

      

  • 相关阅读:
    NHibernate初学二之简单执行SQL及HQL、Linq
    NHibernate初学一之简单增删改查
    System.Func<>与System.Action<>
    C# Lambda表达式
    Lambda表达式详解
    发布订阅者模式之C#委托实现
    在WCF中实现双工通信
    设计模式---订阅发布模式(Subscribe/Publish)
    flex基础示例
    活动倒计时-兼容ios
  • 原文地址:https://www.cnblogs.com/kramer/p/2916084.html
Copyright © 2011-2022 走看看