Showing posts with label python. Show all posts
Showing posts with label python. Show all posts
Monday, June 3, 2013

What is Python, Introduction of Python, Programming with Python, Python Tutorials for Beginners


What is Python, Introduction of Python, Programming with Python

Python is not a new language it was developed before the coupled of months from Java and it can provide the facility to develop any kind of application like standalone applications, system applications, web applications, games, robotics etc.
The python is simple and portable language, it can minimize the code in compare to other languages like C, C++. The Python provide the large number of predefined classes to do work very quickly and easily.



Introduction

Use of Python

Features of Python 

Program Execution 

Run Python Program  

Comments in Python

Variables in Python  

Operators in Python

Standard Types in Python


Friday, March 29, 2013

Python Tutorials - Python Standard Types, Data Types in Python, How to Cast a type to another type in Python, Type casting in Python


Python Standard Types

Python Standard Types, Data Types in Python, How to Cast a type to another type in Python, Type casting in Python



The Python is a loosely typed language and there are no special name for data types like other programming languages like java, In Java, if we want to create a variable we must provide that a data type like int, double, String etc,but in python the variable automatically cast to assigned value type but we can say the python also categorize the variables into types. We now introduce you to the standard data types in Python.They include numbers and strings, or they are “containers,” or data structures, used to group together multiple Python objects. Before we introduce you to the main data types, it is worth first noting all Python objects have some inherent Boolean value.

Object Boolean Values


Like most other languages like java, c# etc, exactly two Boolean values can be expressed: True and False. All Python values can be represented as a Boolean value, regardless of their data values. For example, any numeric type equal to 0 is considered False while all nonzero (negative and positive) numeric values are True. Similarly, empty containers(like bool("")) are False while nonempty (bool(["hello",0]))containers are True. You can use the bool function to determine the Boolean value of any Python object; furthermore, True and False are legitimate values of their own and can be explicitly assigned as a variable’s value.


>>> process_executed = False
>>> bool(process_executed)
False
>>> bool(-3.78)
True
>>> bool(0.0)
False
>>> bool("")
False
>>> bool([None, 0])
True

The ABOVE examples and the output of bool should all make sense.In the Last example the bool() method have a container which has two values in it so it gives a True value as a result.

Numbers

Python has two primary numeric types: int (for integer) and float (for real number). Python has only one integer type, int, as compare to many other languages that have multiple integer types. floats are double-precision real numbers as like other programming languages.The following are some examples of ints and floats as well as


>>> 2.56 + 3.45
6.01
>>> -5 - 2
-7
>>> 2.1
2.1000000000000001

As like other programming languages the addition in above example the result is as expected but what about in the last example?
in C the range of float is only 6 charector but in python it range increase to 16 characters.
Python Built-in Numeric Types


TypeDescriptionExamples
intsigned integer(no size limit)-4, 0, 0xA2E2, 0453, 67
floatDouble precision floating point numbers3.45, 2.3e+5, -9., -4.6e, 0.894 
complexComplex (Real + Imaginary Numbers)4+1j, .5-j, -34.3e+2-40j


Numeric Built-in and Factory Functions(Conversion or Casting)


Each of the numeric types in Python has a factory function that are used  to convert from one numeric type to another. In other words we can say it “conversion” and “casting,” but we don’t use those terms in Python because we are not changing the type of an existing object.We are returning a new object based on the original object. It is very simple to convert as telling int(12.34) to create a new integer object with value 12 and the fraction will terminate while float(12) returns 12.0.

Python also provides the range of  built-in functions that apply to numbers, such as round() to round () floats to a specified number of digits or abs() for the absolute value of a number.The following are a few examples of these and other built-ins functions:


>>> int(63.89)
63

>>> int('256')
256
>>> float(45)
45.0

>>> round(4.25, 1)
4.3

>>> ord('A')
65

>>> divmod(21, 4)
(5, 1)

>>> chr(97)
'a'

Python Tutorials - Operators in Python, how to use operators in Python


Operators in Python, how to use operators in Python


