The basic syntax of the python 22

"Life is short, I used Python". Python programming language is easiest to learn, and powerful language. Just be micro-channel chat, know a little English word can learn the Python programming language. But many people claim to be proficient in Python, then himself write Pythonic code for many common package is not very understanding. Oaks from little acorns, let's start with the most basic Python start.

 

01 Python's advantages

 

· Python is a language similar to human speech

· Extremely easy to learn language

· It is one of the open-source language

· Good portability, can run in the major platforms, such as can run on Windows, Mac, Linux

· Rich third-party libraries: only you can not think, do not have Python

· Interpretive language: can be run directly from the source code.

· Supports both object-oriented programming, and support process-oriented programming.

Scalability is good, part of the program can use other programming languages, such as C / C ++

 

 02 Python development environment

 

At present, the mainstream has three Python development environment (development environment also known as IDE, Integrated Development Environment), are PyCharm, Anaconda and Jupyter Notebook, we call Python Three Musketeers. To illustrate the main difference, I quote Python Circle of Friends real Lao Yuan-hui's words: "The Three Musketeers in my hand, you can feel the carcass into the world reports and models .Anaconda, the most important is the overall development environments and third-party libraries easy installation; Pycharm, the most important scripts and execute the entire report various data sources as well as regular access to the script execution; Jupyter notebook, the most important is the training model, real-time execution of each step can visually see the results. ". This passage accurately summed up the difference between the three. If you want to know more Python combat ring, please sweep the two-dimensional code code the Preamble, or concerns micro-channel public number data_circle.

 

03 variables

 

Constant, by definition is the amount of value can not be changed, such as a 5, 10 or other digital text string. It literally means the meaning of itself, does not need much explanation.

And the constant corresponding variable is, as the name suggests is the amount of value has been changed. Because the value of the change, we need to give it a name, which is the identifier. Python programming, we call identifier for the variable name and the equal sign (=) to associate variable names and values,

Defined variables, and using the print function to print out
 # my_name is a variable name, Andy is value. 
# Variable name unchanged, the value can be changed, for example, replaced by Jay 
my_name = " Andy " 
Print (my_name) 
my_name = " Jay " 
Print (my_name) 

results: 

Andy Lau 

Jay

04 integer

  Int is the integer type in Python, you can be integer arithmetic directly

# Adding 

the Add =. 4. 3 + # the Python in, format is formatted output, is replaced by the value of the variable in {} place. Often used in the rear actual item Print ( ' value is 3 + 4} { ' .format (the Add)) 
run results: 3 + 4 value is 7








05 Float

  With decimal point are floating point arithmetic may be performed integer types, such as addition, subtraction and the like.

# Adding 

the Add = 0.2 + 0.1 # the Python in, format is formatted output, is replaced by the value of the variable in {} place. Later project combat often used Print ( ' value is 0.2 + 0.1} { ' .format (the Add)) of 0.2 + 0.1 is .30000000000000004





06 strings

  String is a series of characters. In Python, single and double quotation marks, or three marks which content is the string. How to include a single string or double quotes, python backslash () for escape characters in the string.

# Single quotes is the string of text 

'the I AM A Boy' 



# double quotes and single quotes in fact, as is generally recommended to use single quotation marks 

" are welcome to join combat python circle " 



# string triple quotes represented generally indicates a long text, quotes did not end as long as you can always write. 

# Generally used to write text notes 

' '' 

learning step by step, not halfway, avoid three days fishing, two days of drying. 
'' '

07 Boolean

  Python supports Boolean data type Boolean True and False only two kinds of value, but a Boolean operation are the following:

True and True  # ==> True

True or True  # ==> True

not True   #==> False

08 List

  List is a series arrangement of elements in a certain order. That is, the list is an ordered collection. In Python, square brackets ([]) to indicate the list by commas to separate its elements. Since the list can give a name, and use (=) to associate a list of names and lists, which is called list assignment.

# Define a list 

# the Python list 

