Python基礎二(變數 input)

2022-05-04 05:15:10 字數 2709 閱讀 3193

變數 是 為了儲存 程式運算過程中的一些中間 結果,為了方便日後呼叫

官方文件說明如下:

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.

變數命名規則:

1. 字母數字下劃線組成

2. 不能以數字開頭,不能含有特殊字元和空格

3. 不能以保留字命名

4. 不能以中文命名

5. 定義的變數名應該有意義

6. 駝峰式命、 下劃線分割單詞

7. 變數名區分大小寫

什麼是常量:

例如:不變的量 π = 3.141592653.... 

在py裡面所有的變數都是可變的 ,所以用全部大寫的變數名來代表次變數為常量

字元編碼:

支援中文的第一張表就叫 gb2312

1980 gb2312 

1995 gbk1.0 

2000 gb18030 

big5 台灣

unicode萬國碼 支援所有國家和地區的編碼

2**16 = 65535 = 存乙個字元 統一占用2個位元組

utf-8 = unicode 的擴充套件集,可變長的字元編碼集

python2.x == assic 預設編碼

#!-*- coding:utf-8 -*-

#coding:utf-8

python3.x == unicode預設編碼

unicode 是向下相容gb2312 , gbk

注釋

單行注釋 用#

多行注釋用三個單引號或三個雙引號 '''被注釋的內容'''

使用者輸入(input)

#使用者互動小練習

death_age = 80name = input("

your name:")

age = input("

your age:

") #

input 接受的所有資料都是字串,即便你輸入的是數字,但依然會被當成字串來處理

print

( type(age) )

#int integer =整數 把字串轉成int,用int(被轉的資料)

#str string =字串 把資料轉成字串用str(被轉的資料)

print("

your name:

",name)

#print("you can still live for ", death_age - int(age)," years ....")

print("

you can still live for

" + str(death_age - int(age)) +"

years ....

")

#

猜成績score = int(input("

score:"))

if score > 90:

print("a"

)elif score > 80:

print("b"

)elif score > 70:

print("c"

)elif score > 50:

print("d"

)else

:

print("

滾")

#

猜年齡age_of_princal = 56guess_age = int( input("

>>:

") )

'''if guess_age == age_of_princal then

print("yes")

else

print("no ")

'''if guess_age ==age_of_princal:

print("

yes,you got it..")

elif guess_age >age_of_princal:

print("

shoud try samller..")

else

:

print("

try bigger ...

")

Python基礎程式設計 二 變數

本篇文章主要是對python學習時的一些總結,作為學習筆記記錄。c users wood python python 3.7.8 tags v3.7.8 4b47a5b6ba,jun 28 2020,08 53 46 msc v.1916 64 bit amd64 on win32 然後就能夠在出現...

Python基礎(二) 變數與運算子

變數是資料的載體,是計算機記憶體裡的一塊儲存空間,通過對變數進行讀寫等操作,就可以對資料進行相應的操作。python變數的命名遵循三條規則 變數名由 數字和字母組成,數字不能作為變數的開頭 python語言大小寫敏感,變數名一般用小寫構成,多個單詞間用下劃線 分隔 變數名不能和python語言中的關...

swift基礎二(變數和常量)

swift變數使用 var 進行宣告,變數可以重新賦值。例如 定義變數,int 型別,賦值 10 var a int 10 a 20 變數可以重新賦值 自動識別型別。給變數 b 賦值 10,由於 10 為整型,所以自動定義 b 為整型 var b 10 自動宣告為 浮點型 var c 10.1 自動...