1. 程式人生 > >python基礎知識---簡單語法

python基礎知識---簡單語法

python

1.if語句

>> a = 10
>> b = 20
>> if(a>b):
print("a max!")
else:
print("b max!")

b max!

>> student = "zhangxiaoyu"
>> if (student == "zhanxgiaoyu"):
print("YES")
else:
print("NO")

NO

>> if (student != "zhanxgiaoyu"):

print("YES")
else:
print("NO")

YES
【in 和 not in 表示包含的關系】

>> hi = "hello python"
>> if ("h" in hi):
print("yes")
else:
print("no")

yes

>> if (" " in hi):
print("yes")
else:
print("no")

yes

>> if ("i" in hi):

print("yes")
else:
print("no")

no

【if語句可以進行布爾類型判斷】

>> a = True
>> if a:
print(" a is True")
else:
print("a is no True")

a is True

2.for語句

>> names = ["xiaoyu","xiaowen","huahua","qiongge"]
>> for name in names:

print(name)

xiaoyu
xiaowen
huahua
qiongge

>> for name in names:
print(name)
if(name == "huahua"):
print("yes")
break

xiaoyu
xiaowen
huahua
yes

>> for name in names:
print(name)
if(name == "huahua"):
print("yes")
continue

xiaoyu
xiaowen
huahua
yes
qiongge

range()函數:默認從零開始循環,也可以設置起始位置和步長

>> for i in range(10):
print(i)

01
2
3
4
5
6
7
8
9

>> for i in range(1,20,5):
print(i)

1
6
11
16
3.數組與字典
(1)數組

>> lists = [1,2,3,4,5,6,‘a‘]
>> lists[0]
1
>> lists[6]
‘a‘
>> lists[6] = ‘n‘
>> lists[6]
‘n‘
>> lists.append("v")
>> lists
[1, 2, 3, 4, 5, 6, ‘n‘, ‘v‘]
(2)字典
>> ret = {"usenames":"xiaoyu","password":"12345"}
>> ret.keys()
dict_keys([‘password‘, ‘usenames‘])
>> ret.values()
dict_values([‘12345‘, ‘xiaoyu‘])
>> ret.items()
dict_items([(‘password‘, ‘12345‘), (‘usenames‘, ‘xiaoyu‘)])
4.函數、類和方法
(1)函數
def關鍵字來定義函數。
>> def add(a,b):
print(a+b)

>> add(1,2)
3
>> sum = add(1,2)
3
>> def add(a,b):
return (a+b)
>> add(1,9)
10

若在調用add()函數時不想傳參,可設置默認參數。

>> def add(a = 1,b = 9):
return (a+b)
>> add()
10
>> add(10,20)
30
(2)類和方法
class A(object):
def add(self,a,b):
return (a+b)

count = A()
print(count.add(3,9))

class A():
def init(self,a,b): #初始化
self.a = int(a)
self.b = int(b)

def add(self):
return self.a+self.b

count = A(0,0)
print(count.add())
class A(): #B繼承A
def add(self, a, b):
return(a+b)

class B(A):
def sub(self,a,b):
return(a-b)

count = B()
print(count.add(10,90))
print(count.sub(10-9,9))
5.模組(類庫、模塊)
(1)在time模塊下面有一個ctime()方法用於獲取當前時間
import time
print(time.ctime())

from time import ctime #只會用到tiem下的ctime()方法
print(ctime())

import time
print(time.ctime())
print(time.sleep(2))
print("休眠兩秒")
print(time.sleep(2))
print(time.ctime())

from time import #“”用於表示模塊下面的所有方法
print(ctime())
print("休眠3秒")
print(sleep(3))
print(ctime())
5.異常
(1)
try:
open("abc.txt",‘r‘)
except FileNotFoundError: #試圖打開一個不存在的文件與目錄
print("異常了!")
(2)
try:
print(aa)
except NameError: #使用一個還未賦值對象的變量
print("這是一個name異常")
(3)
try:
open("abc.txt",‘r‘)
print(aa)
except Exception(BaseException) as msg:
print("這是一個異常")
print(msg)
[Errno 2] No such file or directory: ‘abc.txt‘
在python中所有的異常類都繼承Exception,所以可以使用它來接收所有類型的異常。從python2.5版本之後,所有的異常都有了新的基類BaseException。Exception同樣繼承自BaseException。
(4)異常與if語句配合
try:
aa = "python"
print(aa)
except Exception as msg:
print("有異常存在")
else:
print("輸出正常,沒有異常")
try:
aa = "python"
print(aa)
except Exception as meg:
print(msg)
finally:
print("不管是否有異常存在,都會執行")

python基礎知識---簡單語法