# encoding: utf-8 ''' @author: mark @file: 20200214.py @time: 2020/2/14 14:21 @software: PyCharm ''' ''' #初始學習python print("hello word") #列印hello word price = 10 #初始化變數price rating = 4.9 #初始化變數rating name = "mark" #初始化變數name is_published = False #初始化變數is_published print(price) #列印price ''' ''' #控制臺輸入使用 name = input('what is your name? ') #控制臺輸入值賦值給name favorite_color = input("what is your favorite color ? ") #控制臺輸入值賦值給favorite_color print("Hi " + name + "your facorite_color " + favorite_color) #列印Hi name your facorite_color favorite_color ''' ''' #控制臺輸入使用 birth_year = input("Birth year: ") #控制臺輸入值賦值給birth_year print(type(birth_year)) #列印控制臺輸入值的型別 age = 2020 - int(birth_year) #根據控制臺輸入的值計算年齡 print(type(age)) #列印年齡的型別 print(age) #列印年齡 ''' ''' #控制臺輸入使用,以及加減乘除運算 weight_lbs = input('Weight (lbs): ') #控制臺輸入值賦值給weight_lbs weight_kg = float(weight_lbs) * 0.45 #weight_lbs 轉換為公斤 print(weight_kg) #列印體重kg weight_kg = input('Weight (kg): ') #控制臺輸入體重kg weight_lbs = float(weight_kg) / 0.45 #體重公斤轉換為磅 print(weight_lbs) #列印體重 磅 print(10 / 3) #除法,帶小數點 print(10 // 3) #取商 print(10 % 3) #取余 print(10 ** 3) #次方 x = 10 #初始化X x += 3 #X自加3 X = X + 3 print(x) #列印X x -= 3 #X自減3 X = X - 3 print(x) #列印# x = 10 + 3 ** 3 ** 2 # 同理 x = 3 ** (3 ** 2) print(x) ''' ''' #字串的截取 course = "Python's Course for Beginners " #初始化字串 print(course) #列印字串 print(course[0]) #列印字串第一個字符 print(course[0:3]) #列印字串第一個到第四個字符 print(course[1:]) #列印從第二個字符開始的整個字串 print(course[:5]) #列印開始的五個字串 print(course[-2]) #列印倒數第二個字符 print(course[1:-1]) #列印第二個字符到倒數第二個字符 ''' ''' #f字串使用,呼叫變數 first = 'John' #初始化變數first last = 'Smith' #初始化變數last message = first + ' [' + last + '] is a coder' #字串拼接 print(message) #列印拼接的字串 msg = f'{first} [{last}] is a coder' #使用f字串進行拼接字串 print(msg) #列印拼接的字串 ''' ''' #字串操作 course = 'Python for Beginners' #初始化字串 print(course.find('Beginners')) #查找第一次出現Beginners時的腳標 print(course.replace('Beginners', 'Absoulte Beginners')) #Absoulte Beginners替換字串中的Beginners print('Python' in course) #查看Python是否在字串中 print(len(course)) #查看字串的長度 print(course.upper()) #字串全部大寫 print(course.lower()) #字串全部小寫 ''' ''' #math計算 import math #引入math x = 2.9 #初始化X print(round(x)) #四舍五入 print(abs(-x)) #絕對值 print(math.ceil(x)) #向上取整 print(math.floor(x)) #向下取整 ''' ''' #if陳述句 is_hot = True #初始化is_hot is_cold = True #初始化is_cold if is_hot : #判斷是否為真 print("It's a hot day") #列印It's a hot day print("Drink plenty of water") #列印Drink plenty of water elif is_cold: #判斷是否為真 print(" It's a cold day") #列印It's a cold day print("Wear warm clothes") #列印Wear warm clothes else: #其他情況 print("It's a lovely day") #列印It's a lovely day print(" Enjoy your day") #列印Enjoy your day price = 1000000 #初始化price has_good_credit = True #初始化has_good_credit if has_good_credit: #判斷has_good_credit是否為真 down_payment = 0.1 * price #運算 else: #其他情況 down_payment = 0.2 * price #運算 print(f"Down payment: ${down_payment}") #列印 Down payment: $down_payment ''' ''' #if陳述句以及邏輯連接 has_high_income = True #初始化has_high_income has_good_credit = True #初始化has_good_credit has_criminal_record = False #初始化has_criminal_record if has_good_credit and has_high_income: #has_good_credit和has_high_income是否同時為真 print("has_good_credit and has_high_income Eligible for loan") #列印has_good_credit and has_high_income Eligible for loan if has_good_credit or has_high_income: #has_good_credit和has_high_income是否有一個為真 print("has_good_credit or has_high_income Eligible for loan") #列印has_good_credit or has_high_income Eligible for loan if has_good_credit and not has_criminal_record: #has_good_credit為真且has_criminal_record為假 print("Eligible for loan") #列印Eligible for loan temperature = 30 #初始化temperature if temperature > 30: #temperature > 30是否為真 print("It's a hot day") #列印It's a hot day else: #其他 print("It's not a hot day") #列印It's not a hot day #名字長度判斷 name = "John smith" #初始化name if len(name) < 3: #判斷len(name) < 3是否為真 print("Name must be at least 3 characters.") #列印 elif len(name) > 50: #判斷len(name) > 50是否為真 print("Name must be a maximum of 50 characters.") #列印 else: #其他情況 print("Name looks good!") #列印 #體重轉換 weight = int(input('Weight: ')) #對控制臺輸入的值進行型別轉換 unit = input('(L)bs or (K)g: ') #控制臺輸入值賦值給unit if unit.upper() == "L": #unit.upper() == "L"是否為真 converted = weight * 0.45 #運算 print(f"You are {converted} kilos") #列印You are converted kilos else: #其他情況 converted = weight / 0.45 #運算 print(f"You are {converted} kilos") #列印You are converted kilos ''' ''' #while回圈陳述句 i = 1 #初始化i while i <= 5: #i <= 5是否為真 print('*' * i) #列印* i += 1 #自加 print("Done") #列印Done #猜數字 secret_number = 9 #初始化secret_number guess_count = 0 #初始化guess_count guess_limit = 3 #初始化guess_limit while guess_count < guess_limit: #guess_count < guess_limit是否為真 guess = int(input('Guess: ')) #控制臺輸入賦值給guess guess_count += 1 #自加 if guess == secret_number: #guess == secret_number是否為真 print('You win!') #列印You win! break #跳出回圈 else: #其他 print('Sorry , you failed!') #列印Sorry , you failed! ''' ''' #while回圈和if陳述句結合的案例 #開車,①quit為退出,help為幫助資訊,start為開始,stop為結束,②且已經開啟的不能再次開啟 command = "" #初始化command用以判斷是否跳出回圈 started = False #初始化started,默認車是關閉狀態,防止車被重復開啟 while command.lower() != "quit": #判斷是否可以跳出回圈 command = input("> ").lower() #控制臺輸入值賦給command if command.lower() == "help": #判斷是否需要幫助 print('start/stop/quit') #輸出幫助資訊 elif command.lower() == 'start': #判斷是否為開車資訊 if started: #車是否已經開啟 print("car is already start") #列印car is already start else: #車沒有開啟 started = True #變更車的狀態資訊 print('Start the car') #列印Start the car elif command.lower() == 'stop': #判斷是否為停車資訊 if not started: #車是否已經停 print("car is already stopped") #列印car is already stopped else: #車沒有停 started = False #變更車的狀態資訊 print('stop the car ') #列印stop the car elif command == "quit": #判斷是否為退出資訊 break #跳出回圈 else: #其他情況 print("don't understand") # 列印don't understand ''' ''' #回圈遍歷 prices = [10, 20, 30] #初始化prices total = 0 #設定遍歷的初始值 for price in prices: #遍歷的條件 total += price #自加 print(f"Total:{total}") #輸出遍歷結果 ''' ''' #所有坐標 for x in range(4): #設定回圈條件 for y in range(3): #設定二級回圈條件 print(f'({x},{y})') #輸出結果 numbers = [5, 3, 5, 3, 3] #設定初始numbers for x_count in numbers: #設定回圈條件 # print('X' * x) output = '' #輸出值初始設定 for count in range(x_count): #設定輸出值回圈條件 output += 'X' #輸出特征設定 print(output) #列印輸出 #陣列串列 names = ['John', 'Bob', 'Mosh', 'Sarah'] #設定初始串列names print(names) #列印輸出串列 print(names[0]) #列印第一個元素 print(names[2:]) #列印第三個到最后一個元素 print(names) #查詢串列中的最大值 numbers = [3, 6, 2, 8, 4, 10] #設定初始串列numbers max = numbers[0] #設定最大值的初始值 for number in numbers: #設定回圈條件 if number > max: #進行判斷是否為最大值 max = number #賦值 print(max) #輸出最大值 ''' ''' #陣列操作 matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ] #初始化陣列matrix print(matrix[0][2]) #列印輸出陣列中第一個元素中的第三個元素 for row in matrix: #設定回圈串列中的第一層 for low in row: #設定回圈串列中的第二層 print(low) #輸出列印 ''' ''' #串列中的操作 numbers = [5, 2, 1, 5, 7, 4] #初始化串列 numbers print(numbers.sort()) #反向排序 numbers.reverse() #按照從大到小排列 print(numbers) #列印 numbers.append(14) #串列中加入新的值 print(numbers) #列印 numbers.insert(2, 100) #在串列的第三個位置加入100 print(numbers) #列印 numbers.remove(2) #去除串列中的2 print(numbers) #列印 numbers2 = numbers.copy() #賦值 print(numbers2) #列印 numbers.pop() #去除末尾的值 print(numbers) #列印 print(numbers.index(1)) #串列中的第二個值 print(numbers.count(5)) #串列中出現5的次數 numbers.clear() #清除串列 print(numbers) #列印 #清楚串列中的重復項 numbers = [5, 2, 3, 3, 2, 5, 23, 32] #初始化串列numbers for number in numbers: #設定回圈條件 if numbers.count(number) != 1: # 查詢是否在串列里唯一 numbers.remove(number) #移除重復的元素 # numbers.append(number) # print(numbers) #列印沒有重復值的串列 uniques = [] #設定一個空串列 for number in numbers: #設定回圈條件 if number not in uniques: #設定判斷條件 uniques.append(number) #將符合條件的元素添加到空串列中 print(uniques) #列印新的串列 ''' ''' #字典 customer = { "name": "John Smith", "age": 30, "is_verified": True } #初始化字典customer customer["name"] = "Mark" #更改對應key的value print(customer["name"]) #列印對應key的value print(customer["is_verified"]) #列印對應key的value print(customer.get("name")) #列印對應key的value ''' ''' #字典應用,輸入數字對應英文 digits_mapping = { "1": "One", "2": "Two", "3": "Three", "4": "Four", "5": "Five", "6": "Six", "7": "Seven", "8": "Eight", "9": "Nine", } #初始化字典digits_mapping phone = input("Phone: ") #控制臺輸入 output = '' #初始化輸出 for ch in phone: #設定回圈條件 output += digits_mapping.get(ch, "!") + " " #獲取對應字典中的value print(output) #列印 '''
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/190368.html
標籤:Python
