Python 1 basic grammar two (identifiers, keywords, variables and strings)

A. Identifier

Programmers variable name is an identifier naming their own. In view of the need to know the name of the name of the effect of righteousness, not arbitrarily named; for example, a = 1 a is a variable, a variable name belongs identifiers

= Company. 1 ' millet
 2 employeeNum = 9999

There Identifier: variable names, function names, class names

Identifier has its own requirements:

  • The first character must be an alphabet letter or an underscore _.
  • Other partial identifiers from the letters, numbers and underscores.
  • Identifiers are case sensitive.

In Python 3, you can use Chinese as a variable name, non-ASCII identifiers are also allowed up.

II. Keyword (Python reserved word reserved word that is key)

Key identifier that is used inside the python already
has a special meaning and function key
developer identifier to allow definition of the same name keywords.

Python's standard library module provides a keyword, you can output the current version of all keywords:

code show as below:

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', ''elif, 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'

import keyword may be introduced a "tool kit"
different in python toolkit provides different tools

III. Variables

1. (popular) Definition: variable name as the name of our social reality, when a value is assigned to the name, Ta will be stored in memory, called variable (Variable), in most languages, all these kind of behavior is called "variable assignment" or "the value stored in the variable."

1 variable name = value

2. Features: Python practice but most other computer languages ​​is slightly different, Ta is not the value stored in a variable, but rather the name attached to the upper value. So some Python programmers would say, "Python" no "variable" only "name."

Sample code (with IDLE, which is an interactive command-line page ):

>>> colour = 'black'
>>> print (colour)
black
>>> colour = "blue"
>>> print (colour)
blue
>>> num1 = 2
>>> num2 = 7
>>> num3 = num1 + num2
>>> print (num3)
9

3. Note:

(1) Before using a variable, you need to assign it to.
(2) variable names can include letters, numbers, underscores, but the variable name can not start with a number .
(3) letters can be uppercase or lowercase, but the case is different. That is first and First Python is totally different for two.
  (4) equal sign (=) is the assignment of meaning, the left is the name , the right value is not writable counter-strategy.
  Naming Theory (5) variable can take any legal name, but want to become a good programmer, try to take a little bit of a professional name to the variable.

IV. String (String)
1. (popular) Definition: So far, we perceived the string is everything in quotation marks , we also called the string of text , text and numbers are very different. Examples are as follows:
Sample code:
1 >>> 1+6
2 7
3 >>> '1' + "6"
4 '16'

3,4 splicing two strings above behavior ( To illustrate the need, typically numbers before the python code shows only the relevant site without the spaces forward number, code is running, no number of the lines, and to be deleted )

2. Note:
(1) To tell Python you create a string, we must put quotation marks around character, can be single or double quotes, Python is not picky. But it must be in pairs, you can not quote a single side, the other side has to spend the end of the double quotes.
(2) how to do should the need arise single or double quotes string For example, I want to print a string:? Let's go!
There are two ways.
The first commonly used, is the use of our escape character (\) in a string escape quotes:
When the second is a single quotation mark, an outer double quotes.
>>> 'Let's go!'
SyntaxError: invalid syntax
>>> 'Let\'s go!'
"Let's go!"
>>> "Let's go!"
"Let's go!"

3. original string:

Backslash found good use.
Sample code:
>>> str='C:\now '
>>> str
'C:\now '
>>> print(str)
C:
ow 

We can itself be escaped with a backslash:

Sample code:
>>> str='C:\\now '
>>> str
'C:\\now '
>>> print(str)
C:\now 

But if there are many, such as a string for a backslash:

>>>str =“C:\Program Files\Intel\WiFi\Help

Need to use the original string, the original string is very simple, just to r plus a letter string in front:
Sample Code:
>>> str =r'C:\Program Files\need\now\name'
>>> str
'C:\\Program Files\\need\\now\\name'
>>> print(str)
C:\Program Files\need\now\name

4. Long strings

If you want to get a string span multiple lines, for example:
 
Once in front of me
but disappear
This is the sixth over today's
movie soundtracks
like your eyes
I love you quickly come back to me
 
That we need to use triple-quoted string! '' '    ' '' And "" "    " "" can
Str = >>> "" " I was in front of me
But disappear
This is the sixth over today
Movie soundtrack
Like your eyes
I love you quickly come back to me "" " 
>>> str
 ' once in front of me \ n but disappear \ n This is today the sixth over \ n movie soundtrack \ n like your eyes \ n I love you quickly come back to me ' 
>>> Print (str)
Once in front of me
But disappear
This is the sixth over today
Movie soundtrack
Like your eyes
I love you, come back to me soon
Str = >>> '' ' I was in front of me
But disappear
This is the sixth over today
Movie soundtrack
Like your eyes
I love you quickly come back to me '' ' 
>>> Print (str)
Once in front of me
But disappear
This is the sixth over today
Movie soundtrack
Like your eyes
I love you, come back to me soon

 5. literally cascade string, such as "this" "is" "string" will be automatically converted to this is string.

6. The + operator strings can be connected together, with the * operator repeats.

The two indexing 7.Python string from left to right starting with 0, -1 from right to left to begin.

8.Python string can not be changed.

9.Python no separate character type is a character string of length 1.

10. The string taken syntax is as follows: the variable [the header: Tail index: step]

= Word ' String ' 
sentence = " This is a sentence. " 
paragraph = "" " This is a paragraph,
May consist of multiple lines . "" "

Example:

str='Runoob'
 
Print (STR)                  # output string 
Print (STR [0: -1])            # output to the first characters in the second reciprocal 
Print (STR [0])               # output of the first character 
Print (STR [ 2: 5])             # output from the start of the third to fifth character 
print (STR [2:])              # output all the characters from the start of the third 
print (STR * 2)              # output string twice 
print (STR + ' hello ' )         # connection string
 
print('------------------------------')
 
Print ( ' Hello \ nrunoob ' )       # backslash (\) + n escape special characters 
Print (r ' Hello \ nrunoob ' )      # Add a r before the string, represents the original string, escaping does not occur

A difficulty, r here refers to raw, i.e. raw string.

Difficulties two, print (str [2: 5 ]) instead of letters with numbers 0. 1 Runoob 2. 4. 3 Red numerals 5 [2: 5] from the third to the fifth character start end. (Fine chemicals)

The output is:

======================= RESTART: E:/python/change.py =======================
Runoob
Runoo
R
us
noob
RunoobRunoob
Hello Runoob
------------------------------
hello
runoob
hello\nrunoob

 

V. Note that when programming in English and Chinese characters, English characters only.

Guess you like

Origin www.cnblogs.com/9587cgq/p/12609069.html