Introduction to basic data types and variable declarations in the python foundation tutorial

  • 2020-04-02 13:59:50
  • OfStack

Variables do not need to be declared

Python variables do not need to be declared, you can directly enter:


>>>a = 10

So you have a variable a in memory, it has a value of 10, and it's of type integer. You don't have to make any special declarations until then, and the data types are automatically determined by Python.

>>>print a >>>print type(a)

Then there will be the following output:

10
<type 'int'>

Here, we learned about a built-in function, type(), that queries for the type of a variable.

Reclaim variable name

If you want a to store different data, you don't need to delete the original variable to assign the value directly.


>>>a = 1.3 >>>print a,type(a)

There will be the following output

1.3 <type 'float'>

We see another use of print, where print is followed by multiple outputs, separated by commas.

Basic data type


a=10         # int The integer a=1.3        # float Floating point Numbers a=True       # The true value (True/False) a='Hello!'   # string

These are the most commonly used data types, but you can also use double quotes for strings.

(there are also other data types such as fractions, characters, and complex Numbers, which you can learn if you are interested.)

conclusion

Variables do not need to be declared, do not need to be deleted, can be directly recycled applicable.

Type (): query data type

Integers, floating point Numbers, truth values, strings


Related articles: