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
    

      

  • 相关阅读:
    [PHP]算法-归并排序的PHP实现
    [PHP] 数据结构-二叉树的创建PHP实现
    [PHP] 数据结构-循环链表的PHP实现
    [PHP] 数据结构-链表创建-插入-删除-查找的PHP实现
    [PHP] 算法-两个n位的二进制整数相加问题PHP实现
    [PHP] 数据结构-线性表的顺序存储结构PHP实现
    [日常] 链表-头结点和头指针的区别
    [日常] C语言中指针变量
    [日常] 算法-单链表的创建-尾插法
    [日常] 算法-单链表的创建
  • 原文地址:https://www.cnblogs.com/kramer/p/2916084.html
Copyright © 2011-2022 走看看