Python就業班——Python函數與模塊

#!/usr/bin/env python3
# coding=utf-8
# Version:python3.6.1
import random
from functools import reduce

from datetime import datetime

import os

__date__ = '2020/6/26 23:48'
__author__ = 'LGSP_Harold'


# 序列傳參
# def calc(a, b, c):
#     return (a + b) * c
#
#
# lst = [1, 5, 10]
# print(calc(*lst))

# ————————————————————————

# 字典傳參
# def person(name, age, height):
#     print('{},{},{}'.format(name, age, height))
#
#
# param = {'name': 'Harold', 'age': 18, 'height': 180}
# person(**param)

# ————————————————————————

# 返回值包含多個數據

# def get_detail_info():
#     dict1 = {
#         'employee': [
#             {'name': 'Jim', 'age': 18},
#             {'name': 'Leslie', 'age': 18}
#         ],
#         'device': [
#             {'id': 999, 'title': '筆記本'},
#             {'id': 9999, 'title': '臺式機'}
#         ],
#         'asset': [{},{}],
#         'project': [{}, {}],
#     }
#     return dict1
#
#
# print(get_detail_info())
# d = get_detail_info()
# print(type(d))
# name = d.get('employee')[1].get('name')
# print(name)

# ————————————————————————

# 包的引用
# 引入整個包:import module
# 只引入所需要的屬性和方法:from module.xx.xx import xx
# 指定別名:from module.xx.xx import xx as rename
# 引入所有:from module.xx.xx import *

# __init__.py:一般爲空,可以批量導入所需的模塊

# ————————————————————————

# os模塊
# 創建多級目錄
# import os
# f = os.path.join('.//imooc', 'i', 'love', 'you')
# os.makedirs(f)

# 獲取目錄下所有文件
# filePath = ".\\"
# dirList = os.listdir(filePath)
# print(dirList)

# ————————————————————————

# 虛擬環境

# virtualenv
# 安裝:pip install virtualenv
# 創建
#     新建虛擬環境目錄:mkdir envs
#     進入目錄:cd envs
#     創建所需的環境(如:django1.11)
#         創建一個虛擬環境(Python):virtualenv django1.11
#         進入Scripts:cd django1.11/Scripts
#         進入虛擬環境:activate
#         安裝所需的環境:pip install django==1.11
#         測試
#             python
#             import django
#             quit()
#         退出:deactivate.bat
# 多個虛擬環境切換
#     安裝virtualenvwrapper:pip install virtualenvwrapper-win
#     設置環境變量:
#         變量名:WORKON_HOME
#         變量值:D:\ProgramFiles\Development\PythonEnvs\PythonVirtualenv
#     使用(在django虛擬環境中):workon 虛擬環境名稱
#         如:workon flask


# pipenv
# 安裝:pip install pipenv
# 安裝需要的python版本
# 創建
#     新建虛擬環境目錄:mkdir py27
#     進入目錄:cd py27
#     瞭解pipenv:pipenv -h
#     創建所需的Python環境:pipenv --python 2.7
#     進入虛擬環境:pipenv shell
#         安裝所需的環境(需要在前面新創建的目錄【py27】內執行):pipenv install flask
#         測試
#             python
#             import flask
#             quit()
#         退出:exit

# ————————————————————————

