What are the types of python variable data?

  Variables are one of the basic building blocks of programs written in Python. Variables hold data in memory, they have names, and can be referenced by those names. Variables also have a type, which specifies the type of data the variable can store. So what are the types of python variable data? The following is the specific content introduction.

  1. Digital type

  Numeric types in Python include integer, floating-point, and complex types. The sample code is as follows:

  Integer: 0101 83 -239 0x80 299384938832

  Float: 3.14154 2E-10 -2.34E-19

  Complex type: 3.12+1.23j -1.23-98j

  2. Boolean type

  The Boolean type is a special integer type with only two values, True and False. If you perform numerical operations on Boolean values, True will be treated as an integer 1, and False will be treated as an integer 0.

  3. String type

  A string in Python is defined as a collection of characters enclosed by quotes, which can be single quotes, double quotes, or triple quotes. Strings have indexing rules, the index of the first string is 0, the index of the second character is 1, and so on.

  Here is sample code for strings:

  string_one='python'

  string_two="python"

  string_three='''python'''

  4. List and tuple types

  We can think of lists and tuples as ordinary "arrays" that can hold any number of values ​​of any type, called elements. The elements in the list are enclosed in square brackets [], and the number and value of elements can be modified at will. The elements in the tuple are enclosed in parentheses (), and the elements cannot be modified. Let's look at how lists and tuples are represented.

  list_name=[1,2,'hello'] # This is a list

  tuple_name=(1,2,'hello') #This is a tuple

  5. Dictionary type

  A dictionary is a mapping data type in Python, consisting of key-value pairs. Dictionaries can store different types of elements, and elements are enclosed by curly braces {}. Typically, the keys of a dictionary are represented as strings or numbers, and the values ​​can be of any type. The sample code is as follows:

  dict_name=["name":"zhangsan","age":18] # This is a dictionary

  In the above code, the variable dict_name is a dictionary type, which stores two elements. The key of the first element is name, and the value is zhangsan; the key of the second element is age, and the value is 18.

Guess you like

Origin blog.csdn.net/oldboyedu1/article/details/132449375