python初識及變數

來源:互聯網
上載者:User

標籤:代碼塊   編碼   布爾值   單行注釋   python解譯器   尾碼   使用者輸入   使用者   相等   

一、基礎

1、檔案的尾碼是  .py

2、兩種執行方式

    python解譯器   .py檔案路徑

    python進入解譯器  即時輸入並擷取執行結果

3、解譯器路徑

   #!/usr/bin/evn python (Linnux)

   #-*-coding:utf8-*-

 python 2中 只要出現中文,頭部必須加編碼 (想要加頭部,兩行必須相鄰)

 python3 中無需關注

4、執行一個操作

 python中 單行注釋用#,多行注釋用   ‘‘‘

input 的用法 永遠等待 直到使用者輸入了值 就將輸入的值賦給一個東西

5、變數只能由數字、字母 、底線組成 但不能以數字開頭

  python的一些關鍵字不能作為變數名

  變數名最好有意義一些

  最好不能和python內建的東西重複

 user-id  python中一般不用userId

6、條件陳述式

if 條件:

  print()#內部代碼塊

else:

  print()

直接用tab 代替四個空格

同一個代碼塊下,縮排的字元必須相等

if 支援嵌套

if、elif (類似於多條件判斷)

#pass 什麼都不執行 過

7、基礎資料型別 (Elementary Data Type)

數字、字串、列表、元祖、字典、布爾值

字串 用印號引起來   " "   """  """   ‘  ‘   ‘‘‘  ‘‘‘ 
加法 乘法

數字   %  #對一個數 取餘

       ** 一個數的冪

 判斷奇偶性

temp=a%2if temp==0:    print(‘偶數‘)else:    print(‘奇數’)

 使用while迴圈輸入 123456 89

n=1 while n<=10    if n==7:        pass    else:        print(n)    n+=1print(‘end‘)

求1-100所有數的和

s=0n=1while n<101    s=s+n    n=n+1print(s)

求1-2+3-4+5 ...99的所有數的和

n=1s=0while n<100    temp=n%2    if temp==0:        s=s-n    else:        s=s+nprint(s)

輸出1-100內所有奇數

n=1while n<101    temp=n%2    if temp!=0:        print(temp)    else:        pass
n=n+1

使用者登入 (三次重試)

count=0while  count <3       user=input(">>>")    psd=input(">>>")    if  user=="alex" ,psd=="123":        print("歡迎登陸")        break    else:        print("使用者名稱或密碼錯誤")    count+=1    

 

python初識及變數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.