# # 函數進階
# # lambda函數
# def f(n):
#     return n % 2 != 0
#
#
# # filter函數:函數用於過濾序列,過濾掉不符合條件的元素,返回一個迭代器對象,如果要轉換爲列表,可以使用 list() 來轉換。該接收兩個參數,第一個爲函數,第二個爲序列,序列的每個元素作爲參數傳遞給函數進行判,然後返回 True 或 False,最後將返回 True 的元素放到新列表中。
# def use_filer(l):
#     """
#     獲取指定列表/元組中的奇數
#     :param l: list/tuple要過濾的數據
#     :return: 過濾好的奇數列表
#     """
#     # rest = filter(f, l)
#     rest = filter(lambda n: n % 2 != 0, l)
#     return rest
#
#
# # map函數:第一個參數 function 以參數序列中的每一個元素調用 function 函數,返回包含每次 function 函數返回值的新列表。
# def pow_number(l):
#     """
#     根據給定的列表數據,計算裏面每一項的立方
#     :param l: list/type int類型的列表或者是元組
#     :return:原來列表中每一項的立方
#     """
#     rest_list = []
#     for x in l:
#         rest_list.append(x * x * x)
#     return rest_list
#
#
# def map_f(n):
#     return n * n * n
#
#
# def pow_num_use_map(l):
#     """
#     使用map函數計算給定列表的每一項立方
#     :param l: list/type int類型的列表或者是元組
#     :return:原來列表中每一項的立方
#     """
#     # return map(map_f, l)
#     # return map(lambda n: n * n * n, l)
#     return map(lambda n: pow(n, 3), l)
#
#
# # reduce函數:函數將一個數據集合(鏈表,元組等)中的所有數據進行下列操作:用傳給 reduce 中的函數 function(有兩個參數)先對集合中的第 1、2 個元素進行操作,得到的結果再與第三個數據用 function 函數運算,最後得到一個結果。
# def get_sum(l):
#     rest = 0
#     for i in l:
#         rest += i
#     return rest
#
#
# def get_sum_use_py(l):
#     return sum(l)
#
#
# def reduce_f(m, n):
#     return m + n
#
#
# def get_sum_use_reduce(l):
#     return reduce(reduce_f, l)
#
#
# def get_sum_use_reduce_lambda(l):
#     return reduce(lambda m, n: m + n, l)


# 文件讀寫
# f = open('file.txt')
# f.close()
#
# with open('file.txt') as f:
#     pass

# read()
# def read_file():
#     file_name = 'test.txt'
#     file_path1 = 'D:\\ProgramFiles\\Development\\PycharmProjects\\practice\\test.txt'
#     file_path2 = 'D:/ProgramFiles/Development/PycharmProjects/practice/test.txt'
#
#     f = open(file_name, encoding='utf-8')
#     # result = f.read()
#     # result = f.read(8)
#     # print(result)
#     # print(f.read(9))
#     # f.seek(20)
#     # print(f.read(9))
#
#     # print(f.readline(9))
#     # print(f.readline(9))
#     # print(f.readline())
#
#     # rest = f.readlines()
#     # print(rest)
#     # print(len(rest))
#     # for i in rest:
#     #     print(i)
#
#     # rest = f.readlines(19) # 參數爲字符數
#     # for i in rest:
#     #     print(i)
#
#     f.close()
#
#     with open(file_path1, encoding='utf-8') as f:
#         pass

# 文件寫入
# def test_write_file():
#     file_name = 'write_test.txt'
#     with open(file_name, 'w', encoding='utf-8') as f:
#         f.write('hello')
#         f.write('\n')
#         f.write('world')

# def write_mult_line():
#     file_name = 'write_mult_line.txt'
#     with open(file_name, 'w', encoding='utf-8') as f:
#         l = ['第1行', '\n', '第2行', '\r\n', '第3行', '\r', '第4行']
#         f.writelines(l)

# 登錄日誌
# def write_user_log():
#     file_name = 'user_log.txt'
#     rest = 'id:{0}-訪問時間:{1}'.format(random.randint(1000, 9999), datetime.now())
#     print(rest)
#     with open(file_name, 'a', encoding='utf-8') as f:
#         f.write(rest + '\n')


# 先讀後寫
# def read_and_write():
#     file_name = 'read_and_write.txt'
#     with open(file_name, 'r+', encoding='utf-8') as f:
#         read_rest = f.read()
#         if '1' in read_rest:
#             f.write('ccc\n')
#         else:
#             f.write('aaa\n')


if __name__ == '__main__':
    # # filter函數
    # filter_list = [1, 2, 3, 4, 5, 6]
    # rest = use_filer(filter_list)
    # print(list(rest))
    #
    # # map函數
    # map_list = [1, 2, 3, 4, 5, 6]
    # rest = pow_number(map_list)
    # print(rest)
    # # rest_map = pow_num_use_map(map_list)
    # # print(list(rest_map))
    # rest_map_lambda = pow_num_use_map(map_list)
    # print(list(rest_map_lambda))
    # # reduce函數
    # reduce_list = [1, 2, 3, 4]
    # print(get_sum(reduce_list))
    # print(get_sum_use_py(reduce_list))
    # print(get_sum_use_reduce(reduce_list))
    # print(get_sum_use_reduce_lambda(reduce_list))
    # read_file()
    # test_write_file()
    # write_mult_line()
    # write_user_log()
    # read_and_write()
    pass

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章