current position:Home>Comments and variables in Python

Comments and variables in Python

2022-01-30 21:09:42 Don't laugh and meditate

This is my participation 11 The fourth of the yuegengwen challenge 1 God , Check out the activity details :2021 One last more challenge


What is annotation

  • It won't be in the code Python Directly run statements

Why use notes

  • Facilitate the later reading of the code
  • Facilitate the later maintenance of the code

Three uses of annotations

# Single-line comments
''' ''' Multiline comment
""" """ Multiline comment

Python Script execution entry

What is a script entry

  • The execution of the program also needs such an entry
  • — Generally, we call the entry of code execution as the main function main

How to write code script


If your name is Xiaoming .py, In the eyes of friends , You are Xiaoming (__name_==‘ Xiao Ming ’__);

In your own eyes , You are yourself ( __name_ == '__main_' )


# coding:utf-8

import os

if __name__ == '__main':
print(" I return 1 Level code block ")
 Copy code 
  • Indent the same level of proof code

For the code entry, hope is the last of the code


What is a variable

for instance :

Prepare a glass , We can pour coke into the glass or sprite into the glass , The glass doesn't change during this process , What changes is the liquid in the glass . Here we can think of the glass as a variable name , Treat the liquid in the glass as a variable value, and this process is assignment .

Pictured :


summary : What is a variable

  • Assignment statement
  • You can change the value by assigning a value to the variable name
  • The variable name can remain unchanged , Variable values can be changed
  • A variable name with a variable value —— Variable

Where do you live

  • Variables exist in the memory of our computer
  • Each variable is defined and stored in a memory block

Rules for variable names

  • form : It has to be numbers , Letter , Underline composition
  • length : Any length , But not too long ,20 Within characters
  • requirement : Must start with a letter
  • Be careful : Case sensitive 、 meaningful

Common errors about variables

#  The variable name naming rule is wrong, and the error message 
SyntaxError: invalid token
#  Using undefined variables 
NameError: name 'worker' is not defined
 Copy code 

Defining variables

# coding: utf-8

usename = ' Xiaomo '
age = 20
is_sleep = 'ok'
is_eat,is_learn,is_play = 'yes','no','no'

if __name__ == '__main__':

 Copy code 

Be careful : If you define multiple variables on the same line , Then the defined variable value must correspond to the variable name one by one , Otherwise, an error will be reported

Error message :ValueError: not enough values to unpack (expected 3, got 2)

copyright notice
author[Don't laugh and meditate],Please bring the original link to reprint, thank you.

Random recommended