知識內容:
1.python輸出及注釋
2.變量及常量
3.python用戶輸入
4.Hello,World程序
一、python輸出及注釋
1.python輸出
在python2和python3中的輸出均依靠print來實現,不過區別是python2中print為語句而在python3中print為內置函數
python3中的print原型:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
python輸出:
1 print("Hello, Python!") # 輸出: Hello, Python!
python中的參數:
value是輸出的值;sep是輸出值之間的間距,默認為一個空格; end是一行后的最后輸出,默認為\n,也就是說python的輸出語句默認輸出完后就換行;
file將文本輸入到file-like對象中,可以是文件,數據流等等,默認是sys.stdout;flush值為True或者False,表示是否立刻將輸出語句輸入到
參數file指向的對象中(默認是sys.stdout),默認為Flase即只要將文件關閉文件中才有輸入的數據,如果為true表示就算沒有關閉文件數據也將被寫入文件
python的print參數示例:
1 print("1", "2", "3", sep='sss') 2 print("Hello", end=', ') 3 print("Python", end=' This is end!') 4 5 # 輸出結果: 6 # 1sss2sss3 7 # Hello, Python This is end!
2.python中的注釋
在程序開發中,一個項目多是由幾個或幾十個開發者一起做,你要調用別人寫的代碼,別人也要用你的,如果代碼不加注釋,自己都看不懂更不用說別人了,為了避免這種情況的發生,在程序中一定要求適當的注釋
單行注釋: # 被注釋的內容
多行注釋: ''' 被注釋的內容 '''
代碼注釋原則: 不用全部給代碼加上注釋,只需要在自己覺得重要或者不好理解的地方加上注釋即可,注釋可以用英文或中文,但是絕對不可以用拼音!
1 # bulitins.py中的print函數 2 def print(self, *args, sep=' ', end='\n', file=None): # known special case of print 3 """ 4 print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) 5 6 Prints the values to a stream, or to sys.stdout by default. 7 Optional keyword arguments: 8 file: a file-like object (stream); defaults to the current sys.stdout. 9 sep: string inserted between values, default a space. 10 end: string appended after the last value, default a newline. 11 flush: whether to forcibly flush the stream. 12 """ 13 pass
二、變量及常量
1.變量的定義
官方解釋:
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中不需要像C/C++那樣提前聲明變量名及其類型,直接賦值即可創建各種類型的變量
1 x = 0 # 創建一個變量變量名為x並賦值為0 2 x = "python" # 創建一個變量變量名為x並賦值為python
2. 變量命名的規則
(1)變量名只能是 字母、數字或下划線的任意組合
(2)變量名的第一個字符不能是數字,只能以字母或下划線開頭,但是最好命名變量時不要以下划線開頭,因為以下划線開頭的變量在Python中有特殊含義
(3)以下關鍵字不能聲明為變量名:
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
(4)不建議使用系統內置的模塊名、類型名或函數名以及已經導入的模塊名和其中的成員名來作為變量名,這樣可能會導致一些錯誤
(5)變量名區分英文字母的大小寫,例如username和Username是兩個不同的變量
(6)變量命名的方法有駝峰法和下划線分割法,如下所示
駝峰法: StudentName、FirstDateTime
下划線分割法: student_name、First_Date_Time
python官方提倡使用下划線分割法
注: 在python中采用的是基於值的內存管理方式,如果為不同變量賦值為相同的值,那么這個值在內存中只有一份,多個變量指向同一塊內存地址; python具有內存管理功能,會跟蹤所有的值,並自動刪除不再有變量指向的值,因此在python中不需要考慮太多關於內存管理的問題
4.比較low的變量命名方式(不推薦使用!)
(1)變量名為中文、拼音
(2)變量名過長
(3)變量名和意思不符
1 姓名 = "wyb" 2 xingming = "wyb" 3 my_love_girl_name = "zl" 4 name = "wyb" 5 name = "湖北武漢"
5.常量
常量是指不變的量,如π = 3.141592623...,或者在程序運行過程中不會改變的量,python中沒有一個專門的語法表示常量,程序員約定俗成變量名全部大寫表示常量
1 MY_AGE = 21
三、python用戶輸入
1. python3中的用戶輸入
在python3中用戶輸入依靠input函數實現,python3input() 函數接受一個標准輸入數據,返回為 string 類型。
函數語法: input([prompt]) # prompt為提示信息,一般是字符串形式
示例:
type()函數在python中是用來返回對象的類型,對象是python中最基本的概念之一,在python中的一切都是對象。python中有很多內置對象供我們使用,比如說數字、字符串、列表、元組、字典、集合、del命令以及type()、print()等大量內置函數。
注: python3 里input()接收所有輸入,並默認將所有的輸入都看作字符串來處理,返回字符串類型
2. python2中的用戶輸入
在python2中用戶輸入依靠input()和raw_input()實現,input() 相等於 eval(raw_input(prompt)) ,用來獲取控制台的輸入。
raw_input() 將所有輸入作為字符串看待,返回字符串類型。而 input() 在對待純數字輸入時具有自己的特性,它返回所輸入的數字的類型( int, float ),input()處理字符串時輸入的字符串必須用引號括起來,否則會引發一個 SyntaxError錯誤
input()示例:
raw_input()示例:
注意:
input() 和 raw_input() 這兩個函數均能接收字符串 ,但 raw_input() 直接讀取控制台的輸入(任何類型的輸入它都可以接收)。而對於 input() ,它希望能夠讀取一個合法的 python 表達式,即輸入字符串的時候必須使用引號將它括起來,否則會引發一個 SyntaxError 。
在python2中除非對 input() 有特別需要,否則一般情況下我們都是推薦使用 raw_input() 來與用戶交互。在python3中就不用糾結,直接使用input()
四、Hello,World程序
1.python的Hello,World程序文件執行
(1)在桌面保存一個hello.py文件,並寫入以下代碼:
1 print("Hello, World") # 輸出Hello, World
(2)在命令行中進入桌面的路徑,敲入以下命令運行程序:
python3是指定了python3.exe來解釋執行hello.py文件
2.python的Hello,World程序交互式執行
除了上面的那樣把Hello,World程序寫入一個文件然后來執行外,也可以使用python的交互式環境來執行Hello,World程序
下面演示了python3中的Hello,World程序交互式執行和python2中的Hello,World程序交互式執行:
3.其他經典語言的Hello, World程序
(1)C語言:
1 #include <stdio.h> 2 int main(void) 3 { 4 printf("hello world!\n"); 5 return 0; 6 }
(2)C++:
1 #include <iostream> 2 using namespace std; 3 int main(void) 4 { 5 cout<<"Hello world" << endl; 6 }
(3)C#:
1 public class HelloWorld 2 { 3 public static void Main() 4 { 5 System.Console.WriteLine("HELLO WORLD"); 6 } 7 }
(4)Java:
1 public class HelloWorld{ 2 // 程序的入口 3 public static void main(String args[]){ 4 // 向控制台輸出信息 5 System.out.println("Hello World!"); 6 } 7 }
(5)JavaScript:
1 console.log("Hello, World!") //在瀏覽器的console中輸出Hello, World!
(6)PHP:
1 <?php 2 echo "hello world!"; 3 ?>
(7)Ruby:
1 puts "Hello world."
(8)Go:

1 package main 2 3 import "fmt" 4 5 func main(){ 6 7 fmt.Printf("Hello World!\n"); 8 9 } 10 11 Go
附:
1.用戶交互:

1 # __author__ = "wyb" 2 # date: 2018/3/3 3 4 death_age = 80 5 6 # input 接受的所有數據都是字符串,即便你輸入的是數字,但依然會被當成字符串來處理 7 name = input("your name: ") 8 age = input("your age: ") 9 print(type(name)) # type() -> 輸出對象的類型 10 print(type(age)) 11 print("Your name:", name) 12 13 # int() -> 轉換成整數 str() -> 轉換成字符串類型 14 # 以下兩種方法輸出結果一樣,但是本質不一樣: 15 # 字符串直接輸出: 16 print("You can still live for ", death_age - int(age), " years ....") 17 # 字符串拼接: 18 print("You can still live for " + str(death_age - int(age)) + " years ....")
2.自然數取位:

1 # __author__ = "wyb" 2 # date: 2018/3/10 3 4 # 用戶輸入一個三位自然數,計算並輸出其佰位、十位和個位上的數字 5 6 # 方法1: 7 # x = input('請輸入一個三位數:') 8 # x = int(x) 9 # a = x // 100 # 取百位 10 # b = x // 10 % 10 # 取十位 11 # c = x % 10 # 取個位 12 # print(a, b, c) 13 14 # 方法2: 15 # x = input('請輸入一個三位數:') 16 # x = int(x) 17 # a, b = divmod(x, 100) # 取百位和百位后的數 18 # b, c = divmod(b, 10) # 取十位和個位 19 # print(a, b, c) 20 21 # 方法3: 22 # x = input('請輸入一個三位數:') 23 # a = int(x[0]) # 取百位 24 # b = int(x[1]) # 取十位 25 # c = int(x[2]) # 取個位 26 # print(a, b, c) 27 28 # 方法4: 29 x = input('請輸入一個三位數:') 30 a, b, c = map(int, x) 31 print(a, b, c)