Python Input and print

Source: Internet
Author: User
Tags chr decimal to binary logical operators pow

      Input (): Receive user input        print (): Output
    1. print("aaa","bbb")      2. name="张三"         age=18         print("我叫"+name+"今年"+str(age)+"岁")      3. 不换行输出        print("中国",end="")        print("北京")        print(r"中国\n北京")          输出换行        print("中国\n北京")      4. 格式化输出        name="张三"        age= 12        herght=1.999        print("我叫%s,今年%d,身高%.2f"%(name,age,herght))        #%s字符型;%d int型;%。2f 输出2位小数       5. 格式化输出如果遇到了显示%,要用两个%%来表示原样输出        n = 99.99        print("您战胜了%s%%全国的用户"%n)  
Second, the Format function
    1. functions that format the output string to quickly process various strings
      UserName = "Shi"
      PassWord = "123"
      Print ("username: {}, Password: {}". Format (UserName, PassWord)
      Print ("user name: {0}, Password: {1}". Format (Username,password))
    2. Multiple locations output a variable
      print (username: {0}, password: {1 }{1} ". Format (Username,password))
    3. Define a variable in the Format function
      Print (" username: {0}, Password: {1}, Captcha: {code} ". Format ( username,password,code=4211))
      Pyint ("{}={}". Format ("Python", "123.1111")
      Print ("{1}={0}". Format ("Python" , "123.1111"))
      Print ("{1}={0}". Format ("2")
    4. Reserved is 10 bytes

        print ("{0:a>10}={2:a ^10}={1:A<10} ". Format (" Shi "," Yi "," Sheng ")) Comments:: The edge is the key value, a fill empty;> right alignment, ^ center,< left-aligned, 10 is the number of reserved bytes of different types, showing the results of the difference     
    5. Floating-point display
      Print ("{: f},{:.2f},{}". Format (3.141592654,3.141592654,3.141592654))

        Note: F retains 6 decimal places by default. 2f retains 2 decimal places {} output  
    6. Format binary conversion
      Print ("{: X},{:o},{:b}". Format (230,23 0,230))
      {: x} decimal to hexadecimal
      {: o} decimal to octal
      {: b} decimal to binary

Third, the data type:
    Data type:        numeric type: int, float        Boolean: true, False        null object: None        sequence: list, tuple (tuple), Dict (dictionary), str, range ()
  1. type int, str type, float type
    Print (Type (18))
    Print (Type ("18"))
    Print (Type (1.88))

  2. Boolean nature (True = = 1 False = = 0)
    Print (BOOL) #输出True
    Print (bool ( -1)) #输出True
    Print (BOOL ("123")) #输出True
    Print (BOOL ("")) #输出False
    Print (bool ([])) #输出False

    注释:  0, 0.0 ,None , "", [] , () 这些都会被当做False处理  
  3. Built-in functions: Rounding round ()
    Print (Round (3.6))
    Print (Round (3.1))

  4. Forcing type conversions
    num = "123"
    int = int (num)
    print (int)
    Print (type (int))

  5. Eval () automatically judges and converts strings to the appropriate type
    e1 = eval ("1.23")
    E2 = eval ("11")
    Print (E1)
    Print (E2)
    Print (Type (e1))
    Print (Type (e2))

  6. Binary conversion
    # binary 0b
    # octal 0o
    # hex Ox
    Print (0b1101)
    Print (0o177)
    Print (0X9FF)

          # bin() 将十进制转换成二进制        # hex() 将十进制转换成十六进制        # oct() 将十进制转换成八进制    print(bin(12))    print(hex(12))    print(oct(12))  **ascii **   **chr() 接收的是0-255,返回的是对应的ascii值的字符**  print(ord("a"))  print(chr(97))  
Iv. Module Math
      import math   #先调用模块      print(math.floor(5.66))        print(math.ceil(5.66))         print(math.trunc(-1.66))        注释:            math.floor() 向下取整(坐标轴左边取值)            math.ceil() 向上取整(坐标轴右边取值)            math.trunc() 截断   (往0的方向取整)
V. Decimal module
      运算结果出现问题,我们需要处理浮点类型          import decimal   #先调用模块          print (0.1+0.1+0.1-0.3)            n=decimal.Decimal("0.1")+decimal.Decimal("0.1")+decimal.Decimal("0.1")-decimal.Decimal("0.3")            print (n)            from  decimal import Decimal            n1 = Decimal("0.1")+Decimal("0.3")             print(n1)  
    1. logical operators
      Print (1==2 and 2==2) # returns True if both expressions are formed
      Print (1==2 or 2==1) # Both sides of the expression will return true if one is set
      Print (not 1==2) # not negate

    2. Pi
      Print (Math.PI)

    3. Recurses
      Pow () 2 of the 3-time party
      Print (POW (2,2))

Python Input and print

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.