原創:叫我詹躲躲
連接:https://segmentfault.com/a/11...python
# print('hello world!')
# python -v
# message = 'hello world!' # print(message)
name = 'jetty' print(name.title()) #Jetty 首字母大寫 print(name) # jetty name.upper() #JEETY 轉大寫 name.lower() #jetty 轉小寫
first_name = 'hongzhu' last_name = 'zhan' full_name = last_name +" "+ first_name print(full_name) # zhan hongzhu
language = 'python\nJavascript\nC\nRust' print(language) # 打印 python Javascript C Rust
_blank = ' python ' print(_blank.rstrip()) #去除右側空白 print(_blank.strip()) #去除兩側空白 print(_blank.lstrip()) #去除左側空白
num = 2.340 print(int(num)) # 整型 2 print(float(num)) # 浮點型 2.34
color = ['red','green','yellow','pink'] # 訪問元素 print(color[0]) #red # 修改 color[0] = 'black' # 添加元素 color.append('orange') # 插入元素 color.insert(0,'blue') 插到第一位 print(color) # 刪除元素 del color[0] #刪除當前元素 color.pop() # 刪除數組最後一個元素 color.remove('red') # 刪除紅色
num_list = [1,2,3,4,2,1,3,1,2] num_list.sort() print(num_list) #[1, 1, 1, 2, 2, 2, 3, 3, 4]
num_list = [1,2,3,4,2,1,3,1,2] print(sorted(num_list)) #[1, 1, 1, 2, 2, 2, 3, 3, 4]
num_list = [1,2,3,4,2,1,3,1,2] num_list.reverse() print(num_list) #[2, 1, 3, 1, 2, 4, 3, 2, 1]
num_list = [1,2,3,4,2,1,3,1,2] print(len(num_list)) # 9
num_list = [1,2,3,4,2,1,3,1,2] for i in num_list: print(i,end=" ") # 一行顯示
num_list = [1,2,3,4,2,1,3,1,2] for i in range(len(num_list)): print(num_list[i],end=" ")
squares = [] for i in range(1,6): squares.append(i**2) print(squares) #[1, 4, 9, 16, 25]
num_list = [1,2,3,4,2,1,3,1,2] print(max(num_list)) #4 print(min(num_list)) #1 print(sum(num_list)) #19
squeres = [value**2 for value in range(1,11)] print(squeres) #[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
odd_number =[] for i in range(1,11,2): odd_number.append(i) print(sum(odd_number))
three_nmu = [] for i in range(3, 91): if (i % 3==0): three_nmu.append(i) print(three_nmu)
squares = [] for i in range(3,11): squares.append(i**3) print(squares)
squares = [i**3 for i in range(3,11)] print(squares)
num_list = [1,2,3,4,2,1,3,1,2] print(num_list[0:5]) #[1, 2, 3, 4, 2] 從第一個開始取值到第五位 print(num_list[:5]) #[1, 2, 3, 4, 2] 默認會從第一個開始取值 print(num_list[5:]) #[1, 3, 1, 2] 取後面的4位
dimensions = (100,300) print(dimensions[0]) #100 for i in dimensions: print(i) #100 300
num_list = [1, 2, 3, 4, 2, 1, 3, 1, 2] for i in num_list: if i == 2: print(i)
num_list = [1, 2, 3, 4, 2, 1, 3, 1, 2] for i in num_list: if i != 2: print(i)
num_list = [1, 2, 3, 4, 2, 1, 3, 1, 2] for i in num_list: if i >=1 and i <=2: print(i)
alien = {'color':0,'points':1} print(alien['color']) #color
alien = {'color':0,'points':1} alien['color'] = 'red' print(alien) #{'color': 'red', 'points': 1}
alien = {'color':0,'points':1} del alien['color'] print(alien)
people_nums1 = {'name':'jetty','name1':'jack'} people_nums2 ={'name':'kitty','name1':'james'} peoples = [people_nums1,people_nums2] for i in peoples: print(i)
ipt = input('你是小黃麼?1(true) or 2(false)?') if ipt =='1': print('是本人') else: print('不是本人')
print(4 % 2) # 0 print(4 // 2) # 2
count = 0 arr = [] while count < 20: for j in range(1, 100): if j % 11 == 0: count = count+1 arr.append(j) print(arr)
# 簡單求和 def num_sum(arr): result =0 for i in arr: result =result+i return result print(num_sum([1,2,3,4])) #10
def num_sum(arr=[1,2,3]): result =0 for i in arr: result =result+i return result print(num_sum()) #6
def make_prize(*top): return top print(make_prize(1)) print(make_prize(1,2,3)) print(make_prize(1,3,4,5)) print(make_prize(1,1,1,1,1)) # 返回 (1,) (1, 2, 3) (1, 3, 4, 5) (1, 1, 1, 1, 1)
# 隨機數 import random print(random.randint(1,19))
class Dog(): def __init__(self,name,age): self.name =name self.age = age def sit(self): print(self.name+''+self.age) dog = Dog('jeety',24) print(dog.name)
class Car(): def __init__(self,make,model,year): self.make = make self.model = model self.year = year def getCarName(self): print(self.model) car = Car('audi','ad4',2016) print(car.make)
class Car(): def __init__(self,name): self.name = name class Elastic(Car): def __init__(self, name): super().__init__(name) myTesla = Elastic('tesla') print(myTesla.name)
class Car(): def __init__(self,make,name,color): self.make = make self.name = name self.color = color def getCarName(self): print('獲取車的名字爲'+self.name+'獲取汽車的顏色'+self.color) class Batery(): def __init__(self,batery='60'): self.batery = batery def discribe_batery(self): print('This car has'+str(self.batery)+'batery') class Elatrity(Batery): def __init__(self, batery): super().__init__(batery) self.batery = Batery() elatrity = Elatrity('100') print(elatrity.discribe_batery())
f = open('file.txt',mode="w",encoding='utf-8') print(f) f.write('叫我詹躲躲\n') f.write('叫我詹躲躲1\n') f.close()
import json numbers = [1,2,23,3,4,5,6,7,87] filename = 'numbers.json' with open(filename,'w') as f_obj: json.dump(numbers,f_obj)
import json username = input('存儲輸入的數據') filename = 'numbers.json' with open(filename,'w') as f_obj: json.dump(username,f_obj)
import json filename = 'numbers.json' with open(filename) as f_obj: username = json.load(f_obj) print('Welcome back',username)
import json filename = 'numbers.json' try: with open(filename) as f_obj: username = json.load(f_obj) except FileNotFoundError: username = input('存儲輸入的數據') with open(filename,'w') as f_obj: json.dump(username,f_obj) else: print('Welcome back',username)
import json def get_username(): filename = 'numbers.json' try: with open(filename) as f_obj: username = json.load(f_obj) except FileNotFoundError: return None else: return username def get_greeting(): username = get_username() if username: print('Welcome back',username) else: username = input('存儲輸入的數據') filename = 'numbers.json' with open(filename,'w') as f_obj: json.dump(username,f_obj) print('Welcome back',username) get_greeting()
原創:叫我詹躲躲
連接:https://segmentfault.com/a/11...json