close

python教學影片:

https://www.youtube.com/watch?v=1PC3etgLwVc&feature=youtu.be&list=PLXO45tsB95cIRP5gCi8AlYwQ1uFO2aQBw

寫入檔案

my_file = open('my_file.txt','w') write       覆蓋寫入檔案

my_file = open('my_file.txt','a') append   加入在後面

my_file.write(text)

my_file.close()

 

讀取檔案

file = open('my file.txt','r') read

content = file.read() 讀取檔案到content

content = file.readline() 讀取單行內容到content

content = file.readlines()             讀取內容並轉換為陣列到content

 

Class

 

class Human:

   name = 'Goods MAN'

   price = 18

   def __init__(self,name,price,hight,width,weight):

       self.name = name

       self.price = price

       self.h = hight

       self.wi = width

       self.we = weight

   

   def add(self,x,y):

       print(self.name)

       result = x+y

       print(result)

   def minus(self,x,y):

       result = x-y

       print(result)

   def times(self,x,y):

       result = x*y

       print(result)    

   def divide(self,x,y):

       result = x/y

       print(result)

 

Input

a_input = input('plz enter a num:') #return a sting

print('This input num is :',a_input)

 

if a_input == '1':

   print('This is a good one')

elif a_input == '2':

   print('This is Two')

else:

   print('good luck')

 

tuple 跟 list

 

a_tuple = 5,4,1,25,11

 

a_list = [11,254,1548,1230]

 

for index in range(len(a_list)):

   print('index=',index,' number in list=',a_list[index])

 

List 基礎運用

 

a = [5,1,3,6,4,8,7]

 

a.append(11)

a.insert(2,150)

a.remove(8)

 

a.sort(reverse = True)

print(a)

 

多維list

a = [1,2,3,4,5]

 

muti = [[1,2,3],

       [4,5,6],

       [7,8,9]

       ]

 

print(muti[0][2])

 

字典

 

d = {'apple':[1,2,3],'pear':{'k1':'apple','k2':10},'orange':2}

print(d)

 

載入模組

#import time

#import time as t

#from time import time,localtime

from time import *

 

print(localtime())

print(time())

 

#Break #continue

 

a =True

 

while a:

   b = input('type ans: ')

   if b == '1':

       continue

   else:

       pass

   print('still in loot')

   

Try Error處理

 

try:

   file = open('NONONO.txt','r+')

except Exception as e:

   print(e)

   response = input('do you want to create a new file')

   if response == 'y':

       file = open('NONONO.txt','w')

   else:

       pass

else:

   file.write('GOGOGO')

   file.close()

 

zip lambda map

 

a = [1,2,3]
b = [4,5,6]

list(zip(a,b))

for i,j in zip(a,b):
print(i/2,j*2)

def fun1(x,y):
   return(x+y)

fun2 = lambda x,y:x+y

print(fun1(5,6))

print(fun2(7,8))

r = list(map(fun1,[1],[2]))

r2 = list(map(fun1,[1,3],[2,5]))

print(r)
print(r2)

 

Copy DeepCopy

 

import copy

 

a = [1,2,3]

b = a      #完全共用ID位置與值

 

print('a ID:',id(a))

 

print('b ID:',id(b))

 

a[0] = 21

 

print(b)

 

copy.copy     #除第一層以外,其餘數值共用ID位置

 

c = copy.copy(a)

 

print("c ID = a ID ? :",id(a) == id(c))

 

a = [1,2,[3,4]]

d = copy.copy(a)

print("d ID = a ID ? :",id(a) == id(d))

 

print("d[2] ID = a[2] ID ? :",id(a[2]) == id(d[2]))

 

e = copy.deepcopy(a)   #完全不共用,產生出的新變量,ID是完全獨立的

print("e[2] ID = a[2] ID ? :",id(a[2]) == id(e[2]))


 

pickle 存放資料


 

import pickle

 

a_dict = {'da':111,2:[23,1,4],'23':{1:2,'d':'sad'}}

 

#file = open('pickle_example.pickle','wb')

#pickle.dump(a_dict,file)

#file.close()

 

with open('pickle_example.pickle','rb') as file:

   a_dict1 = pickle.load(file)

   #讀取先前的結果

print(a_dict1)

 

Set

 

char_list = ['a','b','c','c','d','d','d']

 

print(set(char_list))

print(type(set(char_list)))

 

print(type({1:50}))

 

sentence = "Hello world"

 

print(set(sentence))

 

unique_char = set(char_list)

unique_char.add('x')

#unique_char.clear()

#unique_char.remove('x')

#unique_char.discard('y')

print(unique_char)


 

set1 = unique_char

set2 = {'a','e','i'}

 

print(set1.difference(set2))      #set1與set2差集


 

print(set1.intersection(set2))   #set1與set2聯集

arrow
arrow
    全站熱搜

    suker0409 發表在 痞客邦 留言(0) 人氣()