基礎語法
- 編碼問題:文件開頭加
# -*- coding: utf-8 -*-
- 變量賦值無需聲明類型
- 語句末尾無需" ; "等表示結尾
- 以4個空格的縮進控制模塊
- 函數及邏輯判斷后要加冒號
執行.py
文件的方式
-
cd
到文件所在的目錄 -
python filename.py
(如果有問題的話考慮chmod 777 filename.py
)
模塊
hello.py
def say_hello(par):
print 'Hello, ', par
return
main.py
(同一目錄下)
import hello
hello.say_hello('Jack')
或
from selenium import webdriver
I/O
print 'Hello, %s! * %d' % ('python', 3)
#涉及到中文的輸出要在引號前加上字母u,強制進行unicode編碼
str1 = raw_input('raw_input輸入')
print u'raw_input輸出', str1
輸出結果
Hello, python! * 3
raw_input輸入QWERTY
raw_input輸出 QWERTY
數據類型
五個標準的數據類型:
Numbers(數字),String(字符串),List(列表),Tuple(元組),Dictionary(字典)
賦值
age = 23 #整型
height = 1.85 #浮點
name = 'jackie' #字符串
a, b, c = 10, 20, 'jjqqkk' #為多個變量賦值
list1 = ['cat', 'dog', 100, 200] #列表
tup1 = ('apple', 'iphone', 2016, 2017) #元組
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
字符串操作
str = 'Welcome to PYTHON'
print str #完整字符串 'Welcome to PYTHON'
print str[0] #第一個字符 'W'
print str[1:4] #第2個到第4個之間的字符串(不包括冒號后面那一位) 'elc'
print str[5:] #從第6個字符開始的字符串 'me to PYTHON'
print str * 3 #輸出3次字符串 'Welcome to PYTHONWelcome to PYTHONWelcome to PYTHON'
print str + ' lol' #字符串拼接 'Welcome to PYTHON lol'
運算符
以下假設變量: a=10,b=20
運算符 | 描述 | 實例 |
---|---|---|
+ | 加 - 兩個對象相加 | a + b 輸出結果 30 |
- | 減 - 得到負數或是一個數減去另一個數 | a - b 輸出結果 -10 |
* | 乘 - 兩個數相乘或是返回一個被重復若干次的字符串 | a * b 輸出結果 200 |
/ | 除 - x除以y | b / a 輸出結果 2 |
% | 取模 - 返回除法的余數 | b % a 輸出結果 0 |
** | 冪 - 返回x的y次冪 | a**b 為10的20次方, 輸出結果 100000000000000000000 |
// | 取整除 - 返回商的整數部分 | 9//2 輸出結果 4 , 9.0//2.0 輸出結果 4.0 |
Python2.x的版本中,整數除整數,得到的結果都是整數。要想結果是小數,需要除數和被除數中至少有一個浮點數。
列表
list1 = ['cat', 'dog', 100, 200]
#輸出指定項
print list1[0]
print list1[1:3]
#更list1新列表
list1[2] = 1100
list1.append(300)
for x in list1:
print x
#刪除元素
del list1[2]
print list1
print len(list1)
#拼接列表
list2 = ['qqq', 111]
list1 += list2
print list1
輸出結果
cat
['dog', 100]
cat
dog
1100
200
300
['cat', 'dog', 200, 300]
4
['cat', 'dog', 200, 300, 'qqq', 111]
元組
tup1 = () #創建空元組
tup2 = (123,) #創建只有一個元素的元組
del tup1 #刪除元組
字典
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};
#更新字典
dict['Age'] = 8;
dict['School'] = "DPS School";
print dict
#刪除鍵值對
del dict['Age']
print dict
#清除字典
dict.clear()
print dict
輸出結果
{'School': 'DPS School', 'Age': 8, 'Name': 'Zara', 'Class': 'First'}
['School', 'Name', 'Class'] ['DPS School', 'Zara', 'First']
{}
語句
條件語句
num = 10
if num < 0 or num > 10: # 判斷值是否在小于0或大于10
print 'hello'
else:
print 'undefine'
# 輸出結果: undefine
while循環
num1 = 0
while num1 < 10:
num1 += 1
if num1 %2 > 0:
continue
print num1
print '---------'
num2 = 0
while 1:
print num2
num2 += 1
if num2 > 5:
break
輸出結果
2
4
6
8
10
---------
0
1
2
3
4
5
for循環
animals = ['cat', 'dog', 'monkey']
#函數len()返回列表的長度,也就是列表中元素的個數,range()用于返回一個序列的數。
for index in range(len(animals)):
print u'動物有:', animals[index]
輸出結果
動物有: cat
動物有: dog
動物有: monkey
其他
隨機數
import random
randomNumber = int(random.uniform(1,10)) #取1-10之間的隨機整數
日期和時間
import time
import calendar
print time.time()
print time.localtime(time.time())
print time.asctime(time.localtime(time.time()))
print time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
print '----------------'
print calendar.month(2017, 7)
輸出結果
1506503049.9
time.struct_time(tm_year=2017, tm_mon=9, tm_mday=27, tm_hour=17, tm_min=4, tm_sec=9, tm_wday=2, tm_yday=270, tm_isdst=0)
Wed Sep 27 17:04:09 2017
2017-09-27 17:04:09
----------------
July 2017
Mo Tu We Th Fr Sa Su
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
異常處理
while 1:
try:
num1 = raw_input('被除數:')
num2 = raw_input('除數:')
result = int(num1) / int(num2)
except BaseException:
print '異常' + '\n'
else:
print '結果:', result, '\n'
輸出結果
被除數:3
除數:2
結果: 1
被除數:1
除數:
異常
文件操作
寫入
fileObj = open('text1.txt', 'wb') #若文件已存在,則覆蓋;若不存在,則新建
fileObj.write('content content.\n')
fileObj.close()
讀取
fileObj = open('text1.txt', 'r+') #讀寫模式
str = fileObj.read()
print str
fileObj.close()
os文件方法
import os
os.rename('test1.txt', 'test2.txt') #重命名
os.remove('test2.txt') #刪除文件