Welcome to the Python Tutorials, Python works much like the two previous categories, PHP and ColdFusion as they are all server side programming languages.
Apart from the tutorials, we will also cover Interview Questions, Issues and How To’s of Python.
Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where as other languages use punctuation, and it has fewer syntactical constructions than other languages.
-Python is Interpreted: Python is processed at runtime by the interpreter. You do not need to compile your program before executing it. This is similar to PERL and PHP.
-Python is Interactive: You can actually sit at a Python prompt and interact with the interpreter directly to write your programs.
-Python is Object-Oriented: Python supports Object-Oriented style or technique of programming that encapsulates code within objects.
-Python is a Beginner’s Language: Python is a great language for the beginner-level programmers and supports the development of a wide range of applications from simple text processing to WWW browsers to games.
Python was developed by Guido van Rossum in the late eighties and early nineties at the National Research Institute for Mathematics and Computer Science in the Netherlands.
Python is copyrighted. Like Perl, Python source code is now available under the GNU General Public License (GPL).
Python is now maintained by a core development team at the institute, although Guido van Rossum still holds a vital role in directing its progress.
Pythons mainly used for
-Programming video games
-Artificial Intelligence algorithm
-Programming various scientific programs such as statistical models
Applications of Python
-GUI based desktop applications
-Image processing and graphic design applications
-Scientific and computational applications
-Web frameworks and web applications
-Enterprise and business applications
A loop statement allows us to execute a statement or group of statements multiple times. Python programming language provides following types of loops to handle looping requirements.
Python while Loop Statements
A while loop statement in Python programming language repeatedly executes a target statement as long as a given condition is true.
The syntax of a while loop in Python programming language is −
statement(s) may be a single statement or a block of statements. The Condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.
In Python, all the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements.
#!/usr/bin/python count = 0 while (count < 9): print 'The count is:', count count = count + 1 print "Goodbye!" Output
The count is: 0 The count is: 1 The count is: 2 The count is: 3 The count is: 4 The count is: 5 The count is: 6 The count is: 7 The count is: 8 Good bye!
The Infinite Loop
A loop becomes infinite loop if a condition never becomes FALSE. You must use caution when using while loops because of the possibility that this condition never resolves to a FALSE value. This results in a loop that never ends. Such a loop is called an infinite loop.
An infinite loop might be useful in client/server programming where the server needs to run continuously so that client programs can communicate with it as and when required.
#!/usr/bin/python var = 1 while var == 1 : # This constructs an infinite loop num = raw_input("Enter a number :") print "You entered: ", num print "Good bye!" Output
Enter a number :20 You entered: 20 Enter a number :29 You entered: 29 Enter a number :3 You entered: 3 Enter a number between :Traceback (most recent call last): File "test.py", line 5, in <module> num = raw_input("Enter a number :") KeyboardInterrupt To exit the program you need to use CTRL+C
Python for Loop Statements
It has the ability to iterate over the items of any sequence, such as a list or a string.
for iterating_var in sequence:
If a sequence contains an expression list, it is evaluated first. Then, the first item in the sequence is assigned to the iterating variable iterating_var. Next, the statements block is executed. Each item in the list is assigned toiterating_var, and the statement(s) block is executed until the entire sequence is exhausted.
#!/usr/bin/python for letter in 'Python': # First Example print 'Current Letter :', letter fruits = ['banana', 'apple', 'mango'] for fruit in fruits: # Second Example print 'Current fruit :', fruit print "Good bye!" Output:
Current Letter : P Current Letter : y Current Letter : t Current Letter : h Current Letter : o Current Letter : n Current fruit : banana Current fruit : apple Current fruit : mango Good bye
Python nested loops
Python programming language allows to use one loop inside another loop. Following section shows few examples to illustrate the concept.
for iterating_var in sequence:
for iterating_var in sequence:
The syntax for a nested while loop statement in Python programming language is as follows −
A final note on loop nesting is that you can put any type of loop inside of any other type of loop. For example a for loop can be inside a while loop or vice versa.
The following program uses a nested for loop to find the prime numbers from 2 to 100 −
#!/usr/bin/python i = 2 while(i < 100): j = 2 while(j <= (i/j)): if not(i%j): break j = j + 1 if (j > i/j) : print i, " is prime" i = i + 1 print "Good bye!" output:
2 is prime 3 is prime 5 is prime 7 is prime 11 is prime 13 is prime 17 is prime 19 is prime 23 is prime 29 is prime 31 is prime 37 is prime 41 is prime 43 is prime 47 is prime 53 is prime 59 is prime 61 is prime 67 is prime 71 is prime 73 is prime 79 is prime 83 is prime 89 is prime 97 is prime Good bye!
Advantages of Python
-Time-wasting matters of style, especially regarding enclosing curly-bracket positioning blocks, simply go away.
-The “easy” way to do a thing in Python is usually right for the most common use-case. Python is tight code.
-You can become productive in Python fairly quickly even as a beginner, yet it is powerful enough for professionals.
-The Python Package Index (PyPI) contains numerous third-party modules that make Python capable of interacting with most of the other languages and platforms.
-Python provides a large standard library which includes areas like internet protocols, string operations, web services tools and operating system interfaces. Many high use programming tasks have already been scripted into the standard library which reduces length of code to be written significantly.
-Python language is developed under an OSI-approved open source license, which makes it free to use and distribute, including for commercial purposes.
-Further, its development is driven by the community which collaborates for its code through hosting conferences and mailing lists, and provides for its numerous modules
-Python offers excellent readability and uncluttered simple-to-learn syntax which helps beginners to utilize this programming language. The code style guidelines, PEP 8, provide a set of rules to facilitate the formatting of code. Additionally, the wide base of users and active developers has resulted in a rich internet resource bank to encourage development and the continued adoption of the language.
-Python has built-in list and dictionary data structures which can be used to construct fast runtime data structures. Further, Python also provides the option of dynamic high-level data typing which reduces the length of support code that is needed.
-Python has clean object-oriented design, provides enhanced process control capabilities, and possesses strong integration and text processing capabilities and its own unit testing framework, all of which contribute to the increase in its speed and productivity. Python is considered a viable option for building complex multi-protocol network applications.
Disadvantages of Python
-If you like meta programming of the sort popular in LISP, then Python’s one-liner anonymous functions called Lambda’s are going to be limiting.