1. 程式人生 > >學習筆記:python3,一些基本語句(2017)

學習筆記:python3,一些基本語句(2017)

val from 列表 除法 方法 是否 主程 return tuple

數學運算

1、整除、取模

a = 36
b = 10
c = d = 0
c = a//b          #取整除 - 返回商的整數部分
d = a % b         #取模 - 返回除法的余數
print (a,"",b,"等於", c,",余",d)          # 36 除 10 等於 3 ,余 6

2、and or

a = True
b = False
if ( a and b ):
   print (" 變量 a 和 b 都為 true")
if ( a or b ):
   print (" 變量 a or b 為 true")

3、for循環、if、列表

list = [2, 3, 4, 5 ]
for i in range(10):
    if ( i in list ):
        print("變量",i,"在給定的列表中 list 中")
    else:
        print("變量",i,"不在列表中")

4、數學運算

import math
x = 4.56789
a = math.floor(x)   #(要import math)返回數字的下舍整數,如math.floor(4.9)返回 4
b = math.ceil(x)    #(要import math)返回數字的上入整數,如math.ceil(4.1) 返回 5
c = round(x) #返回浮點數的四舍五入值,如math.floor(4.56789)返回 5 c = round(x ,2) #返回浮點數的四舍五入值,如給出n值,則代表舍入到小數點後的位數,如math.floor(4.56789,2)返回 4.57 print(a,b,c)

5、隨機數

import random
a = random.choice(range(10))        #(需要 import random)從0到9中隨機挑選一個整數。
a = random.random()              #(需要 import random)隨機生成的一個實數,它在[0,1)範圍內
a = random.randrange (50 , 70 ,1) #返回 >=50 ,<70的隨機數。第三個參數是步長 print(a)

6、PI

import math
pi = math.pi
#theta = math.pi / 4            #相當於45度角
theta = math.radians(30)        #radians(x)將角度轉換為弧度。degrees(x)將弧度轉換為角度
y = math.sin(theta)
x = math.cos(theta)
print(pi)
print(theta)
print(y)
print(x)

字符串操作

str = "大胖三百磅不是二百磅陪著一百磅的小胖"
print (str.replace("", ""))
print (str.replace("", "", 2))
print ("這句話的字數:",len(str))
len = len(str)
print(str[5:len-8])     #截取第五個字符~倒數第八個字符。結果:不是二百磅

列表:遍歷列表、排序列表、判斷、元組轉換為列表

list = [1,2,3,6,5,4]
for x in list:
    print(x, end=",")   #運行結果:1,2,3,6,5,4,

list.sort()             #對原列表進行排序
for x in list:
    print(x, end=",")   #運行結果:1,2,3,4,5,6,

if 5 in list:           #判斷元素是否存在於列表中
    print("在list中")

aTuple = (123, haha, she, hehe)
list1 = list(aTuple)   #將元組轉換為列表。運行結果:[123, ‘haha‘, ‘she‘, ‘hehe‘]

元組:

Python 的元組與列表類似,不同之處在於元組的元素不能修改。

元組中的元素值是不允許刪除的,但我們可以使用del語句來刪除整個元組

tup2 = (111, 22, 33, 444, 55, 6, 77 )
for x in (tup2):      #遍歷
    print(x)


list2 = [111, 22, 33, 444, 55, 6, 77 ]
tup2 = tuple(list2)    #將列表轉變為元組

字典:

dict = {name: pp, age: 27,"gender":"man"}
dict["name"]="sss"
for key,val in dict.items():     #遍歷字典。字典的 items() 方法以列表返回可遍歷的(鍵, 值) 元組數組。
    print(key, "-", val)
for key in dict.keys():       #遍歷字典。字典的 keys() 方法以列表返回可遍歷的(鍵) 元組數組。
    print(key)
for val in dict.values():     #遍歷字典。字典的 values() 方法以列表返回可遍歷的(值) 元組數組。
    print(val)

字典的多級嵌套:

citys={
    北京:{
        朝陽:[國貿,CBD,天階],
        海澱:[圓明園,蘇州街,中關村,北京大學],
        昌平:[沙河,南口,小湯山,],
        懷柔:[桃花,梅花,大山]
    },
    河北:{
        石家莊:[石家莊A,石家莊B,石家莊C],
        張家口:[張家口A,張家口B,張家口C]
    }
}
for i in citys[北京]:
    print(i)

for i in citys[北京][海澱]:
    print(i)

叠代器(略)  http://www.runoob.com/python3/python3-iterator-generator.html

生成器(略)

函數(不定長度參數)

def printinfo(*vartuple):
    print("打印任何傳入的參數: ")
    for var in vartuple:
        print(var)
    return
printinfo(10)
printinfo(70, 60, 50, 5)

__name__屬性

一個模塊被另一個程序第一次引入時,其主程序將運行。如果我們想在模塊被引入時,模塊中的某一程序塊不執行,我們可以用__name__屬性來使該程序塊僅在該模塊自身運行時執行。

#!/usr/bin/python3
# Filename: using_name.py

if __name__ == __main__:
   print(程序自身在運行)
else:
   print(我來自另一模塊)

‘‘‘運行輸出如下:
$ python using_name.py
程序自身在運行
$ python  a.py       #裏面包含此句:   import using_name
我來自另一模塊
‘‘‘

dir() 函數

內置的函數 dir() 可以找到模塊內定義的所有名稱。以一個字符串列表的形式返回

目錄只有包含一個叫做 __init__.py 的文件才會被認作是一個包

如果包定義文件 __init__.py 存在一個叫做 __all__ 的列表變量,那麽在使用 from package import * 的時候就把這個列表中的所有名字作為包內容導入。

...

學習筆記:python3,一些基本語句(2017)