current position：Home>Python from 0 to 1 (day 13) - Python data application 3
Python from 0 to 1 (day 13) - Python data application 3
2022-01-31 22:48:34 【My family】
This is my participation 11 The fourth of the yuegengwen challenge 18 God , Check out the activity details ：2021 One last more challenge
The way to convert data to integer type is also very simple , Namely int() function . Its use method is the same as str() equally , Put the content you need to convert in parentheses , like this ：int( Converted content ).
Now let's try , To calculate the number1 and number2 The sum of these two variables .【 The point tip ：1. int() Data type conversion function ;2. Operator + Calculation ;3. print() function 】
number1 = '618' number2 = '1111' print(int(number1) + int(number2)) Copy code
But for the
int() Use of functions , We should pay attention to ： Only those that conform to the integer specification String class data , Can be
int() Coercive transformation .
Although it has only one sentence , But it has three meanings ： First , A string of integers, for example '618' and '1111', Can be
int() Function cast .
Last , String in decimal form , because Python Grammatical rules , Can't be used
int() Function cast .
Let's say the following code , If you click Run , The program will report an error 【ValueError：invalid literal for int() with base 10: '6.18'】（ Value anomaly ： Floating point type string cannot be used int() Coercive transformation ）.
print(int('6.18')) # Display the results after running ：ValueError：invalid literal for int() with base 10: '6.18' Copy code
Does that mean , Floating point number cannot be converted to integer type ？
No , Although floating-point form character string , Out of commission
int() function . but Floating point numbers Yes, it can be.
int() Function cast .
print(int(6.18)) Copy code
The code string above , Is a floating point number 6 .18 Cast statement . however , Why the output is 6 Well ？
You see ,
int() The essence of a function is to convert data to integers . So for floating point numbers ,
int() The function does rounding . however , It is different from our usual method of rounding decimals ,
int() function Will erase directly , Direct output integer part .
Last , Let's summarize
int() Knowledge points of function .
But , If this happens ： A string is in decimal form , such as '3.8' such . I don't want it to be an integer 3, Want to keep it in decimal form 3.8, What am I going to do ？
This involves our last knowledge point ——
float() function .
float() Use of functions , Also put the data to be converted in brackets , like this ：
float( data ).
float() The function can also put Integers and character string Convert to floating point type . But at the same time , If the data in brackets is character string type , Then the data must be in digital form .
that , After before
int() practice ,
float() Function is not easy to understand some ？
below , Let's play a piece of code and practice .
width = 40.0 length = 120.0 weight = "20" print(width) print(length) print(float(weight)) Copy code
In the last question ,
lenght The variable is already a floating point type ,
weight It's a string type （ It can be used
type( Variable ) To see the data types ）, Therefore, before printing, it will
float(weight) Just convert . Up to now , You already know how to convert data types . below , Let's summarize these three functions through a graph .
What about? ？ Master the data type 、 You have three knowledge points of data splicing and data conversion , Do you have a sense of achievement to be a big man ？
that , Let's take a look at what you have learned ！
But these , It's just the beginning , Later you will learn more complex code and commands , Master more communication skills with computers .
author[My family],Please bring the original link to reprint, thank you.
The sidebar is recommended
- leetcode 1560. Most Visited Sector in a Circular Track（python）
- leetcode 1995. Count Special Quadruplets（python）
- How to program based on interfaces using Python
- leetcode 1286. Iterator for Combination（python）
- leetcode 1418. Display Table of Food Orders in a Restaurant （python）
- Python Matplotlib drawing histogram
- Python development foundation summary (VII) database + FTP + character coding + source code security
- Python modular package management and import mechanism
- Django serialization (II)
- Python dataloader error "dataloader worker (PID XXX) is killed by signal" solution
guess what you like
apache2. 4 + Django + windows 10 Automated Deployment
leetcode 1222. Queens That Can Attack the King（python）
leetcode 1387. Sort Integers by The Power Value （python）
Tiger sniffing 24-hour praise device, a case with a crawler skill, python crawler lesson 7-9
Python object oriented programming 01: introduction classes and objects
Baidu Post: high definition Python
Python Matplotlib drawing contour map
Python crawler actual combat, requests module, python realizes IMDB movie top data visualization
Python classic: explain programming and development from simple to deep and step by step
Python implements URL availability monitoring and instant push
- Python avatar animation, come and generate your own animation avatar
- leetcode 1884. Egg Drop With 2 Eggs and N Floors（python）
- leetcode 1910. Remove All Occurrences of a Substring（python）
- Python and binary
- First acquaintance with Python class
- [Python data collection] scrapy book acquisition and coding analysis
- Python crawler from introduction to mastery (IV) extracting information from web pages
- Python crawler from entry to mastery (III) implementation of simple crawler
- The apscheduler module in Python implements scheduled tasks
- 1379. Find the same node in the cloned binary tree (Java / C + + / Python)
- Python connects redis, singleton and thread pool, and resolves problems encountered
- Python from 0 to 1 (day 11) - Python data application 1
- Python bisect module
- Python + OpenGL realizes real-time interactive writing on blocks with B-spline curves
- Use the properties of Python VTK implicit functions to select and cut data
- Learn these 10000 passages and become a humorous person in the IT workplace. Python crawler lessons 8-9
- leetcode 986. Interval List Intersections（python）
- leetcode 1860. Incremental Memory Leak（python）
- How to teach yourself Python? How long will it take?
- Python Matplotlib drawing pie chart
- Django paging (II)
- Concurrent. For Python concurrent programming Futures or multiprocessing?
- Programmers over the age of 25 can't know a few Chinese herbal medicines. Python crawler lessons 9-9
- Python crawler from introduction to pit full series of tutorials (detailed tutorial + various practical combat)
- The second bullet of class in Python
- Python object oriented programming 03: class inheritance and its derived terms
- How IOS developers learn Python Programming 13 - function 4
- Python crawler from introduction to mastery (VI) form and crawler login
- Python crawler from entry to mastery (V) challenges of dynamic web pages
- Deeply understand pandas to read excel, TXT, CSV files and other commands
- Daily python, Chapter 18, class
- "I just want to collect some plain photos in Python for machine learning," he said. "I believe you a ghost!"
- Django view
- Python implements filtering emoticons in text
- When winter comes, python chooses a coat with temperament for mom! Otherwise, there's really no way to start!
- Python crawler - get fund change information
- Highlight actor using Python VTK
- Python crawler actual combat: crawling southern weekend news articles
- leetcode 406. Queue Reconstruction by Height（python）
- leetcode 1043. Partition Array for Maximum Sum （python）