程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python function exercises

編輯:Python

1、 Defined function , Complete the calculation of addition, subtraction, multiplication and division of two numbers Tips :def cal(num1,num2,opr): return result

def cal(num1,num2,opr):
if num1 < num2:
num1,num2=num2,num1
if opr == "+":
return num1+num2
elif opr == "-":
return num1 - num2
elif opr =="*":
return num1*num2
elif opr == "/":
return num1/num2
num1,num2 = map(int,input(" Please enter two numbers :").split())
opr = input(" Please enter a symbol :")
rt = cal(num1,num2,opr)
print(f"{num1}{opr}{num2}={rt}")

2、 Defined function , Calculate the perimeter and area of the rectangle

def sc(width,height):
print(" Perimeter :",(width+height)*2)
print(" area :",width*height)
sc(10,20)

3、 Defined function , Get the suffix from the file name , Such as e:/project/demo/homework.py Obtained suffix .py

str = "e:/project/demo/homework.py"
str = input(" file name :")
def name(s):
num = s.rindex(".")
str1 = s[num:]
return str1
print(name(str))

4、 Defined function , Extract the domain name from the request address Such as http://www.qqzone.com?qqid=270808123&pwd=111111, The domain name is www.qqzone.com

s = "http://www.qqzone.com?qqid=270808123&pwd=111111"
def select(str):
num = str.index("www")
num2 = str.index("com")
return str[num:num2+3]
print(select(s))

5、 Defined function , Determine whether the email format is correct , Validation rules : The mailbox must contain @ Symbol

str = input(" Mail box number :")
def check(s):
try:
return s.index("@")
except:
return -1
num = check(str)
if num != -1:
print(" The format is correct ")
else:
print(" Format error ")

6、 Defined function , Count the number of times each character appears in a list

d = {
}
dict = {
}
str = "ssssssssjjjjjjjhhhhhhhhh44444"
#1.
def countchr(s):
for i in s:
d[i] = s.count(i)
for key, value in d.items():
print(f" character {key} Number of occurrences :", value)
countchr(str)
#2.
for i in str:
# Add... If it exists 0, Add if it doesn't exist 1
dict[i] = dict.get(i, 0) + 1
print(dict)

7、 Defined function , Determine whether there is... In a list 4 Consecutive identical numbers

ls = [1,2,2,2,2,4,5,7,8,9,8,8,8,8,8,6,66,6,6,6,6,]
s =set()
def count(l):
for i in range(len(l)):
a = 0
if l.count(l[i]) < 4 or len(l[i:])<5:
continue
else:
for j in range(i,i+4):
if l[i] == l[j]:
a = 1
else:
a = 0
break
if a == 1:
s.add(l[i])
count(ls)
print(s)

8、 Defined function , Sort integer list data by parity , Even numbers come first , Odd number after Example : Input :[3,1,2,4] Output :[2,4,3,1] Output [4,2,3,1],[2,4,1,3] and [4,2,1,3] It will also be accepted .

ls = [3,1,2,4]
#1.
def sort1(A):
"""
A is a list
"""
return sorted(A, key=lambda x: x % 2)
print(sort1(ls))
#2.
def sort2(ls):
"""
ls is a list
"""
even = []
odd = []
for i in range(len(ls)):
if ls[i] % 2 == 0:
even.append(ls[i])
else:
odd.append(ls[i])
return even + odd
print(sort2(ls))

9、 Before output 100 Palindrome prime , And each 10 A new line Tips : Defined function , Judge whether a number is palindrome number Defined function , Judge whether a number is a prime number

def shu(n):
for i in range(2, n):
if n % i == 0:
return False
return True
def h(x):
str1 = str(x)
if str1 == str1[::-1]:
return True
else:
return False
sum = 0
for i in range(1,10000):
if shu(i) and h(i):
print(i,end=" ")
sum += 1
if sum % 10 == 0:
print()
elif sum == 100:
break

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved