python入門的基本歷程

Python入門

1.1環境安裝

python官網下載,安裝以後添加環境變量.python

1.2集成開發環境

PyCharm官網下載,安裝後:new project-﹥pure python 設置python解釋器,新建File和python File,在新建下進行開發。linux

1.3變量

如何定義變量?
語法:
變量名=值
變量名是對值的引用
示範:
level=0
age=19

is_live=True
is_live=False

name='SB'windows

1.4python垃圾回收機制

python自動的垃圾回收機制 垃圾:值身上的引用計數爲0iphone

增長引用計數
x=1
y=xui

減小引用計數
x='SB'
del y # 刪除y與1的綁定關係spa

1.5變量命名規範

變量的命名規範?
1. 變量名只能是 字母、數字或下劃線的任意組合
2. 變量名的第一個字符不能是數字
3. 關鍵字不能聲明爲變量名
定義方式?
駝峯體
AgeOfOldboy=58
下劃線
age_of_oldboy=58

1.6變量特徵

1.61變量的三個特徵(重點)

id: type value對象

1.62 =和is

#==:比較的是值
s1='name:alex,age:73'
s2='name:alex,age:73'
#is:身份運算,比較的是id
x is y     #id(x)==id(y),若是是同一個對象則返回True
        x is not y  #id(x)!==id(y),若是引用的不是同一個對象則返回True

1.7if語句

if 條件1:
   代碼塊1
elif 條件2:
   代碼塊2
...
else:
   代碼塊n

有break則退出while循環,continue則結束本次循環,執行下次循環ip

1.8while語句

while 條件:
   代碼塊1(break)
else:
   代碼塊2

當while循環正常執行完,中間沒有被break停止的話,就會執行else後面的語句。開發

若是執行過程當中被break,就不會執行else的語句。string

1.9 for 循環

for循環:
goods=['mac','iphone','windows','linux']
for i in range(len(goods)):
print(i,goods[i])
   
或者

for x,y in enumerate(goods):
   print(x,y)
(0, 'mac')
(1, 'iphone')
(2, 'windows')
(3, 'linux')

1.91打印九九乘法表

打印九九乘法表:
1*1=1 #layer=1 運算次數1
2*1=2 2*2=4 #layer=2 運算次數2
3*1=3 3*2=6 3*3=9


for layer in range(1,10):
     for j in range(1,layer+1):
            print('%s*%s=%s ' %(j,layer,layer*j),end='')
     print()

1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

1.92打印金字塔


                          #max_layer=5
    *                   #space=4,star=1
   ***                #space=3,star=3
  *****              #space=2,star=5
 *******           #space=1,star=7
*********        #space=0,star=9

space=max_layer - current_layer
star=2*current_layer-1


max_layer=50
for current_layer in range(1,max_layer+1):
# print(current_layer)

  for i in range(max_layer - current_layer): # 打印空格
    print(' ',end='')
  for j in range(2*current_layer-1):# 打印星號
    print('*',end='')

  print()

相關文章
相關標籤/搜索