Python Basics
This article will introduce the most basic processing and operations in Python.
1. Variables
In Python, variables do not need to be declared; they can be directly assigned values, as follows:
Python | |
---|---|
1 2 3 |
|
2. Output Variables
Variables' contents can be output using multiple methods, as follows:
2.1 Using the print()
Method
The print()
method directly outputs the content of the variable, as follows:
Python | |
---|---|
1 2 |
|
Output:
Text Output | |
---|---|
1 |
|
2.2 Using the type()
Method
The type()
method returns the type of the variable and can be used with print()
, as follows:
Python | |
---|---|
1 2 |
|
Output:
Text Output | |
---|---|
1 |
|
2.3 Using the repr()
Method
The repr()
method returns detailed information about the variable and can be used with print()
, as follows:
Python | |
---|---|
1 2 |
|
Output:
Text Output | |
---|---|
1 |
|
Using the repr()
method outputs strings with quotes, which helps distinguish between numbers and string representations of numbers.
2.4 Outputting Multiple Values
print()
can output multiple variable values at once, making it convenient to output key program information, as follows:
Python | |
---|---|
1 2 3 4 |
|
Output:
Text Output | |
---|---|
1 |
|
You can also use a "title, content" format for output, which makes it easy to check each variable's value, as follows:
Python | |
---|---|
1 2 |
|
Output:
Text Output | |
---|---|
1 |
|
3. Basic Data Types
Python includes the following basic data types:
Data Type | Description | Example |
---|---|---|
None |
Null Value | a = None |
int |
Integer | a = 100 |
float |
Float | a = 100.0 |
str |
String | a = 'Hello, World! |
bool |
Boolean | a = True b = False |
When writing Python code, avoid using data type names directly as variables, such as:
Python | |
---|---|
1 |
|
3.1 Null Value None
None
is a value that represents "nothing," and can be checked using is
or is not
, as follows:
Python | |
---|---|
1 2 3 4 |
|
Output:
Text Output | |
---|---|
1 2 |
|
3.2 Integers int
, Floats float
int
, float
can perform mathematical operations and comparisons, as follows:
Python | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Output:
Text Output | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
3.3 Strings str
str
can perform string concatenation, formatting, equality/comparison (i.e., alphabetical order), and containment checks, as follows:
Python | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Output:
Text Output | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Generally speaking, it is recommended to use formatted string generation (called f-string
in Python), which makes the code more intuitive compared to simple string concatenation with plus signs.
The syntax structure of f-string
is as follows:
3.4 Booleans bool
bool
can perform logical operations, as follows:
Python | |
---|---|
1 2 3 4 5 6 7 8 9 |
|
Output:
Text Output | |
---|---|
1 2 3 4 5 6 |
|
4. Common Data Types
In addition to basic data types, Python also provides some very common data types.
4.1 Lists list
A list is an ordered collection of several elements, similar to arrays Array
in other programming languages.
Elements in Python lists can be of any type and do not have to be of the same type, such as:
Python | |
---|---|
1 2 |
|
When writing Python code, avoid using data type names directly as variables, such as:
Python | |
---|---|
1 |
|
Common operations supported by lists are as follows:
Python | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
Output:
Text Output | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
4.2 Tuples tuple
Tuples are similar to lists and are also ordered collections of several elements, but tuples cannot be modified once created.
Elements in Python tuples can be of any type and do not have to be of the same type, such as:
Python | |
---|---|
1 2 |
|
When writing Python code, avoid using data type names directly as variables, such as:
Python | |
---|---|
1 |
|
Since parentheses used to represent tuples might be confused with regular parentheses, when defining a tuple with only one element, you need to add an extra comma to indicate this, as follows:
Python | |
---|---|
1 2 |
|
Output:
Text Output | |
---|---|
1 |
|
4.3 Dictionaries dict
Dictionaries are unordered collections of several elements organized in Key-Value pairs, similar to maps Map
in other programming languages.
In Python dictionaries, Keys are usually strings, while Values can be of any type and do not have to be of the same type, such as:
Python | |
---|---|
1 2 3 4 5 6 |
|
When writing Python code, avoid using data type names directly as variables, such as:
Python | |
---|---|
1 |
|
Common operations supported by dictionaries are as follows:
Python | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
Output:
Text Output | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
4.4 Sets set
Sets are unordered collections of non-repeating elements.
Elements in Python sets can be of any type and do not have to be of the same type, such as:
Python | |
---|---|
1 2 |
|
When writing Python code, avoid using data type names directly as variables, such as:
Python | |
---|---|
1 |
|
Common operations supported by sets are as follows:
Python | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
Output:
Text Output | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
4.5 Mutual Nesting
Lists, dictionaries, and sets can be mutually nested to create complex data structures, such as:
Python | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Complex data consisting of basic data types, lists, tuples, and dictionaries can be output as JSON strings using Python's built-in json
library, such as:
Python | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Output:
Text Output | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
5. Conditional Handling with if-elif-else
Syntax
In Python, conditional handling is performed using if ... elif ... else
, such as:
Python | |
---|---|
1 2 3 4 5 6 7 8 9 |
|
Output:
Text Output | |
---|---|
1 |
|
Conditional handling can also be nested to implement more complex controls, such as:
Python | |
---|---|
1 2 3 4 5 6 7 |
|
Output:
Text Output | |
---|---|
1 2 |
|
6. Loop Handling with for
and while
Syntax
In Python, loops can be handled using for ...
or while ...
.
6.1 for
Loops
for
loops are generally used for known loop counts, such as iterating over lists, as follows:
Python | |
---|---|
1 2 3 |
|
Output:
Text Output | |
---|---|
1 2 3 |
|
You can also use range(...)
to directly specify the number of iterations, as follows:
Python | |
---|---|
1 2 |
|
Output:
Text Output | |
---|---|
1 2 3 |
|
You can also use enumerate(...)
to iterate over lists and obtain indices simultaneously, as follows:
Python | |
---|---|
1 2 3 |
|
Output:
Text Output | |
---|---|
1 2 3 4 |
|
6.2 while
Loops
while
loops are generally used for known loop conditions. The loop executes as long as the condition is met and ends when the condition is no longer satisfied, as follows:
Python | |
---|---|
1 2 3 4 5 |
|
Output:
Text Output | |
---|---|
1 |
|
When using while
loops, be especially careful with the condition setting to avoid infinite loops caused by conditions that are always true.
6.3 Skipping, Interrupting Loops
During the loop process, you can use the continue
statement to immediately jump to the next iteration or use the break
statement to terminate the entire loop, as follows:
Python | |
---|---|
1 2 3 4 5 6 7 |
|
Output:
Text Output | |
---|---|
1 |
|
7. Functions
Functions are reusable blocks of code that reduce coding complexity, such as:
Python | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Output:
Text Output | |
---|---|
1 2 3 |
|
7.1 Function Parameters
In the example above, the plus
function contains two parameters a
and b
as input parameters. These input parameters can be directly used inside the function for processing.
Additionally, function parameters support default values. Parameters with default values do not need to be specified when calling the function, as follows:
Python | |
---|---|
1 2 3 4 5 |
|
Output:
Text Output | |
---|---|
1 2 |
|
Function parameters can also be called using parameter=value
without strictly following the order, as follows:
Python | |
---|---|
1 2 3 4 5 6 |
|
Output:
Text Output | |
---|---|
1 2 3 4 |
|
7.2 Function Return Values
The return
statement is used to return the function's return value. Besides returning a single value, functions can also return multiple values at once, as follows:
Python | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Output:
Text Output | |
---|---|
1 2 |
|
8. Importing Modules with import
Statement
In more complex projects, code is often placed in multiple different scripts for easier maintenance and management.
When needing to call code between scripts, the import
statement is used to import modules.
Imported Script Location | Import Method |
---|---|
Python script in the same directory | import {script name} |
Python script in a subdirectory | import {directory name}.{script name} |
Importing specific parts | from {script name} import {object 1}, {object 2}, ... |
Additionally, for external scripts with long names, you can use import xxx as yyy
to assign aliases, as follows:
Python | |
---|---|
1 2 3 4 5 6 7 8 9 |
|
Python | |
---|---|
1 2 3 |
|
Python | |
---|---|
1 2 3 4 5 6 7 8 9 |
|
Output:
Text Output | |
---|---|
1 2 3 4 |
|
The description of import
here is only the simplest commonly used part. For a complete and detailed introduction to import
, please refer to professional tutorials.
8.1 Scripts in DataFlux Func
Due to the special mechanism of script management in DataFlux Func, scripts in DataFlux Func do not exist as files but are stored in databases. Therefore, importing other scripts in DataFlux Func differs from original Python implementations.
In DataFlux Func, Python scripts are organized in two layers: the first layer is the "Script Collection", and the second layer is the "Script". A script collection is merely a collection of scripts and is not a "folder".
Both script collections and scripts have IDs and are related. In a certain script collection, the script ID must be {associated script collection ID}__{script name}
(with two underscores in between), as shown in the figure below:
8.2 Importing Scripts in DataFlux Func
Assume there are the following script collections and scripts:
Then, the import methods are as follows:
Imported Script Location | Import Method |
---|---|
Python script in the same collection | import __{script name} or import {script ID} |
Python script in another collection | import {script ID} |
Importing specific parts | from {script name} import {object 1}, {object 2}, ... |
For Python scripts in the same collection, it is recommended to use import __{script name}
for importing, so that after cloning the entire script collection, the reference path remains correct.
9. Code Comments
Adding comments in scripts is a good habit.
Comments can be used to explain the written code or make notes on special treatments, so that others or yourself can quickly understand the details of the code when reviewing it later.
The level of detail in comments can be chosen reasonably based on the actual project, code, and complexity of the treatment.
Example | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
10. Object-Oriented Programming
In the examples above, all are based on procedural programming, where the code consists entirely of functions.
Python is a multi-paradigm programming language that can be fully written procedurally but can also incorporate object-oriented techniques, making the code more concise and easier to maintain.
For simple processes, if procedural programming works well, there is no need to force the use of object-oriented techniques. This section is merely a simple demonstration of object-oriented programming development in Python.
10.1 Classes and Inheritance
A "class" refers to a collection of objects with common or similar functions and data. What a class is depends on each individual's specific implementation approach and there are no strict unified rules.
Classes can have inheritance relationships, where the inherited class is called the "parent class" and the inheriting class is called the "child class." Inheritance relationships are also written according to each individual's specific implementation approach and there are no strict unified rules.
In general:
- The parent class writes some common methods that all child classes possess, and child classes inheriting from the parent class automatically gain all the methods of the parent class without needing to rewrite them.
- Child classes can write additional methods that can only be called on child class objects.
- If a method in the parent class is not applicable in the child class, the child class can rewrite the method with the same name to change its behavior in the child class objects.
10.2 Typical Example
Assume there is a "School Personnel Management System," with the following main business objects:
Personnel | Description |
---|---|
Teacher | Has a name, access card, and performs teaching activities |
Student | Has a name, access card, and performs learning activities |
It can be seen that different personnel all have access cards, but the activities they perform differ. Thus, in specific code implementation, the access card-related functionality can be uniformly handled, while the activities need separate handling.
Example code as follows:
Python | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|
Output:
Text Output | |
---|---|
1 2 3 4 |
|
11. Error Handling
For some uncertain places that may cause problems (such as calling interfaces from other systems), errors during code execution may cause the entire program to crash, as follows:
Python | |
---|---|
1 2 3 4 5 6 7 8 |
|
Output:
Text Output | |
---|---|
1 2 3 4 5 6 |
|
At this point, we need to handle potentially problematic code separately.
Since program crashes themselves output useful error information, it is not necessary to handle every piece of code for errors. This is merely a simple demonstration of Python error handling.
11.1 Error Handling with try-except
Syntax
You can use try...except
to handle possible errors, as follows:
Python | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Output:
Text Output | |
---|---|
1 2 |
|
11.2 Raising Errors with raise
Statement
When writing your own code, if you encounter non-standard input parameters, you can also manually throw errors, as follows:
Python | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Output:
Text Output | |
---|---|
1 2 |
|