變量是用于存儲(chǔ)數(shù)據(jù)
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名注冊、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、白云鄂網(wǎng)站維護(hù)、網(wǎng)站推廣。
name = "vita"
pep8規(guī)范:等號兩邊要有空格
駝峰體
AgeOfVita = 27
下劃線
age_of_vita = 27
變量名為中文、拼音 名字 = "vita"
變量名過長 qeqweqwe_qweqweqw_qwewqeq
變量名詞不達(dá)意 a = 23
常量即指不變的量,如pai 3.1415926 ,或在程序運(yùn)行過程中不會(huì)改變的量。
Python中沒有專門的語法定義常量,程序員約定用變量名全部大寫代表常量。
python中可自定義一個(gè)不修改的常量的類。
AGE_OF_VITA = 27
Java中定義常量,修改該常量會(huì)報(bào)錯(cuò)
public static final String SUNDAY = "SUNDAY";
c語言中定義常量,修改該常量也會(huì)報(bào)錯(cuò)
const int count = 60;
在使用中變量和常量沒什么區(qū)別
Python 2.7.16 (v2.7.16:413a49145e, Mar 4 2019, 01:37:19) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> a = 1
>>> b = a
>>> print(a)
1
>>> print(b)
1
>>> a=2
>>> print(a)
2
>>> print(b)
1
>>>
>>>
>>> A = 10
>>> B=A
>>> print(A)
10
>>> print(B)
10
>>> A=20
>>> print(A)
20
>>> print(B)
10
>>>
name = input("please input your name:")
age = input("please input your age:")
hometown = input("please input your hometown:")
print("your name is:"+name
+ " your age is:"+age
+ " your hometown is:"+hometown)
運(yùn)行
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
please input your name:vita
please input your age:27
please input your hometown:jl
your name is:vita your age is:27 your hometown is:jl
本行注釋
age = input("please input your age:") # age pep8規(guī)范:#號前面至少兩個(gè)空格,后面一個(gè)空格
單行注釋
hometown = input("please input your hometown:")
# print age pep8規(guī)范:#號前面無空格,后面一個(gè)空格
多行注釋
"""
print age
name hometown
"""
'''
print age
name hometown
pep8規(guī)范:注釋與下面的代碼的空行不能多于兩行
'''
print("your name is:"+name
+ " your age is:"+age
+ " your hometown is:"+hometown)
int(整型)
在32位機(jī)器上,整數(shù)的位數(shù)為32位,取值范圍為-2**31~2**31 -1,即-2147483648~214748364
在64位系統(tǒng)上,整數(shù)的位數(shù)為64位,取值范圍為-2**63~2**63-1,即-9223372036854775808~9223372036854775807
long(長整型)
跟C語言不同,Python的長整型沒有指定寬度,即python沒有限制長整數(shù)數(shù)值的大小,但實(shí)際上由于內(nèi)存的限制,我們使用的長整數(shù)數(shù)值不可能無限大。
注意:
自從Python2.2起,如果整數(shù)發(fā)生溢出,Python會(huì)自動(dòng)把int轉(zhuǎn)換為long,所以現(xiàn)在在long數(shù)據(jù)后面不加字母L也不會(huì)導(dǎo)致嚴(yán)重后果了。
在Python3中沒有int和long的區(qū)分了,全部都是int
除了int和long外,還有float浮點(diǎn)型,復(fù)數(shù)型
(venvP3) E:\PythonProject\python-test>python2
Python 2.7.16 (v2.7.16:413a49145e, Mar 4 2019, 01:37:19) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> type(2**4)
<type 'int'>
>>> type(2**65)
<type 'long'>
>>> quit()
(venvP3) E:\PythonProject\python-test>python3
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> type(2**4)
<class 'int'>
>>> type(2**65)
<class 'int'>
>>>
在Python中,加了引號的字符就被認(rèn)為是字符串
Name = "vita" # 雙引號
Age = "27" # age是字符串
AgeNum = 27 # age是int
Msg = 'My name is vita,i am 27 years old!' # 一對單引號
# 一對三個(gè)單引號
MsgThree = '''
My name is vita,
i am 27 years old!
'''
# 三個(gè)雙引號
MsgDouThree = """
My name is vita,
i am 27 years old!
"""
單引號和雙引號沒有任何區(qū)別,只是在下面情況下需要考慮單雙引號配合
Msg = "my name is vita,I'm 27 years old"
多引號的作用是多行字符串,需要使用多引號
# 一對三個(gè)單引號
MsgThree = '''
My name is vita,
i am 27 years old!
'''
# 三個(gè)雙引號
MsgDouThree = """
My name is vita,
i am 27 years old!
"""
字符串可以進(jìn)行“相加”和“相乘”運(yùn)算
>>> name = "vita"
>>> age = "27"
>>> print("your name is:"+name+" your age is:"+age)
your name is:vita your age is:27
>>> print("v"*3)
vvv
字符串不能喝整數(shù)拼接
>>> name = "vita"
>>> age = 27
>>> print(name+age)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be str, not int
布爾型就兩個(gè)值,true和false,主要用于邏輯判斷
>>> a =3
>>> b = 5
>>> a>b
False
>>> a<b
True
name = input("input your name:")
age = input("input your age:")
hometown = input("input your hometown:")
msg = '''
------the info of %s is ------
name: %s
age: %s
hometown: %s
------the info of end ------
''' % (name, name, age, hometown)
print(msg)
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
input your name:vita
input your age:27
input your hometown:jl
------the info of vita is ------
name: vita
age: 27
hometown: jl
------the info of end ------
運(yùn)算符種類算數(shù)運(yùn)算符、比較運(yùn)算符、邏輯運(yùn)算符、賦值運(yùn)算符
>>> a = 3
>>> b = 4
>>> a>b and a==3
False
>>> a<b and a==3
True
>>> a<b or a!=3
True
>>> a>b or a==3
True
>>> not a==3
False
>>>
if 條件成立:
執(zhí)行代碼
# _*_coding:utf-8_*_
"""
輸入姓名,性別和年齡,
如果是女生且年齡小于28,輸出我喜歡年輕女孩
"""
name = input("input your name:")
sex = input("input your sex(boy|BOY/girl|GIRL):")
#input()讀入的age是字符串,要轉(zhuǎn)換為int才能與28做大小比較
age = int(input("input your age:"))
if sex.lower() == "girl" and age < 28:
print(name+" i like young girl")
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/singleif.py
input your name:vita
input your sex(boy|BOY/girl|GIRL):GIRL
input your age:26
vita i like young girl
if 條件:
條件滿足,執(zhí)行代碼
else:
條件不滿足,執(zhí)行代碼
# _*_coding:utf-8_*_
"""
輸入姓名,性別和年齡,
如果是女生
年齡小于28,輸出我喜歡年輕女孩,
年齡不小于28,輸出年齡比我大也可以
如果不是女生
輸出我不喜歡男生
"""
name = input("input your name:")
sex = input("input your sex(boy|BOY/girl|GIRL):")
#input()讀入的age是字符串,要轉(zhuǎn)換為int才能與28做大小比較
age = int(input("input your age:"))
if sex.lower() == "girl" :
if age < 28:
print(name+" i like young girl")
else:
print("the age is greater than me is also ok!")
else:
print("i do not like boy")
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/singleif.py
input your name:vita
input your sex(boy|BOY/girl|GIRL):girl
input your age:12
vita i like young girl
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/singleif.py
input your name:vita
input your sex(boy|BOY/girl|GIRL):girl
input your age:28
the age is greater than me is also ok!
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/singleif.py
input your name:vita
input your sex(boy|BOY/girl|GIRL):boy
input your age:23
i do not like boy
if 條件:
滿足條件執(zhí)行代碼
elif 條件:
上面條件不滿足,執(zhí)行這里
elif 條件:
上面條件不滿足,執(zhí)行這里
else:
所有條件都不滿足,執(zhí)行這里
# _*_coding:utf-8_*_
"""
輸入姓名,性別和年齡,
如果是女生
年齡小于28,輸出我喜歡年輕女孩,
年齡不小于28,輸出年齡比我大也可以
如果不是女生
輸出我不喜歡男生
"""
name = input("input your name:")
sex = input("input your sex(boy|BOY/girl|GIRL):")
#input()讀入的age是字符串,要轉(zhuǎn)換為int才能與28做大小比較
age = int(input("input your age:"))
if sex.lower() == "girl" :
if age < 28:
print(name+" i like young girl")
else:
print("the age is greater than me is also ok!")
elif sex.lower() == "boy":
print("i do not like boy")
else:
print("your input is not manual human")
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/singleif.py
input your name:vita
input your sex(boy|BOY/girl|GIRL):w
input your age:23
your input is not manual human
while 條件:
執(zhí)行代碼
"""
只打印1-10的偶數(shù)
"""
count = 0
while count < 10:
if count%2 == 0:
print(count)
count = count + 1
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
0
2
4
6
8
"""
打印1-10,第五次不打印,6-8打印值的平方
"""
count = 0
while count <= 10:
if count == 5:
pass
elif count>=6 and count<=8:
print(count*count)
else:
print(count)
count = count + 1
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
0
1
2
3
4
36
49
64
9
10
只要運(yùn)行起來,就不再停止,直到把內(nèi)存耗盡
"""
只打印1-10的偶數(shù)
"""
count = 0
while count < 10:
print(count)
# count = count + 1
#注釋掉最后一行,程序一直成立,會(huì)一直運(yùn)行
break:用于完全結(jié)束一個(gè)循環(huán),跳出循環(huán)體執(zhí)行循環(huán)后面的語句
continue:終止本次循環(huán),下次的循環(huán)繼續(xù)
"""
打印1-10,第五次不打印,6-8打印值的平方
"""
count = 0
while count <= 10:
if count == 5:
# 這里一定要記得把count+1,否則count就一直等于5,就死循環(huán)了
count = count + 1
continue
elif count>=6 and count<=8:
print(count*count)
else:
print(count)
count = count + 1
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
0
1
2
3
4
36
49
64
9
10
"""
打印1-10,第五次不打印,6-8打印值的平方
"""
count = 0
while count <= 10:
if count == 5:
# 這里一定要記得把count+1,否則count就一直等于5,就死循環(huán)了
count = count + 1
break
elif count>=6 and count<=8:
print(count*count)
else:
print(count)
count = count + 1
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
0
1
2
3
4
只有在Python中有while...else
while后面的else指,當(dāng)while循環(huán)正常執(zhí)行完,中間沒有被break中止的話,就會(huì)執(zhí)行else
count = 0
while count <= 3:
if count == 2:
count = count + 1
continue
else:
print(count)
count = count + 1
else:
print("success")
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
0
1
3
success
count = 0
while count <= 3:
if count == 2:
count = count + 1
break
else:
print(count)
count = count + 1
else:
print("success")
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
0
1
本章代碼案例:
"""
猜年齡游戲:
允許用戶最多猜三次,猜了三次后,詢問是都繼續(xù)玩,如果輸入Y,可以繼續(xù)猜三次,否則退出
"""
age = 23
count = 0
while count < 3:
try:
guess_age = int(input("input the age of you think:"))
except ValueError:
print("you should input one number!")
count = count + 1
continue
if guess_age > 23:
print("the age you input is too big!")
elif guess_age < 23:
print("the age you input is too small!")
else:
print("excellent!you are right!")
break
count = count + 1
while count == 3:
your_choice = input("you only have three chances,would you like to continue(Y|y/N|n):")
if your_choice.lower() == "y":
count = 0
elif your_choice.lower() =="n":
break
else:
print("your input is illegal!input again!")
運(yùn)行
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/guessage.py
input the age of you think:2
the age you input is too small!
input the age of you think:45
the age you input is too big!
input the age of you think:33
the age you input is too big!
you only have three chances,would you like to continue(Y|y/N|n):y
input the age of you think:2w
you should input one number!
input the age of you think:24
the age you input is too big!
input the age of you think:55
the age you input is too big!
you only have three chances,would you like to continue(Y|y/N|n):y
input the age of you think:23
excellent!you are right!
Process finished with exit code 0
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
分享文章:基礎(chǔ)語法-創(chuàng)新互聯(lián)
本文路徑:http://vcdvsql.cn/article34/csihpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、品牌網(wǎng)站制作、網(wǎng)站收錄、定制網(wǎng)站、自適應(yīng)網(wǎng)站、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容