17db96d56Sopenharmony_ci.. _tut-intro: 27db96d56Sopenharmony_ci 37db96d56Sopenharmony_ci********************** 47db96d56Sopenharmony_ciWhetting Your Appetite 57db96d56Sopenharmony_ci********************** 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ciIf you do much work on computers, eventually you find that there's some task 87db96d56Sopenharmony_ciyou'd like to automate. For example, you may wish to perform a 97db96d56Sopenharmony_cisearch-and-replace over a large number of text files, or rename and rearrange a 107db96d56Sopenharmony_cibunch of photo files in a complicated way. Perhaps you'd like to write a small 117db96d56Sopenharmony_cicustom database, or a specialized GUI application, or a simple game. 127db96d56Sopenharmony_ci 137db96d56Sopenharmony_ciIf you're a professional software developer, you may have to work with several 147db96d56Sopenharmony_ciC/C++/Java libraries but find the usual write/compile/test/re-compile cycle is 157db96d56Sopenharmony_citoo slow. Perhaps you're writing a test suite for such a library and find 167db96d56Sopenharmony_ciwriting the testing code a tedious task. Or maybe you've written a program that 177db96d56Sopenharmony_cicould use an extension language, and you don't want to design and implement a 187db96d56Sopenharmony_ciwhole new language for your application. 197db96d56Sopenharmony_ci 207db96d56Sopenharmony_ciPython is just the language for you. 217db96d56Sopenharmony_ci 227db96d56Sopenharmony_ciYou could write a Unix shell script or Windows batch files for some of these 237db96d56Sopenharmony_citasks, but shell scripts are best at moving around files and changing text data, 247db96d56Sopenharmony_cinot well-suited for GUI applications or games. You could write a C/C++/Java 257db96d56Sopenharmony_ciprogram, but it can take a lot of development time to get even a first-draft 267db96d56Sopenharmony_ciprogram. Python is simpler to use, available on Windows, macOS, and Unix 277db96d56Sopenharmony_cioperating systems, and will help you get the job done more quickly. 287db96d56Sopenharmony_ci 297db96d56Sopenharmony_ciPython is simple to use, but it is a real programming language, offering much 307db96d56Sopenharmony_cimore structure and support for large programs than shell scripts or batch files 317db96d56Sopenharmony_cican offer. On the other hand, Python also offers much more error checking than 327db96d56Sopenharmony_ciC, and, being a *very-high-level language*, it has high-level data types built 337db96d56Sopenharmony_ciin, such as flexible arrays and dictionaries. Because of its more general data 347db96d56Sopenharmony_citypes Python is applicable to a much larger problem domain than Awk or even 357db96d56Sopenharmony_ciPerl, yet many things are at least as easy in Python as in those languages. 367db96d56Sopenharmony_ci 377db96d56Sopenharmony_ciPython allows you to split your program into modules that can be reused in other 387db96d56Sopenharmony_ciPython programs. It comes with a large collection of standard modules that you 397db96d56Sopenharmony_cican use as the basis of your programs --- or as examples to start learning to 407db96d56Sopenharmony_ciprogram in Python. Some of these modules provide things like file I/O, system 417db96d56Sopenharmony_cicalls, sockets, and even interfaces to graphical user interface toolkits like 427db96d56Sopenharmony_ciTk. 437db96d56Sopenharmony_ci 447db96d56Sopenharmony_ciPython is an interpreted language, which can save you considerable time during 457db96d56Sopenharmony_ciprogram development because no compilation and linking is necessary. The 467db96d56Sopenharmony_ciinterpreter can be used interactively, which makes it easy to experiment with 477db96d56Sopenharmony_cifeatures of the language, to write throw-away programs, or to test functions 487db96d56Sopenharmony_ciduring bottom-up program development. It is also a handy desk calculator. 497db96d56Sopenharmony_ci 507db96d56Sopenharmony_ciPython enables programs to be written compactly and readably. Programs written 517db96d56Sopenharmony_ciin Python are typically much shorter than equivalent C, C++, or Java programs, 527db96d56Sopenharmony_cifor several reasons: 537db96d56Sopenharmony_ci 547db96d56Sopenharmony_ci* the high-level data types allow you to express complex operations in a single 557db96d56Sopenharmony_ci statement; 567db96d56Sopenharmony_ci 577db96d56Sopenharmony_ci* statement grouping is done by indentation instead of beginning and ending 587db96d56Sopenharmony_ci brackets; 597db96d56Sopenharmony_ci 607db96d56Sopenharmony_ci* no variable or argument declarations are necessary. 617db96d56Sopenharmony_ci 627db96d56Sopenharmony_ciPython is *extensible*: if you know how to program in C it is easy to add a new 637db96d56Sopenharmony_cibuilt-in function or module to the interpreter, either to perform critical 647db96d56Sopenharmony_cioperations at maximum speed, or to link Python programs to libraries that may 657db96d56Sopenharmony_cionly be available in binary form (such as a vendor-specific graphics library). 667db96d56Sopenharmony_ciOnce you are really hooked, you can link the Python interpreter into an 677db96d56Sopenharmony_ciapplication written in C and use it as an extension or command language for that 687db96d56Sopenharmony_ciapplication. 697db96d56Sopenharmony_ci 707db96d56Sopenharmony_ciBy the way, the language is named after the BBC show "Monty Python's Flying 717db96d56Sopenharmony_ciCircus" and has nothing to do with reptiles. Making references to Monty 727db96d56Sopenharmony_ciPython skits in documentation is not only allowed, it is encouraged! 737db96d56Sopenharmony_ci 747db96d56Sopenharmony_ciNow that you are all excited about Python, you'll want to examine it in some 757db96d56Sopenharmony_cimore detail. Since the best way to learn a language is to use it, the tutorial 767db96d56Sopenharmony_ciinvites you to play with the Python interpreter as you read. 777db96d56Sopenharmony_ci 787db96d56Sopenharmony_ciIn the next chapter, the mechanics of using the interpreter are explained. This 797db96d56Sopenharmony_ciis rather mundane information, but essential for trying out the examples shown 807db96d56Sopenharmony_cilater. 817db96d56Sopenharmony_ci 827db96d56Sopenharmony_ciThe rest of the tutorial introduces various features of the Python language and 837db96d56Sopenharmony_cisystem through examples, beginning with simple expressions, statements and data 847db96d56Sopenharmony_citypes, through functions and modules, and finally touching upon advanced 857db96d56Sopenharmony_ciconcepts like exceptions and user-defined classes. 867db96d56Sopenharmony_ci 877db96d56Sopenharmony_ci 88