Learn the first chapter of Python

Source: Internet
Author: User

Python Introduction:

Python advocates beauty, clarity and simplicity, and is an excellent and widely used language.

What kind of language is Python:

Programming languages are mainly categorized from the following angles: compiled and interpreted, static and dynamic, strongly typed, and weakly typed.
We mainly look at the compiled and interpreted languages here.

What is the difference between compilation and interpretation?
The compiler compiles each statement of the source program into a machine language and saves it as a binary file so that the computer can run the program directly in the machine language, quickly
The degree is very fast;
And the interpreter is only in the execution of the program, only one interpretation of the machine language to the computer to execute, so the speed is not as fast as the compiled program to run.
This is because the computer does not directly recognize and execute the statements we write, it only knows the machine language (in binary form)

Compiled vs explanatory type
Compiled type
Pros: Compilers typically have pre-compiled procedures to optimize code. Because the compilation is done only once, the runtime does not need to compile, so the program execution efficiency of the compiled language
High. Can run independently from the language environment.
Cons: After compilation, the entire module needs to be recompiled if modifications are required. Compile the machine code according to the corresponding operating environment, different operating systems are migrated between
There is a problem and you need to compile different executables according to the operating system environment you are running.
Explanatory type
Pros: Good platform compatibility, can be run in any environment, provided the interpreter (virtual machine) is installed. Flexible, modify the code directly when you can
So that it can be deployed quickly without downtime maintenance.
Cons: Every time you run, you have to explain it again, performance is not as good as the compiled language.

Python runs on the interpreter,

When we write the Python code, we get a text file that contains the Python code with a. py extension. To run the code, you need to
Python interpreter to execute the. py file.
Since the entire Python language is open source from spec to interpreter, in theory, anyone can write a Python interpreter if the level is high enough to
Line Python code (of course, it is very difficult). In fact, there are a number of Python interpreters. Includes the following.

CPython IPython pypy Jython IronPython. We mainly use Cpython.

Python classification

Python is now mainly divided into two categories due to historical reasons, the class is 2. The X class is 3. X

2.X thread is needed to be eliminated. Recommended familiarity with 3. X.

Python installation

The Windows Python 2.7 and Python 3.x installation references are as follows:

Http://edu.51cto.com/course/10049.html

The Linux Python 2.x and Python 3.x installation references are as follows:

Https://www.cnblogs.com/kimyeee/p/7250560.html

The first Python program

Forever helloworld!.

CMD in interactive

Print ("Hello world!")

Variable

Variable definition: The intermediate result of an operation is staged into memory for subsequent program invocation.

And here are the following recommendations:

1. Variables can only be combined by alphanumeric and underscore.

2. Cannot be a pure number, or start with a number

3. Not too long

4. Cannot be a python keyword.

5. Cannot be Chinese

6. Try to make sense.

7. It is recommended to use the camel body or underline.

Constant

There is no absolute constant in Python, which is usually capitalized as a constant.

such as Pi = 3.14159265358

However, you can still assign values such as the following

PI = 4

Comments

There are two types of Python annotations

Single-line Comment

#

Multi-line comments

‘‘‘ ‘‘‘

"" "This is also a multi-line comment" ""

Basic data type (initial several)

The way to view the variable file type is type ()

int type, integral type.

The int range is different depending on the user's machine.

The 32-bit machine range is as follows:

-2**31 ~2**31-1

The 64-bit machine range is as follows:

-2**63~2**63-1

converting other types to int is done by this method

Int ()

STR type String type.

All the quotes in Python are strings,

A reference to a string can be "," "," "," "" "".

These few references are not different, just different cases with different references.

The concatenation of strings is with the + number stitching.

Example:

A = "abc"
b = "CBA"
c = A + b
Print (c)

The string can also be multiplied.
Examples are as follows:
Print (C*3)
The results are as follows:
Abccbaabccbaabccba

Boolean type (BOOL)
The Boolean types are divided into ture and false to note the spelling of these two words.

User interaction
User interaction using the input () function

Input ("Enter a hint here")

Examples of use are:

D = input ("Please enter content")
Print (d)

Note that the type of intput obtained here is the STR type.


Process Control
If type
If there are four ways to use

   code block

Example:
Age = Input ("Please enter your Ages:")
if int (age) > 18:
Print ("You are a grown man!")
Print (age)




2. If condition:
code block
Else
code block

Example:
Age = Input ("Please enter your Ages:")
if int (age) > 18:
Print ("You are a grown man!")
Else
Print ("You're still a kid!")
Print (age)


3. If Condition:
code block
Elif Conditions:
code block
Elif Conditions:
code block
Else
code block
Example:
Age = Input ("Please enter your Ages:")
if int (age) > 50:
Print ("You're an old man!")
elif int (age) > 30:
Print ("You are a greasy middle-aged man.")
elif int (age) > 20:
Print ("You are Grown")
Else
Print ("You're still a kid!")
Print (age)


4. If condition:
code block
If condition:
code block
Else
code block
Else
code block
can be nested in infinite nesting. But in the actual development. Try not to exceed three levels of nesting

Example:
Age = Input ("Please enter your ages")
Sex = input ("Please enter your gender")
if int (age) > 18:
Print (age)
if sex = = "Male":
Print ("You are an adult man")
Else
Print ("You are an adult woman")
Else
Print ("You are a child ....")

While loop
The format is as follows:
While condition:
code block
If the conditions are true, then the cycle will continue.

Example code:
Count = 0
sum = 0
While Count < 101:
sum = sum + count
Count = Count + 1

Print ("Sum within 100 is", sum)


Break and Continue
Break means jumping out of a loop.
Continue refers to jumping out of the current loop.

Break Example:
Count = 0
sum = 0
While Count < 11:
Print (count)
if Count = = 8:
Break
Count = Count + 1
Print ("==============")

Continue example
Count = 0
While Count < 10:
Count = Count + 1
if Count = = 7:
Continue
Print (count)




Learn the first chapter of Python

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.