麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

Python基礎

2019-11-14 17:41:08
字體:
來源:轉載
供稿:網友
 
      也是自己之前學習的筆記。
讀文件
file_obj2=open('hello.txt','w')conta='my name is Bb'file_obj2.write(conta)v=file_obj2.readlines()PRint v

 


輸出不唯一數1 (這是一個國外Python練習網站上的題目 我其他博文有介紹 )

def checkio(data):    from collections import Counter    nonunique = Counter(data) - Counter(set(data))    return [x for x in data if x in nonunique]print checkio([1,31,5,13,13,1,1])print counter(set([1,31,5,13,13,1,1]))

 


輸出不唯一數2
#Your optional code here#You can import some modules or create additional functionsdef checkio(data):    return [x for x in data if data.count(x) > 1] #Some hints#You can use list.count(element) method for counting.#Create new list with non-unique elements#or remove elements from original list (but it's bad practice for many real cases)#Loop over original list#These "asserts" using only for self-checking and not necessary for auto-testingif __name__ == "__main__":    assert isinstance(checkio([1]), list), "The result must be a list"    assert checkio([1, 2, 3, 1, 3]) == [1, 3, 1, 3], "1st example"    assert checkio([1, 2, 3, 4, 5]) == [], "2nd example"    assert checkio([5, 5, 5, 5, 5]) == [5, 5, 5, 5, 5], "3rd example"    assert checkio([10, 9, 10, 10, 9, 8]) == [10, 9, 10, 10, 9], "4th example"

 


#python while 循環語句
i=1;s=0;while i<=100:    print s,i    s=s+i    i=i+1print 'exit'

 


#第八課 python自定義函數預定義  1.預定值放在前面 2. 后面賦值可以把先前預定的值覆蓋#

def test_e(n1,n2=15):    print n1    print n2    return n1+n2test = test_e(2)print test

 

 


#python函數實參賦值順序#
def test_e(val1,val2,val3):    print val1    print val2    print val3    return val1+val2+val3test_e(5,23,55)

 


#python賦值 進行一一對應的賦值 以防出現錯誤#

#python if語句#
#注意使用 raw_input()的時候 把字符強制轉化成 整形#

count =int(raw_input('input your number:'))print countif count>=80:    print 'a'elif count>=70:    print'b'else :    print 'nono'

 



    #字符區域代碼#

sex=raw_input('put your sex')if sex=='Male':    print 'right'else:    print 'no'

 

 


#python if分支語句表達式構造  非0 即為真#
#關系表達式 > < =   邏輯表達式 and or not 


#網絡刷博客瀏覽量#

import os
import
webbrowser as webimport timei=0while i<=10: web.open('http://blog.sina.com.cn/s/blog_9a0d1f710101vm5k.html?tj=1') time.sleep(0.8) os.system('taskkill /F /IM Chrome.exe') print '%d times already run' % i i=i+1

 




#循環體for基礎# 
list1=[2,65,'g',5656] #list 數據類型i=0;for lists in list1:    print format(i,'2d'),lists    i=i+1

 



    #string 數據類型
j=0;string ='hello world'list2=list(string)  #for str in list2:  #string 直接用數據也可以    print format(j,'2d'),str;    j=j+1;k=0;for k in range(1,101,2):  #range 函數 范圍在 start 到 end-1 最后一位為每個數字間隔數    print k;

 


s1='hello world'
#for循環 循環體遍歷文件和元組#

tup = (1,2,3,4,5)  #tup  元組for tups in tup:    print tups# file.readlines 獲取文件列表 file.readline 獲取文件字符串第一行#記住 open 的用法str =open('python16.py','r').readline()print len(str)for c in open('python16.py','r').readlines():    print c;    open('temp.txt','a++').write(r)  # 在原目錄下面增加一個txt文件 在文件里面寫入打開的文件內容

 

 


#python 字符串基礎
#轉移字符串print r'hello/nworld'  #r關閉轉義字符串的作用print u'unicode'  #unicode 字符串#格式化字符串print 'your age is %d'%(28) 

 


# open('c://temp//temp.txt','a+') 用一個‘、’可能被認為是轉義字符

#字符串基本操作

s1='www.hxend's2='.com'print s1,s2    #這樣輸出的話  中間會存在一個空格print s1+s2   #用‘+’連接兩個字符串#字符串的重復  *s='abcdefg'print s*5

 

#訪問字符串的某個元素  index索引ch = s[3]print ch#python 切片  slice s[i:j]sub =s[3:5]  #從 start 到 end-1print subprint s[:5]print s[3:]print s[-4:-1]  #如果發生錯誤 就會出現 空格表示#   s[i:j:k]print s[-1:-4:-1]  #倒序輸出  依次減一s2='www.hxend.com'print s2[9:4:-1]print s2[-1:0:-1]print s2[-1::-1]#用for 循環遍歷整個字符串s5='www.hxend.com'for ch in s5:    print ch;

 


#字符串的高級函數

