Python variables (top)

Source: Internet
Author: User

Python uses variables to refer to the values in memory, and how much space a variable's value occupies is determined by the type of the variable. Declaring a variable does not need to specify the type of the variable, and the interpreter automatically judges it based on the value. Variable names must conform to the definition of identifiers

Identifier

Identifiers are composed of letters, numbers, and underscores.
Identifiers cannot begin with a number.
Identifiers are case-sensitive.
Identifiers that begin with an underscore have special meanings, such as a _foo class attribute that represents a property that cannot be accessed directly. A double underline __foo represents a private member that cannot be accessed directly. For example, a __foo__ special method that represents a class in which a double underline begins and ends.
Identifier cannot be a reserved word

Reserved words

Reserved words have a special meaning in a python program. cannot be used for variable names. These reserved words are:

1And,ExecNotAssert,finallyor,Breakfor,pass,class,from,print,continue,global,raise 2 def,if,return,del,import, try,elif,in,while,else,is,with,except,lambda,yield     
Variable declaration

The following example declares different types of variables that can be arbitrarily assigned to print their

1 x = 3#Numeric type number2 F = 3.1415926#Float type float ting point number3 name =‘Kady ' # string type String4 a = 1,2,3,4,5, #5 b = [ ' hello  ",  ' world",  ' ! " Span style= "COLOR: #008000" ># list List6 d = dict (Name= ' kady ", age=18) # dictionary dictionary         

Python has 5 standard data types.

    1. Numeric type number
    2. string-type String
    3. List Lists
    4. Tuple tuples
    5. Dictionary Dictionary

When you declare a variable, you do not need to specify the variable type, you can assign any type of value directly to the variable.

Numeric type number

Python supports many data types

    1. Integer number
    2. Floating point Float
    3. Plural Complex
# integer print(a)3 1   
 1 b = 3.14 # floating-point 2 print (b) 3 3.14        
# Complex print(c)3 (2+3j)# Complex 6 (2+3j)#A+BJ and complex (A, b) equivalent  
String

A string type is an immutable sequence of characters that is represented by a Str object.
There are several different definition forms for string literals

    • Single quote: ' foo '
    • Double quotes: "foo"
    • Three quotes: ' f00 ' or ' foo ' ""
1 >>> A = 2  ... aaa 3  ... bbb 4  5 >>> A 6  '  7 >>> print (a)  8  9  ... aaa10  ... bbb11 ...  

The

string is equivalent to an expression, and you can write multiple strings consecutively using spaces, which is equivalent to defining one. For example (' foo ' bar ') equals (' foo bar ').
If the string contains double quotes, single quotation marks, you need to use \ Escape.

 1 >>> a =  '  Hello \ "world\"  ' 2 >>> print (a) 3 Hello  "world" 4 >>> a =  ' hello \ " World\ '  '  Print (a) 6 Hello  "world "       

You can escape single quotes, double quotes. The above output does not have a \. If you want to output \, you need to write two consecutive \. For example:

'\ \'print(a)# output "\"     

or add the prefix R, cancel escape.

1 >>> a = r'hello \ ' world\'2print( A)3 hello \'world\ '

Strings can use +, concatenate multiple strings, and return a new string.

1 ' Hello ' 2 '  World ' 3 >>> A + b4'helloworld'

Python variables (top)

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.