1. 程式人生 > >Python 教程2 編輯器及編寫簡單應用

Python 教程2 編輯器及編寫簡單應用

本文介紹簡單的Python的開發編輯器及簡單應用,程式碼部分可以直接複製執行即可

Pycharm使用方法

下載pycharm:http://www.jetbrains.com/pycharm/

破解Professional請自行百度,初學者學習使用Community即可

下載後安裝即可

pycharm設定

修改設定的字型,File - Setting - Editor - Font中,修改字型為Console

修改預設快捷鍵 setting - keymap

因人而異,本人經常使用Visual Studio進行開發,所以設定成與VS相同的快捷鍵,如沒有特殊需要不需要修改

幾個常用預設的快捷鍵

Ctrl+Shift+F10 運行當前的頁面

Ctrl + / 註釋(取消註釋)選擇的行

Ctrl + D 複製當前行、或者選擇的塊

Shift + Enter 開始新行

Ctrl+Shift+F 高階查詢

Alt + Enter 快速修正
Ctrl + Alt + L 程式碼格式化

簡單使用

在編輯器左邊Project面板,新建xxx.py檔案,在新出現的頁面中輸入

print("hello world")

編輯器右上角,點選執行按鈕    下方出現執行結果

編輯器右上角,點選結束按鈕 

,結束執行

練習:建立一個python檔案,複製下列程式碼,執行;執行後,輸入你想查詢的城市天氣(漢字,國內城市),回車檢視結果

import urllib.request

import tkinter
import json
import gzip


cityname = input('你想查詢的城市\n')

url = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(cityname)

weather_data = urllib.request.urlopen(url).read()
weather_data = gzip.decompress(weather_data).decode('utf-8')
weather_dict = json.loads(weather_data)
print(weather_dict)
if weather_dict.get('desc') == 'invilad-citykey':
    print(weather_dict)
elif weather_dict.get('desc') == 'OK':
    forecast = weather_dict.get('data').get('forecast')
    startoday = '城市:'+weather_dict.get('data').get('city') +'\n' \
                + '日期:' + forecast[0].get('date') + '\n' \
                + '溫度:' + weather_dict.get('data').get('wendu') + '℃\n' \
                + '高溫:' + forecast[0].get('high') + '℃\n' \
                + '低溫: ' + forecast[0].get('low') + '℃\n' \
                + '風向:' + forecast[0].get('fengxiang') + '\n' \
                + '風力:' + forecast[0].get('fengli') + '\n' \
                + '天氣:' + forecast[0].get('type') + '\n' \
                + '感冒:' + weather_dict.get('data').get('ganmao') + '\n'

    one_day = '日期:' + forecast[1].get('date') + '\n' \
              + '天氣:' + forecast[1].get('type') + '\n' \
              + '高溫:' + forecast[1].get('high') + '\n' \
              + '低溫:' + forecast[1].get('low') + '\n' \
              + '風向:' + forecast[1].get('fengxiang') + '\n' \
              + '風力:' + forecast[1].get('fengli') + '\n'

    two_day = '日期:' + forecast[2].get('date') + '\n' \
              + '天氣:' + forecast[2].get('type') + '\n' \
              + '高溫:' + forecast[2].get('high') + '\n' \
              + '低溫:' + forecast[2].get('low') + '\n' \
              + '風向:' + forecast[2].get('fengxiang') + '\n' \
              + '風力:' + forecast[2].get('fengli') + '\n'

    three_day = '日期:' + forecast[3].get('date') + '\n' \
                + '天氣:' + forecast[3].get('type') + '\n' \
                + '高溫:' + forecast[3].get('high') + '\n' \
                + '低溫:' + forecast[3].get('low') + '\n' \
                + '風向:' + forecast[3].get('fengxiang') + '\n' \
                + '風力:' + forecast[3].get('fengli') + '\n'

    four_day = '日期:' + forecast[4].get('date') + '\n' \
               + '天氣:' + forecast[4].get('type') + '\n' \
               + '高溫:' + forecast[4].get('high') + '\n' \
               + '低溫:' + forecast[4].get('low') + '\n' \
               + '風向:' + forecast[4].get('fengxiang') + '\n' \
               + '風力:' + forecast[4].get('fengli') + '\n'
print(one_day)
print(two_day)
print(three_day)
print(four_day)

結果如下:

下一篇: