当前位置:网站首页>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
边栏推荐
- 1082 read number in Chinese (25 points)
- Svn password forgetting solution
- 1135: paired base chain
- Teach you a learning method to quickly master knowledge
- Solve the problem that codeblocks20.03 on win11 cannot run for the first time
- Text matching - [naacl 2022] GPL
- How many questions can you answer for the interview of Mechanical Engineer?
- O - ACM contest and blackout (minimum spanning tree, Kruskal)
- Matlab finds a prime number that is greater than a given integer and follows this integer
- Developer practice - the future of Agora home AI audio and video
猜你喜欢

Repair of incorrect deletion of win10 boot entry

Notes on zero basic C language learning -- first introduction -- 1 notes that mom can understand

Win10 one click Reset win10 to solve all system bugs without deleting any files and Applications

Industry analysis | the future of real-time audio and video

Sum of CCF digits (full mark code + problem solving idea) 201512-1

Technology sharing | anyrtc service single port design

CCF Z-scan (full mark code + problem solving ideas) 201412-2

It's so brain - burning that no wonder programmers lose their hair

Solve the problem that codeblocks20.03 on win11 cannot run for the first time

Scattered knowledge of C language (unfinished)
随机推荐
catkin_ Make reports an error, transfers the location of the workspace, and uses other people's workspace files to cause compilation errors
Win10 backup backup shows that creating a shared protection point on the shadow failed
Matlab construction operation example
Matlab judges the number of same purchases
先锋期货安全么?现在期货开户都是哪些流程?期货手续费怎么降低?
Super comprehensive redis distributed high availability solution: sentry mechanism
Text matching - [naacl 2021] augsbert
O - ACM contest and blackout (minimum spanning tree, Kruskal)
Rte2021 review HDR technology product practice and exploration
Notes on zero basic C language learning -- first introduction -- 1 notes that mom can understand
One dimensional and two dimensional array addresses
国债逆回购在哪个平台上买比较安全?
HD mechanical principle · classic dynamic drawing of mechanical design
String connector
DR-TANet: Dynamic Receptive Temporal Attention Network for Street Scene Change Detection
立式加工中心的数控加工对刀具使用基本要求
Average and maximum values of MATLAB matrix
It's so brain - burning that no wonder programmers lose their hair
A little idea about big experiment data
Forward declaration of classes