Getting Started with Python
Python is one of the popular programming languages because of its elegant syntax, dynamic typing and interpreted nature.
Python leads a double life one as an interpreter for the scripts and other as a interactive shell. It comes with large collection of standard modules that can be used as the basis of your programs.
First you need to download and install the latest version of Python fromhttps://www.python.org/downloads/
The latest version is Python 3 and you can download the latest update in this version.
Once you downloaded and installed Python 3, you will find a build-in IDLE which is Python’s Integrated Development Environment. This can be used to write the Python code.
Once you started the IDLE, the python is running in interactive shell mode and ready to accept the code and interprets it.
It is an interactive mode in shell. The three angle brackets are followed by the prompt for the user to enter the instructions. Let’s try it to use it as calculator and pass it some arithmetic expressions to see how they are interpreted by the Python.
You will notice that after entering the expression, it is immediately interpreted and processed by the Python interpreter and result is displayed back in the shell command line.
Now try to enter some text message into the shell.
As you can see the Python interprets it and says it is an invalid syntax.
Because Python only accepts text within single or double quotes and only then it will be interpreted correctly.
Now the Python will interprets the text and displayed back in the shell.
Python’s come with a good collection of some built-in functions that comes really handy to do utility work. These functions are ready and available to use with-in the shell.
The most used function is the print()
It prints the data to the standard output device which is usually the screen. The next screen shots show the various uses of print() in the interactive shell mode of Python.
Using print() function will help you to properly display the data in the command line interface.
min() and max()
Similarly we have min() and max() built-in function to find out the maximum and minimum number from a sequence of numbers. See the demonstration below of these two functions.
The ranges of numbers are provided in the parentheses of the function separated by commas. The function then determines the minimum or maximum number and displays the result in the shell.
It is an evaluation function to evaluate an arithmetic expression with all the combination of arithmetic operator. The eval() function will apply the precedence rules to the give expression and display the result back on the screen. See the next screenshot of this function.
As you can see the eval() function takes the expression and process it for you.
Note down that eval() takes the expression as a text in double inverted commas, otherwise the Python will not recognise it and display an error as shown above.
This is to the power of function as in mathematics the statement 2 to the power of 3 results in 8. Similarly you can use the pow() function by providing it both the base and power number with the parentheses separated with comma. You can see this methods in action below.
Similarly each of this built-in function is used to do a particular task and you can use it as part of your code when solving a given programming problem.
You can also combine two or more functions to perform any task. For example look at this code:
Here the eval() function is wrapped inside the print function to display the evaluated value on the command-line screen.
Just make sure that you need to close the same number of parentheses as you opened because each function has its own pair of parentheses.
This built-in function will take you to the help mode of Python. It is a great help utility tool to know more about Python and explore the different areas, by accessing complete help and tutorial on Python. The great thing is that it is built-in utility and you can access it at any time.
So let’s try it and see how it works.
As you can see the help() function enters you in the help mode with its own prompt.
When you enter the word "keywords", help utility will display all the keywords supported by this current version of Python.
This is the complete list of Python keywords. You can further write the name of the keyword and get more information on it usage and syntax.
Try the while keyword and you see complete description about the while.
Now write the word symbols in the help utility and it will show all the symbols supported and can be used in Python code.
You can also search the help by topics and get more information for what you looking for.
You can also display the list of all the modules in this current version of Python. Modules are ready-made software programs that you can use to accelerate the development of your system. You need to know the name of the module you want to use, the correct path of it in order to import in to your program, and then you can use it. We will cover this next time but it is an important feature of Python.
When you finish with the help utility and want to go back to the Python interpreter, you must enter quit to exit the help utility tool. You can see below how it works:
You are now back in interactive shell mode and the Python interpreter is ready to accept you code.
There is still a lot you can do with Python. But this step is vital and makes your foundation firm on how to communicate and convey your instructions and get help from Python.
This article was posted in the Python category by Shahid Khan.