zoukankan      html  css  js  c++  java
  • python 第一天


    ****************第一天*****************
    ****铺垫****
    linux 安装python3
    yum install -y gcc gcc-c++
    yum install readline-devel
    yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
    echo /usr/local/lib >> /etc/ld.so.conf.d/local.conf
    ldconfig
    tar -xvf Python-3.5.1.tgz
    cd Python-3.5.1/
    ./configure --prefix=/usr/local/python3
    make
    make  install
    cd /usr/local/python3/
    cp bin/python3 /usr/bin    #支持全局变量

    随地调用
    python3
    Python 3.5.1 (default, May  9 2016, 21:08:00)
    [GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>>

    1.字符集:
    概念:
    python 2.0 只支持ascii,需要声明字符集,需要解析器
    python 3.0 默认编码unicode ,默认支持中文,不需要关心字符编码,不需要解析器
    ascii 码: 转换标准:转为01计算机码

    字符编码
    演变史
    ascii
    Unicode
    UTF-8


    使用:
    python 2.0 
    #!/usr/bin/env python
    #_*_coding:utf-8_*_ 
        print("王明虎")
        print("Hello World! 牛逼")   
         

    python 3.0
    print("王明虎")     
         
         
    #!/usr/bin/env python  
    print("Hello World!")
    print("Hello World! wmh")

    2.变量
    概念:经常变的值,一般为小写

    test:
    x = 2
    y = 3
    print(x + y)

    3.引号
    凡是单引或双引都是字符
    name = 'wmh'
    age = 21

    三引号
    python 开发规范每一行最多不能超过80个字符
    print """
    fdsalkjfljdsaffdjsfdsalkfsafsa
    jfdlksajflksafdjlsaffafdsaf
    kjflksajfdlsajfldsafljsaflja
    """


    4.用户输入

    2.0  用raw_input
    3.0  用input

    使用:
    python 2
    user_input = raw_input("your name:")
    print user_input

    python 3
    user_input = input("input your name:")
    print ("is:",user_input)

    多input同步输出1,调用变量msg
    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    # Author:Minghu Wang

    name = input("Please input your name:")
    age = int(input("Please input your age:"))    #字符转数字,因为3.0,默认变量全是字符,不加会报错
    job = input("Please input your job:")

    msg='''
    Personal information of %s:
    ------------start-------------
        Name: %s
        Age : %d
        Job : %s
    ------------End---------------
    ''' % (name,name,age,job)

    print(msg)

    多input同步输出2,直接print
    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    # Author:Minghu Wang


    name = input("Please input your name:")
    age = int(input("Please input your age:"))    #字符转数字,因为3.0,默认变量全是字符,不加会报错
    job = input("Please input your job:")

    print('''
    Personal information of %s:
    ------------start-------------
        Name: %s
        Age : %d
        Job : %s
    ------------End---------------
    ''' % (name,name,age,job))


    密文输入:只能在LINUX使用
    vi test_pass.py

    import getpass
    username = input("username:")
    password = getpass.getpass("password:")
    print(username,password)

    5.模块:
    概念:内置模块,第三方模块

    os模块,使用命令
    import os
    os.system("df -h")
    os.mkdir('yourdir')
    cmd_res = os.popen('df -h').read()
    print(cmd_res)

    每一个脚本就是一个模块

    当前目录调用脚本即模块test_pass.py
    import test_pass


    6.TAB补全
    python tab补全模块,保存tab.py
    #!/usr/bin/env python
    # python startup file
    import sys
    import readline
    import rlcompleter
    import atexit
    import os
    # tab completion
    readline.parse_and_bind('tab: complete')
    # history file
    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

    import sys
    print(sys.path)    找到目录存tab.py,以后在那个目录都能使用table补全

    >>> print(sys.path)
    ['', '/usr/local/python3/lib/python35.zip', '/usr/local/python3/lib/python3.5', '/usr/local/python3/lib/python3.5/plat-linux',
    '/usr/local/python3/lib/python3.5/lib-dynload', '/usr/local/python3/lib/python3.5/site-packages']

    在/usr/local/python3/lib/python3.5  目录存放tab.py


    7.条件判断
    if else


    使用:

    用户相等测试
    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    # Author:Minghu Wang

    user = 'alex'
    passwd ='alex3714'
    username = input("username:")
    password = input("password:")


    if user == username:
        print("username is correct...")

    密码用户登录验证
    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    # Author:Minghu Wang

    user = 'alex'
    passwd ='alex3714'
    username = input("username:")
    password = input("password:")


    if user == username:
        print("username is correct...")
        if passwd == password:
            print("welcome login")
        else:
            print("password is invalid...")
    else:
        print("连用户名都没蒙对,滚粗。。。。。")

    密码用户同时相等
    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    # Author:Minghu Wang

    user = 'alex'
    passwd ='alex3714'
    username = input("username:")
    password = input("password:")


    if user == username  and passwd ==password:
        print("Welcome login")
    else:
        print("Invalid username or password..")


    猜年龄1
    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    # Author:Minghu Wang

    age = 22
    guess_num = int(input("input your guess num:"))

    if guess_num == age:
        print("Congratulations  you got it.")
    elif guess_num > age:
        print("Think smaller!")
    else:
        print("Think Big...")


    for i range(10):
        print(i)


    猜3次报次数多
    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    # Author:Minghu Wang

    age = 22
    for i in range(10):
        if i < 3:
            guess_num = int(input("input your guess num:"))
            if guess_num == age:
                print("Congratulations  you got it.")
                break #不往后走了,跳出整个loop
            elif guess_num > age:
                print("Think smaller!")
            else:
                print("Think Big...")
        else:
            print("too many attempts...bye")
            break

    猜3次,提示是否继续
    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    # Author:Minghu Wang


    age = 22
    counter = 0
    for i in range(10):
        if counter < 3:
            guess_num = int(input("input your guess num:"))
            if guess_num == age:
                print("Congratulations  you got it.")
                break #不往后走了,跳出整个loop
            elif guess_num > age:
                print("Think smaller!")
            else:
                print("Think Big...")
        else:
            counter_confirm = input("Do you want to continue because you are stupid:")
            if counter_confirm == 'y':
                counter = 0
                continue
            else:
                print("bye")
                break
        counter +=1  #counter = counter + 1

  • 相关阅读:
    为什么电影里的黑客都不屑用鼠标? (转)
    专注做好一件事(转) focus---这个世界上最成功的人,他们在某一领域获得成功之后,可通过经营杠杆进入任何他们想要涉足的领域。而这都得依赖于他们曾极致的专注在做好一件事情上。
    世间万物都是遵循某种类似的规律,谁先把握了这些规律,谁就最早成为了强者。
    走的时候不要太急,有时间要停下来想一想当初为什么而走,这样,才会走的更稳,走的更明白。
    Android笔记: Android版本号
    Android笔记:真机调试无法输出Log 信息的问题
    阿里云服务器试用
    Android笔记:利用InputStream和BufferedReader 进行字节流 字符流处理
    Android笔记:java 中的数组
    Android笔记:C memory copy
  • 原文地址:https://www.cnblogs.com/wangminghu/p/5476732.html
Copyright © 2011-2022 走看看