Chapter 3: Ten Useful Tips from Python
Experts
This chapter focuses on tips provided by tech experts regarding the usage of
the Python
programming language. These tips are intended for beginners.
Dynamic Input – When using programming languages that involve statically typed
values like C++ and Java, you need to define every function
argument as well as the
data type for your function return values.
Python, however, is a language that uses dynamic inputs. Here, you will never need to
define the data info of anything. Python will automatically take note of the data types
that you enter, depending on the values that you will use.
Running scripts through Python – It is easy to run Python scripts. For example, on
Unix
operating systems, you can simply use the command line:
$ python –r & mypyprog.py
Interconnecting Strings – If you want to combine or link several strings, you can
simply use the (+) symbol. For example:
>> print “light” + “saber”
lightsaber
Finding names that are defined by modules – The “dir()” function can be utilized to
know which names are defined by a module. It produces a classified set of strings.
Accessing the documentation of a module – If you want
to access the integrated
documentation of a module, you can just use ._doc_.
Running the _init_method – This will be activated immediately once an object that
belongs to a class is manifested. The “constructor” in other programming languages
like Java, C++ or C is similar to Python’s _init_method.
Using the Python Interpreter to run programs – The interactive
interpreter used in this
programming language lets you use all the commands available in Python. You can
just send the commands through the command prompt and you will get results
quickly.
The = and == operators – when it comes to assignments, the symbol “=” is used by
Python, while the symbol “==” is employed to compare values. Currently, the Python
language does not accept linear assignment, which means the programmer will not
accidentally assign values when he is trying to perform a comparison.
Working with Statements using Python – When separating blocks of code, Python
uses an indentation and a colon, while for separating assignments, a carriage return is
used. C++, C, and other compiled languages utilize semicolons
to divide statements
and brackets (the curly ones) to divide blocks of codes.
Employ modules to manage your files – You may want to break up your programs
into multiple files to make them more manageable as they increase in size.
Python lets you place several functions into a single file and convert them into a
module. You can incorporate this file into other applications and scripts. They should
have .py extensions.