As Like other programming Languages like C, C++, Java, C# etc the Python also supports the series of Operators, These include arithmetic operators, such as +, -, and *, and so on, assignment operators, +=, -=, *=, and so forth.This just means instead of z = z + 1, you can use z += 1.Absent are the increment/decrements operators (++ and --) you may have used in other programming languages. The standard comparison operators, such as <, >=, ==, !=, and so on, are also available,
and you can group clauses together with Boolean AND and OR with and & or, respectively.

There is also a not(!) operator that negates the Boolean value of a comparison.The
following example show you how its use.

display_output = True if display_output and shoo == 1:

print 'Django and %s are used to developed websites %d' % ('Python', shoo)

I assume you are familiar to programming with other programming languages and you know how to separates the one block of code to other block of code with curly braces({}) and terminate a statement with semi colon (;) but, in python there are no need to these symbols each block of code is separated with indentation

Python has an absence of symbols in general.

  • There are no delimiting braces({}) for separation of blocks of codes.
  • No trailing semicolon (;) to end lines of code
  • No dollar signs ($) for declaring variables like PHP,
  • No required parentheses (( )) for conditional statements (such as the preceding if , loops).
num1=10
num2=14
num3=num1+num2
for lettor in "Welcome in Python"
     print "current lettor :",lettor
print num3

output
current lettor :W
current lettor :e
current lettor l
current lettor :c
current lettor :o
current lettor :m
current lettor :e
current lettor :i
current lettor :n
current lettor :P
current lettor :y
current lettor :t
current lettor :h
current lettor :o
current lettor :n
24


Python Tutorials - Variables and assignments in Python, How to create a variable in Python, How to assign a value to a variable in Python


Variables and assignments in Python, How to create a variable in Python, How to assign a value to a variable in Python


Python is a loosely typed(loosely coupled) programming language it means Python’s variables do not need to be “declared” as holding a specific type of value, as in some other programming languages like C, C++, Java etc we must need to declare a variable with its specific type like int, float,double etc. Python is a “dynamically typed” language.Value assigned to the variable to convert implicitly to the its type and any range we can give it.
>>> shoo = 'Restaurant'
>>> shoo
'Restaurant'
>>> shoo = 4
>>> shoo
4

in above example, the variable shoo is mapped to a string object, 'Restaurant', but is then
remapped to an integer object, 4. Note the string that shoo used to refer to disappears, unless other variables are also referring to it (which is entirely possible!). Because you can remap variable names like this, you are never really 100 percent sure what type of object a variable is pointing to at any given time, unless you ask the interpreter for more information.

Python Tutorials - Comments in Python, How to Create Comments in Python, How to create Multiline Comments in Python


Comments in Python, How to Create Comments in Python, How to create Multiline Comments in Python


