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

Python variables

編輯:Python

  be based on python:3.7.8 

One 、 What is a variable

A variable is a box , It can hold all kinds of things . Packed with apples , When boxes work with other things , It's the apple ; Packed with pears , When working with something else , It is a pear .

Compiler language (JAVA、Dart etc. ) The variable of requires that the box be fixed , Those who hold fruit will hold fruit , If you want to pack pastry, just pack pastry , Assembly parts of assembly parts .

Explanatory language (python、javascript etc. ) Variables of do not require , Casually , What you like to put on . When it cooperates with fruit, use it as fruit ; When it cooperates with pasta, use it face to face .

A generalized variable is relative to a constant , A variable quantity . Variables are absolute , Constants are relative , There is no absolute constant .

From Zhihu : What do variables mean ?

Two 、 How to define variables

1. Variable rule

  • The first character must be a letter or underscore in the alphabet  _ .
  • The rest of the identifier consists of the letters 、 Numbers and underscores .
  • Identifiers are case sensitive .
  • stay Python 3 in , You can use Chinese as the variable name , Not ASCII Identifiers are also allowed .
messsage = "hello,world!"
number = 12;
_Name = " Zhang San ";
Zhang Mes = " I am a Chinese character variable ";
print(messsage);
print(number);
print(_Name);
print( Zhang Mes);
hello,world!
12
Zhang San
I am a Chinese character variable 

  Variable names are not recommended , Lowercase letters l, And number 0, because 1 and l,0 and o Easy to confuse ; Chinese characters are not recommended ;

2. Built in keywords

Python 3.7.8 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
>>>
'False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'

  3、 ... and 、 Constant

python Syntax is not used to enforce definitions in Constant , in other words ,python In the definition of Constant It is essentially a variable . If you have to define Constant , Variable names must be all uppercase ;

From a normative point of view , By default, the constants are capitalized , Don't change its value ;

PI=3.1415;
print(PI)


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