Home > Backend Development > Python Tutorial > Analyze examples of variables, operators, and flow control in Python.

Analyze examples of variables, operators, and flow control in Python.

WBOY
Release: 2023-05-08 19:16:18
forward
1297 people have browsed it

    1. Two ways to execute Python programs

    1.Interactive

    Enter "python3" in the terminal, and then enter python code

    Analyze examples of variables, operators, and flow control in Python.

    2. Command line

    Enter "python3 text file path" in the terminal

    Analyze examples of variables, operators, and flow control in Python.

    2. Variables

    1. Composition of variables

    Variables in Python do not need to be declared. Each variable must be assigned a value before use. The variable will not be created until the variable is assigned a value.

    In Python, a variable is a variable, it has no type. What we call "type" is the type of the object in memory pointed by the variable.

    The equal sign (=) is used to assign values ​​to variables.

    The left side of the equal sign (=) operator is a variable name, and the right side of the equal sign (=) operator is the value stored in the variable.

    Variable name = variable value.

    The variable name is used to receive the variable value

    name = 'nick' 
    age = 19
    Copy after login

    2. The definition specification of the variable name

    • The variable name has A certain meaning

    • consists of numbers/letters/underscores, and cannot start with numbers and underscores

    • Cannot use Python keywords

    ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', ' else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not' , 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

    3. Define variables Two ways

    • Camel case:NameOfNick

    • Underscore:name_of_nick(recommended)

    4. Constants

    Constants are a convention, and constant names are defined in all uppercase letters. can actually be modified.

    AGE_OF_NICK = 19
    print(AGE_OF_NICK)
    Copy after login

    3. Python variable memory management

    1. Reference counting

    Number of references to variable values

    x = 257  # 257的引用计数为1
    y = x   # 257的引用计数为2
    del x  # 257的引用计数为1
    Copy after login

    2. Garbage collection mechanism

    When the reference count of the variable value is 0, the variable value will be automatically recycled by Python to occupy its memory

    3, small integer pool

    [-5, 256] integers will automatically open a memory to store these integers when the Python interpreter starts. In other words, these small integers will not be deleted because the reference count is 0.

    4. Memory address and data type

    Getting the id of a variable can be understood as the address of the variable in memory.

    x = 10
    print(x)  # 获取变量的变量值
    print(id(x) )  # 获取变量的id,可以理解成变量在内存中的地址
    print(type(x) )  # 获取变量的数据类型,下章会详细介绍数据类型
    Copy after login

    Result:

    10
    8790885504960
    < class 'int'>

    5. Example:

    Variables with equal ids must have equal values ​​because they point to the same memory address;

    Variables with equal values ​​may not have equal ids.

    x = 11
    y = x
    z = 11
    print(x == y)  # True
    print(x is y)  # True
    print(x is z)  # True,整数池的原因
    
    x = 255
    z = 255
    print(id(x) is id(z) )  # False
    Copy after login

    4. Fancy assignment

    1. Chain assignment

    The following example creates an integer object with a value of 10 and assigns three values ​​from back to front. Variables are assigned the same numerical value.

    a = b = c = d = 10 
    print(f&#39;a:{a}, b:{b}, c:{c}, d:{d}&#39;) #a:10, b:10, c:10, d:10
    Copy after login

    2. Cross assignment

    # 交叉赋值
    x = 10
    y = 20
    
    x, y = y, x
    print(x, y) #20 10
    
    # 相当于使用临时变量
    x = 10 
    y = 20
    
    temp = x
    x = y
    y = temp
    
    print(x, y) #20 10
    Copy after login

    3. Multiple variable assignment

    You can also specify multiple variables for multiple objects. For example:

    a, b, c = 1, 2, "runoob"
    Copy after login

    In the above example, two integer objects 1 and 2 are assigned to variables a and b, and the string object "runoob" is assigned to variable c.

    Write a Fibonacci series:

    # 两个元素的总和确定了下一个数
    a, b = 0, 1
    while b < 10:
        print(b, end=&#39;,&#39;)
        a, b = b, a+b
    # 1,1,2,3,5,8,
    Copy after login

    5. Interacting with the user

    name = input(&#39;请输入你的姓名:&#39;)
    pwd = input(&#39;请输入你的密码:&#39;)
    
    print(type(name))
    print(type(pwd))
    
    # 请输入你的姓名:a
    # 请输入你的密码:1
    # 
    # &#39;str&#39;>
    Copy after login

    No matter the value we enter is a numeric type, a string type, or a list type , the input values ​​received are all string types.

    6. Formatted output

    1. Placeholder (old string formatting, gradually phased out)

    • %s: receive arbitrary data Type data

    • %d: Receive numeric type data

    name = &#39;nick&#39;
    age = 19
    print(&#39;my name is %s my age is %d&#39; % (name, age))
    # my name is nick my age is 19
    Copy after login

    2, format format

    • {}: Receive data of any data type.

    1. Format: