Python Basics-Variables

Source: Internet
Author: User

A variable is simply a reserved memory location used to store values. This means that when a variable is created, it retains some space in memory.

Depending on the data type of a variable, the interpreter allocates memory and determines how it can be stored in the reserved memory. So, by assigning variables of different data types, you can store integers, decimals, or characters in these variables.

Assignment of variables:

' 123 '  'ABC'== 123

Variable type:

1. String
2. Boolean type
3, Integer
4. Floating point number
5. Digital
6. List
7, meta-group
8. Dictionaries
9. Date

1. String

' python '  "python"'hellopython'print  #  pythonprint#  pythonprint# Hello python preserves line breaks

2. Boolean type

bool == False

3. Integer

I= = Int ('+')

4. Floating point number

f = 23.4= float ('23.4')

Other types are described in detail in other chapters.

Scope of the variable:

    • The code snippet in which Python can change the scope of a variable is Def, class, Lamda.
    • If/elif/else, try/except/finally, For/while are not involved in variable scope changes, which means that the variables in their code block are also accessible externally.
    • Variable search path is: local variable, global variable

A = 1if a = =1    := []print(a)#>>>[1,2,3]  If statements do not affect the scope of the variable  = 1def  F ():    = 2print#  1f ()    The execution of the Print # 1 function does not affect the value of variable a

If you want to manipulate variables in Def,class,lambda, you need to declare them internally:

A = 1def  F ():    Global  a    = 2print#  1f ()  print#  2 manipulating variables inside a function by declaring global variables

Python Basics-Variables

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.