#isalnum 判斷是否為 數字或者為字母 s.isalnum() s='wwwhxend55com'print s.isalnum()# s.isalpha 判斷是否為字母s='abcdef'print s.isalpha() # s.isdigit()  判斷是否為數字s='626611'print s.isdigit()# s.islower s.isupper 判斷大小寫s='FDFD'print s.islower()print s.isupper()# s.isspace  判斷是否為 空格s=' 'print s.isspace()#  s.upper()   s.lower()  大小寫互換s='sdgsdgsJLKF'print s.upper()#字符串查找  s.startswith()   s.endswith()  返回 bool值s='www.hxend.com's1='www'print s.startswith(s1)print s.endswith(s1)# find 函數  s.replace('','')  函數代替 找到即可替換s='www.hxend.com'if s.find('hxend'):    s5=s.replace('hxend','baidu')  #不改變原來的值print s5

 

 


#字符串分割函數

s='    abcdef 1515 dhgghl    sdjgsjdg hgkjdhgjk g  gskdj gjkg kjsd   'print slist1=s.split()print list1print len(list1)#  s.strip() 去掉字符串開頭 和結尾的 空格s1=s.strip()print s1#find 函數返回 出現這個字符的位置  找不到 返回 -1a= s1.find('c')print as1=list(s1)print s1#  s.find('',k)   find 從k開始# s.find('')  find 從頭開始import maths1=s.strip()a=s1.find(' ')print s1[:a]list11=s1.split()lens=len(list11)print format(lens,'2d')   #format 的用法i=0for n in range(0,lens-1):   #動態變化次數    while s1[a] == ' ':        a=a+1    b=s1.find(' ',a)    if b!=-1:                #考慮到后面結尾部分 不存在空格了        print s1[a:b]      else:        print s1[a:]    a=b'''count = 5  #這里也是動態變化次數的方法之一for i in list1:    if count == 0:        pass    else        # do sth here...        count = count - 1 #'''

 

 


#字符串分割函數的實現

s='sdgsdg..sdg.f.gs.dh.sd..fsd.g.sd's1=s.split('.')  #分割出字符串 去除'.' 然后錄入到列表中去print s1#  s.find('',k)   find 從k開始# s.find('')  find 從頭開始#  s.find('',k)   find 從k開始# s.find('')  find 從頭開始import mathdef my_split(sep):    s1=s.strip()    a=s1.find(sep)    print s1[:a]    list11=s1.split(sep)    lens=len(list11)    print format(lens,'2d')   #format 的用法    i=0    for n in range(0,lens-1):   #動態變化次數        while s1[a] == sep:            a=a+1        b=s1.find(sep,a)        if b!=-1:                #考慮到后面結尾部分 不存在空格了            print s1[a:b]          else:            print s1[a:]        a=b      my_split('.')

 

 


#append()的使用
list2=[2,32,32,23,232,3,2]list2.append(1)  #append()  增加在尾部print list2
#字典
adict={'mother':45,'father':55}print adictadict['grandfather']=87 #增加在首部print adictprint adict.keys()  #  名字print adict['father']  #提取內容#print 默認為每個輸出 輸出一個換行  后面加上一個  , 可以改變這個狀態a='sdgsdgsdsdh'for i ,ch in  enumerate(a):   #enumrate() 函數    print ch,i

 


   
'''2.13    列表解析 
這是個術語, 表示你可以在一行中使用一個for循環將所有值放到一個列表 當中: '''
sqdEvens = [x ** 2 for x in range(8) if not x % 2]print sqdEvens`'''handle = open(file_name, access_mode = 'r')file_name 變量包含我們希望打開的文件的字符串名字, access_mode 中 'r' 表示讀取,'w' 表示寫入, 'a' 表示添加。'''file1=open('second.py','r')   #文件操作print file1.readline()#print file1.read()a=file1.readlines() #readlines() 自動將文件內容分析成一個行的列表,#該列表可以由 Python 的 for ... in ... 結構進行處理print a[1]

 



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 成人三级电影在线 | 久久精品亚洲成在人线av网址 | 久久国产精品一区 | 国产黄色网| 黄色免费在线视频网站 | 欧美日本一 | 欧美精品一区二区三区在线 | 天天透天天狠天天爱综合97 | 久久国产精品成人免费网站 | av久草 | 国产精品视频在线观看免费 | 国内精品久久久久久2021浪潮 | 国产精品久久久久久久四虎电影 | 久久人人爽人人爽人人片av高清 | 国产精品久久久久久久久久 | 亚洲午夜影院在线观看 | 高清视频一区二区 | 欧美高清另类自拍视频在线看 | 久久人人人| 欧美性生视频 | 九九热精品在线 | 亚洲免费视频大全 | 国产精品99久久久久久大便 | 最新一级毛片 | 亚洲一区二区免费 | 精品成人免费一区二区在线播放 | 777sesese| 大西瓜永久免费av在线 | 天天骑夜夜操 | 成人在线视频精品 | 国产1区在线 | 成人在线影视 | 好骚综合在线 | 国产精品久久久久久久久久三级 | 一区二区三区在线观看免费视频 | 一区二区三高清 | 欧美大屁股精品毛片视频 | 91久久久久久久久久久久久久 | 久久午夜神器 | 国产成人精品区 | 黄色免费播放网站 |