Python Basics-03. Data Type-02. Variables and Constants

Source: Internet
Author: User

This article mainly introduces the variables and constants in Python, including the naming conventions of variables, the use of considerations

1. Variables 1.1. Variable Understanding

When do I need to use a variable?
A program or value needs to be used frequently in the future to use variables
Variables include: variable name, variable value
For a computer, a variable can be seen as a container for information stored in a computer program that needs to be referenced and manipulated.
For people, a method of tagging data with descriptive names so that the programs we write can be more clearly understood by the reader and ourselves.

Concept distinction:
What is the difference between declaring, defining, assigning, and initializing a variable?
Variable declaration or definition: Specifies the name of a variable in the environment. Represents a piece of space in memory.
Assignment: Destroys the original value of a variable and assigns a new value. The equivalent of changing the state of a variable. Change the value in the memory area.
Initialize: A variable that has not been initialized after a declaration has a meaningful initial value. The value that is assigned when the memory area is opened. If not specified, the default value of the type is assigned when it is opened.

Note: You should be aware of the distinction between initialization and assignment, and you should not allow assignment of variables before initialization.

1.2. Definition specification of variable name:

1) to have a descriptive role
2) variable names can only be any combination of letters, numbers, or underscores, and cannot begin with a number, and may be preceded by an underscore.
3) variable names can be written in Chinese, but do not do this
4) The following keywords cannot be declared as variable names (occupied by the system)

[' and',' as','assert',' Break','class','Continue','def','del','elif','Else','except','exec','finally',' for',' from','Global','if','Import','inch',' is','Lambda',' not','or','Pass','Print','Raise','return','Try',' while',' with','yield']

5) variables defined by the equal sign are suggested to be separated by spaces (with or without spaces effect the same)

Instance:
Student_number = 30# python commonly used this, underline join phrase
Studentnumber = 30# Hump body, java,asp developers commonly used
Summary: The left variable name to write simple and intuitive to understand

function is defined in lowercase
def sayhi# Full lowercase

Class
Class luffycity# Hump Body

Variables in the 1.3.Python

Python variables do not require special declarations, data types are automatically determined by Python and can be entered directly:
Define a variable A in memory, its value is 10, its type is an integer (integer), then the following output:

Print (a) Print (Type (a))<class'int'>

Recycling of variables in 1.4.Python

If you want a to store different data, you do not need to delete the original variable can be directly assigned value.
1) Re-assignment, equivalent to canceling the relationship between the variable name and the value of the variable

>>>a = 1.3>>>print(A,type (a))1.3 <class'  float'>

2) Delete the variable delete

>>>del AAA

3) Timeout cleanup

Python has its own memory-recycling mechanism, checking variable values without variable names at regular intervals to clean up, usually without worrying about full memory.

1.5. Two-time assignment problem for variables
" AAA ">>>name2 = name1>>>print  (name1,name2) AAA AAA"  BBB">>>print  (name1,name2) BBB AAA

Conclusion

# Assign a variable name1 to a Variant name2, name1 just deliver the value of his own variable to name2, and after assigning the value name2 get the new value,
# after the two of them have no relationship, at this time no matter how name1 changes will not affect the value of name2,
# because "name2 = name1" is not the establishment of name1 and name2 relationships

Expand
Other apps for annotation symbols---multi-line printing

""print"ddd" = name1bbb aaa 

Summary: The printed multiline content is only used as a string and does not involve the assignment of variables

1.6. Variable assignment problems:

1) Assigning strings directly
2) assigning values to variables with variables
# Print variables, print strings and print blank lines

2. Constant 2.1 constant: Unchanged amount

All caps are used to represent constants
For example: PIE = 3.1415926 ...

2.2. Constants and variable differentiation

1) Uppercase letters are used to denote constants, lowercase letters are used to denote variables,
2) All variables in Python are mutable, but constants can also be modified

Finished, hehe hehe

Python Basics-03. Data Type-02. Variables and Constants

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.