程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Introduction to Python zero Basics - 08 - variables and keywords in Python

編輯:Python

List of articles

  • Python Variables and keywords in
    • Variable
      • What is a variable ?
      • Variables exist there ?
      • Naming rules for variables
      • About Python Naming conventions in
    • keyword
      • What are keywords ?
      • Classification of keywords ?
      • A small case of trying to use keywords as variable names

Python Variables and keywords in

Variable

What is a variable ?

A variable is Python The storage unit used by the program to store the calculation results , For easy access to this storage unit ,Python Name this storage unit , This name is the variable name , stay Python Access the storage unit that holds the calculation results through the variable name in .

This official textbook interpretation is somewhat obscure , Combined with the... In our life Drink lots of water Scene to explain .

In our daily life, we will use glass Come and drink some water , But besides drinking water , We can also use the same cup to drink tea 、 Drink coke 、 Drink Maotai and so on ( I drink Maotai with a cup ).

The cup here can be seen as “ Variable ”, water 、 tea 、 Cola and so on are the values of variables , Pour water into the cup 、 pour tea 、 Pour coke for The assignment of the cup variable .

From this scene we can know , A cup of water is a variable , The cup is the variable name , And water 、 Tea is the value of the variable . Variables consist of variable names and values , The variable is constant, but the value of the variable can change .

summary :

  • A variable is the process of an assignment statement
  • You can change the value of a variable by assigning a value to its name
  • The variable name can remain unchanged , Variable values can be changed at any time
  • A variable name with a variable value is a variable

When we name a variable , Where do variables exist ? After all, it is invisible and untouchable , In fact, the memory of our computer is composed of many memory blocks . Every time we declare a variable , Will be stored in our memory . Of course , Variables are not stored indefinitely in memory , After all, our memory is limited , So when many variables are stored in our memory , There will be insufficient memory space .

summary :

  • Variables exist in the memory of our computer , The computer's memory is divided into many memory blocks
  • After each variable is defined, it is stored in a memory block , If there are too many variables , Memory may be out of space

Variables exist there ?

The picture below is Python Diagram of variables and memory in



Naming rules for variables

  • form : It has to be numbers 、 Letter 、 Underline _ form
  • requirement : The beginning must be non numeric , Do not use special symbols , for example :!@#$% etc.
  • Be careful : Case sensitive , The name should be meaningful , Hump nomenclature or underline nomenclature
  • length : Although it is of any length , But not too long , Poor readability
  • a key : It can't be Python Keywords already exist , If you use keywords, the original keyword function will be lost , This is not recommended
  • chinese : stay python2 Chinese cannot be used as the variable name in ; stay python3 Chinese variable names can be used , However, it is not recommended to use in the development environment

Legal variable names are as follows :

  • name
  • SEX
  • creat_table
  • _drop_user
  • select__mobile, The name contains 2 Root underline
  • _init_, The name contains 2 Root underline
  • OpenLesson, Although this hump type variable name can be used in Python Use in , But it's not a standard method , So it's not recommended

Illegal variable names are as follows :

  • get t a b l e , The life name package contain 了 word operator table, The name contains the characters table, The life name package contain 了 word operator
  • ab/c, The name contains the characters / And it doesn't make any sense
  • 01class_name, The number cannot start with a variable name

Now let's do a little exercise on variable names

name = "Neo"
birthday = "2000-01-01"
cat_name, cat_years = "mimi", "3" # Multiple variable names , It can be used continuously , But the left and right numbers must be the same 
if __name__ == '__main__':
print("my name is:" + name)
print("my birthday is:" + birthday)
print("I have a cat,name is:" + cat_name, "years is:" + cat_years)

The operation results are as follows :



About Python Naming conventions in

It should be noted that the explicit specifications of different programming languages are also different ~

Python General principles for naming recommendations :

  • Modules should be named in lowercase as much as possible , Keep the initials in lowercase , Try not to underline
  • Class names use humps (CamelCase) Naming style , title case , Private classes can start with an underscore
  • All function names are lowercase , If there are more than one word , Separate... With underscores
  • Private functions can start with an underline
  • Try to lower the variable name , If there are more than one word , Separate... With underscores
  • Constants are in uppercase , If there are more than one word , Use an underline to separate

keyword


What are keywords ?

python Internal special words for processing business logic , We mentioned above , Variable names should never be named by keywords


What is the difference between variable names and keywords ?

Variable names are used to assign values to variables ; Keyword is used for business logic processing .


Classification of keywords ?

Actually in python There is no keyword classification in . But if we use some keywords as variable names , Will report an error directly , This kind of keyword can be called Strong keyword ;

On the contrary, when we use some keywords as variable names , No errors are reported , But it will lose the original function of keywords , This kind of keyword can be called Weak keyword , It's usually python Built in functions in .


python Common keywords in , The keywords in the following table are Strongly typed keywords Never use it as a variable

keyword meaning True Boolean type , Express as true False Boolean type , Express as true def Function definition if If in logic elif In logic or if else Otherwise in logic try Opening of exception statement is Determine whether a variable is an instance of a class not Logical operations , Non operation or Logical operations , Operation of or pass No practical significance , Place holder raise Actively throw an exception in Determine whether the variable is in the sequence whilewhile Loop statement with simplify python sentence yield Return data from a loop or function in turn import Import statement , But with from share

A small case of trying to use keywords as variable names

Let's try to use Strong keyword As a variable name



False yes Python Key words of , So the prompt syntax error :SyntaxError: can’t assign to keyword.

Try again Weak keyword As a variable name




  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved