Python基礎一,Python基礎

來源:互聯網
上載者:User

Python基礎一,Python基礎
一、Python介紹及版本

  Python崇尚優美、清晰、簡單,是一個優秀並廣泛使用的語言。

  目前Python主要應用領域:

    雲端運算:雲端運算最火的語言

    WEB開發:眾多優秀的WEB架構,眾多大型網站均為Python開發,典型WEB架構有Django

    科學運算、人工智慧:典型庫NumPy, SciPy, Matplotlib, Enthought librarys,pandas

    系統營運:營運人員必備語言

    金融:量化交易,金融分析

    圖形GUI: PyQT, WxPython,TkInter

二、程式設計語言的分類及優缺點

  解釋型:當程式運行時,將代碼一行一行的解釋成二進位,解釋一行運行一行。

    優點:快速排錯,開發效率高,可跨平台

    缺點:程式運行效能相當較低

    典型:Python

  編譯型:將程式碼一次性全部編譯成二進位,然後再運行。

    優點:程式效能高

    缺點:排錯慢,開發效率高,不能跨平台

    典型:C語言

三、運行第一個python程式
#!/usr/bin/env python# -*- coding: utf-8 -*-print("hello,world!")
四、變數五、常量

  不變的量:出生日期、社會安全號碼等。python沒有規定,預設全部大寫的變數成為常量,BIRTH = 19970425

六、注釋

  作用:方便自己隨時記起代碼的作用,以及其他人容易看懂你的代碼。

  種類:

  •  單行注釋:#。 例如:#被注釋內容
  •     多行注釋:'''被注釋內容'''或"""被注釋內容"""  
七、基礎資料類型

   type() 判斷此資料是什麼資料類型。

 1.  數字類型int

    用於計算或比較。

i = 2print(i * 3)

  2.  字串類型str

    用於字串(加引號就是字串)

name = '老男孩'name2 = '路飛學院'print(name,'與',name2)#'與"混合使用防止錯亂msg = "My name is jim,I'm 26 years old!"msg = '''   今天我想寫首小詩,   歌頌我的同桌,   你看他那烏黑的短髮,   好像一隻炸毛雞。'''#字串拼接n1 = '你在'n2 = '看部落格'n3 = n1 + n2print(n3)

  3.  布爾值

    布爾值用於判斷正確與否,其結果只能為True或False

print(1 > 2 and 3 < 4 or 4 > 5)
八、使用者互動

  input資料類型全部是字串類型。

    python 2x:raw_input()

    python 3x:input()

name = input("請輸入你的名字:")age = input("請輸入你的年齡:")hobby = input("請輸入你的愛好:")s = "你的名字是" + name + ",今年" + age + "歲," + "愛好" + hobbyprint("我的名字是%s,我的年齡是%s,我的愛好%s" % (name,age,hobby))print(s)
九、if及while迴圈的一些基本結構

  if

if 條件:    結果
if 條件:    結果else:    結果
1 choice = input('請輸入你猜的數字:')    2 if choice == '2':3     print('二')4 elif choice == '3':5     print('三')6 elif choice == '4':7     print('四')
1 choice = input('請輸入你猜的數字:')    2 if choice == '2':3     print('二')4 elif choice == '3':5     print('三')6 elif choice == '4':7     print('四')8 else:9     print('選擇錯誤...')
1 if 條件:2     if 條件:3         結果4     else:5         結果6 else:7     結果
1 age = int(input('請猜我的年齡:'))2 if True:3     if age <= 18:4         print('恭喜你猜對了!')5     else:6         print('這都看出來...')7 else:8     print(666)

  while

while 條件:    結果
while True:   print('青花')   print('王妃')   print('涼涼')   print('深夜地下鐵')print(222)while True:   print(111)   print(222)print(444)

  跳出迴圈的條件:

    1、改變條件

    2、break

  break:結束迴圈

  continue:結束本次迴圈,繼續下一次迴圈

flag = Truewhile flag:print('等一分鐘')print('你的背包')print('彩色的黑')flag = False
flag = Truecount = 1while flag:print(count)count = count + 1if count == 101:flag = False
while True:print(111)print(222)breakprint(333)
count = 1while True:print(count)count = count + 1if count == 101:break

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.