Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of variables, parameters and Modules in Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces Python variables, parameters and module example analysis, the article is very detailed, has a certain reference value, interested friends must read!

1 Variables First, in Python, variables are values stored in memory, the program creates a space in memory when it executes to create variables, and the Python parser allocates the specified memory depending on the data type of the variable. A variable marks or points to a value.

The example is as follows: color in the play is a variable name pointing to the string blue, and the code color = "blue" is = assigned to the variable.= A sign is called an assignment operator and it causes a variable to point to a value. Python replaces variables with the value they point to when they are encountered, and can assign values to multiple variables simultaneously.

>>> color = "blue">>> color'blue' >>> count = 5>>> 100 * count500>>> a,b,c = "hello",888,666>>> a'hello'>>> b888>>> c666 variable is relatively simple to use, but also need to adhere to the following naming rules:

1. The first character of a variable name cannot be a number, but must be a letter or underscore.

2. Python keywords cannot be used as variable names. For example, if, else, while, def, or, and, not, in, and is are Python keywords;

3. Variable names are unlimited in length, but the characters must be letters, numbers, or underscores. Spaces, hyphens, punctuation marks, quotation marks, or other characters are not allowed.

4. Python is case-sensitive, so A, B and a, b are distinct variable names.

An example of a mistake:

>>> While = "nihao">>> While'nihao' Of course, since case is sensitive, we can change the lowercase w to the uppercase W, so that we can use variable names, but it is generally not recommended to use this.

Related to the function described last time, one of the problems it poses for variables is scope. The so-called scope refers to where the variable can be accessed or manipulated in the program, which is generally divided into local variables and global variables. Compare the following two functions:

import mathdef example1(x, y, a, b): s = (x - a) ** 2 + (y - b) ** 2 return math.sqrt(s) def example2(x, y, a, b): w = abs(x - a) h = abs(y - b) return w * h

The value of the global variable color has not changed, it is still 'blue'. The problem is that Python treats the variable color in the function my_color as a local variable, so it is not the global variable color that is modified. In this case, you must use the keyword global to modify the above code slightly, as follows: This applies the variable of my_color() to the what_color() function.

2 Parameters In Python programming, the role of parameters is mainly to pass data to functions, and support a variety of parameters. Python generally uses pass-by-reference when passing parameters. That is, when we pass parameters, the function calls the original value with the new variable. Look at the code below:

Memory state after i and j are set to 10.9 and 3.7 respectively Memory state just after count(x,y) is called: x and y point to the values i and j point to respectively

When count(i,j) is called, Python creates two new variables--x and y--that point to the values of i and j, respectively, as shown above. Assignment is done in permutation order, so x points to i because i is the first argument. Instead of copying the values of the arguments, you simply assign them new names, and the function references them with those new names.

As a typical example, there are cases where parameters are not passed by the function. The function is prepared to fix the return value of all the variables passed in to 100, but the actual value of j does not change at all.

>>> def get(i):i = 100... >>> j = 12>>> get(j)>>> j12 The above code can be executed in the following steps:

1. assign 12 to j;

2. Call the function get(j) to assign the value of j to i, both i and j point to 100;

3. assign 100 to i;

4. Finally, after the function ends, i is deleted, and it can be seen that there is no access to variable j in the function.

In Python use we will also use keyword parameters, mainly to play a fixed splicing role on the output, there are two characteristics:

1. Clearly point out the parameter values, help to improve the readability of the program;

2. The order of keyword arguments does not matter.

3 Module In Python, a module is a series of uses of functions and variables. It consists of functions and variables. If you run with Python interpreter, after entering and exiting, you will find that all variables and functions defined have disappeared, so there is the origin of python module. First, creating a module requires creating a *.py file that contains the functions needed to complete the task. For example, the previous example: find the area of a circle with different radii.

import math def round_area(x): return math.pi * x ** 2 if __name__ == "__main__": x = int(input()) area = round_area(x) print("Area of circle with radius" + str(x) +"cm: "+ str(area))

The only difference between a module and a regular Python program is its purpose: a module is a toolbox of functions for writing other programs. Therefore, modules usually do not have a main() function. It is not easy to write module function examples here, for example, a python module, math module.

>>> math.exp(9)8103.083927575384

For custom module writing, we also need to pay attention to its namespace-a unique set of variable names and function names, which we will often use when calling module packages. Let's use jupyter as a simple example: the first line refers to the entire math module, and the second line simply indicates the use of sqrt functions in the math module.

import math refers to the python standard library's math.py module, and math.exp() is a function with argument 9. The module search path in Python is: When the Python interpreter encounters the import keyword, it will first search all directory lists, as follows:

#!/ usr/bin/python#file_name:test.pydef print_func( name): print ("Hello : ", name) return#!/ usr/bin/python# Filename: train.py #Import module import test #You can now call the functions contained in the module test.print_func("kaka")

That's all for "Example Analysis of Variables, Parameters, and Modules in Python." Thanks for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report