当前位置:网站首页>4.8 data type conversion
4.8 data type conversion
2022-06-30 15:16:00 【Andy Python learning notes】
4.8 Data type conversion
frequently-used 3 Type of data :
1、Python The first data type : character string (str).
2、Python The second data type : Integers (int).
3、Python The third data type : Floating point numbers (float).
4.8.1 Convert to string
str( ) Converts an integer or floating-point number to a string type .
# Set an integer variable
number = 10
# take number Convert to string
str(number)
# View conversion types
type(str(number))
str
number The data type of is an integer (int), adopt str( ) Function to string type (str), Using type Function to view the converted data type .
# Set a floating point variable
number_2 = 0.66
# Convert a variable to a string
str(number_2)
# View conversion types
type(str(number_2))
str
number_2 The data type of is originally a floating point number (float), adopt str( ) Function to string type (str).
4.8.2 Convert to integer
int( ) The string 、 Convert floating point numbers to integers .
# Set a floating point variable
number_3 = 6.66
# Convert variables to integers
int(number_3)
# View conversion types
print(type(int(number_3)))
# View the conversion results
print(int(number_3))
<class 'int'>
6
When converting a floating-point number to an integer, only the number before the decimal point is retained .
# Set a string variable
number_4 = '66'
# Convert variables to integers
int(number_4)
# View conversion types
type(int(number_4))
int
# Set a string variable
number_5 = '66.66'
# Convert variables to integers
int(number_5)
# View conversion types
type(int(number_5))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
ValueError: invalid literal for int() with base 10: '66.66'
Be careful
1、 Convert floating point numbers to integers : Do not use rounding , Keep only the number before the decimal point . for example : Floating point numbers 66.99 Convert to integer 66, instead of 67.
2、 String to integer : Only string class data conforming to integer specification , Can be int() Coercive transformation .
for example : character string '66.66' Can't convert to an integer , character string '66' It can be converted into an integer .
4.8.3 Convert to floating point number
float( ) The integer 、 String to float .
# Set a string variable
weight = '50'
# Convert variables to floating point numbers
float(weight)
# View conversion types
print(type(float(weight)))
# View the conversion results
print(float(weight))
<class 'float'>
50.0
# Set an integer variable
height = 180
# Convert variables to floating point numbers
float(height)
# View conversion types
print(type(float(height)))
# View the conversion results
print(float(height))
<class 'float'>
180.0
summary
1、str( ): Convert to string , There is no limit to .
2、int( ): Floating point number to the number before the reserved point ; Literal and decimal strings cannot be converted to integers .
3、float( ): Literal strings cannot be converted to floating point numbers .
Practice after class 1
The completion code , Output : Today is my study Python Of the 100 God
# Set a variable
phrese_1 = ' Today is my study Python'
word_1 = ' Of the '
number_1 = 100
word_2 = ' God '
# Output today is my study Python Of the 100 God
Practice after class 2
The completion code , Output : Our class has 50 people
# Set a variable
phrese_2 = ' Our class has '
number_2 = '50.8'
word_3 = ' people '
# Output our class has 50 people
边栏推荐
- Three types of technical debt that programmers often encounter: code, data, and architecture
- 1062 talent and virtue (25 points)
- 立式加工中心调试的步骤
- 1076 forwards on Weibo (30 points)
- NPM install --global --save --save dev differences
- day02
- 1018 public bike Management (30 points)
- CCF adjacent number pairs (Full Score code + problem solving ideas + skill summary) 201409-1
- Text matching - [naacl 2022] GPL
- Basic requirements for tool use in NC machining of vertical machining center
猜你喜欢

Database connection to company database denied

Technology sharing | how to quickly realize audio and video online calls
![001 data type [basic]](/img/36/90fdf51a20f8c708b9c38610b377d6.jpg)
001 data type [basic]

The principle of fluent 2 rendering and how to realize video rendering

CCF string matching (Full Score code + problem solving ideas + skill summary) March 3, 2014

Web technology sharing | whiteboard toolbar encapsulation of Web

Review 2021, embrace change and live up to Shaohua

What would you choose between architecture optimization and business iteration?

Basic literacy - four common software architectures

Webrtc: industrial application based on Internet of things
随机推荐
Matlab judges the number of same purchases
Is Domain Driven Design (DDD) reliable?
Is pioneer futures safe? What are the procedures for opening futures accounts? How to reduce the futures commission?
Working principle and fault treatment of cutting cylinder in CNC machining center
1082 read number in Chinese (25 points)
Shift operator (detailed)
1137: encrypted medical record
立式加工中心的数控加工对刀具使用基本要求
O - ACM contest and blackout (minimum spanning tree, Kruskal)
1018 public bike Management (30 points)
On which platform is it safer to buy Treasury reverse repo?
1133: output family and friends string
Text matching - [naacl 2021] augsbert
C. Registration system(map)
Some reference routines for cache update
Operator%
Matlab calculates the factorial sum of the first n numbers (easy to understand)
Four solutions to cross domain problems
The crystal ball "data insight" was officially launched: insight into the change of consumption trend and the details of interactive experience
CCF adjacent number pairs (Full Score code + problem solving ideas + skill summary) 201409-1