|
||||||
Starting Programming with PythonHow to Create Scripts with the Python Programming LanguagePython is a simple yet effective scripting language, and it's one that the programmer can learn very quickly regardless of the platform that they're using.
Python has been a popular programming language for nearly 20 years. It's popular for scripting and for rapid application development, and that popularity is for a number of reasons:
But, most importantly, it is easy to read and easy to maintain, and so any programmer (regardless of any previous experience) can learn to use Python very quickly. A Simple Python ScriptThe simplest Python script contains a single line of code, something like: print ("Hello World")
This can be run interactively in IDLE (as shown in figure 1 at the bottom of this article), or it can be saved to a file and then run from the command line (as shown in figure 2). However, this will only work if the system environment PATH variable has been updated correctly. Adding Some Process Control with PythonPython, just like most programming languages, has an if..then..else process control structure. However, it's handled in a slightly different way. Most programmers will be familiar with formats such as:
Python does not use any of those to denote nesting, instead is uses indentation. For example: light = "Y"
if light == "Y":
___print ("It's day time")
else:
___print ("It's night time")
The result of running this code can be seen in figure 3, but before moving on it's important to stress that the indentation forms part of the code. If the indentation is remove then the code will run differently (if it actually runs at all). However, once the programmer remembers that fact then it's a simple task to start extending the script. Creating and Running a Python FunctionThe Python programmer creates a function by using the def keyword: def daycheck():
And again rather than using brackets, the indentation is used to define the nesting of the code within the body of the function: ___light = input ("Is it light outside?[Y/N]")
___if light.upper() == "Y":
______print ("It's day time")
___else:
______print ("It's night time")
This can then be called from the script: daycheck()
And, as figure 4 shows, it is very easy to write and extend a Python script. Because of this any application is not limited due to the complexity of the programming language. In fact, the only limit is the time and the programming abilities of the programmer themselves.
The copyright of the article Starting Programming with Python in Computer Programming Languages is owned by Mark Alexander Bain. Permission to republish Starting Programming with Python in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||