python學習

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# my first py demo! 2018-08-13
age = input("請輸入您的年齡:");
if age >= '18':
	print("您是成年人!");
else:
	print("您是未成年!");

print("""#標準數據類型6種
#Number(數字)
#String(字符串)
#List(列表)
#Tuple(元組)
#Sets(集合)
#Dict(字典)
""");

print(r'nihao \"linlin"')
print('nihao \\"linlin"')

print('Hello, {0}, 成績提升了 {1:.1f}%'.format('小明', 17.125))
print('Hello, %s, 成績提升了 %d' % ('小王', 5))
height = int(input("請輸入身高(cm):")) / 100
weight = int(input("請輸入體重(kg):"))
bmi = weight / (height*height)
print(height,bmi)
if bmi < 18.5:
	print("體重過輕")
elif 18.5 <= bmi < 25:
	print("正常")
elif 25 <= bmi < 28:
	print("過重")
elif 28 <= bmi < 32:
	print("肥胖")
else:
	print("嚴重肥胖")
arr = range(6)
re = 0
for x in arr:
	re += x
print(re)

num = 100
re2 = 0
while num > 0:
	re2 += num
	num -= 2
print(re2)

names = ["小王", "小李", "小豬", "小吳"]
for x in names:
	if x == "小豬":
		continue
	print("hello", x)
dict1 = {"ming": "輔助",
"xiaohu": "中單",
"letme": "上單",
"uzi": "ADC",
"mlxg": "打野",
"others": ["zitai", "qiqi", "karsa"]
}
print(dict1["others"][1])

list1 = [1,2,3,3,4,5,5]
list2 = [7,7,6,5,8]
set1 = set(list1) & set(list2)
print("&交集:",set1)
set2 = set(list1) | set(list2)
print("|並集:",set2)

def re_abs(d):
	if d >= 0:
		return d
	else:
		return -d
re = re_abs(int(input("輸入要求絕對值的數")))
print(re)
#可變參數(arguments長度不受限制啦)
def count(*num):
    s = 0;
    for i in num:
        s = s + i;
    print(s);
nums = [1,2,3]
count(*nums);

#關鍵字參數(可以傳遞dict啦)
def person(name,age,**city):
    print('my name is:',name,'I come from:',city['city']);
p = {'city':'Boston'};
person('wyang','20',**p);

#命名關鍵字參數(可以傳遞指定的dict屬性啦)
def persons(name,age=21,*,city,job):
    print('my name is',name,'I\'m',age,'years old this year.','I come from:',city,'My job is ',job,' engineer.');
ps = {'city':'Boston','job':'web'};
persons('wyang',**ps);

#參數組合
def personss(name,age=20,*,city,job):
    print('my name is',name,'I\'m',age,'years old this year.','I come from:',city,'My job is ',job,' engineer.');
args = ['wyang',22]
kw = {'city':'Boston','job':'web'};
personss(*args,**kw);
#遞歸去字符串前後空白
def trim(s):
	if s[:1] == " ":
		return trim(s[1:])
	elif s[-1:] == " ":
		return trim(s[:-1])
	else:
		return s
str1 = '   hello world  '
print(trim(str1))

#用迭代取list最小值和最大值
def findMinAndMax(arr):
	if len(arr) == 0:
		return None,None;
	min = arr[0];
	max = arr[0];
	for v in arr:
		if v > max:
			max = v;
		elif v < min:
			min = v;
	return min,max;
if findMinAndMax([]) != (None, None):
    print('測試失敗!')
elif findMinAndMax([7]) != (7, 7):
    print('測試失敗!')
elif findMinAndMax([7, 1]) != (1, 7):
    print('測試失敗!')
elif findMinAndMax([7, 1, 3, 9, 5]) != (1, 9):
    print('測試失敗!')
else:
    print('測試成功!')

 

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