Python variables and data types

1.Python variables

In Python, variables are identifiers used to store data. You can think of a variable as a container that stores values.

Python is a dynamically typed language, which means you don’t need to declare the type of a variable in advance, Python will depend on the value assigned to it.

Automatically infer the type of variables. You can assign values ​​directly to variables, and you can change the value and type of variables at any time.

Here are some basic rules and usage of Python variables:

  1. Variable naming rules:

    • Variable names consist of letters, numbers, and underscores.
    • Variable names must start with a letter or underscore, not a number.
    • Variable names are case-sensitive.
  2. Variable assignment: In Python, variable assignment is performed using the equal sign (=), which assigns the value on the right to the variable on the left. For example:

    x = 5 name = "John"
  3. Variable types: Variables in Python can store different types of data, including integers (int), floating point numbers (float), strings (str), etc.

  4. Variable usage:

    • When using variables in a program, you can directly use the variable name to access the value of the variable. For example:

      x = 5 print(x)
    • Variables can be used to perform arithmetic operations, string concatenation, etc. For example:

      x = 5 y = 3 z = x + y name = "John" greeting = "Hello, " + name
    • The value of a variable can be changed at any time. The value of a variable can be changed by reassigning it. For example:

      x = 5 x = x + 1

Summary: Variables in Python are identifiers used to store data. They can store different types of data, and the value and type of the variable can be changed at any time. To improve code readability, use meaningful names when naming variables and follow variable naming rules.

2.Python data types

In Python, common data types include:

  1. Number type:

    • Integer (int): represents an integer value, such as 1, 2, -3, etc.
    • Float: represents a value with a decimal part, such as 3.14, -0.5, etc.
    • Complex number (complex): represents a numerical value with real and imaginary parts, such as 1+2j, -3+4j, etc.
  2. String type (str): Represents text data, which can be enclosed in single quotes or double quotes, such as "Hello World", "Python", etc. Strings are immutable, which means that once created, the characters within them cannot be modified.

  3. List type (list): Represents an ordered variable container that can store multiple elements of different types. Each element is separated by commas and enclosed in square brackets, such as [1, 2, 3], [ 39;apple', 'banana', 'orange'], etc.

  4. Tuple type (tuple): Similar to a list, but a tuple is immutable, that is, it cannot be modified after creation. Tuples are enclosed in parentheses, such as (1, 2, 3), ('apple', 'banana', 'orange'), etc.

  5. Dictionary type (dict): Represents an unordered collection of key-value pairs. Each key-value pair is separated by a comma, and the key and value are separated by a colon and enclosed in curly braces, such as {'name' ;: 'John', 'age': 25}, {'apple': 1, 'banana': 2, ' orange': 3} etc.

  6. Set type (set): Represents an unordered collection of unique elements. Each element is separated by commas and enclosed in curly braces, such as {1, 2, 3}, {'apple', & #39;banana', 'orange'} etc. Sets can perform operations such as intersection, union, and difference.

In addition to the common data types mentioned above, Python also provides some other built-in data types, such as Boolean type (bool), null value type (NoneType), etc. In addition, you can use more data types and data structures through module import, such as datetime type (datetime), regular expression type (re), etc.

In Python, you can use the built-in functiontype() to view the data type of a variable. For example, type(42) will return <class 'int'> , indicating the integer type. For complex type data, you can use the corresponding type constructor to create and operate, such as list(), tuple(), dict(), < /span>set() etc.

Summary: Common data types in Python include integers, floating point numbers, complex numbers, strings, lists, tuples, dictionaries and sets, etc. Each data type has its specific purpose and operation method. In programming, choose the appropriate data type to process data according to your needs.

Guess you like

Origin blog.csdn.net/weixin_49016330/article/details/131604596