Simple Python getting started

Source: Internet
Author: User

In a C ++ application, we use a set of plug-ins to implement functions with unified interfaces. We can use Python to replace the plug-ins in the form of dynamic link libraries, the following is a summary of the Python getting started tutorial.

Idea: use shell programming (Linux is usually bash and Windows is a batch processing script). For example, run the ping ip command on Windows to test each machine in sequence and obtain console output. The console text is usually "Reply from... "The text is" time out... ", so you can find the string in the result to know whether the machine is connected.

This code runs well. The problem is that you need to do some additional work to run this code. these additional tasks include writing a class file and writing a main method to compile it into byte code. Because Byte Code cannot be directly run, you need to write a little bat or bash script to run it.

Of course, C/C ++ can do the same job. however, C/C ++ is not a cross-platform language. in this simple enough example, the differences between C/C ++ and Java implementations may not be seen. However, in some more complex scenarios, such as recording connected information to a network database. because the network interfaces of Linux and Windows are implemented in different ways, you have to write two function versions. there is no such concern when using Java.

The Python getting started tutorial for the same job is as follows:

 
 
  1. Print """
  2. Usage: thingy [OPTIONS]
  3. -H Display this usage message
  4. -H hostname Hostname to connect
  5. """
  6. How is a string accessed? See this example:
  7. Word="Abcdefg" 
  8. A=Word[2]
  9. Print "a is:" +
  10. B=Word[1: 3]
  11. Print "B is:" + B # index 1 and 2 elements of word.
  12. C=Word[: 2]
  13. Print "c is:" + c # index 0 and 1 elements of word.
  14. D=Word[0:]
  15. Print "d is:" + d # All elements of word.
  16. E=Word[: 2] + word [2:]
  17. Print "e is:" + e # All elements of word.
  18. F=Word[-1]
  19. Print "f is:" + f # The last elements of word.
  20. G=Word[-4:-2]
  21. Print "g is:" + g # index 3 and 4 elements of word.
  22. H=Word[-2:]
  23. Print "h is:" + h # The last two elements.
  24. I=Word[:-2]
  25. Print "I is:" + I # Everything blocks t the last two characters
  26. L=Len(Word)
  27. Print "Length of word is:" + str (l)
  28. Note the differences between ASCII and UNICODE strings:
  29. Print "Input your Chinese name :"
  30. S=Raw_input("Press enter to be continued ");
  31. Print "Your name is:" + s;
  32. L=Len(S)
  33. Print "Length of your Chinese name in asc codes is:" + str (l );
  34. A=Unicode(S, "GBK ")
  35. L=Len()
  36. Print "I'm sorry we shoshould use unicode char! Characters number of your Chinese \
  37. Name in unicode is: "+ str (l );

Compared with the implementation of the JavaPython getting started tutorial, you can write it faster. you do not need to write the main function, and the program can be directly run after being saved. in addition, Python is also cross-platform like Java. Experienced C/Java programmers may argue that writing in C/Java is faster than writing in Python. this is a matter of opinion. my idea is that when you master both Java and Python, you will find that writing such programs using Python is much faster than Java.

For example, when operating a local file, you only need a line of code instead of many Java stream packaging classes. various Languages have their own natural application scopes. using Python to process some short programs, similar to interactive programming with the operating system, saves the most time and effort.

To sum up, you will find this Python getting started tutorial quite simple. many Python features are implicitly proposed in code. These features include: Python does not need to explicitly declare data types, keyword descriptions, and interpretation of string functions. I think a skilled programmer should have a good understanding of these concepts.

  • In-depth analysis of Python script Functions
  • The application scope of Python is increasing dramatically day by day
  • Where can I run the Python development language?
  • What are the advantages of the Python Module Language?
  • Advantages of the Python application language

In this way, after reading this short tutorial for an hour, you can familiarize yourself with Python as soon as possible through the existing knowledge migration analogy, and then start programming with it as soon as possible. Of course, learning Python in an hour is quite a bit of a favor. specifically, programming languages include syntax and standard libraries. the syntax is equivalent to the martial arts trick, while the practical experience of the standard library is similar to internal strength and requires long-term exercise.

Python has learned the advantages of Java and provides a large number of easy-to-use standard libraries for programmers to "take things ". (This is also the reason for Python's success). At the beginning, we saw an example of how Python calls Windows cmd. In the future, I will try my best to write the usage of each standard library and some application skills, let everyone really master 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.