PythonThere are several built-in types.
If you createname
Assign the value "Roger" to the variable, this variable now automatically represents aStringtype of data.
name = "Roger"
you can use ittype()
Function, pass the variable as a parameter, and then combine the result withstr
:
name = "Roger"
type(name) == str #True
Or useisinstance()
:
name = "Roger"
isinstance(name, str) #True
Please note that you see
True
Value in Python outside of the REPL, you need to wrap this code in itprint()
, But for clear reasons, I avoid using it
We usedstr
The class is here, but other data types can also be used.
First, we have numbers. For integersint
class. Floating point numbers (fractions) are typesfloat
:
age = 1
type(age) == int #True
fraction = 0.1
type(fraction) == float #True
You learned how to create a type from value literals, as follows:
name = "Flavio"
age = 20
Python automatically detects the type from the value type.
You can also create a specific type of variable by using the class constructor, and pass the value literal or variable name:
name = str("Flavio")
anotherName = str(name)
You can also use the class constructor to convert one type to another. Python will try to determine the correct value, such as extracting a number from a string:
age = int("20")
print(age) #20
fraction = 0.1
intFraction = int(fraction)
print(intFraction) #0
This is calledcasting. Of course, depending on the value passed, this conversion may not always work. If you writetest
instead20
In the above string, you will get aValueError: invalid literal for int() with base 10: 'test'
error.
These are just the basis of the type. We have more types in Python:
complex
For complex numbersbool
For Booleanlist
For the listtuple
For tuplesrange
For the rangedict
For the dictionaryset
Suit
And more!
We will explore it as soon as possible.
More python tutorials:
- Introduction to Python
- 在macOS上安装Python 3
- Run Python program
- Python 2 and Python 3
- Basics of using Python
- Python data types
- Python operators
- Python string
- Python boolean
- Python numbers
- Python, accepts input
- Python control statements
- Python list
- Python tuple
- Python set
- Python dictionary
- Python functions
- Python objects
- Python loop
- Python module
- Python class
- Python standard library
- Debug Python
- Python variable scope
- Python, accept parameters from the command line
- Python recursion
- Python nested functions
- Python Lambda function
- Python closure
- Python virtual environment
- Use Python to use GoPro as a remote webcam
- Python, how to create a list from a string
- Python decorator
- Python Docstrings
- Python introspection
- Python notes
- Python, how to list files and folders in a directory
- Python, how to check if a number is odd or even
- Python, how to get detailed information of a file
- Python, how to check if a file or directory exists
- Python exception
- Python, how to create a directory
- Python, how to create an empty file
- Python, `with` statement
- Python, create a network request
- Python, use `pip` to install third-party software packages
- Python, read file content