# create a list of values import sys sys.path.append('../../../python') from main_module import create_divider create_divider('create a list of values',1,"*") colors=['red','green','bule','yellow','orange','purple','brown'] print(colors) print(type(colors)) create_divider('it\'s\' important to understand that you can add values of any data type to a list',1,"*") mixed_list=['jhon',3.14,7,False] print(mixed_list) print(type(mixed_list)) create_divider('use an index to access individual elements',1,"*") print(colors) #從左邊0~n,-1開始為倒數第1個 print(f'0-based indexing into the list ...second item: {colors[1]}') print(f'last item of the list: {colors[-1]}') print(f'next to last item in the list: {colors[-2]}') create_divider('create a slice',1,"*") print('\nPrint a slice,starting at index 2 and excluding index5:') print(colors[2:5]) print(type(colors[2:5]))
import sys sys.path.append('.././../python') import main_module main_module.create_divider(" print type", 1, symbol='*') print('type(value)') print(type('hello world')) print(type(7)) print(type(True)) print(type(False)) print(type('True')) print(type('False')) main_module.create_divider("# what's bool to object ", 1, symbol='*') main_module.create_divider("1. empty string :false", align="<", symbol=' ') main_module.create_divider("2 .0;0.0;0+0j:false", align="<", symbol=' ') main_module.create_divider("3. empty list [] :false", align="<", symbol=' ') main_module.create_divider("4. emtpy tuple () :false", align="<", symbol=' ') main_module.create_divider("5. emtpy {} dict :false", align="<", symbol=' ') main_module.create_divider("6. special None :false", align="<", symbol=' ') main_module.create_divider("# bool(str) ", 1, symbol='*') print(bool('True')) print(bool('False')) print(bool('')) print(bool(' ')) print(bool('hello world!')) main_module.create_divider("# bool(int) ", 1, symbol='*')
import sys sys.path.append('../../../python') from main_module import create_divider create_divider("return odd number between 1 and 100", 1, "-") odd_number = [odd for odd in range(100) if odd % 2 > 0] print(odd_number) create_divider("use comprehension build a dict", 0, "-") create_divider("key are 11 to 15", 0, "-") create_divider("value are key number square", 0, "-") nums = {num: num * num for num in [11, 12, 13, 14, 15]} print(nums)
import sys sys.path.append('../../../python') from main_module import create_divider create_divider("迴圈建立", 1, "*") create_divider("建立52張牌撲克,從牌組隨機抽取5張,列出剩下牌組數量與抽取的撲克牌", 1, "*") import random suits = ["hearts", "spades", "clubs", "diamonds"] ranks = [ "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king", "ace" ] cards = [] for suit in suits: for rank in ranks: cards.append(f'{rank} of {suit}') print(cards) print(f'there are {len(cards)} cards in the deck') print('dealing...') player_count = "" while isinstance(player_count, int) == False: player_count = input('any number any card :') if player_count.isnumeric(): player_count = int(player_count) else: print('please enter a number ,not string or others') selected_cards = random.choices(cards, k=int(player_count)) for selected_card in selected_cards: cards.remove(selected_card) print(f'there are {len(cards)} cards in the deck')
import sys sys.path.append('../../../python') from main_module import create_divider create_divider('test a value for inclusion in a list',1,"*") numbers=[1,3,5] print(5 in numbers) print(8 in numbers) print(5 not in numbers) print(8 not in numbers) create_divider('loop through a list',1,"*") cities=['chicage','london','tokyo'] for city in cities: print(city) create_divider('break out of a for loop',1,"*") numbers=[42,77,16,101,23,8,4,15,55] numbers.sort() for number in numbers: if number>42:break; print(number) create_divider('use an else statement',1,"*")
import __init__ import main_module from challenage1_comprehension import Game main_module.create_divider("建立52張牌撲克,從牌組隨機抽取5張,列出剩下牌組數量與抽取的撲克牌", 1, "*") main_module.create_divider("使用comprehension建立", 1, "*") game = Game() print(f'there are {len(game.all_cards)} cards in the deck') print('dealing...') game.start() print(f'there are {len(game.all_cards)} cards in the deck') print('player has the following cards in their hand: ') print(game.selected_cards)
import sys sys.path.append('../../../python') from main_module import create_divider create_divider('tuple unpacking',1,"*") numlist=[(1,2),(3,7),(9,5)] result=0 for num1,num2 in numlist: result=result+(num1*num2) print(result) create_divider('delete all nagative number',1,"*") x_list=[1,3,5,0,-1,3,-2] non_nagative_list=[x for x in x_list if x>=0] print(non_nagative_list) create_divider('please sum this list nagative number',1,"*") y_list=[[1,-1,0],[2,5,-9],[-2-3,0]] nagative_sum=sum([item for sublist in y_list for item in sublist if item <0]) print (nagative_sum) create_divider('x<5 display "very low"',0,"*") create_divider('-5>x>0 display "low"',0,"*") create_divider('x=0 display "neutral"',0,"*")
import sys sys.path.append('../../python') from main_module import create_divider create_divider("print input value",1) print("what is your name") name=input() print("hello ,"+name)
import sys sys.path.append('../../python') from main_module import create_divider create_divider("how much calorie eats today? print summary", 1) print("Today's date?") current_date = input() print("Breakfast calories?") breakfast_cal = int(input()) print("Lunch calories?") lunch_cal = int(input()) print("Dinner calories?") dinner_cal = int(input()) print("Snack calories") snack_cal = int(input()) cal_sum = breakfast_cal + lunch_cal + dinner_cal + snack_cal print("Calorie contetn for " + current_date + ":" + str(cal_sum))
import sys sys.path.append('../../python') from main_module import create_divider import random #python是dynamic lan,參數不需另外宣告,但在使用時機需準確保握並且轉換為正確型別 create_divider('使用者輸入一個1到5的數字,判斷是否猜中,並且顯示總共猜了幾次', 1) ans = random.randint(1, 5) guess = 0 count = 0 while guess != ans: count += 1 guess = input('Guess a number between 1 and 5: ') if guess.isnumeric(): guess = int(guess) else: print(f' you guessed it in {count} tries!')