Python Variable Type

Source: Internet
Author: User
Tags alphanumeric characters

PythonVariable Type

The value that the variable is stored in memory. This means that there is a space in memory when creating variables.

Based on the data type of the variable, the interpreter allocates the specified memory and determines what data can be stored in memory.

Therefore, variables can specify different data types, which can store integers, decimals, or characters.

Assigning values to variables

Variables in Python do not need to be declared, and the assignment of variables is the process of declaring and defining variables.

Each variable is created in memory and includes information about the identity, name, and data of the variable.

Each variable must be assigned before it is used, and the variable will not be created until the variable is assigned.

The equals sign (=) is used to assign a value to a variable.

The left side of the equals sign (=) operator is a variable name, and the right side of the equals sign (=) operator is the value stored in the variable. For example:

#!/usr/bin/pythoncounter = 100miles = 1000.0 # a floating pointname = "John" # a stringprint counterprint milesprint name
[email protected] gechong]# python Test.py1001000.0john

  

Assigning values to multiple variables

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

A = b = c = 1

The above example creates an integer object with a value of 1 and three variables allocated to the same memory space.

You can also specify multiple variables for multiple objects. For example:

A, b, C = 1, 2, "John"

For the above example, two integer objects 1 and 2 are assigned to variables A and B, and the string object "John" is assigned to variable C.

Standard data types

There are several types of data that can be stored in memory.

For example, the person age is stored as a numeric value and his or her address is alphanumeric characters.

Python has some standard types for defining operations on them and for each of them as a storage method possible.

Python has five standard data types:

    • Numbers (digital)
    • String (String)
    • List (lists)
    • Tuple (tuple)
    • Dictionary (dictionary)

Python numbers

Numeric data types are used to store numeric values.

Python supports four different numeric types:

    • int (signed integral type)
    • Long (longer integer [can also represent octal and hexadecimal])
    • Float (float type)
    • Complex (plural)

They are immutable data types, which means that changing the numeric data type assigns a new object.

When you specify a value, the number object is created:

VAR1 = 1
VAR2 = 10

You can also use the DEL statement to delete some object references.

The syntax for the DEL statement is:

Del Var1[,var2[,var3[....,varn] []]

You can delete single or multiple objects by using the DEL statement. For example:

del var
Del var_a, Var_bpython string

A string or series (string) is a string of characters consisting of numbers, letters, and underscores.

Generally recorded as:

s= "A1A2 An

It is the data type that represents the text in the programming language.

The Python string list has 2 order of values:

    • Left-to-right index starts at default 0, with a maximum range of 1 less string lengths
    • Right-to-left index starts with default-1, the maximum range is the beginning of the string

If you want to get a substring, you can use the variable [head subscript: Tail subscript], you can intercept the corresponding string, where the subscript is starting from 0, can be positive or negative, subscript can be null to take the head or tail.

Like what:

s = ' Ilovepython '

S[1:5] The result is love.

When using a colon-delimited string, Python returns a new object that contains the contiguous content identified with the offset, and the beginning of the left contains the bottom bounds.

The result above contains the value L of s[1], and the maximum range taken does not include the upper boundary, or the value p of s[5].

The plus sign (+) is a string join operator, and an asterisk (*) is a repeating operation. The following example:

#!/usr/bin/python

str = ' Hello world! '

Print str # output full string
Print Str[0] # The first character in the output string
Print Str[2:5] # The string between the third and fifth in the output string
Print str[2:] # Outputs a string starting from the third character
Print str * 2 # output String two times
Print str + "TEST" # Output concatenated string

The result of the above example output:

Hello world!
H
Llo
Llo world!
Hello world! Hello world!
Hello world! TEST

Python Variable Type

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.