Introduction :
Python is a
widely used high level language. It is a general purpose language which is
mostly used by the developers.
History:
Python was
initially designed by Guido van Rossum in 1991 and developed by Python software
foundation.
Why Python?
The
programming languages which were designed before python was introduced mostly
contained complex syntax which the developers found difficult to learn and deal
with.
For example,
languages like C, C# and java contained complex syntactical structures which
lead to large number of errors in a program. As a result, the programmer finds
it difficult to find the errors. This reduces the code readability.
On the other
hand, python was developed for emphasis on code readability. It contained
simple syntax which allowed programmers to easily understand and write code.
The major
advantage of python over other languages is that the python code can be written
in fewer lines of code compared to other programming languages.
Python is a
strongly typed language as it checks the datatype of operands before performing
any operation on them. Java ,C are loosely typed languages.
Example : 5+”str”
- valid in java
5+”str” - invalid in python.
Python lets
you work quickly and integrate systems more efiiciently.
Python is
widely used in developing web based and client side applications.
Is python a compiled language or
interpreted language?
First off, interpreted/compiled is not a
property of the language but a property of the implementation. For most
languages, most if not all implementations fall in one category, so one might
save a few words saying the language is interpreted/compiled too, but it's
still an important distinction, both because it aids understanding and because
there are quite a few languages with usable implementations of both kinds
(mostly in the realm of functional languages, see Haskell and ML). In addition,
there are C interpreters and projects that attempt to compile a subset of
Python to C or C++ code (and subsequently to machine code).
Second, compilation is not restricted to ahead-of-time
compilation to native machine code. A compiler is, more generally, a program
that converts a program in one programming language into a program in another
programming language (arguably, you can even have a compiler with the same
input and output language if significant transformations are applied). And JIT
compilers compile to native machine code at runtime, which can give speed
very close to or even better than ahead of time compilation (depending on the
benchmark and the quality of the implementations compared).
But to stop nitpicking and answer the question you meant to ask:
Practically (read: using a somewhat popular and mature implementation), Python
is compiled. Not compiled to
machine code ahead of time (i.e. "compiled" by the restricted and
wrong, but alas common definition), "only" compiled to bytecode, but
it's still compilation with at least some of the benefits. For example, the
statement a = b.c()
is compiled to a byte stream which, when
"disassembled", looks somewhat like load 0 (b); load_str 'c'; get_attr;
call_function 0; store 1 (a)
. This is a simplification, it's
actually less readable and a bit more low-level - you can experiment with the
standard library dis
module and see what the real deal looks like.
Interpreting this is faster than interpreting from a higher-level
representation.
That bytecode is either interpreted (note that there's a
difference, both in theory and in practical performance, between interpreting
directly and first compiling to some intermediate representation and interpret
that), as with the reference implementation (CPython), or both interpreted and
compiled to optimized machine code at runtime, as with PyPy.
Programming in python :
Interpreter:
We need an interpreter to run our python programs. The python
interpreter can be downloaded from the official website https://www.python.org/.
Currently there are two stable versions of python:
1. Python2 2. Python3
Python3 has
some syntactical differences from Python2. It is your interest to learn any of
these versions. If you are a beginner then I would recommend you to learn
python3
Python is a
scripting language. We can write and execute our scripts on the interpreter.
For example :
#Script begins
print(“geeksforgeeks”)
#script
ends
The above
syntax is based on python3.In the case of python2, we have:
#script begins
print
“geeksforgeeks”
#script
ends
The way of
taking input from the keyboard is also different in python2 and python3
Python2 :
input=raw_input(“enter a string”)
Python3 : input=input(“enter a string”)
Here ‘input’
is a variable which stores the value returned from the keyboard.
Raw_input is
a function which is used to take input from the keyboard.
‘Input(“enter
a string”)’ is also a function which reads the input from the keyboard.
This is
in case of Python3.
The ‘#’ is
used to specify comments. It is used for single line comments in python.
No multiline
comments in python:
Most of the
tutorials say that in order to do a multiline comment one should use """triple
quotes""". This is wrong. Python only has one way of doing
comments and that is using #.
Triple
quotes are treated as regular strings with the exception that they can span
multiple lines. By regular strings I mean that if they are not assigned to a
variable they will be immediately garbage collected as soon as that code
executes. Hence are not ignored by the interpreter in the same way
that #a comment is.
If you have
any comments or suggest any improvements please feel free to comment below.
Comments
Post a Comment
Thanks for your comments!