標籤:檔案夾 put 文檔 3.x 官方 通過 學習筆記 拉丁字母 templates
聲明:本文只用作記錄自己的學習過程,請勿用於商業用途,部分內容引用自他人文章,如有侵權,聯絡刪除。
本周內容:
1、安裝python 3.5 和 PyCharm 社區版
2、第一個python程式
3、變數
4、字元編碼
5、使用者輸入
6、字串格式化輸出
7、if 、else 、elif
8、for 迴圈
9、while 迴圈
一、安裝python 3.5 和 PyCharm 社區版
python 3.5 : 連結:https://pan.baidu.com/s/1RzQmDc5H15XO26J1VfgYzA 密碼:su3q
PyCharm 可以自行去官網下載,初學者使用社區版即可
安裝python3.5 時一開始要記得勾選 “添加到環境變數” 。
安裝完PyCharm後 ,點擊File->setting->Project:Py_project->Project Interpreter 中設定需要的解譯器
二、第一個python程式
建立一個工程目錄: 依此選擇 File->New Project, 設定工程路徑和工程名後選擇建立。
建立python檔案:在建立的工程檔案夾上右鍵,選擇New -> Python File
設定檔案名稱,點擊ok,即可建立python檔案
檔案預設添加的內容是自己設定的,添加方法如下:
依此點擊setting -> Editor -> File and Code Templates -> Python Script , 在右側空白處添加上你需要的預設內容,第一行表示:檔案以 utf-8 格式儲存 ,第二行可以添加作者資訊等
列印“hello world” :
在python檔案中輸入 :
1 print("hello world")
註:基於python3.x 以上, 2.x版本有所不同。
雙引號括起來的內容是字串,也可以用單引號括起來,但是不可混用。
在python檔案中右鍵選擇 Run “helloworld” ,即可看到下方輸出視窗列印出來的“ hello world ” 。
至此第一個python程式編寫完成。
三、變數
變數的官方文檔定義是:
Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program.
聲明一個變數的格式:
變數名 = 變數內容
聲明變數的規則:
- 變數名只能是 字母、數字或底線的任意組合
- 變數名的第一個字元不能是數字,可以是底線
- python內建的關鍵字不可以作為變數名,如 and 、 as 、break 等等
- python 可以將中文作為變數名,但是不建議這樣做
變數的賦值:
用 “ = ” 給變數賦值
1 name = "MR"2 Time_hour = 60
四、字元編碼
python解譯器在載入.py 檔案中的代碼時,會對檔案中的內容進行編碼(預設為ACSCII 碼)
ASCII(American Standard Code for Information Interchange,美國標準資訊交換代碼)是基於拉丁字母的一套電腦編碼系統,主要用於顯示現代英語和其他西歐語言,其最多隻能用 8 位來表示(一個位元組),即:2**8 = 256-1,所以,ASCII碼最多隻能表示 255 個符號。
ASCII碼:ASCII(American Standard Code for Information Interchange,美國標準資訊交換代碼)是基於拉丁字母的一套電腦編碼系統,主要用於顯示現代英語和其他西歐語言,其最多隻能用 8 位來表示(一個位元組),即:2**8 = 256-1,所以,ASCII碼最多隻能表示 255 個符號。
由於ASCII 碼無法支援世界上所有的文字和符號,而python 預設使用ASCII 碼,所以在代碼中包含中文的時候,需要在最初 添加上一條語句
# -*- coding:utf-8 -*-
來告訴python 解譯器,應該用 utf -8 編碼來執行原始碼
五、使用者輸入
1、普通輸入
1 name = input ("Please input your name:")2 print(name)
2、輸入密碼時,如果想要不可見,則需要使用getpass 模組中 的getpass 方法:
1 import getpass2 password = getpass.getpass("Please input the password:")3 print(password)
六、字串格式化輸出
1 name = "MR"2 print ("I am %s " % name)3 #輸出 : I am MR
解釋: %s 可以用於大部分地方,當一條語句有多個地方需要進行格式化的時候,注意格式化的前後順序
1 name = input("name:") 2 age = input ("age:") 3 job = input ("job:") 4 5 info =""" 6 Name :{_name} 7 Age: {_age} 8 Job :{_job} 9 10 """ .format(_name=name,_age = age, _job = job)11 12 print (info)
解釋: 使用 .format() 的方法進行格式化時,只要將 { } 中的臨時變數名 和 真正的變數名對應起來即可, 臨時變數名可以是任意內容,比如數字。
註: # 表示單行注釋
三對引號括起來的內容既可以表示多行注釋,也可以用作多行字串。
七、if 、else 、elif
1、if、else
1 username = input("username:") 2 password = input("password:") 3 if ((username == "MR" or "mr" )and password =="123"): 4 { 5 print("恭喜通過") 6 } 7 8 else: 9 {10 print("使用者名稱或密碼錯誤")11 }
解釋: 使用者名稱不區分大小寫,當條件滿足if語句時,只執行if 語句下的內容, 否則執行 else。
如果含有elif, 則elif 和 if 是並列關係,只執行條件滿足的語句。
注意: 冒號不能少
八、for 迴圈
1、for + range() :
range() 用於產生一組數,格式是 range (a,b[,c]) :表示產生一組從a -> b 的數(不包括b),a、c為選擇性參數,a是起點,預設為0,c是步長,預設為1
1 for i in range(10):2 print (i)3 #依次列印0-9
#列印內容:
0123456789
2、for + if
1 for i in range(0,10):2 if i < 3 :3 {4 print("loop", i)5 }6 else:7 continue8 print("hehe...")
九、while 迴圈
1、while (True):
無限迴圈
1 count = 02 while True:3 print("你是風兒我是沙,纏纏綿綿到天涯...",count)4 count +=1
2、while (條件):
滿足條件時一直迴圈
執行個體:讓使用者猜年齡,三次猜錯,讓使用者選擇是否繼續
1 age_of_oldboy = 56 2 x= 3 3 while x > 0: 4 guess_age = int(input("input age:")) 5 if (guess_age == age_of_oldboy): 6 print("yes,you got it") 7 break 8 elif (guess_age > age_of_oldboy): 9 print("think smaller ...")10 else:11 print("think bigger ...")12 x -= 113 if x ==0:14 game_continue = input("Do you want continue? Y/N")15 if game_continue == "Y" or game_continue =="y" :16 x = 317 else:18 x=0
Python 3.5學習筆記(第一周)