PYTHON (PART-1)


Welcome to the learning of python,one of most powerful language.Django ,a framework used to build apps and big projects is written in python.Other Frameworks written in python are Pyramid and Micro-frameworks such as Flask and Bottle.If you are a beginner then this is the best language for you.This is the language you can easily learn and implement. :) Lets start with Python basics.
Python is a general-purpose language(can be used with various application domains) and high-level programming language(hides details of computer hardware). Python is easy to learn,read and understand, Free and Open Source and is Object Oriented Programming Language,less no of codes than in C.
PYTHON INSTALLATION IN LINUX:- Python is mostly installed in most of the Operating Sytems.If it's not then,just download python from internet and it will be downloaded on your system,right click and a menu will appear.Click on extract here and edit it if you want to.Run ./configure script and make/make install.
RUNNING PYTHON :- You can run python on either command prompt.Open it using ctrl+alt+t and write python there.A command prompt will appear.You can run your programs here.or what you can do is getting IDE(Integrated Devolpment Environment)for your System.
ON COMMAND PROMPT :-
~$ python
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
You can also make a file and write your code there.The file of python code has extension .py.eg - abc.py
For that steps are as follows :-
1) Make a directory in command prompt.
command - mkdir python(python is the name of your directory,you can give any name)
2) Then go to your directory.
command - cd python
3) Then make a .py file in your directory.
command - nano hello.py
4)A file will be opened.You can write any python code in it.As you are a beginner,So write a simple code :
print "hello"
5)Exit the file
command :- ctrl + x
6)Then execute it with the help of following command :-
python hello.py
eg of program
muskan@muskan-pc:~$ mkdir python
muskan@muskan-pc:~$ cd python
muskan@muskan-pc:~/python$ nano hello.py
A file will open.
write python code -- print "hello"
press ctrl + x
save the file and exit
muskan@muskan-pc:~/python$ ls
hello.py
muskan@muskan-pc:~/python$ python hello.py
hello
Thus your output is shown.
PYTHON IS INTERCTIVE:
PYTHON PROMPT :- Write python on command prompt.A python prompt will appear
muskan@muskan-pc:~$ python
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
these three greater than signs will appear.You can write any command here.
Interactive in general language means which can interact.Same is for python .python prompt is interactive i.e. you write one command and it will respond.
Python Identifiers:
A Python identifier is a name used to identify a variable, function, class, module or other object. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9). Python does not allow punctuation characters such as @, $ and % within identifiers. Python is a case sensitive programming language.
1) Class names start with an uppercase letter and all other identifiers with a lowercase letter.
2) Starting an identifier with a single leading underscore indicates by convention that the identifier is meant to be private.
3) Starting an identifier with two leading underscores indicates a strongly private identifier.
4) If the identifier also ends with two trailing underscores, the identifier is a language-defined special name.
Reserved Words:
The following list shows the reserved words in Python. These reserved words may not be used as constant or variable or any other identifier names. All the Python keywords contain lowercase letters only.
and del from not while
as elif global or with
assert else if pass yield
break except import print class
exec in raise is return
LINES and INDENTATION:
Ever got a error even if the whole code is right? And if that error is identation error,then you must read this carefully. :)
Unlike C++,in python there are no braces to indicate block of codes,but in python it is depicted by identation i.e. spacing.Identation is necessary.
eg :
if True:
print "RED"
else:
print "PINK"
Now this code has accurate spacing but if it is written like this :
if True:
print "RED"
else:
print "PINK"
This will definately give error.So you must keep a chaeck on identation. :)
Multi-Line Statements:
Have you ever thought "Is it possible to break a long line to multiple lines in Python" ?
But you can't just write it simply as then you will get an indenatation error .There is a specific syntax for it which is as follows :-
a = x + y + \
z + u
'\' is used for newline(without quotes).
You can't simply write :
a = x +y +
z + u
Quotation in Python:
In Python we use single,double and triple quotes.
If you want to print a string you can use any string : double,triple or single quotes.You can print hello in many ways :
print 'hello'
or
print "hello"
or
print """hello"""
or
print '''hello'''
OUTPUT : hello
But you can't write :
print '"hello"'
Otherwise output will be "hello"


COMMENTS IN PYTHON :- While coding,you can also write some comments for yourself so that while going through it ,it will become easy to understand.Python Interpretor doesen't consider it as a part of code. :)
Syntax for single line and multi-line comments are different.
for single line comment syntax is :-
#this is a single-line comment in Python
for multi-line comment in python :- Use triple quoted strings.
""" This is an example of a multiline comment """
Multiple Statements on a Single Line: The semicolon ( ; ) allows multiple statements on the single line given that neither statement starts a new code block. Here is a sample snip using the semicolon:
import math ; xy+z;a+b=c
Asignment Operator :
You can assign a value to a variable.This storing of some value in a variable is called assignment whose syntax is :-
a=6
for storing a string syntax is :-
b = 'HELLO'
string must be enclosed in quotes. :)
Following is my shell output. :)
$ python
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a =10
>>> print a
10
>>> b = hello
File "", line 1
b = hello
^ IndentationError: unexpected indent
>>> b = hello
Traceback (most recent call last):
File "", line 1, in
NameError: name 'hello' is not defined
>>> b = 'hello'
>>> print b
hello
>>> c = '10'
>>> print c
10
>>> d = 'hello';
>>> print d
hello
>>> e = '10'
>>> e = '10';
>>> print e
10
Multiple Assignment in Python :
You can assign value like this
a=b=c=10
One program with output :-
>>> y = 20
>>> x = y
>>> y = x+y
>>> print x
20
>>> print y
40
>>> p = 10
>>> q = 20
>>> p,q=q,p+q
>>> print p
20
>>> print q
30
In the above program,assignments are same still result is different due to the way assignment is done. :)

No comments:

Post a Comment

SAY HELLO!!