python3 認真學習的第一天,從零開始

python 認真學習的第一天,從零開始
‘’’
#學習參考書李寧老師的python從菜鳥到高手

print('a','b','c','d','e',end='')
print('a','b','c','d','e',sep='&')
print('a'+'b'+'c'+'d'+'e')
print('hello',end=' ')
print('world')

#有趣的賦值操作
a,b,c=1,2,3
print(a,b,c,sep=',')
x,y=20,30
x,y=y,x
d=f=20#鏈式賦值
print(x,y,d,f,sep=',')
# 條件語句
n=1
if n == 1:
    print('n=3啊!')
print('if 語句結束!')

n=2
if n == 1:
    print('n==1')
else:
    print('n!=1')
print('結束')
#不能區分大小寫
from click._compat import raw_input
name=input('請輸入你的姓名:')
if name.startswith("b"):
    print('名字以b開頭')
elif name.startswith("c"):
    print('名字以c開頭')
elif name.startswith("d"):
    print('名字以d開頭')
else:
    print('名字以其它字母開頭')
print('if 語句結束啊!')

name=input('請輸入你的姓名:')
if name.startswith('Bill'):
    if name.endswith('Gatess'):
        print('歡迎 Bill Gatess先生')
    elif name.endswith('Clinton'):
        print('歡迎克林頓先生')
    else:
        print('未知的姓名')
elif name.startswith('李'):
    if name.endswith('明'):
        print('歡迎李明老師')
    else:
        print('未知的姓名')
else:
    print('未知的姓名')
print('程序結束!')


**#簡單邏輯運算符
#==等於,!=不等於,<小於,>大於,>=大於或等於,<=小於或等於,x is y xy是同一個對象\
#x is not y  xy不是同一個對象,x in y x是y容器的成員,y=[1,2,3,4],x=1屬於
#x not in y ,x=12就不屬於y容器的成員**
>>> 'hello'=='hello'
True
>>> 'Hello'=='hello'
False
>>> 20==30
False
>>> 20>30
False
>>> 30>20
True
>>> 'hello'!='Hello'
True
>>> 'hello'>'Hello'
True
>>> 'hello'>'Hello World'
True
>>> x=y=[1,2,3]
>>> z=[1,2,3]
>>> x==y
True
>>> x==z
True
>>> x is y
True
>>> x is y #x和z 的值是一樣,但是對象不一樣
True
>>> x is z
False
>>> x is not z
True
>>> s='hello world'
>>> 'e' in s
True
>>> 'hello' in s
True
>>> 'hewo' in s
False
>>> 'lo' in s
True
>>> 'ow' in s #空格也算是字符
False
>>> 'o w'
'o w'
>>> 'o w' in s
True

**#斷言,條件不滿足,會拋出異常**
value = 20
assert value <10 or value>30 ,'value的值必須小於十或者大於30'
#Traceback (most recent call last):
  #File "<stdin>", line 1, in <module>
#AssertionError: value的值必須小於十或者大於30

name="Bill"
age=200
assert  0<age<150,'年齡必須是0~150之間'
print("hello world!")
#while for 循環
x=1
while x<=10:
    print(x)
    x+=1

keywords=['this','is','an','pig','bgd','66']
for asss in keywords:
    print(asss)


number=[1,2,3,4,5,6,7,8,9,10]
for number in number:
    print(number,end=' ')

for number in range(1,11):
    print(number,end =' ')

x=1
while x<=10:
    print(x,end=" ")
    x+=1

for a in range(1,101):
    print(a,end=' ')

for b in range(1,99):
    print(b*b,end=' ')

**#跳出循環 break continue**
x=1
while x<=1000:
    if x== 10:
        break
    print(x,end=' ')
    x+=1
#1 2 3 4 5 6 7 8 9跳出整個循環
x=1
while x<=10:
    if x == 5:
        x+=1
        continue
    print(x,end=' ')
    x+=1
**#1 2 3 4 6 7 8 9 10只跳出滿足當前條件的循環**


name=['black','book','cloo','aoo']
for i in name:
    if not i.startswith('b'):
        break
    print(i,end=' ')

name=['black','cloo','aoo','book']
for i in name:
    if  not i.startswith('b'):
        continue
    print(i,end=' ')

arr1=[1,2,3,4,5]
arr2=['book','bule','keys']
arr3=[arr1,arr2]
i = 0
while i < len(arr3):
    for value in arr3[i]:
        print(value,end=' ')
    i=i+1
    print()


import random
x=0
while x < 10:
    x+=1
    if x==random.randint(1,20):
        print(x,end=' ')
        break
else:
    print('沒有中斷while循環')
    

import random
x=0
break_flag=False
while x<=10:
    x+=1
    if x==random.randint(1,20):
        break_flag=True
        print(x)
        break
if not break_flag:
    print('沒有中斷while循環')

import random
number=[1,2,3,4,5,6,7,8,9]
for i in number:
    if i==random.randint(1,10):
        print(i)
        break
else:
    print('正常退出循環')

'''
**#和電腦玩剪刀石頭布**
import random
i=random.randint(1,3)
user=int(input('請出拳(1剪刀2石頭3布)'))
assert 1<=user<=3,'輸入的值有誤,請輸入正確的數值' 
if i==user:
    print('平手')
    if i == 1:
        print('電腦出的是剪刀')
    elif i==2:
        print('電腦出的是石頭')
    else:
        print('電腦出的是布')
elif  (  user== 1 and i==3) or (user == 2  and  i == 1) or (user==3 and i==2):
    print('我贏了')
    if i == 1:
        print('電腦出的是剪刀')
    elif  i == 2:
        print('電腦出的是石頭')
    else:
        print('電腦出的是布')
else:
    print('電腦贏')
    if i == 1:
        print('電腦出的是剪刀')
    elif i==2:
        print('電腦出的是石頭')
    else:
        print('電腦出的是布')
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章