Python is a very powerful, fun to code programming language. With a very simple and close to English syntax, writing codes in python enables more focus on algorithms rather than on syntax and conventions.
Python Scripting Console
Unlike C/C++, Python is a scripting language. The code executes line-by-line rather than compiling at once. Hence on a console, you write one line of code, and it is compiled and executed then and there only.
If you have Mac or Linux, chances are that Python comes preinstalled. To launch the console, open a Terminal window and simply execute the following command:
If python is not installed, go Here and follow instructions to install python.
Getting familiar with the python scripting environment
Open the python console and run the following
>> 6 + 4
As you can notice, python has actually compiled and executed that statement. This is the power of console. Now lets try assigning data to variables
Data and Variables
Variables can be created on the fly in python. There is no need to specify data type of variable.
num = 3
result = num * 9print result
In python, a collection of similar objects is known as list. Below are some examples of list.
Numlist = [3,5,10,9,4]
Stringlist =[“python”, “ruby”, “java”]print Numlist print Stringlist
Iteration over list
To iterate over a list, python has a very powerful feature – foreach. Normally, you would find length of the list, then iterate with an increment variable length times. In python, here’s how you can iterate:
for str in Stringlist :
print “The string is – “, str
This would have given you a feel of how easy and simple the syntax is.
In python, a block of code taking arguments is called a method (similar to functions in c/c++/java). To define a method, we use the def keyword. Also, to define a block of code in python, indentation is used rather than curly braces. This is an excellent approach since it forces you to write code with proper indentation, otherwise your code won’t even compile.
result = a + b
Recently graduated, majoring in Mathematics and Computing from IIT Kharagpur, Rohan is a technology enthusiast and passionate programmer. Likes to apply Mathematics and Artificial Intelligence to devise creative solutions to common problems.