Python First Chapter Summary

Source: Internet
Author: User
Tags locale python script

The first chapter summarizes the differences between compiled and interpreted languages, and lists which languages you know are compiled and which are interpreted

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.

    • Languages such as C C + + Delphi are compiled languages

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. ----Cross-platform.

    • Languages such as Python Java PHP Ruby are interpreted languages

High-level language programming program can not be known by the computer, you have to say that the conversion can be executed, according to the conversion mode, you can divide it into two categories, one is the compilation class, one is the interpretation of the class

Compile class: The program source code is "translated" Into the target code (machine language) before the application code program executes, so its target program can be executed independently from its locale. The use is convenient, the efficiency is also high, but once the application needs to modify, must first modify the source code, and then recompile builds the new target (*.obj, namely obj file) to execute, only the target file but does not have the source code, the modification is very inconvenient.

Features: After compiling the program does not need to re-translate, directly using the results of the compilation is OK. High efficiency of program execution, relying on compiler, poor cross-platform, such as C,c++,delphi, etc.

Advantages: 1, when executing the program, do not need source code, do not rely on the locale, because the machine source files are executed.

2, the execution speed is fast, because the program code has been translated into machine language that the computer can understand.

Cons: 1, each time you modify the source code, you need to recompile, generate machine-encoded files

2, cross-platform is not good, different operating systems, call the bottom of the machine instructions are different, you need to generate different machine code files for different platforms.

Interpretation class: The execution is similar to the "simultaneous translation" in our life, the application source code is "translated" by the interpreter of the corresponding language into the target code (machine language), while executing, while translating, so the efficiency is lower.

Features: Low efficiency, unable to generate a standalone executable file, the application can not be separated from its interpreter, but this method is more flexible, dynamic adjustment, modify the application. such as Python,java,php,ruby and other languages.

Advantages: 1, the user invokes the interpreter, executes the source file, and can be modified at any time, immediate effect, change the source code, directly run to see the results

2, the interpreter to the source file as a machine instruction, one side to the CPU execution, natural cross-platform, because the interpreter has done a different platform for the interactive processing, user-written source code does not need to consider platform differences.

Cons: 1, code is clear text

2, the operating efficiency is low, all the code is required to interpret the side of the interpreter side execution, the speed is much slower than the compiler type.

What are the two ways to execute a python script
    1. Interactive execution, running temporary input code on the console
    2. File operation, execute a saved py file

      The difference between the two is: one is memory operation, one is the hard disk operation,
      Memory Features: Fast read speed, but loss of data when power is lost
      Hard disk features: slow, but can save data

What are the considerations for declaring variables?

Variable definition rules:

1, variable names can only be any combination of letters, numbers, or underscores
2, the first character of a variable name cannot be a number
3, keyword cannot life ask variable name

Precautions:
1, variable name cannot be too long
2, variable nouns do not reach meaning
3, the variable name is Chinese, pinyin

How do I see the address of a variable in memory?
print(id.__doc__)Return the identity of an object.This is guaranteed to be unique among simultaneously existing objects.(CPython uses the object‘s memory address.)
Write code to implement user input user name, when the user name is James, and the password is 123456, the login is successful, otherwise the login failed.
1Username ="Seven"2Password ="123"3 4_username = input ("Please enter user name:")5_password = input ("Please enter your password:")6 7 if_username = = Username and_password = =Password:8     Print("Congratulations on landing success .....")9 Else:Ten     Print("Login failed, user name or password error ....")
View Code

Implement user input user name, when the user name is James, and the password is 123456, the login is successful, otherwise the login failed, the number of failures allowed to repeat three times
1Username ="Seven"2Password ="123"3Count =04  whileCount < 3:5_username = input ("User name:")6_password = input ("Password:")7     if_username = = Username and_password = =Password:8         Print("Welcome", username,"log in .....")9          BreakTen     Else: One         Print("User name or password error ....") ACount + = 1 -         Print("you also have", 3-count,"Second chance to re-login") - Else: the     Print("Login Failed")
View Code

Implement user input user name and password, when the user fame Sevev or Alex and the password is 123, the display is successful, otherwise the login failed, the failure to allow repeated input three times.
1Username ="Seven"2UserName1 ="Alex"3Password ='123'4Count =05  whileCount < 3:6_username = input ("Username:")7_password = input ("Password:")8     if_username = = Usernameor_username = =username1:9         if_password = =Password:Ten             Print("Welcome", _username,"login ... ..") One              Break A         Else: -             Print("wrong Password....please try again.") -Count + = 1 the             Print(" you have", 3-count,"Times to try.") -             Continue -     Else: -         Print("wrong username...please try again") +Count + = 1 -         Print(" you have", 3-count,"Times to try.") +         Continue A Else: at     Print("You tried too many times ...")
View Code

Using the while loop to implement output 2-3+4-5+6......+100 and
  1  count = 2 2  count1 = 0   3  while  count < 101< Span style= "COLOR: #000000" >:   4  if  Count% 2 == 0:   5  count1 += count   6  else  :   7  count1-= count   8  count + = 1 9  else  :  10  print  (count1) 
View Code

Using a while loop to implement output 1,2,3,4,5,7,8,9,11,12
1 count = 12 while Count <:3     ifor count = = 10 : 4         Pass 5     Else : 6         Print (count) 7     Count + = 1
View Code

Use the while loop to output 100-50, from large to small, such as 100,99,98 ...., and then output from 0 to 50, then end
1Count = 1012  whileTrue:3     ifCount > 50:4Count-= 15         Print(count)6     Else:7         Print(50-count)8Count-= 19         ifCount <0:Ten              Break
View Code

Use the while loop to implement all the odd numbers in output 1-100
1Count = 12 #While count <:3 #print (count)4 #count + = 25 6  whileTrue:7     ifCount% 2 = = 1:8         Print(count)9Count + = 1Ten     ifCount = = 100: One          Break
View Code

Use the while loop to implement all the even numbers in output 1-100
1 #count = 22 #While count <=:3 #print (count)4 #count + = 25 6 7Count = 18  whileCount <= 100:9     ifCount% 2 = =0:Ten         Print(count) OneCount + = 1
View Code

Python First Chapter Summary

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.