Python Learning Day1

Source: Internet
Author: User
Tags ruby on rails

1, Python language division:

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

Compiled and interpreted type

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 machine language at a very fast speed.

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 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 of the compiled language is highly efficient. Can run independently from the language environment.
Cons: After compilation, the entire module needs to be recompiled if modifications are required. When compiling the machine code according to the corresponding running environment, porting between different operating systems will be problematic, 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 when the direct modification can be quickly deployed, without downtime maintenance.

Cons: Every time you run, you have to explain it again, performance is not as good as the compiled language.

2. Python language Advantages and disadvantages

Advantages
Simple ———— Python is a language that represents the idea of simplicity. Reading a good Python program feels like reading English, even though the requirements of this English are very strict! This pseudo-code nature of Python is one of its greatest advantages. It allows you to focus on solving problems rather than figuring out the language itself.

Easy to learn ———— as you will see, Python is extremely easy to get started with. As mentioned earlier, Python has a very simple syntax.

Free, open source ———— python is one of floss (free/open source software). Simply put, you are free to publish a copy of the software, read its source code, make changes to it, and use it as part of the new free software. Floss is the concept of sharing knowledge based on a group. That's one of the reasons why Python is so good-it's created and constantly improved by a group of people who want to see a better python.

High-level language ———— when you write programs in the Python language, you don't have to consider the underlying details such as how to manage the memory that your program uses.

Portability ———— because of its open source nature, Python has been ported on many platforms (modified to make it work on different platforms). If you are careful to avoid using system-dependent features, all your Python programs can run without modification on any of the platforms listed below. These platforms include Linux, Windows, FreeBSD, Macintosh, Solaris, OS/2, Amiga, AROS, as/400, BeOS, os/390, z/OS, Palm OS, QNX, VMS, Psion, Acom RISC OS, VxWorks, PlayStation, sharp Zaurus, Windows CE and even PocketPC, Symbian, and Google Linux-based Android platforms!

Explanatory ———— This requires some explanation. A program written in a compiled language, such as C or C + +, can be converted from a source file (that is, C or C + +) to a language used by your computer (binary code, 0 and 1). This process is done through the compiler and different tags and options. When you run your program, the connection/reprint software copies your program from the hard disk into memory and runs. Programs written in the Python language do not need to be compiled into binary code. You can run the program directly from the source code. Inside the computer, the Python interpreter translates the source code into an intermediate form called bytecode, and then translates it into the machine language used by the computer and runs it. In fact, because you no longer need to worry about how to compile the program, how to make sure that the connection is reprinted with the correct library, and so on, all this makes using Python easier. Since you only need to copy your Python program to another computer, it will work, which makes your Python program easier to migrate.

Object-oriented ———— Python supports both process-oriented programming and object-oriented programming. In a "process-oriented" language, a program is built from a procedure or simply a function of reusable code. In "Object-oriented" languages, programs are built from objects that combine data and functionality. Python implements object-oriented programming in a very powerful and simple way, compared to other major languages such as C + + and Java.

Scalability ———— If you need a piece of your critical code to run faster or you want some algorithms to be private, you can write some of your programs in C or C + + and then use them in your Python program.

The rich library ———— Python standard library is really huge. It can help you with all kinds of work, including regular expressions, document generation, unit tests, threads, databases, Web browsers, CGI, FTP, e-mail, XML, XML-RPC, HTML, WAV files, cryptography systems, GUI (graphical user interface), TK, and other system-related operations. Remember that all of these features are available as long as Python is installed. This is called Python's "fully functional" concept. In addition to the standard library, there are many other high-quality libraries, such as Wxpython, twisted, and Python image libraries, and so on.

Canonical code ———— python uses forced indentation to make the code extremely readable.

Disadvantages
Speed, speed requirements, use C + + to rewrite the key part of it. (unless the program is particularly strict with speed requirements, generally can be ignored)
The domestic market is small (the main development of Python in China, currently only some web2.0 companies). But time goes by, now many domestic software companies, especially game companies, are also beginning to scale using him.
Chinese information is scarce (good python is a handful of Chinese data). The blessing of the community, there are several excellent teaching materials have been translated, but the entry-level teaching materials, high-level content can only read English version.
There is too much choice in architecture (there is no official. NET architecture like C #, nor is the relative concentration of architecture developed as Ruby because of its short history. Ruby on Rails architecture to develop small and medium web programs invincible. But it also shows from another side that Python is excellent, attracts more people and has more projects.

3. Python Code

Python2 The default encoding is Ascii,python3 the default encoding is Utf-8

Python2 2  3 #!/usr/bin/python 4 #-*-Coding:utf-8-*-5   6 print "Hello, World"  #如何不强制指定字符集是会出现乱码 7  8  py thon310 #!/usr/bin/python11 print ("Hello, World")  #不指定默认也可以输出中文

Asiic:8 bit = = 1 bytes, representing one character

Universal code: Unicode contains the language of all countries

Unicode Upgrade:
Utf-8: A minimum of 8 bits to represent one byte

Conversion of units:
8bit 1bytes
1024bytes = = 1kb
1024KB = = 1MB
1024MB = = 1Gb
1024Gb = = 1Tb

4. Variables

The intermediate results of running the program are temporarily present in memory so that subsequent code calls

declaring variables

1 #!/usr/bin/env python2  3 name = "Tom"

The code above declares a variable named: Name, and the value of the variable name is: "Tom"

Rules for variable definitions:

Variable names can only be any combination of letters, numbers, or underscores
The first character of a variable name cannot be a number
The following keywords cannot be declared as variable names
[' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ', ' F ' Rom ', ' Global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' WI Th ', ' yield ']
The definition of a variable must be descriptive.

5. Constants

Constants are constant quantities, such as π:3.1415826 .... In Python, there is no specific syntax for constants, and programmers have used the variable names to represent constants in all uppercase: Age = 18

6. User interaction
1 #!/usr/bin/env python2   3 # Assigns the user input to the name variable 4 name = input ("Please enter user name:") 5 6 print   (name) # Prints the input content

Python2 has raw_input () and input () two ways, the first output type is str, the second output is only input () type in Int;python3, the output type is str

7. Formatted output
Method One: 2 name = input (' Please enter user name: ') 3 age = input (' Please enter old: ') 4 job = input (' Please enter work: ') 5 Hobbie = input (' Please enter hobby: ') 6  7 MSG1 = "------------info of%s-----------8 Name  :%s 9 Age   :%d10 job   :%S11 Hobbie:%S12------ -------End-----------------"% (Name,name,int (age), Job,hobbie) Print method Two: + dic = {' name ': ' Leige ', ' age ': ' ' ', ' ' job ': ' IT ', ' hobbie ': ' Wu '}18 dic = {' name ': Name, ' Age ': Age, ' job ': Job, ' Hobbie ': hobbie}19 msg2 = ' '- -----------Info of% (name) s-----------, name:% (name) S22 Age:% (age) S23 job:% (Job) S24 Hobbie:% (hobbie) S25-------- -----End-----------------"% dic27 print (msg2) print (' Your age%s, your name%s, learning Progress 5%% '% (age,name)) #如果输出结果中含有 % number, which needs to be escaped with a%         
8, if, while simple to use
1 #求1-100 The sum of the numbers and 2 #方法一: 3 i = 1 4 count = 0 5 while true:6     if I <=: 7         print(i) 8         i + = 1 9     else:         break11 #方法二: While I <=: + count + i15 i + = + print (count)  

Python Learning Day1

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.