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
    

      

  • 相关阅读:
    IOS中的国际化的使用(Xcode 6.0之后的使用步骤)
    KVC,KVO的区别和使用
    通知,代理,block 单例的使用和区别
    NSoperation的使用
    多线程之Nsthread的使用方法
    多线程的之GCD的介绍
    IOS中生成证书、真机调试、上线发布程序的步骤
    IOS之NavigationController跳转到指点的界面
    IOS之截取字符串的使用方法
    ios 之定时器的使用的技巧(结合runloop)使用
  • 原文地址:https://www.cnblogs.com/kramer/p/2916084.html
Copyright © 2011-2022 走看看