Python之路,Python基础篇(第

发布时间:2019-06-26 09:44:18编辑:auto阅读(1585)

    一、Python介绍:
    python的创始人为吉多·范罗苏姆(Guido van Rossum),俗称龟叔。在圣诞节期间为打发无聊而开发得语言。
    
    Python是一门什么语言:
    Python是一门解释性的语言
    
    Python的优缺点:
    Python语言,优雅、明确、简单,虽然不如C或JAVA语言快,但并不影响它得正常使用。
    
    Python解释器:
    Python默认使用的解释器是CPython,C语言开发得。
    
    Python发展史:
    Python2.x 版本只支持到2020年,Python2.7是个过渡版本,Python3.x 是以后的主流。建议使用Python3.x版本进行开发。
    
    py2与3的详细区别:
    
    Python3 的字符编码不用手动指定了,print写法稍有不同。
    
    二、Python的安装:
    Windows:
    	1、官方下载地址:
    	https://www.python.org/downloads/
    	2、安装:
    	默认安装路径: C:\Python27
    	手动指定Python3.x的安装路径:C:\Python35
    	3、配置环境变量
        【右键计算机】--》【属性】--》【高级系统设置】--》【高级】--》【环境变量】--》
        【在第二个内容框中找到 变量名为Path 的一行,双击】 --> 【Python安装目录追加到变值值中,用 ; 分割】
        如:原来的值;C:\python27,切记前面有分号
    	
    Linux:
    	Centos6.x默认安装的是Python2.6,需要手动升级2.7或3.x。
    
    Mac:
    	无需安装,自带。
    
    三、Hello World程序,编辑之神的诅咒:
    print("Hello World!")
    
    指定解释器:
    #!/usr/bin/env python
    
    指定字符编码:
    # -*- coding: utf-8 -*-
    
    注释:
    当行注释:# 被注释内容
    多行注释:'''被注释内容'''
    
    变量:
    user = 'luchuan'
    passwd = 'luchuan123'
    #user,passwd = 'luchuan','luchuan123' 也可以这么写
    		
    常量:
    MYSQL_CONNNECTION = '192.168.1.1'
    #有一种特殊的变量叫常量,必须用大写,并且不允许直接修改得。
    
    四、用户输入及格式化字符串:
    
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # Author: Luchuan Gao
    
    name = input("input your name:")
    age = int(input("input your age:"))
    job = input("input your job:")
    
    msg = '''
    Information of user %s:
    ---------------------
    Name:   %s
    Age:    %d
    Job:    %s
    --------End-----------
    ''' % (name,name,age,job)
    print(msg)
    
    五:常用模块
    
    getpass:
    
    	import getpass
    	your_name = input("username:")
    	your_password = getpass.getpass("password:")
    	print(your_name,your_password)
    
    os:
    	import os
    	os.system('df -h')
    	#os.mkdir('luchuan')
    	cmd_res = os.popen("df -h").read()
    	print(cmd_res)
    
    sys:
    	import sys
    	print(sys.path)
    	#/usr/local/python35/lib/python3.5/site-packages 自己写模块
    
    	>>> import sys
    	>>> print(sys.path)
    	['', '/usr/local/python35/lib/python35.zip', '/usr/local/python35/lib/python3.5', '/usr/local/python35/lib/python3.5/plat-linux', '/usr/local/python35/lib/python3.5/lib-dynload', '/usr/local/python35/lib/python3.5/site-packages']
    	# 最前面的'',代表当前目录,优先于所有其他的环境变量。
    	
    tab模块:自己写
    	#!/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
    	#/usr/local/python35/lib/python3.5/site-packages ,放到这个目录里
    
    六、表达式if...else
    
    猜年龄游戏:
    
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # Author: Luchuan Gao
    
    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
            elif guess_num > age:
                print("Think smaller!")
            else:
                print("Think Big...")
        else:
            print("too many atempts ... bye")
            break
            
    #外层变量,可以被内层代码使用
    #内层变量,不应被外层代码使用
    
    七、表达式for循环:
    
    实现用户不断猜年龄,猜不对还可以再猜
    
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # Author: Luchuan Gao
    
    age = 22
    counter = 0
    for i in range(10):
        print('-->counter:',counter)
        if counter< 3:
            guess_num = int(input("input your guess num:"))
            if guess_num == age:
                print("Congratulations! you got it.")
                break
            elif guess_num > age:
                print("Think smaller!")
            else:
                print("Think Big...")
        else:
            continue_confirm = input("Do you want to continue because you are stupid:")
            if continue_confirm == 'y':
                counter = 0
                continue
            else:
                print("bye")
                break
        counter += 1
    
    	
    
    八、表达式while loop
    
    海枯石烂死循环,100次:
    
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # Author: Luchuan Gao
    
    count = 0
    while True:
        count += 1
        if count > 50 and count <60:
            continue
        print("你是风儿我是沙,缠缠绵绵到天涯...", count)
    
        if count == 100:
            print("去你的风和沙...")
            break


关键字