2024.8.9

shalatia發表於2024-08-12

ATM機互動

  1. 插卡

  2. 輸入密碼

  3. 選擇功能

  4. 存款

1 Python的與使用者互動

input('請輸入瑞星卡號')

input('輸入密碼')
請輸入瑞星卡號5201314
輸入密碼15236





'15236'
print('*'*100)
****************************************************************************************************
print('*'*100)

input('請插入瑞星卡')

print('_'*100)
****************************************************************************************************
請插入瑞星卡25
____________________________________________________________________________________________________
input 只是一個過渡階段
vip_card = input('please input VIP card')

print(vip_card)
please input VIP cardasdfghjkl;
asdfghjkl;
print(vip_card)

print(id(vip_card))

print(type(vip_card))
asdfghjkl;
2263780973424
<class 'str'>

無論你輸入什麼型別,input接受的都是字串

age = input('請輸入你的年齡')

print(age)

print(type(age))

age = int(age)
請輸入你的年齡23
23
<class 'str'>
age = age + 1

print(age)
24

2 格式化輸出

age = input('age:>>>')
            
print('My age is '+ age)
age:>>>23
My age is 23
age = input('age:')

print('My age is',age)
age:23
My age is 23
name = input('name:')

height = input('height:')

age = input('age:')
            
print('My age is ',age,',My name is',name,',My height is',height)
name:xk
height:185
age:23
My age is  23 ,My name is xk ,My height is 185

2.1 佔位符

name = input('name:')

height = input('height:')

age = input('age:')
            
print('My name is %s. My age is %s, My height is %s'%(name,age,height))
name:1
height:1
age:1
My name is 1. My age is 1, My height is 1
name = input('name:')

height = input('height:')

height = int(height)

age = input('age:')

age = int(age)
            
print('My name is %s. My age is %d, My height is %d'%(name,age,height))
name:xc
height:1
age:1
My name is xc. My age is 1, My height is 1

format格式化

name = input('name:')

height = input('height:')

age = input('age:')
            
print('My name is {}. My age is {}, My height is {}'.format(name,age,height))
name:xk
height:185
age:23
My name is xk. My age is 23, My height is 185

f-string格式化

name = input('name:')

height = input('height:')

age = input('age:')
            
print(f'My name is {name}. My age is {age}, My height is {height}')
name:xk
height:23
age:185
My name is xk. My age is 185, My height is 23
print(f'My name is {name}. My age is {int(age)+1}, My height is {height*10}')
My name is xk. My age is 186, My height is 23232323232323232323
print(f'My name is {name}. My age is {int(age)+1:.5f}, My height is {height*10}')
My name is xk. My age is 186.00000, My height is 23232323232323232323
print(f'My name is {"name"}. My age is {int(age) + 1}, My height is {height*10}')
My name is name. My age is 186, My height is 23232323232323232323

基本運算子

算術運算子

比較運算子

賦值運算子

邏輯運算子

身份運算子

算術運算子 加減乘除

比較運算子 大於小於大於等於小於等於 恆等於不等於

#賦值運算子 
x = 2

print(x)
2
#邏輯運算子

#and,or,not

1 <= 1 and 1>=1
True
1 <= 1 or 1>1
True
not'sadsads'
False
#身份運算子 比較的是id

x = 10

y = 10

print(x is y)
True
x = 257

y = 257

print(x is y)
False
x = 10

y = 11

print (x is y)

print(x == y)
False
False
is比較的是id,==比較的是值

python運算子優先順序

1*3 > 2*1
True

鏈式賦值(重要)

x = 10

y = 10

z = 10

print(x,y,z)
10 10 10
x,y,z = 10,10,10

print(x,y,z)
10 10 10

交叉賦值

x = 10

y = 20

z = x

x = y

y = z

print(x,y,z)
20 10 10
x = 10

y = 20

x,y = y,x

print(x,y)
20 10

解壓縮

hobby_list = ['reading','666','2333']

print(hobby_list[2])
2333
hobby_list = ['reading','666','2333']

hobby1,hobby2,hobby3 = hobby_list

print(hobby1)

print(hobby2)
rading
666
hobby_list = ['reading','666','2333','999']

_,hobby1,_,hobby2 = hobby_list

print(hobby1)

print(hobby2)
666
999
hobby_list = ['reading','666','2333','999','reading','666','2333','999']

hobby1,*_,hobby2 = hobby_list

print(hobby1)

print(hobby2)
reading
999

相關文章