python學習day1

 


今日內容:
1. Python介紹

2. Python版本

3. Hello World

4. 編碼

5. 輸入輸出

6. 變量

7. 初始數據類型

8. 條件語句

9. 循環語句

10. 經常使用數據類型


內容詳細:
1. Python介紹

- 爲何寫代碼?百錢買百雞
100文且100只
5文-g
3文-m
1/3文-x

找程序員開發一個軟件
# 問題:公雞5文錢一隻,母雞3文錢一隻,小雞3只一文錢,用100文錢買100只雞,其中公雞,母雞,小雞都必需要有,問公雞,母雞,小雞要買多少隻恰好湊足100文錢?
"""
5*公雞 < 100
3*母雞 < 100
1*小雞 < 300
公雞+母雞+小雞 = 1000
"""
for g in range(1, 21):
for m in range(1, 34):
for x in range(1, 301):
score = g * 5 + m * 3 + x / 3
if score == 100 and g + m + x == 100:
print('公雞 %s 只,母雞 %s 只,小雞 %s 只' % (g, m, x))

目的解決生活中的問題。

- 編譯器解釋器
安裝Python解釋器:+ 編寫解釋器認識的代碼

安裝Java解釋器:+ 編寫解釋器認識的代碼

安裝PHP解釋器:+ 編寫解釋器認識的代碼

- 編程語言?
C
Python
Java
PHP
C#

- c語言和其餘
- 機器碼:C
- 字節碼: 其餘

- 編譯型和解釋性
- Python、PHP
- C#/Java/C

- 難易程度
- C
- C#、Java
- PHP
- Python(類庫齊全/模塊)

總結:
1. 安裝解釋器
2. 學習語言規則
3. 編寫代碼
4. 解釋器運行(解釋)

- 安裝解釋器
- cpython解釋器(*)
- jpython解釋器
- ironPython解釋器
- rubyPyhton解釋器
.....
- pypy解釋器

2. Python解釋器版本:cpython解釋器
- Python2.7
- Python3.6

環境變量:
Python配置:
;C:\Python36;
終端:python
pip配置:
;C:\Python36\Scripts
終端:pip3 install xxxx

;C:\Python36;C:\Python36\Scripts;python


3. 編寫程序
建立任意文件.py
print('hello world')

4. 編碼
ascii:用1個字節=8位來表示計算機能表達的全部東西。
2**8 = 256
00000000 -> A
00000001 -> B
00000010 -> C
00000011
00000100
unicode: 萬國碼,用4個字節=32位來作對應關係
2**32 = 4294967296
00000000 00000000 00000000 00000000 -> A
00000000 00000000 00000000 00000001 -> B
00000000 00000000 00000000 00000010 -> C
00000000 10000000 00010000 00011010 -> 紫
utf-8: 對萬國碼進行壓縮,至少使用1個字節表示
00000000
00000001
00000010
10000000 00010000 00011010
PS: 中文3個字節=24位

gbk:對亞洲國家的文字作的對應關係
PS: 中文2個字節=16位


現象:
py2: 解釋器默認編碼ascii
# -*- coding:utf-8 -*- 解釋器默認編碼utf-8
print('王紫薇')

py3:解釋器默認編碼utf-8
print('要睡覺')


py2/py3
# -*- coding:gbk -*-
print('要睡覺')

5. IDE
- pycharm
- vim

使用:
1. 啓動Pycharm:選擇已存在的解釋器

2. 運行

3. 配置
- 文字大小
- 模板程序員

6. 輸入輸出
輸出:
print("你是風兒我是沙")
輸入:
user = input("請輸入用戶名:")

密碼加密:
import getpass

pwd = getpass.getpass("請輸入密碼:")
7. 變量

格式: 變量名 = 值
規範:
a. 數字、字母、下劃線
b. 不能以數字開頭
c. 不能使用Python的關鍵字
建議:見名知意; user_pwd = "xxx"


注意:
示例一:
name = 'alex'
user = 'alex'

示例二:
name = 'alex'
user = name

8. 數據類型

age = 18 # 整數類型
name = "alex" # 字符串類型

xx = "18"
xx = '18'
xx = '''18'''
xx = """18"""
xx = "123'
9. 條件語句

格式一:
if 條件:
成功以後走這裏

格式二:
if 條件:
成功以後走這裏
else:
失敗以後走這裏編程

格式三:
if 條件:
成功以後走這裏
elif 條件:
成功以後走這裏
elif 條件:
成功以後走這裏
else:
上述都失敗

練習題: 10086提醒
msg = """
歡迎致電10086
1. 查詢話費
2. 查水錶
3. 人工服務
"""

print(msg)

choice = input('請選擇您須要服務')
if choice == "1":
print("1.查詢本機;2.查詢他人手機;3.查詢座機")
search_type = input('請輸入要查詢的類型:')
if search_type == '1':
print('查詢本機')
elif search_type == '2':
print('查詢他人手機')
elif search_type == '3':
print('查詢座機')
else:
print('查詢類型輸入錯誤')
elif choice == "2":
print("查水錶")
elif choice == "3":
print("人工服務")
else:
print("輸入錯誤")vim

問題青年:
錯誤:
print('歡迎登錄')
user = input('請輸入用戶:')
糾正:
print('歡迎登錄')
user = input('請輸入用戶:')

姑娘:
錯誤:
msg = """
歡迎致電10086
1.查詢話費
2.查水錶
3.人工服務ruby

"""
print(msg)編程語言

choice = input("請選擇您須要的服務")
if choice == "1":
print("1.查詢本機;2.查詢他人手機;3.查詢座機")
search_type = input("請輸入要查詢的類型:")
if search_type =='1':
print('查詢本機')
elif search_type == input('2'):
print('查詢他人手機')
elif search_type == input('3'):
print('查詢座機')
else:
print('查詢類型輸入錯誤')
elif choice == input("2"):
print("查水錶")
elif choice == input("3"):
print("人工服務")
else:
print("輸入錯誤")

