溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python的基礎使用

發(fā)布時間:2020-05-29 16:25:43 來源:億速云 閱讀:222 作者:鴿子 欄目:編程語言

一、安裝Python

windows:

1、下載安裝包
    https://www.python.org/downloads/
2、安裝
    默認安裝路徑:C:\python35
3、配置環(huán)境變量
    【右鍵計算機】--》【屬性】--》【高級系統(tǒng)設置】--》【高級】--》【環(huán)境變量】--》【在第二個內(nèi)容框中找到 變量名為Path 的一行,雙擊】 --> 【Python安裝目錄追加到變值值中,用 ; 分割】
    如:原來的值;C:\python35,切記前面有分號

二、解釋器

上一步中執(zhí)行 python /home/dev/hello.py 時,明確的指出 hello.py 腳本由 python 解釋器來執(zhí)行。

如果想要類似于執(zhí)行shell腳本一樣執(zhí)行python腳本,例: ./hello.py ,那么就需要在 hello.py 文件的頭部指定解釋器,如下:

#!/usr/bin/env python

print "hello,world"

如此一來,執(zhí)行: ./hello.py 即可。

ps:執(zhí)行前需給予 hello.py 執(zhí)行權限,chmod 755 hello.py

三、內(nèi)容編碼

python解釋器在加載 .py 文件中的代碼時,會對內(nèi)容進行編碼(默認ascill)
ASCII(American Standard Code for Information Interchange,美國標準信息交換代碼)是基于拉丁字母的一套電腦編碼系統(tǒng),主要用于顯示現(xiàn)代英語和其他西歐語言,其最多只能用 8 位來表示(一個字節(jié)),即:2**8 = 256,所以,ASCII碼最多只能表示 256 個符

Python的基礎使用

顯然ASCII碼無法將世界上的各種文字和符號全部表示,所以,就需要新出一種可以代表所有字符和符號的編碼,即:Unicode

Unicode(統(tǒng)一碼、萬國碼、單一碼)是一種在計算機上使用的字符編碼。Unicode 是為了解決傳統(tǒng)的字符編碼方案的局限而產(chǎn)生的,它為每種語言中的每個字符設定了統(tǒng)一并且唯一的二進制編碼,規(guī)定雖有的字符和符號最少由 16 位來表示(2個字節(jié)),即:2 **16 = 65536,
注:此處說的的是最少2個字節(jié),可能更多

UTF-8,是對Unicode編碼的壓縮和優(yōu)化,他不再使用最少使用2個字節(jié),而是將所有的字符和符號進行分類:ascii碼中的內(nèi)容用1個字節(jié)保存、歐洲的字符用2個字節(jié)保存,東亞的字符用3個字節(jié)保存...

所以,python解釋器在加載 .py 文件中的代碼時,會對內(nèi)容進行編碼(默認ascill),如果是如下代碼的話:

報錯:ascii碼無法表示中文

#!/usr/bin/env python

print "你好,世界"

改正:應該顯示的告訴python解釋器,用什么編碼來執(zhí)行源代碼,即:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

print "你好,世界"

四、變量

1. 聲明變量

#!/usr/bin/env python
# -*- coding: utf-8 -*-

name = "wupeiqi"

上述代碼聲明了一個變量,變量名為: name,變量name的值為:"wupeiqi"

變量的作用:昵稱,其代指內(nèi)存里某個地址中保存的內(nèi)容

Python的基礎使用

變量定義的規(guī)則:

  • 變量名只能是 字母、數(shù)字或下劃線的任意組合
  • 變量名的第一個字符不能是數(shù)字
  • 以下關鍵字不能聲明為變量名
['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']

2. 變量的賦值

#!/usr/bin/env python
# -*- coding: utf-8 -*-

name1 = "wupeiqi"
name2 = "alex"

Python的基礎使用

#!/usr/bin/env python
# -*- coding: utf-8 -*-

name1 = "wupeiqi"
name2 = name1

Python的基礎使用

五、輸入

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# 將用戶輸入的內(nèi)容賦值給 name 變量
name = input("請輸入用戶名:")

# 打印輸入的內(nèi)容
print (name)

六、條件語句

1. if基本語句

縮進用4個空格

if 條件:
    內(nèi)部代碼塊
    內(nèi)部代碼塊
else:
    ...

print('....')

if 1 == 1:
    print("歡迎進1")
    print("歡迎進入2")

else:
    print("歡迎進入3")

2. if支持嵌套

if 1 == 1:
    if 2 == 2:
        print("歡迎進入1")
        print("歡迎進入2")
    else:
        print('歡迎進入3')
else:
    print("歡迎進入4")

3. if elif

inp = input('請輸入會員級別:')

if inp == "高級會員":
    print('高級會員')
elif inp == "白金會員":
    print('白金會員')
elif inp == "鉑金會員":
    print('鉑金會員')
else:
    print('城管')

print('城管....')

補充:pass

if 1==1:
    pass #表示什么也不執(zhí)行,pass 代指空代碼,無意義,僅僅用于表示代碼塊
else:
    print('sb')

4. 條件語句

n1 = input('>>>')

if "alex" == "alex":
    n2 = input('>>>')
    if n2 == "確認":
        print('alex SB')
    else:
        print('alex DB')
else:
    print('error')
if n1 == "alex" or n2 == "alex!23":
    print('OK')
else:
    print('OK')

七、while循環(huán)

1. 基本循環(huán)

while 條件:

    # 循環(huán)體

    # 如果條件為真,那么循環(huán)體則執(zhí)行
    # 如果條件為假,那么循環(huán)體不執(zhí)行

2. break

break用于退出所有循環(huán)

while True:
    print("123")
    break
    print("456")

3. continue

continue用于退出當前循環(huán),繼續(xù)下一次循環(huán)

練習題

1. 使用while循環(huán)輸入 1 2 3 4 5 6     8 9 10

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

2. 求1-100的所有數(shù)的和

n1 = 0
n2 = 1
while n2 < 101:
    n1 += n2
    n2 += 1
print(n1)

3. 輸出 1-100 內(nèi)的所有奇數(shù)

count = 1
while count < 101:
    n = count % 2
    if n == 1:
        print(count, "奇數(shù)")
    count += 1

4. 求1-2+3-4+5 ... 99的所有數(shù)的和

n = 0
n1 = 1
while n1 < 100:
    if n1 % 2 == 1:
        n = n + n1
    else:
        n = n - n1
    n1 = n1 + 1
print(n)

5. 用戶登陸(三次機會重試)

n = 0
while n < 3:
    name = input('name is :')
    pwd = input('pwd is :')

    if name == 'lingxd' and pwd == '123456':
        print('登陸成功')
        break
    else:
        print('密碼或用戶不正確')
    n = n + 1


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI