Python Learning notes (2)

Source: Internet
Author: User

Variable

The variable name is like the name of our real society, when assigning a value to a name, it is stored in the store, called the variable (Variable), and in most languages it is referred to as "assigning a variable" or "storing the value in a variable".

Python, however, differs slightly from most other computer languages by not storing values in variables, but rather by pasting names onto values.

So some python programmers would say "Python" has no "variables", only "names".

The naming theory of variables can take any legal name, but as a good programmer, take the variable to the name of a professional point.

# Assign the string ' Python ' to study ' Python ' # Print Study Print (study) ' Python '

As you can see, the assignment is so simple. This is called a variable because "it" is mutable.

' Python '>>> study'Python'Java'> >> Study'Java'

See, for another assignment, study becomes "Java."

In fact, the variable is like the elementary school algebra, x = 1, y = 2 to find x + y is how much. Don't think X + y = 3, variables are so good to understand

Print (x + y) 3

Of course, you can assign a string to a variable, and then add the variable to stitch

' yy ' ' hh '>>> A + b'yyhh'

If the variable is not assigned, print the variable directly, and you will get an error.

>>> Nametraceback (most recent  ):"<pyshell#15>"  in <module>    'name' are not defined

Places to be aware of

Python variable names cannot start with numbers and special characters

' a ' syntaxerror:invalid Syntax ' a ' syntaxerror:invalid Syntax

Python can use BIF to name variables, but don't use them at the end, otherwise the assignment will overwrite Bif's own features

#Print hehe>>>Print('hehe') hehe#I think printing is too much trouble, let's assign the print value. >>>Print='hehe'#Print directly to print successfully>>>Print'hehe'#I do not want to hehe, I want to haha, results when using print printing, error. This is what happens when BIF to name variables.>>>Print('haha') Traceback (most recent): File"<pyshell#41>", Line 1,inch<module>Print('haha') TypeError:'Str'Object is  notCallable

String

So far, the string we have known is everything inside the quotation marks, we call the string text, the text and the numbers are very different

#Integral type addition>>> 2 + 24#string Addition>>>'2'+'2'' A'#integral type and string added, error>>> 2 +'2'Traceback (most recent): File"<pyshell#2>", Line 1,inch<module> 2 +'2'typeerror:unsupported operand type (s) for+:'int'  and 'Str'

If the variable is quoted, it is not a variable, but a string. So when manipulating variables and string concatenation, be aware of the

>>> x = 1>>> y = 2#  Direct print x + yprint (x + y)3#  If you want to print X + y = x + y, it is not possible to do so.  Print('x + y = x + y'+ y = x + y#  should be written correctly, by The value obtained from X + y is an integer, and the integer and string cannot be added, so the integer is converted to the string print(' + str (x +  + y = 3

So to tell Python to create a string, you need to enclose the string in quotation marks, either single or double quotes, Python is not picky. But must be in pairs, cannot be single quotes, the other half does double quotes end. Python it will "rage" to Tell You "SyntaxError".

' Python "  while scanning string literal

What if I need a single or double quotation mark in a string?

---For example I want to print thestring: Let's Go!

There are two ways, the first method is more commonly used, is to use our escape symbol (\) to escape the quotation marks in the string:

Print ('let\ ' s go! ' )let's go!

Another way is to use single quotes in double quotes

Print ("lets go! " )let's go!

Multi-line string

A multiline string can be represented using a pair of three quotation marks.

Print ('aaaaabbbbbcccccddddd') aaaaabbbbbcccccddddd

In Python, it is not possible to use Chinese quotation marks to denote a string, or it will be an error.

>>> in identifier

Learning Summary

This time I learned about variables and strings.

It is important to note that:

Do not name the variable bif, otherwise it will override the BIF feature

String cannot be expressed in Chinese quotation marks, or it will be an error

Python Learning notes (2)

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.