Content summary
- Basic use of pychar
- Annotation syntax
- Variables and constants
- Garbage collection mechanism
- data type
Detailed content
Note that the Python environment should be downloaded locally, and the main.py script file is not checked temporarily
Topic selection:
pycharm switch interpreter:
Click add
Select the desired interpreter and click OK
How to create a Python script file
Right click the folder where you selected to store the project
Just enter a name and press enter
Since the suffix of the file is used to identify the data characteristics inside the file, our Python file also has a unique suffix. py
How to adjust font size
Check and click OK
Then when writing code, you can press Ctrl to slide the pulley on the mouse to zoom in and out
How to run Py files
Right click in the file content area and select run
Python annotation syntax (comments are the mother of code!!!)
What is a comment
A comment is an explanation of a piece of code
How to use annotations
Method 1: use '#' single line annotation
Method 2: use three quotation marks (either single or double) to comment on multiple lines
pycharm annotation shortcut
Ctrl + ?
Note:
# There must be a space between and the comment text
If a single line comment follows a line of code, you need to leave two spaces blank before writing
pycharm also provides automated format code functionality
Python code writing specification: PEP8 specification
Want to quickly grasp the automatic tips with pycharm, compare before and after, and memorize every day
variable
What are variables That is, the amount of change is used to record a certain state of things (imitating the memory ability of human things) How to use variables In daily life: full name: zhangsan Age: 18 Hobbies: Music In the program: username = 'zhangsan' age = 18 hobby = 'music' Syntax format: username = 'zhangsan' Variable name assignment symbol variable value """ Underlying principle (must know!!) In case of assignment symbols, first look to the right of the symbols and then to the left: age = 18 1.Apply for a memory space storage 18 in the memory space 2.Bind the memory space address of 18 to the variable name age 3.Then, if you want to access 18, you can use the variable name age Just visit """
Naming conventions and styles
# Naming conventions Variable names can only be any combination of numbers, letters and underscores user@name(incorrect), _(sure),pwd_123_aaa(sure) Variable names cannot start with numbers. It is recommended not to start with underscores because they have special meanings Variable names cannot conflict with keywords: For example: name = print Variable names must be named according to the name (important) """Variable name is the core, no matter how long the variable is""" # Naming style 1.Hump body Big hump # All words are capitalized UserNameFromDb Small hump # The first letter is lowercase and the rest is uppercase userNameFromDb '''JavaScript Hump body is recommended''' 2.Underline # Words are separated by underscores user_name_from_db '''python Underline is recommended'''
constant
# It is mainly used to record some unchanged states # In Python, there is no constant in the real sense, and we regard all uppercase variables as constants HOST = '127.0.0.1' # Generally, it is used more in the configuration file # In other programming languages, the definition of constants in the real sense cannot be modified const pi = 3.14 # Define constants pi = 4 # Modification is not supported
Three elements of variables
1.Value of variable 2.Memory address of variable 3.Data type of variable name = 'zhangsan' print(name) # value print(id(name)) # A string of numbers corresponds to the memory address number print(type(name)) # Data type < class' STR '>
Python underlying optimization
When the amount of data is very small, if multiple variable names need to be used, they will point to the same address """ A variable name can only point to one memory address A memory address can have multiple variable names pointing to """
Garbage collection mechanism
# Definition of garbage data There is no data in memory that the variable name points to # An automatic recycling scheme is developed for garbage data 1.Reference count There are several variable names on the variable value in memory, and the binding count is just a few. As long as it is not 0, it is not garbage 2.Mark clear When the memory is about to fill up Python It will automatically pause the execution of the program and scan the data in memory from beginning to end After marking, the marked data is cleared at one time 3.Generation recycling The supervision of data will be divided into three levels, and the supervision frequency will decrease with the decrease of the level
data type
# What is a data type? In real life, there are many ways and manifestations of storing data File text table file video file audio file picture file... stay IT The storage methods and forms of data in the world are also changeable
Integer int of data type
# Understanding: integer int Function: record the age of the person and the number of classes... definition: age = 18 # Writing an integer directly is an integer
Floating point float of data type
# Understanding: decimal float Function: record people's weight, salary and height definition: salary = 3.1 # Writing decimals directly is floating point """ Integer and floating point types can be collectively referred to as numeric types It is mainly used for mathematical operation and comparison operation """