計算s中的元素個數
s='abcdefrmcxzoplamc'
count=0
for i in s:
count+=1
print(count) #17
l=[1,2,3,4]
count=0
for i in l:
count+=1
print(count)#4
def my_len():
l=[1,2,3,4]
count=0
for i in l:
count+=1
print(count)
my_len() #4
s='asdfghjklmnbvcxz'
print(len(s)) #16
def my_len():
s='asdfghjlkmnbzvxcc'
count=0
for i in s:
count+=1
print(count)
my_len() #17
def my_len():
#函數體
s = 'dhclvamvmzkfisncnmlwq'
count=0
for i in s:
count+=1
print(count)
my_len() #21
def func():
a=1
b=2
c=a+b
d='alex'
e=[1,2,3]
return e
ret=func()
print(ret,type(ret)) #[1, 2, 3] <class 'list'>
def func():
print(111)
print(112)
print(121)
return
print(333)
func() #111 112 121
def fun1():
a=1
b=2
c=a+b
d='alex'
e=[1,2,3]
return c,d,e
ret=fun1()
print(ret,type(ret)) #(3, 'alex', [1, 2, 3]) <class 'tuple'>
def my_len():
s='asdnfmcznxcvb'
count=0
for i in s:
count+=1
return count
print(my_len()) #13
l1=[1,2,3]
s='asdfglmnvbcxz'
def my_len(argy):
count=0
for i in argy:
count+=1
return count
# print(my_len(l1)) #3
print(my_len(s)) #13
def fun1(x,y):
print(x,y)
fun1(1,2) #1 2
def sum1(x,y):
return x+y
a=3
b=6
print(sum1(a,b)) #9
def compare(x,y):
if x>y:
return x
else:
return y
print(compare(132,245)) #245
def compare(x,y):
return x if x>y else y
print(compare(123,234)) #234
def fun1(a,b,n,x,y):
return x+y
name='alex'
print(fun1(100,200,name,y=100,x=99)) #199
def fun1(a,b,n,x,y):
return x+y
name='alex'
print(fun1(100,234,name,y=90,x=89)) #179
def data(sex,age,hobby):
print(111)
print(222)
print('設置篩選條件:性別:%s,年齡:%s,愛好:%s'%(sex,age,hobby))
print(333)
print(444)
data('男','23-34','打球')
def stu_info(name,age,sex='男'):
print('錄入員工信息')
print(name,age,sex)
print('錄入完畢')
stu_info('張飛',33)
def check(str):
if len(str) >2:
str=str[0:2]
return str
str=input('請輸入元素:')
print(check(str))