The use of symbols in Python basic grammar

       I haven't updated python with my friends for a long time, I'm sorry for this, and I will try to update as much as possible in the future

Table of contents

1. Identifier

A. Definition:

B. Features of use

C. Python identifiers, further explore the details of the following aspects:

1. Rules and conventions:

2. Examples of valid identifiers:

3. Examples of invalid identifiers:

D. Summary

 2. python operator

A. Operators (arithmetic, compound)

 Classification of operators commonly used in B.ython and their examples:

1. Arithmetic operators:

2. Comparison operators:

3. Assignment operator: 

4. Logical operators:

5. Bitwise operators:

6. Membership operator:

7. Identity operator:

C. Examples

1. Arithmetic operators:

2. Comparison operators:

3. Assignment operator:

4. Logical operators:

5. Bitwise operators:

6. Membership operator:


1. Identifier

A. Definition:

Python identifiers are names used to identify named entities such as variables, functions, and classes . It is the name developers use when writing Python code. Identifiers are used to refer to and access these named entities so that developers can use them in programs.

B. Features of use

  • Meaningful: An identifier should clearly describe the object or data it represents.
  • Comply with the naming convention: usually use lowercase letters and underscores to name variables and functions (for example: my_variable, calculate_total_sum), and use camelCase to name classes (for example: MyClass).
  • Avoid using Python keywords : There are some reserved keywords in Python (eg,if,for,whileetc.) that cannot be used as identifier names.

C. Python identifiers, further explore the details of the following aspects:

1. Rules and conventions:

 - Identifiers consist of letters, numbers, and underscores, and can contain any number of characters .
   - The first character must be a letter (az, AZ) or underscore (_), not a number or other special character .
   - Identifiers are case-sensitive , so `myVariable` and `myvariable` are different identifiers .
   - Python keywords (reserved words) cannot be used as identifier names.
   -Naming conventions: The Python community generally follows some naming conventions, such as using lowercase letters and underscores to name variables, functions, and modules (for example: my_variable, calculate_total_sum, my_module), and using the camel case naming method with the first letter capitalized to name classes (for example: MyClass).

2. Examples of valid identifiers:

 - Examples of legal identifiers: `my_variable`, `count`, `total_sum`, `add_numbers`, `MyClass`.
 - These identifier names all conform to the rules and naming conventions of Python identifiers, so they can be used in the code.

3. Examples of invalid identifiers:

   - Identifiers starting with a number are invalid, eg: `1variable`.
   - Identifiers using special characters (except underscore) are invalid, eg: `$total`, `#count`.
   - It is invalid to use reserved words as identifiers, for example, you cannot name variables `if`, `for`, `while`, etc.

To sum up, a Python identifier is a name composed of letters, numbers, and underscores, which is used to identify named entities such as variables, functions, and classes. Following Python's naming rules and conventions can improve the readability and maintainability of your code.

D. Summary

These are some small details of daily python development. You don’t need to remember them deliberately. You just need to use them frequently in the usual python development process. These are summarized by the editor in daily use. If there are shortcomings Please correct me!

 2. python operator

A. Operators (arithmetic, compound)

 Classification of operators commonly used in B.ython and their examples:

1. Arithmetic operators:

   - Addition: `+`, for example: `2 + 3`, the result is `5`.
   - Subtraction: `-`, for example: `5 - 2`, the result is `3`.
   - Multiplication: `*`, for example: `3 * 4`, the result is `12`.
   - Division: `/`, for example: `10 / 5`, the result is `2.0`.
   - Remainder: `%`, for example: `10 % 3`, the result is `1`.
   - Divisibility: `//`, for example: `10 // 3`, the result is `3`.
   - Exponentiation: `**`, for example: `2 ** 3`, the result is `8`.

2. Comparison operators:

 - Equality: `==`, for example: `2 == 3`, the result is `False`.
   - Not equal: `!=`, for example: `2 != 3`, the result is `True`.
   - Greater than: `>`, for example: `5 > 2`, the result is `True`.
   - Less than: `<`, eg: `5 < 2`, the result is `False`.
   - Greater than or equal to: `>=`, for example: `5 >= 2`, the result is `True`.
   - Less than or equal to: `<=`, for example: `5 <= 2`, the result is `False`.