names_python_pc = [ " soybean ' , ' Andy ' , ' Jacky ' , ' beauty ' ] 

Print (F ' the Python list are: {names_python_pc} ' ) 



the Python list are: [ ' soybean ' , ' Andy ' , ' Jacky ' , ' beauty ' ]

09 yuan group

  Another variable is the dictionary data structure, and can store any type of objects, such as strings, numbers, and other lists. And the values ​​of the key dictionary is composed of two parts, i.e. the key and value, separated by a colon. This structure is similar Dictionary, every word in the dictionary has a corresponding explanation.

= l, 2,3 TUP1
 Print (TUP1) 

the result is: 
( 1, 2, 3)

10 dictionaries

  Another variable is the dictionary data structure, and can store any type of objects, such as strings, numbers, and other lists. And the values ​​of the key dictionary is composed of two parts, i.e. the key and value, separated by a colon. This structure is similar Dictionary, every word in the dictionary has a corresponding explanation.

# Build a dictionary, record income of family members 

name_dictionary = { ' Dad ' : 300, ' wife ' : 1000, ' Mom ' : 800, ' own ' : 600, ' child ' : 200 } 

Print (name_dictionary) 

results is: 
{ ' father ' : 300, ' wife ' : 1000, ' mother ' : 800, ' own ' : 600, ' children ' : 200}

11 comments

  In Python, single-line comment with a pound sign (#) identification, i.e. behind the content #; multi-line comment with the pair of three quotation marks ( '' '' '') or a pair of three pairs of quotation marks ( "" "" " ") represents. Note illustrates generally used in single-line description of a row, and multi-line comments are typically used in the entire text or a certain code region of explanation, three double quotes explanatory entire document. For these comments, Python interpreter will not perform specific content. The following examples, Python interpreter ignores comments, only the output Hello World.

print('Hello World')
"""print('结束')"""

12 Data

  Data simply is nothing in the computer, such as listening to music, watching movies, reading articles and so on. Python programming is to process the data using its own syntax and processing rules processing, and then presents the results of data you want

Indent 13

  In Python language, Python indentation is determined according to the relationship between the line and the previous line of code. If the same code indentation, Python consider them as a block of statements; otherwise, it is two block. Indent tab key codes are generally used, and some IDE automatically indent code, such Pycharm.

this is one block

this is a new line in the one block

    this issecond block

    this isa new line in the second block

    xxxxxx

this is the three block

this is a new line in the three block

14 keyword

 

  In Python, some strings having certain features, such as import, class and the like. When we choose variable names, you should pay attention to avoid these reserved characters.

 

Operator 15

 

Operator action is to generate new variables based on existing variables are the following:

 

· Arithmetic operators: +, -, *, /,%, i.e. add, subtract, multiply, divide, modulo

· Comparison operators: ==, =,>, <,> =, <=, i.e. equal, not equal, greater than, less than, greater than or equal, or less!

The assignment operator: =, + =, - =, * =, / =,% =, i.e. assignment, plus assignment, assignment subtraction, multiplication assignment, in addition to the assignment, the assignment modulo

Logical operators: and, or, not, i.e., or non-

16 Conditions

 

Also known as structure selected branch structure, meaning that the program code based on the determination condition choose to perform specific code. If the condition is true, the program execution portion of code; otherwise, performs another portion of the code. Determination condition can be understood as the program is divided into two parts, can only be performed according to the conditions in which a portion of the result. For example, the college entrance examination for the condition, was admitted to go to college; otherwise go to college, do other, All roads lead to Rome, just a little effort can be successful.

 

17 cycle

 

Constantly repeats the cycle. Loop structure is performed under conditions of a portion of the code repeatedly, Python is a structured program highest usage data. In the Python language, a common structure for recycling loop and while loops.

 

18 for circulation

 

for the number of cycles of cycles is generally known in advance, it will be a flag variable iteration from a start value to an end value after the end.

# The X-start from zero, until the end of the 10 
for the X- in the Range (0,11 ):
     Print (the X-)

19 cycle control

 

It includes three main control loop: pass, continue, break.

 

pass representation do nothing but occupy the position of a line of code; continue immediately said the current round exit loop, continue subsequent cycles of; break immediately said the launch cycle, subsequent cycles are no longer executed.

 

20 abnormal

 

There may be some predictable problems Python code, such as key dictionary access does not exist. If not addressed, the problem occurs when Python will exit with an error, you may have to start over and ran a long time ago. Therefore, we need to anomalies that may arise capture and processing. 

 

 

21 function

 

Function is an independent and complete closure of the particular function block, it can be called at any place. For example, the next day the contents inside the print () function, no matter where you are in your program calls are output content () in. This block of code in a separate enclosure, also known as encapsulation, may be understood as a function of the box. The code inside the box is packaged, to accomplish a specific function, the function does not belong to the outer code.

 

22 Object Oriented

 

The first concept is the object-oriented class. In the Python programming language, but also a number of integrated classes similar things. Such as a person, who has two arms, two legs wherein like; and the person walking, talking and other acts. In Python language, these features called attributes describe things, and things that represents behavior method called (i.e. function, all acts which are object-oriented method, no function), the combination of both together is Python language class. Class class is used to describe things that have the same set of attributes and methods. I.e. classes with the same properties and methods.

Guess you like

Origin www.cnblogs.com/maodoudou/p/11365775.html