当前位置:网站首页>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
边栏推荐
- Super comprehensive redis distributed high availability solution: sentry mechanism
- The sound network has fully opened the real-time transmission network sd-rtn, which has been free of network wide accidents for seven years - this is FPA!
- 1031 Hello world for u (20 points)
- What would you choose between architecture optimization and business iteration?
- Sum of CCF digits (full mark code + problem solving idea) 201512-1
- L - Jungle roads (minimum spanning tree)
- B. Moamen and k-subarrays (codeforce+ binary search)
- Preliminary study on AI noise reduction evaluation system of sound network
- Kubernetes: a comprehensive analysis of container choreography
- Explain service idempotency design in detail
猜你喜欢
Repair of incorrect deletion of win10 boot entry
Rte2021 review HDR technology product practice and exploration
Is Domain Driven Design (DDD) reliable?
Shift operator (detailed)
Developer practice - the future of Agora home AI audio and video
Some reference routines for cache update
Matlab judge palindrome number (only numbers)
CCF elimination games (Full Score code + problem solving ideas + skill summary) February 2, 2015
Basic literacy - four common software architectures
Zero basic C language learning notes -- first introduction -- 2 data types & variables and constants
随机推荐
Notes on zero basic C language learning -- first introduction -- 1 notes that mom can understand
1130: find the first character that appears only once
(Niuke) BFS
Svn password forgetting solution
Machine learning feature selection
Three types of technical debt that programmers often encounter: code, data, and architecture
1027 colors in Mars (20 points)
String connector
Help you accumulate audio and video knowledge, Agora developer's roaming guide officially set sail
1035 password (20 points)
Noj1042 - electronic mouse running through maze
Matlab draws the image of the larger function value of the two functions (super simple)
Is pioneer futures safe? What are the procedures for opening futures accounts? How to reduce the futures commission?
Analysis on the problems of irregular step hole on horizontal machining center
Matlab judges the number of same purchases
The principle of fluent 2 rendering and how to realize video rendering
Review 2021, embrace change and live up to Shaohua
CCF elimination games (Full Score code + problem solving ideas + skill summary) February 2, 2015
RTC monthly tabloid programming challenge ended successfully in June; Review of the first anniversary of sound network's listing
Bye civil engineering, hello CS, can you change the certificate to the Blue Bridge Cup