3. Assignment operator: 

 - Simple assignment: `=`, eg: `x = 3`.
   - Addition assignment: `+=`, for example: `x += 2`, equivalent to `x = x + 2`.
   - Subtraction assignment: `-=`, for example: `x -= 2`, equivalent to `x = x - 2`.
   - Multiplication assignment: `*=`, for example: `x *= 2`, equivalent to `x = x * 2`.
   - Division assignment: `/=`, for example: `x /= 2`, equivalent to `x = x / 2`.

4. Logical operators:

   - With: `and`, for example: `True and False`, the result is `False`.
   - Or: `or`, eg: `True or False`, the result is `True`.
   - Not: `not`, for example: `not True`, the result is `False`.

5. Bitwise operators:

 - Bitwise AND: `&`, for example: `5 & 3`, the result is `1`.
   - Bitwise OR: `|`, for example: `5 | 3`, the result is `7`.
   - Bitwise XOR: `^`, for example: `5 ^ 3`, the result is `6`.
   - Negation: `~`, for example: `~5`, the result is `-6`.
   - Shift left: `<<`, for example: `5 << 2`, the result is `20`.
   - Shift right: `>>`, for example: `5 >> 2`, the result is `1`.
 

6. Membership operator:

   - `in`: Check if the element is in the iterable object, for example: `2 in [1, 2, 3]`, the result is `True`.
   - `not in`: Check if the element is not in the iterable object, for example: `4 not in [1, 2, 3]`, the result is `True`.

7. Identity operator:

   - `is`: Determine whether two objects are the same object, for example: `x is y`.
   - `is not`: Determine whether two objects are not the same object, for example: `x is not y`.

These are just some of the common operators in Python, there are other operators and advanced usages that you can learn and explore according to your specific needs.

C. Examples

1. Arithmetic operators:

x = 5
y = 2

print(x + y)  # 输出:7
print(x - y)  # 输出:3
print(x * y)  # 输出:10
print(x / y)  # 输出:2.5
print(x % y)  # 输出:1
print(x // y) # 输出:2
print(x ** y) # 输出:25

2. Comparison operators:

x = 5
y = 2

print(x == y)  # 输出:False
print(x != y)  # 输出:True
print(x > y)   # 输出:True
print(x < y)   # 输出:False
print(x >= y)  # 输出:True
print(x <= y)  # 输出:False

3. Assignment operator:

x = 5
y = 2

x += 2  # 相当于:x = x + 2
print(x)  # 输出:7

x -= 2  # 相当于:x = x - 2
print(x)  # 输出:5

x *= 2  # 相当于:x = x * 2
print(x)  # 输出:10

x /= 2  # 相当于:x = x / 2
print(x)  # 输出:5.0

4. Logical operators:

x = 5
y = 2

print(x > 3 and y < 4)  # 输出:True
print(x > 3 or y > 4)   # 输出:True
print(not(x > 3))       # 输出:False

5. Bitwise operators:

x = 5  # 二进制表示为 101
y = 3  # 二进制表示为 011

print(x & y)   # 输出:1   (二进制表示为 001)
print(x | y)   # 输出:7   (二进制表示为 111)
print(x ^ y)   # 输出:6   (二进制表示为 110)
print(~x)      # 输出:-6  (二进制表示为 11111111111111111111111111111010)
print(x << 1)  # 输出:10  (二进制表示为 1010)
print(x >> 1)  # 输出:2   (二进制表示为 10)

6. Membership operator:

x = 2
list = [1, 2, 3, 4, 5]

print(x in list)        # 输出:True
print(x not in list)    # 输出:False
print(3 in list)        # 输出:True
print(6 not in list)    # 输出:True

7. Identity operator:

x = [1, 2, 3]
y = [1, 2, 3]

print(x is y)      # 输出:False
print(x is not y)  # 输出:True
print(x is x)      # 输出:True

Guess you like

Origin blog.csdn.net/lz17267861157/article/details/131948831