In-depth analysis of variables and value assignment operators in Python

Source: Internet
Author: User
This article mainly introduces how to deeply parse the variables and value assignment operators in Python. It is a basic knowledge in getting started with Python. For more information, see Python variable type
The value of the variable stored in the memory. This means that a space is opened in the memory when a variable is created.
For variable-based data types, the interpreter allocates the specified memory and determines what data can be stored in the memory.
Therefore, variables can specify different data types, which can store integers, decimals, or characters.

Variable assignment
Variables in Python do not need to be declared. Variable assignment is a process of variable declaration and definition.
Each variable created in the memory contains the variable identifier, name, and data.
Each variable must be assigned a value before it can be used.
Equals sign (=) is used to assign values to variables.
On the left side of the equal sign (=) operator is a variable name, and on the right side of the equal sign (=) operator is the value stored in the variable. For example:

#! /Usr/bin/python #-*-coding: UTF-8-*-counter = 100 # assign a value to the integer variable miles = 1000.0 # Floating Point name = "John" # string print counterprint milesprint name

In the above example, 100,100 0.0 and "John" are assigned to the counter, miles, and name variables respectively.
The following results are output when the above program is executed:

1001000.0John

Assign values to multiple variables
Python allows you to assign values to multiple variables at the same time. For example:

a = b = c = 1


For the above instance, create an integer object with a value of 1. The three variables are allocated to the same memory space.
You can also specify multiple variables for multiple objects. For example:

a, b, c = 1, 2, "john"


In the above example, the two integer objects 1 and 2 are allocated to variables a and B, and the string object "john" is allocated to the variable c.


Python assignment operator
Assume that variable a is 10 and variable B is 20:

The following example demonstrates the operations of all the value assignment operators in Python:

#!/usr/bin/pythona = 21b = 10c = 0c = a + bprint "Line 1 - Value of c is ", cc += aprint "Line 2 - Value of c is ", c c *= aprint "Line 3 - Value of c is ", c c /= a print "Line 4 - Value of c is ", c c = 2c %= aprint "Line 5 - Value of c is ", cc **= aprint "Line 6 - Value of c is ", cc //= aprint "Line 7 - Value of c is ", c

Output result of the above instance:

Line 1 - Value of c is 31Line 2 - Value of c is 52Line 3 - Value of c is 1092Line 4 - Value of c is 52Line 5 - Value of c is 2Line 6 - Value of c is 2097152Line 7 - Value of c is 99864

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.