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

Python exercises

編輯:Python

# 2. hold 2.918 It's plastic surgery 
# *
mun_a=2.918
mun_b=int(mun_a)
print(mun_b)
# 3. hold 10 Hexadecimal number 18 Turn into 2 Hexadecimal number 
# *
mun_c=18
print('10 Hexadecimal number 18 Turn into 2 The binary number is :',bin(int(mun_c)))
# 4. ⽤java Replace string :”Python is popular” ⾥⾯ Of Python, and 
# hold java Transform into JAVA
# *
str_a='Python is popular'
str_b=str_a.split()
# print(str_b)
str_c = str_a.replace("Python", "java".upper())
print(str_c)
# 5. The list [1, 2, 3,4 5,6,7,8]⾥⾯ Of 2, 4, 6,8 Print out 
list_01=[1, 2, 3,4,5,6,7,8]
list_02=[]
for i in list_01:
if i%2==0:
list_02.append(i)
print(list_02)
# *
# 6. establish ⼀ A dictionary , Dictionary key Namely name, sex, province , modify 
# *
# original province Value For the new value ” jiangsu ”
dict_01={
"name":" Zhang San ","sex":"12","province":" anhui "}
dict_01["province"]=" jiangsu "
print(dict_01)
# Homework 2
# *
# 1. Test_str=“Python was created in 1989, Python is using in AI, big
# data, IOT.” Align according to the following requirements ⾯⽂ Word processing .
# *
# • Put it on ⾯⽂ All in the word ⼤ Write into ⼩ Write 
# *
# • Put each word in the list ⾥⾯, Cannot contain spaces .
# *
# • Put the middle of the list ⼀ Print out words .
# *
Test_str="Python was created in 1989, Python is using in AI, big data, IOT."
Test_str_01=Test_str.lower()
print(Test_str_01)
list_03=Test_str.replace(","," ").split()
print(list_03)
str_middle = list_03[int(len(list_03)/2)]
print(" The middle word is :", str_middle)
# 2. List1=[“python”, 5,6, 8], list2=[“python”,”5”, 6, 8,10], Yes list1 and list2 Do the following :
# *
# • Put it on ⾯2 individual list The contents of are merged into ⼀ individual 
# *
# • benefit ⽤set⾥⾯ Of ⽅ Law , For the combined list, Remove duplicate elements . most The post output is also 
# yes list =[“python”, 5,6, 8,”5”,10] ( The order may not be ⼀ sample )
List1=["python", 5,6, 8]
list2=["python","5", 6, 8,10]
list3=List1+list2
print(list3)
print(' Remove duplicate elements . most Post output :',set(list3))
# hold 1000-2500 Between , Can be 7 to be divisible by , Can also be 5 The number divided by an integer is taken out , Put it in a list and output 
li=[]
for num in range(1000,2501):
if num%5==0 and num%7==0:
li.append(num)
print(li)
# Print out 0-20 Number between , If this number can be 3 to be divisible by , Output English ”three”, If it can be 5 to be divisible by , Output ”five”, If it can be 3 Divisible also 
# Can be 5 to be divisible by , Output ”threes+fives”, Ask for continue
# 1 2
# Threes
# 4
# fives
# Threes
# 7 … 1
# 4
# Thees+fives
for num_01 in range(21):
if num_01%3==0:
print('three')
continue
elif num_01%5==0:
print('fives')
continue
elif num_01%3==0 and num_01%5==0:
print("threes+fives")
continue
print(num_01)
# Homework 3
# *
# 1. Realization ⼀ A function , Requirements for ⼀ A list ⾥⾯ Sum all numbers , If ⾥ ⾯ contain ⾮ Digital 
# Elements . Just skip .⽐ Such as [1,2,3] The output is 5, If yes [1,2,4,”a”] The output is 7. And in another 
# ⼀ A package (⽬ record )⾥⾯ transfer ⽤ This function 
def sum_01(li):
sum = 0
for num in li:
if isinstance(num, int):#isinstance() Function to determine whether an object is a known type 
sum += num
print('⼀ A list ⾥⾯ Sum all numbers ',sum)
if __name__ == '__main__':
sum_01([1,2,4])
# *
# 2. Existing dictionary dic={“name”:”xiaozhang ”,”sex”:”male”}, Visit the dictionary dic[“grade”],, adopt try… exception Print out the exception information 
dic = {
"name": "xiaozhang", "sex": "male"}
try:
dic["grade"]
except KeyError as e:
print(e)
# *
# 3. Realization ⼀ Functions with indefinite length parameters def flexible(aa, *args, **kwargs):,
# *
# Pass ⼊ Print out the parameters and values of .⽐ Ruzhuan ⼊ Parameter is 
# *
# flexible(aa, 2, 3, x = 4, y = 5, *[1, 2, 3], **{'a':1,'b': 2})
# *
# Output results :(2, 3, 1, 2, 3),{'a': 1, 'y': 5, 'b': 2, 'x': 4}
def flexible(aa, *args, **kwargs):
print(aa, args, kwargs)
if __name__ == '__main__':
flexible('aa', 2, 3, x=4, y=5, *[1, 2, 3], **{
'a': 1, 'b': 2})

answer :


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