zoukankan      html  css  js  c++  java
  • 1、Python基础


    进入python:

    1. [root@likun ~]# python 
    2. Python2.6.5(r265:79063,Jul142010,11:36:05) 
    3. [GCC 4.4.420100630(RedHat4.4.4-10)] on linux2
    4. Type"help","copyright","credits" or "license"for more information.
    5. >>> 
     
    退出python
    [ CTRL + d ]
     
    查看版本:
    [root@likun ~]# python -V
    Python 2.6.5
     
    执行第一个hello程序
    1. [root@likun ~]# cat hello.py 
    2. #!/usr/bin/python
    3. print'hello!'
    4. [root@likun ~]# 
    5. [root@likun ~]# python hello.py 
    6. hello!
    print加颜色:
    >>> print '33[31;1mHello world!33[0m'
     
    print重复输出字符:
    >>> print ('a'*5)
    aaaaa
     
    读取用户输入:
    >>> A = raw_input('pleas input:')
    pleas input:hello!
    >>> print A
    hello!
     
    保证输入为数字:
    >>> A = int(raw_input('pleas input number:'))
    pleas input number:sdf
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: invalid literal for int() with base 10: 'sdf'
     
    转换数字为字符:
    >>> str(12)
    '12'
     
    简单计算:
    >>> 2 * 3
    6
    >>> 32/4
    8
    >>> b=2+3
    >>> print b
    5
     
    变量:
    1. >>> name='Likun'        --定义,并给变量赋值,字符类型要加引号
    2. >>> print name
    3. Likun
    4. >>> name1=name        --变量之间传递赋值
    5. >>> print name1
    6. Likun
    7. >>> print '%s is good'%name1       --%s 为变量占位符
    8. Likun is good
    9. >>> name1=addr            --addr未加引号,会当做变量而不是字符,找不到变量因此报错
    10. Traceback(most recent call last):
    11.   File"<stdin>", line 1, in <module>
    12. NameError: name 'addr' is not defined
     
     
    变量使用单引、双引、三引的区别
    正常字符串用单引
    本身带单引的字符串用双引
    三引可以引住多行字符
    1. [root@likun python_scripts]# cat 1hello.py 
      #!/usr/bin/python
      print 'hello!'
      print "what's your name?"
      n='''good morning
      everyone,
      come on !'''
      print n
    '''    ''' 可以用来注释多行代码
     
    小练习:兑换人民币
    1. [root@likun python_scripts]# cat 3rate.py 
      #!/usr/bin/python
      print 'The rate between HK$ and US$ to RMB
      '
      rmb =int(raw_input('input how much RMB you want to change:'))    --int后才能做浮点运算
      HK=rmb /0.84
      US=rmb /6.4
      print 'HK=',HK,',US=',US                --拼接用逗号
    1. [root@likun python_scripts]# python 3rate.py 
    2. The rate between HK$ and US$ to RMB
    3. input how much RMB you want to change:10
    4. HK=11.9047619048,US=1.5625
     
    缩进要求:
    逻辑上同一层级的,要对齐,不对齐会报错
    不同层级的必须有缩进,否则也会报错
    缩进可以选择制表符、单空格、双空格,全文一定要保证缩进上下一致
    1. [root@likun python_scripts]# cat 1hello.py 
      #!/usr/bin/python
      print 'hello!'
      print 'world'        
      for i in range(1,5):
              print i    
    python命令自动补全脚本:
    vim ~/.pythonstartup 
    import sys
    import readline
    import rlcompleter
    import atexit
    import os
    readline.parse_and_bind('tab: complete')
    histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
    try:
        readline.read_history_file(histfile)
    except IOError:
        pass
    atexit.register(readline.write_history_file, histfile)
    del os, histfile, readline, rlcompleter
    echo "export PYTHONSTARTUP=~/.pythonstartup" >> ~/.bashrc 



  • 相关阅读:
    jdbc基础
    JavaScrip练习
    EL标签
    javaBean和mvc思想
    jsp
    Session
    Cookie
    ServletConfig
    c++、opencv、泊松融合
    目标检测、Iou、nms、soft_nms、
  • 原文地址:https://www.cnblogs.com/kissdb/p/4009558.html
Copyright © 2011-2022 走看看