Study Python There are many different ways , You can watch the video 、 The blog 、 Look at official account, etc . But just talking, not practicing , It's hard to improve quickly . It's best to deal with practical problems with your hands , Only in this way can we apply the learned knowledge more skillfully .
This article explores with you Python A classic case of programming , Let you immerse yourself in learning Python. Help you get high marks in the final exam , Get the favorite of Dachang offer. You can take the problem and think about how many different solutions there are , Then compare with the problem-solving methods in this paper . There are different ways to solve problems. Welcome to official account to discuss with me .
Input : Any three digit positive integer
Output : Corresponding inverted three digit positive integer
example :
Input : 876
Output : 678
Input : Any character
Output : Reverse the character
example :
Input :‘ You are the joy of youth ’
Output :‘ The boy I like is you ’
Define an inversion function , Take the original number as the input value . Take out the bits of the original number in turn 、 ten 、 Hundred bit , Then multiply by different multiples to reverse the position of the number .
The specific code is as follows :
def rev_int1(number):
h1 = int(number/100)
h2 = int(number%100/10)
h3 = int(number%10)
return h3*100+h2*10+h1
rev_int1(876)
Get the results :
678
among number/100: Express the number Divide 100.
Define an inversion function , Take the original number as the input value . First turn numbers into character lists , recycling range Functions in reverse order .
The specific code is as follows :
def rev_all(x):
str_x = list(str(x))
rev_str_x = ''
for i in range(len(str_x)-1, -1, -1):
rev_str_x += str_x[i]
return rev_str_x
rev_all(876)
Get the results :
678
str(x): hold x It becomes a string .
list(str(x)): Turn the string into a list .
range(len(str_x)-1, -1, -1): Arrange the length coordinates of the list in reverse order .
str_x += str_x[i]: Merge the characters arranged in reverse order .
This method can not only reverse three digit integers , And it can be extended to any bit integer , Further, you can reverse any string . For example, reverse a four digit number
rev_all(4131)
Get the results :
678
For example, reverse one 7 A string
rev_all(' You are the joy of youth ')
Get the results :
' The boy I like is you '
thus ,Python Classic programming cases in 【 Examination questions 】 The reversal of a value has been explained . If you want to know more Python The function in , You can go to “ The code of Ali Yiyang ” Look through the official account “ Study Python” Module related articles .
You may be interested in :
use Python Drawing Picasso
use Python Draw a cloud of words
use Python draw 520 Eternal heart
Python Face recognition — You are the only one in my eyes
Python Draw a nice picture of the starry sky ( Aesthetic background )
【Python】 Valentine's Day confession fireworks ( With sound and text )
use Python Medium py2neo Library operation neo4j, Building the association map
Python Romantic confession source collection ( love 、 The roses 、 Photo wall 、 Advertising under the stars )