Python Quick Start Notes (3): constants and variables

This series of essays is my study notes, beginner stage will inevitably be understood inappropriate, wrong place kindly correct me. Please indicate the source: https://www.cnblogs.com/itwhite/p/12297694.html .

constant

Python and does not provide as C / C ++ in as the const keyword from the definition of constants, so in addition to literals, python says this is not constant.

Each data type has a corresponding literal, for example:

type of data Literals example
 Integer  123、-456
 Float  3.14 
 Boolean  True、False 
 None   None 
 String type  'foo'、"bar"、'''qux'''、"""quz""" 
 List Type  [1, 2, 3]、["Jack", "Bob", "Lucy"]
 Tuple type  (1, 2, 3, [4, 5, 6])
 Dictionary Type  { "name": "Jack", "age": 23 }

 

variable

Python, no separate variable declaration, it will be automatically assigned in the first declaration (i.e., the assignment statement can not be used before unassigned), for example:

123 = foo
 Print (foo)   # 123 
Print (bar)   # NameError: name 'bar' IS Not defined, can not be used before it is assigned 
bar = 456

Variable scope

Python variables are also points of global and local variables, unless you use the global keyword stated otherwise, when a local variable with the same name as global variables, global variables will be covered, such as:

# - * - Coding: UTF-. 8 - * - 
X = 123 # global variables 
Y = 456 # global variables 
Z = 789 # global variable 

DEF foo (A, B):
     Print (X)              # 123, used herein are global variable X 
    Y = a + B             # assignment statement i.e., where local variables declared equivalent Y 
    Print ( " Y: D% " % (Y)) # 33 is 
    global Z              # explicitly declared global variables Z 
    Z = a + B             # where z is a global variable is still 
    Print ( " z: D% " % (z)) # 33 is

foo ( . 11, 22 is )
 Print ( " X: D% " % (X))      # 123 
Print ( " y: D% " % (y))      # 456, the value of the global variable y unmodified 
Print ( " Z: D% " % (z))      # 33 is, the value of the global variable z is changed

 

Finish.

Guess you like

Origin www.cnblogs.com/itwhite/p/12297694.html