python筆記22-常用模組

來源:互聯網
上載者:User

標籤:環境   單引號   print   函數   pre   pat   imp   執行   any   

模組就是一個python檔案,用哪個模組就要import哪個模組

1.調用模組
# import model #import的本質就是把這個python從頭到尾執行一遍
#
# model.run1()#調用model模組下的run1函數
# model.run2()
# model.run()

2.調用模組下函數
# from model import run,run1 (方法1)
# #只匯入某個函數的時候,只匯入run,run1函數
# run()
# run1()

# from model import *
# from model2 import *
# #這種你不要用,從一個模組裡面匯入所有的函數
# run()
# run1()
# run2()
# from model import name (方法2)


3.在其他檔案目錄下調用
# from day4.set1 import hhh 在day4目錄下的set1檔案
# hhh()

4.環境變數目錄
環境變數就是用來讓你在任意一個目錄都可以使用這個命令

#匯入模組的時候,python首先在目前的目錄下去找這個模組,如果在當前
#目錄下沒有找到這個檔案的話,那麼就去環境變數裡面的目錄找

# import sys
# print(sys.path)#看系統內容變數

#這個是在其他目錄下的話
# sys.path.append(r‘E:\byz_code‘) #添加環境變數目錄
# from day4.set1 import hhh
# hhh()

5.random模組-隨機數
import random

# print(random.random()) # 隨機浮點數,預設取0-1,不能指定範圍
# print(random.randint(1, 20)) # 隨機整數
# print(random.randrange(1, 20)) # 隨機產生一個range
# print(random.choice([1,2,3,4,5])) # 隨機取一個元素
# print(random.sample([1,2,3,4,‘6‘], 3)) # 從序列中隨機取幾個元素,返回的是一個list
# print(random.uniform(1, 88888)) # 隨機取浮點數,可以指定範圍
x = [1, 2, 3, 4, 5, 6]
random.shuffle(x) # 洗牌,打亂順序,會改變原list的值

import string
# print(string.ascii_letters + string.digits) # 所有的數字和字母


import json
#json和python的字典類型,但是json裡面只能是雙引號,不能是單引號
#json串實際上就是一個字串
json_str = """
{
"username": "niuhanyang",
"passwd": 123456,
"flag": true
}
"""
# fr = open(‘users‘)
# json_dic = json.loads(json_str)
# #json串(字串)轉成字典,loads方法是把json轉成字典
# json_dic_file = json.load(fr)
# #json串(字串)轉成字典,load方法是傳入一個檔案對象,然後load方法自動去讀這個檔案的內容,然後轉成字典
# print(json_dic_file)
# print(type(json_dic_file))

d = {
"hhh": {
"price": "90000",
"password": "123456"
},
"admin": {
"password": "123456",
"money": 8000
}
}

# fw = open(‘users.json‘,‘w‘)
# dic_str = json.dumps(d)
# #把字典變成json串(字串)
# json.dump(d,fw)
# print(dic_str)
# print(type(dic_str))


#json可以用字典和list
#dump和dumps字典轉json串的
#load和loads是json串轉字典
#帶s就和字串沾邊,不帶s和檔案對象沾邊

 

python筆記22-常用模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.