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

來源:互聯網
上載者:User

標籤:匯入   http   浮點數   迴圈   變數   span   ack   內容   浮點   

 

數學運算

1、整除、模數

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

 

2、and   or

a = Trueb = Falseif ( 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 mathx = 4.56789a = math.floor(x)   #(要import math)返回數位下舍整數,如math.floor(4.9)返回 4b = math.ceil(x)    #(要import math)返回數位上入整數,如math.ceil(4.1) 返回 5c = round(x)        #返回浮點數的四捨五入值,如math.floor(4.56789)返回 5c = round(x ,2)     #返回浮點數的四捨五入值,如給出n值,則代表舍入到小數點後的位元,如math.floor(4.56789,2)返回 4.57print(a,b,c)

 

5、隨機數

import randoma = 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 mathpi = 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)    returnprintinfo(10)printinfo(70, 60, 50, 5)

 

__name__屬性

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

#!/usr/bin/python3# Filename: using_name.pyif __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)

聯繫我們

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