一起學Python吧~Day02

#!/bin/env python3
#-*- coding:utf8 -*-
#學python3的第二天
#語法結構\
# py_str = 'Python'
# if 'th' in py_str:
#     print('th在%s'% py_str)
# if -0.0:
#     print('值爲0的數字爲假')
# if ' ':
#     print('空格也是個字符,爲真')
# if []:
#     print('空列表爲假') #空的都不打印
# if ['']:
#     print('列表不爲空所以打印')
# if (1,2):
#     print('非空元組打印')
# if {'name': 'bob'}:
#     print('非空字典打印')
# if not {}:
#     print('空字典爲假,取反打印')
# if not None:
#     print("None表示空對象,類似於mysql中的null,爲假,取反爲真")
# #知識簡用:阿坤的QQ號位數截取
# num = input("Please input number: ")
# if num == 1:
#     n = input('Please a n[]')
#     if n == 9:
#         print('This is QQ NO.1 number 9!')
#     print('This is number one!')
# elif  num == 2:
#     n2 = input('Please a n[]')
#     if n2 == 7:
#         print('This is QQ NO.2 number 7!')
#     print('This is number two!')
# elif  num == 3:
#     n3 = input('Please a n[]')
#     if n3 == 4:
#         print('This is QQ NO.3 number 4!')
#     print('This is number three!')
# elif  num == 4:
#     n4 = input('Please a n[]')
#     if n4 == 8:
#         print('This is QQ NO.4 number 8!')
#     print('This is number four!')
# elif  num == 5:
#     n5 = input('Please a n[]')
#     if n5 == 5:
#         print('This is QQ NO.5 number 5!')
#     print('This is number five!')
# elif  num == 6:
#     n6 = input('Please a n[]')
#     if n6 == 2:
#         print('This is QQ NO.6 number 2!')
#     print('This is number six!')
# elif  num == 7:
#     n7 = input('Please a n[]')
#     if n7 == 7:
#         print('This is QQ NO.7 number 7!')
#     print('This is number seven!')
# elif  num == 8:
#     n8 = input('Please a n[]')
#     if n8 == 2:
#         print('This is QQ NO.8 number 2!')
#     print('This is number eight!')
# elif  num == 9:
#     n9 = input('Please a n[]')
#     if n9 == 7:
#         print('This is QQ NO.9 number 7!')
#     print('This is number nine!')
# else:
#     print('Please input options!')
# #方法一
# a = 10
# b = 20
# if a < b:
#     s1 = a
# else:
#     s1 = b
# print('s1=%s'% s1)
# #方法二(今後必須掌握)
# s2 = a if a < b else  b
# print('s2=%s'% s2)
# #案例一
# import getpass #導入getpass模塊(不會明文顯示密碼)
#
# uname = input('username: ')
# upass = getpass.getpass('password: ')
#
# if uname == 'bob' and upass == '123456':
#     print('\033[1;32mWelcome go home %s\033[0m'% uname) #顯示高亮度綠色
# else:
#     print('\033[1;31mError: username or password is Null!\033[0m') #顯示高亮度紅色
# #案例二 grade.py
# #方法一:常規
# grade = input('Please input your grade: ')
# gd = int(grade)
# if gd >= 90:
#     print('\033[1;32m恭喜你成績優秀!\033[0m')
# elif gd >= 80:
#     print('\033[1;33m恭喜你成績優秀!\033[0m')
# elif gd >= 70:
#     print('\033[1;34m恭喜你成績良好!\033[0m')
# elif gd >= 60:
#     print('\033[1;35m恭喜你成績合格!\033[0m')
# else:
#     print('\033[1;31m成績不合格!!\033[0m')
# #方法二:區間
# score = int(input('分數: '))
#
# if 60 <= score < 70:
#     print('及格')
# elif 70 <= score < 80 :
#     print('良好')
# elif 80 <= score < 90 :
#     print('優秀')
# else:
#     print('你要努力了')
# #方法三:and
# score = int(input('分數: '))
#
# if score <= 60 and score < 70:
#     print('及格')
# elif score <= 70 and score < 80 :
#     print('良好')
# elif score <= 80 and score < 90 :
#     print('優秀')
# else:
#     print('你要努力了')
# #猜拳遊戲
# #思路:人的三種情況(結構)+每種情況下出現的子情況=計算機的三種情況(內嵌)
# #方法一:常規
# import random #導入隨機模塊
#
# all_choices = ['石頭','剪刀','布']
# computer = random.choice(all_choices)
# player = input('請出拳(石頭/剪刀/布): ')
#
# if player == '石頭':
#     if computer == '石頭':
#         print('It ends in a draw!')
#     elif computer == '剪刀':
#         print("You Win!")
#     else:
#         print('Game Over!')
# elif player == '剪刀':
#     if computer == '石頭':
#         print('Game Over!')
#     elif computer == '剪刀':
#         print("It ends in a draw!")
#     else:
#         print('Game Over!')
# else:
#     if computer == '石頭':
#         print('You Win!')
#     elif computer == '剪刀':
#         print("Game Over!")
#     else:
#         print('It ends in a draw!')
# if '': #如果用戶不輸入
#     print('Please input options!!!')
# #方法二:自己的方法
# import random #導入隨機模塊
#
# all_choices = ['石頭','剪刀','布']
# win_list = [['石頭','剪刀'],['剪刀','布'],['布','石頭']]
# computer = random.choice(all_choices)
# player = input('請出拳(石頭/剪刀/布): ')
# result = 'It ends in a draw!' if computer == player else ('WIN!' if [player,computer] in win_list else 'LOSE!')
#
# print('You %s'% result)
# #方法三:張老師版本
# import random #導入隨機模塊
#
# all_choices = ['石頭','剪刀','布']
# #人在前,計算機在後,組成小列表,把人贏的情況再放大到列表中
# win_list = [['石頭','剪刀'],['剪刀','布'],['布','石頭']] #也可以換成lose_list/draw_list
# computer = random.choice(all_choices)
# player = input('請出拳(石頭/剪刀/布): ')
#
# print("Your choice: %s, computer's choice: %s"% (player,computer))
# if player == computer:
#     print('It ends in a draw!')
# elif [player,computer] in win_list:
#     print('You WIN!!!')
# else:
#     print('You LOSE!!!')
# #優化版本
# import random #導入隨機模塊
#
# all_choices = ['石頭','剪刀','布']
# #人在前,計算機在後,組成小列表,把人贏的情況再放大到列表中
# win_list = [['石頭','剪刀'],['剪刀','布'],['布','石頭']] #也可以換成lose_list/draw_list
# prompt = """(0)石頭
# (1)剪刀
# (2)布
# 請選擇(0/1/2): """
# computer = random.choice(all_choices)
# ind = int(input(prompt)) # 將用戶輸入的數字字符轉爲數字
# player = all_choices[ind] # 將數字作爲下標從列表中取出元素
#
# print("Your choice: %s, computer's choice: %s"% (player,computer))
# if player == computer:
#     print('It ends in a draw!')
# elif [player,computer] in win_list:
#     print('You WIN!!!')
# else:
#     print('You LOSE!!!')
# #優化版本2
# import random #導入隨機模塊
#
# all_choices = ['石頭','剪刀','布']
# #人在前,計算機在後,組成小列表,把人贏的情況再放大到列表中
# win_list = [['石頭','剪刀'],['剪刀','布'],['布','石頭']] #也可以換成lose_list/draw_list
# prompt = """(0)石頭
# (1)剪刀
# (2)布
# 請選擇(0/1/2): """
# pwin = 0 # 人的計分板
# cwin = 0 # 計算機的計分板
#
# while pwin < 2 and  cwin < 2:
#     # 人和計算機都沒有贏夠兩次則繼續
#     computer = random.choice(all_choices)
#     ind = int(input(prompt)) # 將用戶輸入的數字字符轉爲數字
#     player = all_choices[ind] # 將數字作爲下標從列表中取出元素
#
#     print("Your choice: %s, computer's choice: %s"% (player,computer))
#     if player == computer:
#         print('\033[1;32mIt ends in a draw!\033[0m')
#     elif [player,computer] in win_list:
#         print('\033[1;31mYou WIN!!!\033[0m')
#         pwin += 1 # 人贏得時候,計算器加1(人計分板)
#     else:
#         print('\033[1;31mYou LOSE!!!\033[0m')
#         cwin += 1 # 計算機贏的時候,計算器加1(計算機計分板)
# #1)
# import random #導入隨機模塊
#
# all_choices = ['石頭','剪刀','布']
# #人在前,計算機在後,組成小列表,把人贏的情況再放大到列表中
# lose_list = [['剪刀','石頭'],['布','剪刀'],['石頭','布']]
# computer = random.choice(all_choices)
# player = input('請出拳(石頭/剪刀/布): ')
#
# print("Your choice: %s, computer's choice: %s"% (player,computer))
# if player == computer:
#     print('It ends in a draw!')
# elif [player,computer] in lose_list:
#     print('You LOSE!!!')
# else:
#     print('You WIN!!!')
# #2)
# import random #導入隨機模塊
#
# all_choices = ['石頭','剪刀','布']
# #人在前,計算機在後,組成小列表,把人贏的情況再放大到列表中
# peace_list = [['石頭','石頭'],['剪刀','剪刀'],['布','布']] #也可以換成lose_list/draw_list
# win_list   = [['石頭','剪刀'],['剪刀','布'],['布','石頭']]
# computer = random.choice(all_choices)
# player = input('請出拳(石頭/剪刀/布): ')
#
# print("Your choice: %s, computer's choice: %s"% (player,computer))
# if [player,computer] in win_list:
#     print('You WIN!')
# elif [player,computer] in peace_list:
#     print('It ends in a draw!!!')
# else:
#     print('You LOSE!!!')
# #午間練習:時長兩小時
# print('hello world!')
#
# if 3 > 0:
#     print('OK')
#     print('yes')
#
# x = 3;y = 4 #不推薦,還是應該寫成兩行,可讀性差
# print(x + y)
# print('hello world!')
# print('hello','world')
# print('hello' + 'world')
# print('hello','world',sep='***') #單詞間用***分隔
# print('#'*50) #*號表示重複50遍
# print('How are you?',end='') #默認print會打印回車,end=''表示不要回車
# print(5/2) #2.5
# print(5//2) #丟棄餘數,只保留商
# print(5%2) #求餘數
# print(5**3) #5的3次方
# print(5>3) #返回True
# print(3 > 5) #返回False
# print(20 > 10 > 5) #python支持連續比較
# print(20 > 10 and 10 > 5) #與上面相同含義
# print(not 20 > 10) #False
# number = input('Please input number: ') #input用於獲取鍵盤輸入
# print(number)
# print(type(number)) #input 獲得的數據是字符型
#
# print(number + 10) #報錯,不能把字符和數字做運算
# print(int(number) + 10) #int 可以將字符串10轉換成數字10
# print(number + str(10)) #str將10轉換爲字符串後實現字符串拼接
# username = input('username: ')
# print('welcome',username) #print各項間默認以空格作爲分隔符
# print('welcome' + username) #注意引導號內最後的空格
#
# sentence = 'tom\'s pet is a cat' #單引號中間還有單引號,可以轉譯
# print(sentence)
# sentence2 = "tom's pet is a cat" #也可以用雙引號包含單引號
# print(sentence2)
# sentence3 = "tom said:\"Hello World!\""
# print(sentence3)
# sentence4 = 'tom said:"Hello World!"'
# print(sentence4)
# #三個連續的單引號或雙引號,可以保存輸入格式,允許輸入多行字符串
# words ="""
# hello
# world
# abcd"""
# print(words)
#
# py_str='python'
# print(len(py_str)) #取長度
# print(py_str[0]) #第一個字符
# print('python'[0])
# print(py_str[-1]) #最後一個字符
# #py_str[6] #錯誤,下標超出範圍
# print(py_str[2:4]) #切片取法,起始下標包含,結束下標不包含
# print(py_str[2:]) #從下標爲2的字符取到結尾
# print(py_str[:2]) #從開頭取到下標是2之前的字符
# print(py_str[:]) #取出全部
# print(py_str[::2]) #步長值爲2,默認是1
# print(py_str[1::2]) #取出yhn
# print(py_str[:-1]) #步長爲負,表示自右向左取
#
# print(py_str + 'is good ') #簡單的拼接到一起
# print(py_str *3) #把字符串重複3遍
#
# print('t' in py_str) # True
# print('th' in py_str) # True
# print('to' in py_str) # False
# print('to' not in py_str) # True
#循環語句
#能預知循環次數就用for,反之用while
# result = 0 # 創建變量用於保存累加的結果
# counter = 1 # 創建計數器,將其值累加到result中
# #0+....+100=5050
# while counter <= 100:
#     result += counter #result: 1
#     counter += 1      # counter: 2
# print(result)
# #現學現賣
# import getpass
#
# while True:
#     usr = input('username: ')
#     if usr == '':
#         print('\033[1;31mUsername not NULL!!!\033[0m')
#     elif usr == 'csdnak' :
#         pas = getpass.getpass('password: ')
#         if pas == '123456':
#             print('\033[1;32mWelcome %s world!\033[0m' % usr)
#             break
#         else:
#             print('\033[1;31mError: username or password input is False!\033[0m')
#             print('\033[1;33mPlease re-enter!\033[0m')
# #計算100以內偶數之和
# result = 0 # 創建變量用於保存累加的結果
# counter = 1 # 創建計數器,將其值累加到result中
#
# while counter < 100:
#     counter += 1
#
#     # if counter % 2 == 1:
#     if counter % 2: # 結果只有1或0,1爲真,0爲假
#         continue
#
#     result += counter
#
# print(result)
# #猜字遊戲
# import random
#
# number = random.randint(1,100)
# counter = 0
# #print(number) #開掛
# while counter < 5:
#     counter += 1
#     answer = int(input('guess(1-100): '))
#     if answer > number:
#         print('猜大了')
#     elif answer < number:
#         print('猜小了')
#     else:
#         print('猜對了')
#         break
# else:
#     #循環也有else語句,如果循環被break,else不執行,否則執行
#     print('這個數是: %s' % number)
# #for循環
# s1 = 'hello'
# nums = [10,20,15,80]
# names = ('tom', 'jerry')
# user = {'name': 'bob','age': 20}
#
# for st in s1:
#     print(st)
# print('*' * 50)
#
# for i in nums:
#     print(i)
# print('*' * 50)
#
# for name in names:
#     print(name)
# print('*' * 50)
#
# for usr in user:
#     print(usr,user[usr])
# print('*' * 50)
# #for循環常與range連用
# print(range(10)) # range只是一個對象,潛在可以生成很多數
# #range沒有給起始值,從0開始,結束數字不包含
# for i in range(10):
#     print(i)
#
# #將range轉換成列表在打印
# print(list(range(10)))
#
# for n in list(range(10)):
#     print(n)
# print('*' * 50)
#
# #打印6到10的列表
# print(list(range(6,11)))
# #倒着打印10到1
# print(list(range(10,0,-1)))
#
# for number in list(range(6,11)):
#     print(number)
# #如果直接用列表顯示,數字多的話就會霸屏
# print(list(range(10000)))
# #神奇的菲波那切數列
# fib = [0, 1]
#
# for i in range(8):
#     fib.append(fib[-1] + fib[-2]) #只能是-1,-2 因爲後面的兩位一直在變(是向後延伸的)
# print(fib)
#打印9x9乘法表
"""
原理:
i取值[1,2,3]
i == 1, j取值[1,2,3]
i == 2, j取值[1,2,3]
...
print默認會在結束打印換行,可以使用end=' '替換爲空格
"""
# #實例:
# for i in range(1,4):
#     for j in range(1,i + 1):
#         print('hello',end=' ')
#     print() #用來起到換行作用(print默認在結束時自帶換行)
# #具體實現
# for i in range(1,10):
#     for j in range(1,i + 1):
#         print(j,"x",i,"=",i*j,end='  ') #根據外觀調試i和j調換了順序
#     print()
# #優化版9*9
# for i in range(1,10): # [1,2,3...9]
#     for j in range(1,i + 1): # [1,2,3...9]
#         print('%sx%s=%s' % (j ,i ,i * j),end=' ')
#     print()
# #列表解析
# print([5])
# print([5 + 2]) # 將表達式到列表
# print([5 + 2 for i in range(1,11)]) # 循環決定表達式計算的次數
# print([5 + i for i in range(1,11)]) # 在表達式中用循環的變量
# #從中抽出了i = [1,3,5,7,9]
# print([5 + i for i in range(1,11) if i %2]) # 判斷條件作爲過濾條件(1爲真0爲假不輸出所以i%2==1和i%2結果一樣)
# print(['192.168.1.%s' % i for i in range(1,255)])
#晚間聯繫:時長兩小時
# alist = [10,20,30,'bob','alice',[1,2,3]]
# print(alist)
# print(alist[-1]) #取出最後一項
# print(alist[-1][-1]) #因爲最後一項是列表,列表還可以繼續取下標
# print([1,2,3][-1]) #[1,2,3]是列表,[-1]表示列表最後一項
# print(alist[-2][2]) #列表倒數第2項是字符串,在取出字符下標爲2的字符
# print(alist[3:5]) #['bob','alice']
# print(10 in alist) #True
# print('o' in alist) #False
# print(100 not in alist) #True
# #修改最後一項的值
# alist[-1] = 100
# print(alist)
# #向列表中追加一項
# alist.append(200)
# print(alist)
#
# atuple = (10,20,30,'bob','alice',[1,2,3]) #元祖與列表基本上是一樣的,只是元祖不可變列表可變.
# print(len(atuple))
# print(10 in atuple)
# print(atuple[2])
# print(atuple[3:5])
# #atuple[-1]= 100 ,這樣寫是不對的(元組是不可變的)
#
# #字典是key-value鍵值對形式的,沒有順序,通過鍵值取出值(key來取value)
# adict = {'name':'csdnak' ,'age':23}
# print(len(adict))
# print('csdnak' in adict) #False
# print('name' in adict) #True
# adict['email']= '[email protected]' #字典中沒有key,則添加新項目
# print(adict)
# adict['age']= 25 #字典中已有key,修改對應的value
# print(adict)
#
# if 3 > 0:
#     print('yes')
#     print('ok')
#
# if 10 in [10,20,30]:
#     print('ok')
#
# if -0.0:
#     print('yes') #任何值爲0的數字都是False
#
# if [1,2]:
#     print('yes') #非空對象都是True
#
# if '':
#     print('yes') #空格字符也是義字符,條件爲True
#
# a = 10
# b = 20
#
# if a < b:
#     smaller = a
# else:
#     smaller = b
#
# print(smaller)
#
# s = a if a < b else b #和上面的if-else語句等價
#
# print(s)
#
#
# import getpass #導入模塊
#
# username = input('username: ')
# #getpass模塊中,有一個方法也叫getpass
# password = getpass.getpass('password: ')
#
# if username == 'csdnak' and password == '123456':
#     print('Login successful!')
# else:
#     print('Login incorrect')
#
# import random
#
# num = random.random(1,10) #隨機生成1-10之間的數字
# answer = int(input('guess a number: ')) #將用戶輸入的字符轉換成整數
# if answer > num:
#     print('猜大了')
# elif answer < num:
#     print('猜小了')
# else:
#     print('猜對了')
#
# print('the number: ',num)
#
#
# score = int(input('分數: '))
#
# if score >= 90:
#     print('優秀')
# elif score >= 80:
#     print('好')
# elif score >= 70:
#     print('良好')
# elif score >= 60:
#     print('及格')
# else:
#     print('你要努力了')
#
# if score >= 60 and score < 70:
#     print('及格')
# elif 70 <= score < 80:
#     print('良好')
# elif 80 <= score < 90:
#     print('優秀')
# else:
#     print('你要努力了')
#
#
# import random
#
# all_choices = ['石頭','剪刀','布']
# computer = random.choice(all_choices)
# player = input('請出拳: ')
#
# #print('Your choice: ',player,"Computer's choice: ",computer)
# print("Your choice: %s,Computer's choice: %s"% (player,computer))
# if player == '石頭':
#     if computer == '石頭':
#         print('平局')
#     elif computer == '剪刀':
#         print('你贏了')
#     else:
#         print('You LOSE ')
# elif player == '剪刀':
#     if computer == '石頭':
#         print('You LOSE')
#     elif computer == '剪刀':
#         print('平局')
#     else:
#         print('You WIN!!!')
# else:
#     if computer == '石頭':
#         print('You WIN!!')
#     elif computer == '剪刀':
#         print('You LOSE!!!')
#     else:
#         print('平局')
#
# import random
#
# all_choices = ['石頭','剪刀','布']
# win_list = [['石頭','剪刀'],['剪刀','布'],['布','石頭']]
# prompt = """(0)石頭
# (1)剪刀
# (2)布
# 請選擇(0/1/2)"""
# computer = random.choice(all_choices)
# ind = int(input(prompt))
# player = all_choices[ind]
#
# print("'Your choice: %s,Computer's choice: %s" % (player,computer))
# if player == computer:
#     print("\033[32;1m平局\033[0m])]")
# elif [player,computer] in  win_list:
#     print('\033[31;1mYou WIN!!!\033[0m')
# else:
#     print('\033[31;1mYou LOSE!!!\033[0m')
#
# import random
#
# num = random.randint(1,10)
# running = True
#
# while running:
#     answer = int(input('guess the number: '))
#     if answer > num:
#         print('猜大了')
#     elif answer < num:
#         print('猜小了')
#     else:
#         print('猜對了')
#         running = False
#
# import random
#
# num = random.randint(1,10)
# counter = 0
#
# while counter < 5:
#     answer = int(input('guess the number: '))
#     if answer > num:
#         print('猜大了')
#     elif answer < num:
#         print('猜小了')
#     else:
#         print('猜對了')
#         break
#     counter += 1
# else: #循環被break就不執行了,沒有被break才執行
#     print('the number is: ',num)
#
#
# sum100 = 0
# counter = 1
#
# while counter < 101:
#     sum100 += counter
#     counter += 1
#
# print(sum100)
#
#
# while True:
#     yn = input('Continue(y/n)')
#     if yn in ['n','N']:
#         break
#     print('running...')
#
# sum100 = 0
# counter = 0
#
#
# while counter < 100:
#     counter += 1
#     #if counter % 2:
#     if counter % 2 ==1:
#         continue
#     sum100 += counter
#
# print(sum100)
#
# astr = 'hello'
# alist = [10,20,30]
# atuple = ('bob','tom','alice')
# adict = {'name':'john','age':'23'}
#
# for ch in astr:
#     print(ch)
#
# for i in alist:
#     print(i)
#
# for name in atuple:
#     print(name)
#
# for key in adict:
#     print('%s:%s'% (key,adict[key]))
#
# for i in range(1,10): # [1,2,3...9]
#     for j in range(1,i + 1): # [1,2,3...9]
#         print('%sx%s=%s' % (j ,i ,i * j),end=' ')
#     print()
# #菲波那切數列
# fib = [0, 1]
# for i in range(8):
#     fib.append(fib[-1] + fib[-2])
# print(fib)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章