The Comments are the very important part of a programming language to give the explanation of a particular code for future reference and the Python is one of them, Comments in Python are denoted with the pound or hash mark (#).When that is the first character of a line of code, the entire line is deemed a comment.The # can also appear in the middle of the line; this means from the point where it is found, the rest of the same line is a comment. For example:

single line Comments

# this entire line is a comment

shoo = 1

# short comment: assign int 1 to 'shoo'

print 'Python and %s are used to develop the web applications these are number %d' % ('Django', shoo)



Multiline Commets

The multiline comments achieved in python with the help of """ for example

"""This is ..

Multiline....

Comments.... """

Comments are not only used to explain nearby code, but also to prevent what would otherwise be working code from executing. and these are also used to help in maintenance of the software in future. 
Monday, March 25, 2013

Python Tutorials - How to Run Python Program, How to run Python Program on Windows, Linux and other platform


How to Run Python Program, How to run Python Program on Windows, Linux and other platform

The Interactive Prompt


The Python is simply run on an interactive prompt which is provided by the python interprator for execute a python script. The interactive Prompt look like as >>> and each of code written after this is simply execute after press enter key.
The All operating system provide the facility to run the python programs with command prompts or terminals, The Python software comes with the interpretor name is python and the Linux comes with built in python interpretor and python interpretor also comes in exe for windows platform.
if you want to start interactive prompt just type python and the below screen comes

% python
Python 3.0.1 (r301:69561, Feb 13 2009, 20:04:18) [MSC v.1500 32 bit (Intel)] ...
Type "help", "copyright", "credits" or "license" for more information.
>>>

now you can type your code and press enter then you can see your output

• On Windows, you can type python in a Command Prompt console window. go to Start→Run... dialog box and type cmd and press enter, the command prompt window will appear now type the python and hit enter.
Note- To do this your python path should be set in environment variables.

• On Unix, Linux, and Mac OS X, you might type this command in a shell or terminal window (e.g., in an xterm or console running a shell such as ksh or csh).

• Other systems may use similar or platform-specific devices. On handheld devices, for example, you generally click the Python icon in the home or application window to launch an interactive session. If you have not set your shell’s PATH environment variable to include Python’s install
directory, you may need to replace the word “python” with the full path to the Python executable on your machine. On Unix, Linux, and similar,

Running Code Interactively

When the >>> has come, it’s started, the Python interactive session begins by printing two lines of informational text, then prompts for input with >>> when it’s waiting for you to type a new Python statement or expression. When we are working with interective prompt the results of your code are displayed after the >>> lines after you hit the Enter key.
For instance, here are the results of two Python print statements (print is really a function call in Python 3.0, but not in 2.6, so the parentheses here are required in 3.0 only): % python

>>> print('Hello Python World!')
Hello Python world!
>>> print(3 ** 4)
81

In above example the print function print the message which is passed into it, first print function print a string and second print the power of 3^4.

>>> status = 'okay'
>>> status
'okay'
>>> 3 ** 4
81
>>>

% <== Use Ctrl-D (on Unix) or Ctrl-Z (on Windows) to exit

Here, the fist line saves a value by assigning it to a variable, and the last two lines typed are expressions (status and 3 ** 4)—their results are displayed automatically. The main thing to notice is that the interpreter executes the code entered on each line immediately, when the Enter key is pressed.

Python Tutorials - Program Execution in Python, How to Program Execution is performed in Python


Program Execution in Python, How to Program Execution is performed in Python

Program Execution in python is a simplest way to write and run the python scripts on interactive prompt of any of the editor used to write the code and save it with .py extension and then run like this
python file_name.py hit enter

The Programmer’s View 


In the simplest way, a Python program is just a text file containing Python statements.
For example, the following file, named example.py, is the simplest way to write Python scripts, but it passes for a fully functional Python program:

>>>print('hello python world')
>>>print(3 ** 100)

The above file contains two Python print statements, which simply print a string ("hello python world") and a numeric expression result (3 to the power 100) to the output stream.

Python’s View

As like other programming languages the Python program also converts into something like a byte code and then also converts in machine code by something like a "virtual machine"

Byte code compilation

After compilation the Python source code converts into a Byte Code internally and you don't able to see it and sometimes the Byte Code converts for the piece of code individually and it is a secret code which is neither a source code nor a machine code, it is intermediate code and provide security and speed to the python programs execution.
The Byte Code can't be readable and it is look something like the combination of some special characters.

The Python Virtual Machine (PVM)

Once your program has been compiled to byte code , then it is send to generally known as the Python Virtual Machine (PVM). The PVM sounds more impressive than it is; really, it’s not a separate program it is inbuilt with the OS kernel, and it need not be installed by itself. In fact, the PVM is just a big loop that iterates through your byte code instructions, one by one, to carry out their operations. The PVM is wok as a real Machine but technically it is not a machine. The PVM is the run- time engine of Python. Technically, it’s just the last step of what is called the “Python interpreter.”


Performance implications

Readers with a background in fully compiled languages such as C and C++, Java, C# etc might notice a few differences in the Python model. One is, there is usually no "build" or “make” step in Python Execution, code runs immediately after it is written. Another is, Python byte code is not binary machine code. Byte code is a Python-specific representation of some hidden code.


In above figure -Python’s traditional run-time execution model, source code you type is translated or converted to byte code, which is then run or executed by the Python Virtual Machine. Your code is automatically compiled, but then it is interpreted.

Thursday, March 21, 2013

Features of Python - strength of Python over other languages, Python Features over the other languages


Strength of Python over other languages, Python Features over the other languages

How Is Python Supported?


Python is an open source programming language, It is a very powerful language to develop any kind of application like web application, mobile application, web services, windows application etc.

All the Operating system supports the python and any user whether he/she is used any of the O/S can write program on any O/S and can be run easily.


What Are Python’s Technical Strengths?


Naturally, this is a developer’s question. The Pythons Technical Strength are explained below


It’s Object-Oriented


Python is an object-oriented language, and as like other OOP's supported languages, Its class model supports advanced notions such as polymorphism, operator overloading, and multiple inheritance but, in the context of Python’s simple syntax and typing, OOP is remarkably easy to apply. In fact, if you don’t know about what are these terms, you’ll find they are much easier to learn with Python than with just about any other OOP's supported languages like C++, Java, C# etc available.


Python’s OOP's nature makes it ideal as a scripting tool for object-oriented systems languages such as C++, Java and C#. Much like C++, Python supports both procedural and object-oriented programming models. You can work with OOP and without OOP in Python


It’s Free


Python is completely free to use and distribute. As with other open source software, such as Tcl, Perl, Linux, and Apache, PHP, MySQL etc. You don't need to purchase any software to program with Python because its freely available for Download from its official site http://python.org.

The very important aspect is you can download the complete source code of python and you can modify it according to your need.

It’s Portable


The standard implementation of Python is written in portable ANSI C,and the Python programs can be execute on any platform easily. For example, Python programs run today on everything from PDAs to supercomputers. Here is a list where, Python is available on:

• Linux and Unix systems.
• Real-time systems such as VxWorks.
• Mac OS (both OS X and Classic).
• Cray supercomputers and IBM mainframes.
• Microsoft Windows and DOS (all modern flavors).
• Cell phones running Symbian OS and Windows Mobile.
• BeOS, OS/2, VMS, and QNX.
• Gaming consoles and iPods.
• PDAs running Palm OS, PocketPC, and Linux.
• Mac OS (both OS X and Classic).
• And more.

Python programs are automatically compiled to portable byte code, which runs the

same on any platform with a compatible version of Python installed

It’s Powerful


Python is an interpreted language but it strong tool set makes it very powerful language, The Python has the feature of scripting languages as well as the compiled languages also so these features makes it very powerful language to develop large scale of product with high functionality.


Dynamic typing


Python is a loosely typed language and there are no data types and no need to provide the range of the value into any of the value template.

The Python variable store any length of the value into it because of the Dynamic type nature of Python language.

Automatic memory management


Python is a OOP's supported language and there is a feature called "Garbage Collection" to automatically deallocates  the unused objects from the memory and free the memory for further storing of objects.


Built-in object types


Python provides commonly used data structures such as lists, dictionaries, tuples and

strings for working with arrays, dynamic arrays, and combination of characters, as you’ll see, they’re both flexible and easy to use. The Python's built-in objects can grow and shrink according to the need.

Built-in tools


Python provides the large collection of built-in library function and objects, To process all those object types and functions, Python comes with powerful and standard operations, including concatenation (joining collections), slicing (extracting sections),

sorting, mapping, and more.

Library utilities


If you want to use a specific task with your program like you want to use the Networking in your program for connect to the server you need to import the some library classes which is provided by the Python's large Library list.


Third-party utilities


Python is an Open Source language and it supports the large scale of third party utilities for communicate with the other software's or include any widget or any third party software to your program then the python provide the third party support for any kind of this work. 



It’s Mixable


Python program can be mixed with the C, C++ & Java, and the Python provides the API for mixing the other programming languages, If we use the Python with C it called the "Cython" and if we will use the Python with Java then it called the "Jython".


It’s Easy to Use


To run a Python program, you simply type it and run it. There are no compile and linking steps, Python also provide the large scale of built-in library function for minimizing the code of a program.

Python programs are very simple, easy to learn and very small as compare to other programming languages like C, C++, java etc.
Program written in Python is more small then program written in C, C++ or java.

It’s Easy to Learn


If you have experience of programming in any other programming languages then you can easily learn Python very quickly in less then 1 week and you don't need specify any starting point of a Python program as like the C, every C program should written inside the main function.





Use of Python - Python's Area - Companies using Python



Use of Python - Python's Area - Companies using Python
Python has a large user base and a very active developer community.
Because Python is 19 years old language, it is also very stable and robust, and portable language. Besides being employed by individual users.
The list of companies which are using Python in Large Scale--
• Google uses Python in its web search systems, and hire the Python Programmer.

• The YouTube video sharing service is written in Python also.


• Google’s very popular App Engine, web development framework uses Python as a primary application development language.


• EVE Online, a Massively Multi-player Online Game makes extensive use of Python.



• The popular Bit Torrent peer-to-peer file sharing system is developed in Python also.


• Intel,  HP, Cisco, Qualcomm, Seagate, and IBM use Python for testing the Hardware.


• Maya, a powerful integrated 3D modeling and animation system, provides a Python scripting API for developing Games.


• JPMorgan Chase, Getco,  UBS,and Citadel apply Python for financial market forecasting.


• Industrial Light & Magic, Pixar, and others use Python for production of large animated movies.

• iRobot develop commercial robotic devices in Python also.


• ESRI uses Python as an end-user customization tool for its popular GIS mapping products.


• Los Alamos, NASA, JPL,  Fermilab, and others use Python for scientific programming tasks.


• The One Laptop Per Child project builds its user interface and activity model in Python.


• The IronPort email server product uses more than 1 million lines of Python code to do its job.


• The NSA uses Python for cryptography and intelligence analysis.





What is Python - Why Python is useful, History of Python


What is Python - Why Python is useful, History of Python

Python is not a new language, it is 22 years old language, in fact it was released by its designer, Guido Van Rossum, in February 1991 while working for CWI also known as Stichting Mathematisch Centrum. Many of Python's features originated from an interpreted language called ABC.


Rossum thinks to correct some of ABC's problems and keep some of its features. At the time he was working on the Amoeba distributed operating system group and was looking for a scripting language with a syntax like ABC but with the access to the Amoeba system calls, so he decided to create a language that was generally extensible.


Python actually got its name from a BBC comedy series from the

seventies "Monty Python's Flying Circus". The designer needed

 a name that was short, unique, and slightly mysterious. Since he

was a fan of the show he thought this name was great.


What Can We Do with Python?


Python is an open source programming language, It is a very powerful language to develop any kind of application like web application, mobile application, web services, windows application etc.
All the Operating system supports the python and any user whether he/she is used any of the O/S can write program on any O/S and can be run easily.
In addition to being a well-designed programming language, Python is useful for accomplishing real-world tasks—the sorts of things developers do day in and day out. 

Python is an object-oriented language, and as like other OOP's supported languages, Its class model supports advanced notions such as polymorphism, operator overloading, and multiple inheritance but, in the context of Python’s simple syntax and typing, OOP is remarkably easy to apply. In fact, if you don’t know about what are these terms, you’ll find they are much easier to learn with Python than with just about any other OOP's supported languages like C++, Java, C# etc available.

Python’s OOP's nature makes it ideal as a scripting tool for object-oriented systems languages such as C++, Java and C#. Much like C++, Python supports both procedural and object-oriented programming models. You can work with OOP and without OOP in Python





Things which  can be done with Python

  • Systems Programming.

  • Graphical User Interface.

  • Internet Scripting and Website Development.

  • Component Integration

  • Database Programming

  • Rapid Prototyping

  • Numeric and Scientific Programming

  • Gaming, Images, Serial Ports, XML, Robots, and More