Python 入門小實例筆記

 

實例1:打印用戶輸入的姓名與手機號碼
知識點:編碼,獲取輸入,變量,標準輸出html

 1 #encoding=utf-8
 2 
 3 import time
 4 
 5 #1.提示用戶輸入信息
 6 
 7 name = input ("請輸入您的姓名:")
 8 tel = input("請輸入電話號碼:")
 9 #獲取輸入的值並輸出
10 
11 #模擬打印過程
12 count = 1
13 while (count<= 3):
14    print("名片正在打印中...%s"%count)
15    time.sleep(1)
16    count = count +1
17 
18 print("======Name Card==============")
19 print("Name:%s"%name)
20 print("tel:%s"%tel)
21 print("=============================")
View Code

知識詳解:
1. 涉及中文編碼,須要用utf-8編碼。
2. 獲取輸入,使用函數input()。
3. 輸出用print()。
4. 從鍵盤輸入的東西,都是一個字符串。 打印字符串用%s(打印整數用%d)。python


實例2:打印用戶輸入的姓名與手機號碼
知識點: 導入time內置庫,while循環判斷正則表達式

#encoding=utf-8

import time

#1.提示用戶輸入信息

name = input ("請輸入您的姓名:")
tel = input("請輸入電話號碼:")
#獲取輸入的值並輸出

#模擬打印過程
count = 1
while (count<= 3):
print("名片正在打印中...%s"%count)
time.sleep(1)
count = count +1

print("======Name Card==============")
print("Name:%s"%name)
print("tel:%s"%tel)
print("=============================")
View Code

----------------
知識詳解:
1. 導入庫用import
2. time.sleep()延遲
3. while循環輸出格式:
while 判斷條件:
執行語句小程序


實例2:小遊戲:剪刀,石頭,布
知識點: if循環判斷,隨機數randomapp

# encoding=utf-8
import random

#1.玩家輸入
player= input("請輸入剪刀0:,石頭:1,布2:")
print("你輸入的是:%s"%player)

#電腦的值
sys=random.randint(0,2)
print("電腦的是:%s"%sys)

#判斷結果

if (player==sys):
print("平局!")
elif (player<sys):
print("你輸了!")
else:
print("你贏了!")
View Code

知識詳解:
1. 隨機數用random庫
2. if循環輸出格式:
if 判斷條件:
執行語句
elif 判斷條件:
執行語句
else:
執行語句dom

 

實例3:聯繫人管理小程序
知識點:函數,函數調用,列表List,程序退出ide

#encoding=utf-8

import sys

# 菜單函數
def disIndex():
print("===============")
print("1. 添加聯繫人")
print("2. 刪除聯繫人")
print("3. 修改聯繫人")
print("4. 查詢聯繫人")
print("5. exit")

#獲取用戶輸入函數
def getInput():
choice=input("請輸入操做:")
return int(choice)

#定義聯繫人列表
namelist =[]

#程序入口,調用函數
while 1==1:
disIndex()

choice = getInput()

if (choice == 1):
print("你選擇了添加聯繫人")
newName=input("請輸入姓名:")
namelist.append(newName)
elif (choice == 2):
print("你選擇了刪除聯繫人")
i=input("請選擇要刪除的聯繫人:")
namelist.remove(i)
elif (choice==4):
print("你選擇了查看聯繫人")
i=len(namelist)
print("當前有聯繫人:%s"%i)
print(namelist)
else:
sys.exit()
View Code

知識詳解:
1. 函數
1) 函數定義:def 函數名():
2) 函數無關緊要返回值,返回值用:return 值
3)調用函數直接寫函數方法
2. 列表
1)建立列表: list = [] 空列表
2)列表添加新元素 list.append() 在list 末尾增長一個元素
3)打印列表print(list) 遍歷列表
4)刪除列表元素list.remove(xx) 刪除list 裏面的一個元素,有多個相同的元素,刪除第一個
3. 退出程序 sys庫的sys.exit()函數

 

實例4:爬蟲抓取指定網站的郵箱
知識點:urllib,正則表達式,網站

#coding=utf-8
import urllib
import re

def gethtml(url):#獲取網頁html
page=urllib.urlopen(url)
html=page.read()
return html

def save(html):#保存到本地
fhandle=open("./get.html","wb")
fhandle.write(html)
fhandle.close()

def getstr(html):#利用正則表達式抓取郵箱
r= r'[\w]+@[\w\.-]+'
fo=re.compile(r)
str1=fo.findall(html)
return str1

url='https://www.***.com' #輸入你要抓取的網站
print("1.開始爬取網頁...%s"%url)
html1=gethtml(url)
print("2.打開網頁完成...")
print("3.開始獲取郵箱...")
res=getstr(html1)
i=0
while i<len(res):
print(res[i])
i=i+1
print("4.獲取郵箱完成...")
View Code

 

知識詳解:
1. urllib是操做URL的模塊,爬蟲取網頁的常常用
1)python3中導入是import urllib.request,python2是import urllib
2)python3打開一個url: page=urllib.request.urlopen(url) 而python2是page=urllib.urlopen(url)
3) 讀取url內容:page.read()編碼

 

 

附:

添加python到系統環境變量

打開,控制面板\系統,選擇高級系統設置,環境變量,選擇Path,點擊編輯,新建,分別添加C:\Users\admin\AppData\Local\Programs\Python\Python37到環境變量。

相關文章
相關標籤/搜索