糾正: 學習

msg = """
歡迎致電10086
1.查詢話費
2.查水錶
3.人工服務編碼

"""
print(msg)加密

choice = input("請選擇您須要的服務")
if choice == "1":
print("1.查詢本機;2.查詢他人手機;3.查詢座機")
search_type = input("請輸入要查詢的類型:")
if search_type == '1':
print('查詢本機')
elif search_type == '2':
print('查詢他人手機')
elif search_type == '3':
print('查詢座機')
else:
print('查詢類型輸入錯誤')
elif choice == "2":
print("查水錶")
elif choice == "3":
print("人工服務")
else:
print("輸入錯誤")code


10. 循環語句

while 條件:
條件成立執行


while True:
print('釣魚要釣刀魚,刀魚要到島上釣')


while 1==1 and 2==2:
print('釣魚要釣刀魚,刀魚要到島上釣')

timer = 0
while timer < 3:
print('釣魚要釣刀魚,刀魚要到島上釣')
timer = timer + 1

print('完成')


練習1: 頁面上輸出 1 - 10

count = 1
while count < 11:
print(count)
count = count + 1



- break,強制終止當前所在循環
while True:
print('釣魚要釣刀魚,刀魚要到島上釣')
break

練習2: 頁面上輸出 1 - 10 (使用break)
count = 1
while True:
print(count)
count = count + 1
if count == 11:
break

count = 1
while True:
print(count)
if count == 10:
break
count = count + 1

- continue,跳出本次循環,繼續下一次循環。



練習題
1. 頁面上輸出 1 - 10

count = 1
while count < 11:
print(count)
count = count + 1

count = 1
while True:
print(count)
count = count + 1
if count == 11:
break

count = 1
while True:
print(count)
if count == 10:
break
count = count + 1
2. 頁面上輸出 1 - 10,排除7
count = 1
while count < 11:
if count == 7:
count = count + 1
continue
print(count)
count = count + 1


count = 1
while count < 11:
if count == 7:
pass
else:
print(count)
count = count + 1

其餘練習題:
三、求1-100的全部數的和

sum = 0

count = 1
while count < 101:
sum = sum + count
count = count + 1

四、輸出 1-100 內的全部奇數
n=3
div = n%2

五、輸出 1-100 內的全部偶數

六、求1-2+3-4+5 ... 99的全部數的和




11. 經常使用數據類型

整數:
age = 18
字符串:
name = "紫薇"
# 獲取紫
n1 = name[0]
n2 = name[1]
列表:
user_list = ["紫薇","爾康","18","海量","小雞"]
n3 = user_list[0]
n4 = user_list[1] # "爾康"

user_list = ["紫薇","爾康","18","海量","小雞"]

for xxx in user_list: print(xxx) if xxx == '18': break 字典: user_info = {"name":"紫薇","age":18} n5 = user_info["name"] n6 = user_info["age"] user_info['count'] = 666 # {"name":"紫薇","age":18,"count":666} 數據類型嵌套: n7 = ["alex","eric",[11,22,33]] n7[1] n7[2][1] n8 = [ "alex", {'name':'日天','age':18}, [11,22,33] ] n8[1]["age"] = 19 問題: 循環用戶列表,打印用戶姓名和密碼 user_list = [ {'username':'alex', 'password':'123', 'count':0 }, {'username':'eric', 'password':'123', 'count':0 }, {'username':'tony', 'password':'123', 'count':0 }, {'username':'oldboy', 'password':'123', 'count':0 }, ] for item in user_list: print(item['username'],item['password'],item['count']) 問題: 將每一個用戶的count改爲1 user_list = [ {'username':'alex', 'password':'123', 'count':0 }, {'username':'eric', 'password':'123', 'count':0 }, {'username':'tony', 'password':'123', 'count':0 }, {'username':'oldboy', 'password':'123', 'count':0 }, ] for item in user_list: if item['username'] == 'alex' and item['password'] == '123': item['count'] = 1 print(item) 問題: 用戶輸入用戶名和密碼,在user_list中進行校驗 user_list = [ {'username':'alex', 'password':'123' }, {'username':'eric', 'password':'123'}, {'username':'tony', 'password':'123'}, {'username':'oldboy', 'password':'123'}, ] user = input("請輸入用戶名:") pwd = input("請輸入密碼:") flag = False for item in user_list: if item['username'] == user and item['password'] == pwd: flag = True break else: pass if flag: print("登陸成功") else: print("登陸失敗") 內容梳理: 1. 編程語言 - c和其餘 - 解釋性和編譯型 - 難以程度 2. 解釋器 - cpython 3. 語法規則 - 頭 - #!/usr/bin/env python - # -*- coding:utf-8 -*- (py2中若是代碼中有中文,必定要找個頭) - 編碼 - ascii - unicode - utf-8 - gbk PS: 一個字節 = 8位 00000000 - 輸入輸出 - input - row_input (py2) - print - 變量 - 規範: - 字母、數字、下劃線 - 數字不能開頭 - 不能是內置關鍵字 建議:見名知意 - 條件語句 if 條件: pass else: pass if 條件: pass elif 條件: pass - 循環語句: while 條件: pass 關鍵字: break continue while True: print(1) while True: print(2) break 注意:縮進 - 數據類型 - 整型 age = 19 - 字符串 name = "xxxx" - 列表 names = ["xx","xx","xx"] - 字典 info = { "name":'alex', # 鍵值對 "age":'18' }

相關文章
相關標籤/搜索