What Is Python?: 1. Immediate Mode
What Is Python?: 1. Immediate Mode
Even though most of today’s Linux and Mac have Python preinstalled in it,
the version might be out-of-date.
In Mac OS we need not worry about this as the installer takes care about
the search path.
1. Immediate mode
Typing python in the command line will invoke the interpreter in immediate
mode. We can directly type in Python expressions and press enter to get
the output.
>>>
is the Python prompt. It tells us that the interpreter is ready for our input.
Try typing in 1 + 1 and press enter. We get 2 as the output. This prompt can
be used as a calculator. To exit this mode type exit() or quit() and press
enter.
2. Script mode
This mode is used to execute Python program written in a file. Such a file is
called a script. Scripts can be saved to disk for future use. Python scripts
have the extension .py, meaning that the filename ends with .py.
To execute this file in script mode we simply write python helloWorld.py at the
command prompt.
We just need to save it with the .py extension. But using an IDE can make
our life a lot easier. IDE is a piece of software that provides useful features
like code hinting, syntax highlighting and checking, file explorers etc. to the
programmer for application development.
Using an IDE can get rid of redundant tasks and significantly decrease the
time required for application development.
IDEL is a graphical user interface (GUI) that can be installed along with the
Python programming language and is available from the official website.
We can also use other commercial or free IDE according to our preference.
We have used the PyScripter IDE for our testing and we recommend the
same. It is free and open source.
Hello World Example
Now that we have Python up and running, we can continue on to write our
first Python program.
Type the following code in any text editor or an IDE and save it
as helloWorld.py
print("Hello world!")
Now at the command window, go to the loaction of this file. You can use
the cd command to change directory.
Hello world!
If you are using PyScripter, there is a green arrow button on top. Press that
button or press Ctrl+F9 on your keyboard to run the program.
In this program we have used the built-in function print(), to print out a
string to the screen. String is the value inside the quotation marks, i.e. Hello
world! . Now try printing out your name by modifying this program.
Python Keywords
Keywords are the reserved words in Python.
We cannot use a keyword as variable name, function name or any other
identifier. They are used to define the syntax and structure of the Python
language.
There are 33 keywords in Python 3.3. This number can vary slightly in
course of time.
All the keywords except True, False and None are in lowercase and they must
be written as it is. The list of all the keywords are given below.
as elif if or yield
If you want to have an overview, here is the complete list of all the
keywords with examples.
Python Identifiers
Identifier is the name given to entities like class, functions, variables etc. in
Python. It helps differentiating one entity from another.
We can also use camel-case style of writing, i.e., capitalize every first letter
of the word except the initial word without any spaces. For
example: camelCaseExample