0% found this document useful (0 votes)
6 views3 pages

Ihffuidhuifhjhfh

The Python Tutorial introduces Python as an efficient programming language suitable for automating tasks, developing applications, and simplifying complex operations. It highlights Python's simplicity, extensibility, and readability compared to other languages like C, C++, and Java, making it accessible for both beginners and professionals. Additionally, it covers error handling, distinguishing between syntax errors and exceptions, and providing examples of common errors encountered during programming.

Uploaded by

sabsebada
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Ihffuidhuifhjhfh

The Python Tutorial introduces Python as an efficient programming language suitable for automating tasks, developing applications, and simplifying complex operations. It highlights Python's simplicity, extensibility, and readability compared to other languages like C, C++, and Java, making it accessible for both beginners and professionals. Additionally, it covers error handling, distinguishing between syntax errors and exceptions, and providing examples of common errors encountered during programming.

Uploaded by

sabsebada
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Tutorial, Release 3.7.

2 CONTENTS
CHAPTER

ONE

WHETTING YOUR APPETITE

If you do much work on computers, eventually you find that there’s some task you’d like to automate. For
example, you may wish to perform a search-and-replace over a large number of text files, or rename and
rearrange a bunch of photo files in a complicated way. Perhaps you’d like to write a small custom database,
or a specialized GUI application, or a simple game.
If you’re a professional software developer, you may have to work with several C/C++/Java libraries but
find the usual write/compile/test/re-compile cycle is too slow. Perhaps you’re writing a test suite for such
a library and find writing the testing code a tedious task. Or maybe you’ve written a program that could
use an extension language, and you don’t want to design and implement a whole new language for your
application.
Python is just the language for you.
You could write a Unix shell script or Windows batch files for some of these tasks, but shell scripts are best
at moving around files and changing text data, not well-suited for GUI applications or games. You could
write a C/C++/Java program, but it can take a lot of development time to get even a first-draft program.
Python is simpler to use, available on Windows, Mac OS X, and Unix operating systems, and will help you
get the job done more quickly.
Python is simple to use, but it is a real programming language, offering much more structure and support
for large programs than shell scripts or batch files can offer. On the other hand, Python also offers much
more error checking than C, and, being a very-high-level language, it has high-level data types built in, such
as flexible arrays and dictionaries. Because of its more general data types Python is applicable to a much
larger problem domain than Awk or even Perl, yet many things are at least as easy in Python as in those
languages.
Python allows you to split your program into modules that can be reused in other Python programs. It
comes with a large collection of standard modules that you can use as the basis of your programs — or as
examples to start learning to program in Python. Some of these modules provide things like file I/O, system
calls, sockets, and even interfaces to graphical user interface toolkits like Tk.
Python is an interpreted language, which can save you considerable time during program development
because no compilation and linking is necessary. The interpreter can be used interactively, which makes it
easy to experiment with features of the language, to write throw-away programs, or to test functions during
bottom-up program development. It is also a handy desk calculator.
Python enables programs to be written compactly and readably. Programs written in Python are typically
much shorter than equivalent C, C++, or Java programs, for several reasons:
• the high-level data types allow you to express complex operations in a single statement;
• statement grouping is done by indentation instead of beginning and ending brackets;
• no variable or argument declarations are necessary.
Python is extensible: if you know how to program in C it is easy to add a new built-in function or module
to the interpreter, either to perform critical operations at maximum speed, or to link Python programs to
libraries that may only be available in binary form (such as a vendor-specific graphics library). Once you

3
CHAPTER

EIGHT

ERRORS AND EXCEPTIONS

Until now error messages haven’t been more than mentioned, but if you have tried out the examples you have
probably seen some. There are (at least) two distinguishable kinds of errors: syntax errors and exceptions.

8.1 Syntax Errors


Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while
you are still learning Python:

>>> while True print('Hello world')


File "<stdin>", line 1
while True print('Hello world')
^
SyntaxError: invalid syntax

The parser repeats the offending line and displays a little ‘arrow’ pointing at the earliest point in the line
where the error was detected. The error is caused by (or at least detected at) the token preceding the arrow:
in the example, the error is detected at the function print(), since a colon (':') is missing before it. File
name and line number are printed so you know where to look in case the input came from a script.

8.2 Exceptions
Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made
to execute it. Errors detected during execution are called exceptions and are not unconditionally fatal: you
will soon learn how to handle them in Python programs. Most exceptions are not handled by programs,
however, and result in error messages as shown here:

>>> 10 * (1/0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>> 4 + spam*3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'spam' is not defined
>>> '2' + 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly

61

You might also like