Python Programming Language
In
this article ,Iam going to explain about python language and how it’s contents
are working like print(), for,if etc.
But first,we have to understand about
python :-
Python programming
is one of the famous languages in programming field. Reason is , it is both easy
to pick up and has vast capabilities. Python Programming language uses simple object-oriented programming(OOPS) approach and very efficient high-level
data structures.
Python Programming also uses very simple and use small type syntax . If you want a language for application building and scripting in several areas.
Python Programming also uses very simple and use small type syntax . If you want a language for application building and scripting in several areas.
One of the key benefits of Python Programming is its
interpretive nature. The Python interpreter and standard library are available
in binary or source form from the Python website, and can run seamlessly on all
major operating systems. Python Programming language is also
freely-distributable, and the same site even has tips and other third-party
tools, programs, modules and more documentation.
Benefits of Python
Programming Language
- Interpreted language: the language is processed by the
interpreter at runtime, like PHP or PERL, so
you don’t have to compile the program before execution.
- Interactive: you can directly interact with the
interpreter at the Python for writing your program.
- Perfect for beginners: for beginner-level programmers,
Python is a great choice as it supports the development of applications
ranging from games to browsers to text processing.
Where Python Programming start
Python is also one of the best web development languages out there, made by Guido van Rossum at National Research Institute for Mathematics and Computer Science in the Netherlands in the early 90s. The language borrows heavily from C, C++, SmallTalk, Unix Shell, Modula-3, ABC, Algol-68 and other scripting languages. Rossum continues to direct the language progress, although a core development team at the institute now maintains most of it.
Learning Python Programming Language
English language keywords make up most of the programming in Python. If you master them, you have mastered Python for the most part. It will take time and some practice, and you need to know the basic concepts before you start . So let’s begin :
Properties
Python is implicitly and dynamically typed, so you do not have to declare variables. The types are enforced, and the variables are also case sensitive, so var and VAR are treated as two separate variables. If you want to know how any object work, you just need to type the following:
Data types
Let’s move ahead to data types. The data structures in Python are dictionaries, tuples and lists. Sets can be found in the sets library that are available in all versions of Python from 2.5 . Lists are similar to one-dimensional arrays, although you can also have lists of other lists. Dictionaries are essentially associative arrays, or hash tables. Tuples are one-dimensional arrays.
You can use the colon to access array ranges. If you leave the
start index empty, the interpreter assumes the first item, so the end index
assumes the last item. Negative indexes count from the last item, so -1 is seen
as the last item. Here is an example:
In the last line, adding a third parameter will see Python step
in the N item increments, instead of one. For instance, in the above sample
code, the first item is returned and then the third, so items zero and two in
zero-indexing.
Strings
Let’s move on to strings. Python strings can either use single or double
quotation marks, and you can use quotation marks of one kind in a string using
another kind, so the following is valid:
“This is a ‘valid’ string”
Multi-strings are enclosed in single or triple double quotes.
Python can support Unicode right from the start, using the following syntax:
u”This is Unicode”
To fill strings with values, you can use the modulo (%) operator
and then a tuple. Each % gets replaced with a tuple item from left to right,
and you can use dictionary substitutions as well.
strString = """This is a multiline
string."""
>>> print ("This %(verb)s a %(noun)s." % {"noun": "test",
"verb": "is"}
This is a test.)
Flow control statements
Python’s flow control statements are ‘while’, ‘for’ and ‘if’.
For a switch, you need to use ‘if’. For enumerating through list members, use
‘for’. For obtaining a number list, use range (number).
Here is the statement
syntax:
rangelist = range(10)
print (rangelist)
Functions
The ‘def’ keyword is used to declare functions. Optional
arguments can be set in the function declaration after mandatory arguments, by
assigning them default values. In case of named arguments, the argument name is
assigned a value. Functions can return a tuple, and you can effective return a variable.
Parameters are passed through reference, but tuples, ints, strings and other immutable types are unchangeable because only the memory location of the item is passed. Binding another object to the variable removed the older one and replaces immutable types. Here is an example:
Parameters are passed through reference, but tuples, ints, strings and other immutable types are unchangeable because only the memory location of the item is passed. Binding another object to the variable removed the older one and replaces immutable types. Here is an example:
func = lambda x: x + 1
print (func(1))
2
def p(list, int=2, string="string"):
list.append("new name")
int = 4
return list, int, string
Classes
Python supports a very limited multiple class inheritance.
Private methods and variables can be declared wth the addition of two or more
underscores and at most one trailing one. You can also bind names to class instances,
like so.
class MyClass(object):
common = 10
def __init__(self):
self.myvariable = 3
defmyfunction(self,
arg1, arg2):
return
self.myvariable
>>>classinstance
= MyClass()
>>>classinstance.myfunction(1,
2)
3
Exceptions
In Python, Exceptions are handled via try-except blocks
[exceptionname]. Here is an example syntax:
def some_function():
try:
10 / 0
except
ZeroDivisionError:
print "Oops,
invalid."
else:
pass
finally:
print ("We're
done with that.")
>>>some_function()
Oops, invalid.
We're done with
that.
In Python, external libraries can be used using the keyword
import[library]. For individual functions, you can use from [funcname] or
[libname] import. Take a look at the following sample syntax:
import random
from time import
clock
randomint =
random.randint(1, 100)
>>> print (randomint)
64
File I/O
The Python programing language comes with a lot of libraries to
begin with. For instance, here is a look at how we convert data structures to
strings with the use of the pickle library using file I/O:
import pickle
mylist =
["This", "is", 4, 13327]
myfile =
open(r"C:\\binary.dat", "w")
pickle.dump(mylist,
myfile)
myfile.close()
myfile =
open(r"C:\\text.txt", "w")
myfile.write("string")
myfile.close()
myfile =
open(r"C:\\text.txt")
>>> print (myfile.read())
'string'
myfile.close()
# file for
reading.
myfile =
open(r"C:\\binary.dat")
loadedlist =
pickle.load(myfile)
myfile.close()
>>> print (loadedlist)
['This', 'is', 4,
13327]
Conditions and variables
Conditions in Python can be changed. For instance, take a look
at this condition:
1 < a < 3
This condition checks that a is greater than one and also less
than three. You can also use ‘del’ to delete items or variables in arrays. A
great way to manipulate and create lists is through list comprehensions, which have an expression and then a
‘for’ clause, followed by a zero or more ‘for’ or ‘if’ clauses. Here is an
example:
>>> lst1 = [1, 2, 3]
>>> lst2 = [3, 4, 5]
>>> print ([x * y for x in lst1 for y in lst2] [3, 4, 5, 6, 8])
>>> print ([x for x in lst1 if 4 > x > 1] [2, 3])
>>> any([i
% 3 for i in [3, 3, 4 ]])
True
>>> sum(1
for i in [3, 3, 4] if i == 4)
2
>>> del
lst1[0]
>>> print (lst1)
[2, 3]
>>>
del lst1
Global variables are called so because they are declared outside
functions and are readable without special declarations. However, if you want
to write them, you need to declare them at the start of the function with the
‘global’ keyword. Otherwise, Python will bind the object to a new local
variable. Take a look at the sample syntax below:
number = 5
def myfunc():
print (number)
def anotherfunc():
print (number)
number = 5
def yetanotherfunc():
global number
number = 5
Conclusion
There is a lot to python than what is mentioned above. As
always,Python, want practicing and
experimenting. Python has a huge array of libraries and functionality that you can discover on
internet. You can also find some other great books and resources to get more
in-depth about Python. From classes and error handling to subsets and more
topics could you learn. There will be syntax errors galore, but keep going at
it and utilize the excellent Python community and resources available.
0 Comments