What are the practical cases of python learning?
This article mainly introduces the cases of python learning practice, which have a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
Task 1: convert the specified decimal to binary, octal, hexadecimal binary conversion
The first and second ways of writing:
Num=int (input ('Please enter a decimal integer')) # converts the str type to the int type print (the binary number of num,' is:', bin (num)) # the first method uses a variable number of position parameters print (the binary number of str (num) +'is:'+ bin (num)) # the second way of writing, using + as a connector, the left and right sides of + are str type
Please enter a decimal integer 95
The binary number of 95 is: 0b1011111
The binary number of 95 is: 0b1011111
The third way to format a string is:
Print (the binary number of'% s is:% s% (num,bin (num) # the third way of writing The binary number of the format string print ('{0}) is: {1} '.format (num,bin (num)) print (the binary number of f' {num} is: {bin (num)}'), the octal and hexadecimal number of the format string and the application of the exception handling mechanism def fun (): num=int (input ('please enter a decimal integer')) # converts the str type to the int type print (the binary number of num,' is:' Bin (num)) # the first method uses a variable number of position parameters print (the binary number of str (num) +'is:'+ bin (num)) # the second way of writing Using + as the connector, the left and right sides of + are str type print (the binary number of'% s is:% s'% (num,bin (num) # the third way of writing The binary number of the format string print ('{0}) is: {1} '.format (num The binary number of bin (num)) print (f'{num}) is: {bin (num)}') print ('- -') print (f'{num}) the octal number is: {oct (num)}') print (f'{num}) hexadecimal number is: {hex (num) }') if _ _ name__ = ='_ _ main__': while True: try: fun () break except: print ('only integers can be entered! There is an error in the program, please re-enter')
Please enter a decimal integer Xiao Jia
Only integers can be entered! There is an error in the program, please re-enter
Please enter a decimal integer
Task 2, Mobile Phone Top-Up Service print ('user's original phone bill amount is:\ 033 [0scape 35m8 yuan\ 033 [m') money=int (input ('please enter user recharge amount:')) money+=8print ('current balance is:\ 033 [0th 32m mother moneyline' yuan\ 033 [m') task 3, calculate the energy consumption
Enter the number of steps you walk today and know how many calories you burned today.
Note: the keyboard input is a string, we need to convert it to an integer
Num=int (input ('Please enter the number of steps you walked that day:') calorie=num*28print (f' consumed a total of calories {calorie}, i.e. {calorie/1000} kcal')
Task 4. Predict the height of future children
Enter the height of the father and the height of the mother to predict the height of the children.
Height will be a decimal, so it needs to be converted to float type
Father_height=float (input ('please enter the father's height') mother_height=float (input ('please enter the mother's height') print (f 'your child's predicted height is {(father_height + mother_height) * 0.54}')
The solution given in the tutorial:
Thank you for reading this article carefully. I hope the article "what are the practical cases of python learning" shared by the editor will be helpful to everyone? at the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!