美文网首页
Python之旅

Python之旅

作者: KeepFighting | 来源:发表于2017-01-11 14:15 被阅读117次

    参考 http://www.ituring.com.cn/book/1863

    • python追求的是最后的解决方案,相比其他语言是追求多种方案
    • 不需要管理内存之类的细节
    • 有丰富的库
    • 使用Python搭建网站,后台服务比较容易维护,新增功能也容易实现---YouTube,知乎,豆瓣
    • 自动化运维:强大的开发工具和完整的工具链,批量处理任务
    • 数据分析师:快速开发的特性让你迅速验证你的想法
    • 游戏开发者:热更新
    • 自动化特性:Python是目前比较流行的script
    • 网站开发:丰富的django,flask框架,设计模板bootstrap,能适配移动端
    • 爬虫或处理大量信息
    • 包装其他语言的程序:混合编译其他语言的库

    下载参考这里
    廖雪峰
    python下载
    IDE下载Pycharm
    Pycharm快速上手视频
    官方文档

    #coding:utf-8
    
    what_he_dose = ' plays '
    his_instrument = 'guitar'
    his_name = 'Robert Johnson 帅哥'
    artist_intro = his_name + what_he_dose +his_instrument
    
    print(artist_intro)
    print(type(artist_intro))
    
    num = 1
    string = '2'
    num2 = int(string)
    print(num+num2)
    word = ('words' * 3 +' ') * 2
    print(word)
    
    word = 'a loooong word'
    num = 12
    string = 'bang! '
    total = string * (len(word) - 12)
    print(total)
    
    name = 'My name is Tank'
    
    print(name[0])
    print(name[-4]) #倒数第四个
    print(name[11:15])
    print(name[1:])
    print(name[:6])
    
    # \ 表示换行 直接敲回车
    word = 'frass' \
           'sdsdsd' \
           'sdsdssd' \
           'sdsddsdsds'
    
    
    url = 'https://www.baidu.com/sdsddfsgraeee244dadsad.jpg'
    file_name = url[-10:]
    print(file_name)
    
    phone_number = '15077123276'
    hiding_number = phone_number.replace(phone_number[:7],"*" * 9)
    print(hiding_number)
    
    search = '168'
    num_a = '1386-168-0006'
    num_b = '1681-222-0006'
    print(search + ' is at ' + str(num_a.find(search)) + ' to ' + str(num_a.find(search) + len(search)) + ' of num_a')
    
    print('{} a word she can get what she {} for'.format('came','with'))
    
    print('{prepostation} a word she can get what she {sufixx} for'.format(prepostation ='came',sufixx = 'with'))
    city = input("write down the name 偶发city \n")
    url = "http://www.baidu.com/city={}".format(city)
    
    print(url)
    
    

    计算词频

    import  string
    
    path = '/Users/JayLee/Desktop/screenshort/Walden.txt'
    
    with open(path) as text:
        words = [ raw_word.strip(string.punctuation).lower() for raw_word in text.read().split()] #去除标点符号
        words_index = set(words) #去重
        couts_dict = {index:words.count(index) for index in words_index} #单词为key 出现的个数为value
        # print(couts_dict)
    for word in sorted(couts_dict,key = lambda x: couts_dict[x],reverse=True): #出现的个数来做排序
        print('{} -- {}times'.format(word,couts_dict[word]))
    
    • class定义

    缩进很重要,已缩进来表示语句的结束

    class Car:
        formula = ['wheel','enqine','glasss']
        def drive(self):
            print('wowowowow~~@!')
    
    
    
    my_car = Car()
    
    # print(my_car.formula)
    #
    # for elemnt in my_car.formula:
    #     print(elemnt)
    
    
    my_car.local_log = 'LOMO' #动态创建属性
    print(my_car.local_log)
    
    my_car.drive()
    

    随机生成姓名

    考察 yield 的用法

    
    ln_path = '/Users/JayLee/Desktop/screenshort/last_name.txt'
    fn_path = '/Users/JayLee/Desktop/screenshort/first_name.txt'
    
    fn = []
    ln1 = []
    ln2 = []
    
    with open(fn_path,'r') as f:
        for line in f.readlines():
            fn.append(line.split('\n') [0]) #过滤\n
    # print(fn)
    
    with open(ln_path,'r') as f:
        for line in f.readlines():
            if len(line.split('\n')[0]) == 1:
                ln1.append(line.split('\n')[0]) #单个字
            else:
                ln2.append(line.split('\n')[0]) #多个字
    
    # print(ln1)
    # print('=' * 70)
    # print(ln2)
    
    fn = tuple(fn) #元组比列表节省内存
    ln1 = tuple(ln1)
    ln2 = tuple(ln2)
    # print(fn)
    
    import random
    
    class FakeUser:
        def fake_name(self,amount = 1,one_word =False,two_word =False):
            n = 0
            while n <= amount:
                if one_word:
                    full_name = random.choice(fn) + random.choice(ln1)
                elif two_word:
                    full_name = random.choice(fn) + random.choice(ln2)
                else:
                    full_name = random.choice(fn) + random.choice(ln1+ln2)
                 # print('full_name: '+full_name)
                yield full_name
                n +=1
    
        def fake_gener(self,amount = 1):
            n=0
            while n <=amount:
                gender = random.choice(['man','woman','unkown'])
                yield gender
                n +=1
    
    class SnsUser(FakeUser):
        def get_followers(self,amount = 1,few = False,a_lot = True):
            n = 0
            while n <= amount:
                if few:
                    followers = random.randrange(1,50)
                elif a_lot:
                    followers = random.randrange(200,10000)
                yield followers
                n+=1
    
    user_a = FakeUser()
    user_b = SnsUser()
    
    for name in user_a.fake_name(30):
        print(name)
    
    for gender in user_b.fake_gener(20):
        print(gender)
    # user_a.fake_name(True)
    # user_b.get_followers(few=True)
    
    

    丰富的三方库

    awesome-python
    中文翻译官方文档
    老齐github教程
    www.runoob.com


    三方库的安装教程

    http://study.163.com/course/courseLearn.htm?courseId=1002794001#/learn/text?lessonId=1003186006&courseId=1002794001

    相关文章

      网友评论

          本文标题:Python之旅

          本文链接:https://www.haomeiwen.com/subject/wdkrbttx.html