1. 程式人生 > >python 快速入門(一)

python 快速入門(一)

一. 安裝

下載python安裝包,https://www.python.org/downloads/

二. 變數和簡單資料型別

####1.變數

message = "Hello Python world!"
print(message)  #Hello Python world!"

####2.變數命名規則

  • 包含字母、數字和下劃線,注意!!開頭不能為數字
  • 無空格
  • 不能與關鍵字重複,如 print
  • 慎用小寫字母l和大寫字母O,容易跟數字1和0混淆

####3.常量
所謂常量就是不能變的變數,比如常用的數學常數π就是一個常量。在Python中,通常用全部大寫的變數名錶示常量

PI = 3.14159265359

####4. 字串
######字串簡單大小寫轉換

name = "ada lovelace"
print(name.title()) #Ada Lovelace    首字母大寫
print(name.upper()) # ADA LOVELACE    全部字母大寫
print(name.lower()) # ada lovelace    全部字母小寫

######字串合併

first_name = "ada"
lase_name = "lovelace"
full_name = first_name + " " + last_name
print(full_name)   #ada lovelace

######單引號’'和雙引號"" 問題
######多行輸入

print('''line1
line2
line3''')

####5.整數
Python可以處理任意大小的整數,當然包括負整數,在程式中的表示方法和數學上的寫法一模一樣,例如:1,100,-8080,0,等等。

計算機由於使用二進位制,所以,有時候用十六進位制表示整數比較方便,十六進位制用0x字首和0-9,a-f表示,例如:0xff00,0xa5b4c3d2,等等

####6.浮點數
浮點數也就是小數,之所以稱為浮點數,是因為按照科學記數法表示時,一個浮點數的小數點位置是可變的,比如,1.23x109和12.3x108是相等的。浮點數可以用數學寫法,如1.23,3.14,-9.01,等等。但是對於很大或很小的浮點數,就必須用科學計數法表示,把10用e替代,1.23x109就是1.23e9,或者12.3e8,0.000012可以寫成1.2e-5,等等。

整數和浮點數在計算機內部儲存的方式是不同的,整數運算永遠是精確的(除法難道也是精確的?是的!),而浮點數運算則可能會有四捨五入的誤差。

####7.布林值 (and、or、not運算)

print(True and True)          #True
print(True and False)         #False
print(False and False)        #False

print(True or True)              #True
print(True or False)             #True
print(False or False)            #False

print(not True)             #False
print(not False)              #True

####8.空值 None

三. 字串編碼

需要在python檔案開頭加上兩行程式碼,不然中文輸出可能會有亂碼

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

四. 列表和元組

在python中是一種有序集合,相當於JavaScript中的陣列

classmate = ["xiaoming","xiaofang","xiaohong"]

####1.訪問列表元素,索引0開始

#正數
print(classmate[0]) #xiaoming
print(classmate[1]) #xiaofang
print(classmate[2]) #xiaohong
print(classmate[3]) 
#Traceback (most recent call last):
#File "F:/neng/python/name.py", line 5, in <module>
#    print(classmate[3])
#IndexError: list index out of range

#倒數
print(classmate[-1]) #xiaohong
print(classmate[-2]) #xiaofang
print(classmate[-3]) #xiaoming
print(classmate[-4]) 
#Traceback (most recent call last):
#File "F:/neng/python/name.py", line 5, in <module>
#    print(classmate[-4])
#IndexError: list index out of range

####2.列表長度 len()

print(len(classmate)) #3

####3.增刪改
a.增:append,在列表最後插入;insert,選擇位置插入
b.刪:pop,預設彈出末尾元素,也可選擇彈出元素(有返回值);del ,刪除列表索引位置元素;remove,刪除首個符合條件的元素(並不是刪除特定的索引)
c.改:直接賦值

classmate = ["a","b","c","d","e","f","g","h"]
#增
classmate.append("f")
print(classmate)  #['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'f']
classmate.insert(2,"www")
print(classmate)  #['a', 'b', 'www', 'c', 'd', 'e', 'f', 'g', 'h', 'f']
#刪
classmate.pop()
print(classmate)  #['a', 'b', 'www', 'c', 'd', 'e', 'f', 'g', 'h']
classmate.pop(2)
print(classmate)  #['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
del classmate[3]
print(classmate)  #['a', 'b', 'c', 'e', 'f', 'g', 'h']
del classmate[3]
print(classmate)  #['a', 'b', 'c', 'f', 'g', 'h']
classmate.remove("b")
print(classmate)  #['a', 'c', 'f', 'g', 'h']
#改
classmate[3] = "hello"
print(classmate)  #['a', 'c', 'f', 'hello', 'h']

####4.操作列表 range,for … in ,min , max ,sum

#建立數字列表
lists = range(1,5) 
print(list) #[1,2,3,4]
#迴圈
for key in lists:
     print(key)
#1
#2
#3
#4
#5
print(max(list)) #4
print(min(list)) #1
print(sum(list)) #10
#列表解析
squares = [value**2 for value in range(1,11)]
print(squares) #[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

####5.列表切片 [:]

my_foods = ['pizza', 'falafel', 'carrot cake', 'rice', 'cannoli']
print(my_foods[1:2]) #['falafel']
print(my_foods[:2])  #['pizza', 'falafel']
print(my_foods[1:])  #['falafel', 'carrot cake', 'rice', 'cannoli']
print(my_foods[-2:])  #['rice', 'cannoli']
print(my_foods)  #['pizza', 'falafel', 'carrot cake', 'rice', 'cannoli']

####6.判斷值是否在列表中 或者不在列表中

my_foods = ['pizza', 'falafel', 'carrot cake', 'rice', 'cannoli']
print('haha' not in my_foods) #True
print('haha' in my_foods) #False

####7.元組 tuple
一旦初始化就不能修改

 classmates = ('Michael', 'Bob', 'Tracy')

五. 條件判斷if

if <條件判斷1>:
    <執行1>
elif <條件判斷2>:
    <執行2>
elif <條件判斷3>:
    <執行3>
else:
    <執行4>