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
    

      

  • 相关阅读:
    项目经理所需要具备的素质
    项目经理的个人修养
    项目拖期怎么办
    创业起步的十大准备步骤
    注册(创办)公司的手续过程
    如何让女人每天更快乐
    全面比较:中美两国百姓的生活成本
    ADO.NET级别的事物
    respondsToSelector的使用
    用js在两个页面之间传值
  • 原文地址:https://www.cnblogs.com/kramer/p/2916084.html
Copyright © 2011-2022 走看看