Wednesday, May 25, 2011

An Interesting Variable Behavior

I have promised you that I will illustrate an interesting example in my next post. Here we go with that interesting variable behavior.

If you type an integer with a leading zero, you might get a confusing error.



Other numbers seem to work, but the results are bizarre.


Can you figure out what is going on? It’s really confusing. Isn’t it?

When we assign an integer with a leading zero, Python assumes that it’s an octal number, and stores its corresponding decimal value. Take a look at the figure below.


We know decimal values of (010)8 = 8, (0100)8 = 64 etc. Here (02132)8 has the decimal value 1114. Then what is the error in 02492? It’s not an octal number at all. We know octal number have only digits 0 to 7.  02492 have 9 in it. So an error occurred.

Just for my curiosity, I tried an integer with two leading zeros and I got the same effect. I tried another one with floating values with leading zeros in their integer part. I found it is same as the normal floating values and no octal-decimal conversion take place.



Have you enjoyed? Keep browsing TaLenCia, I will be back with more interesting facts about Python.

Thanks

AJAY

Tuesday, May 24, 2011

Variables

Here I’m posting explanations and examples related to the variables used in Python. We all are familiar with variables in C programming language. Python variables are almost similar to that.

The variables are used to store values. Example for values are 1, “Hello World”, 23.67 etc. The basic types for variables are integer, string and floating point.

The print statement also works for integers. If you are not sure what type a value has, the interpreter can tell you. This two are illustrated below.


It explains the 3 basic variable types. You may have noticed in the above picture that the type of values ‘17’ and ‘2.9’ are string. It’s because they are in quotation marks like strings.

Have a look at the below picture.


It’s a surprising output. Isn’t it? It’s nothing but Python interprets 1,000,000 as a comma-separated sequence of integers, which it prints with spaces between.

Now take a look at how we can declare variables by just assigning the values.


The first assigns a string to a new variable named message; the second gives the integer 17 to n; the third assigns the approximate value of π to pi. We can also print the values or type of the variables using the print statement.

Programmers generally choose names for their variables that are meaningful. Variable names can be arbitrarily long. They can contain both letters and numbers, but they have to begin with a letter. The underscore character can appear in a name.


In the above picture, 76trombones is illegal because it does not begin with a letter. more@ is illegal because it contains an illegal character, @. Can you guess why class is an invalid variable name? It’s nothing but class is a keyword. Keywords are the tokens that have specified meaning in that programming language and it cannot be used as a user defined variable name. Python have 31 keywords.

and,del,from,not,while,as,elif,global,or,with,assert,else,if,pass,yield,break,except,import,print,class,exec,in,raise,continue,finally,is,return,def,for,lambda,try.

An interesting example of variable nature in Python will be illustrated to you in the next post.

Thanks

AJAY

Monday, May 23, 2011

Hello World!!!!

I want to start this post with a funny thought. I don’t know why all teachers use “Hello World!” example as the beginning to any programming languages. Even the author of “Think Python” Allen B. Downey uses the same. Can they just change the words? I think this program has been accepted world wide as the first program of any language.

“Hello World!” In Python looks like this:
print 'Hello World!'

The output will be
Hello world

In Python 3.0, print is a function, not a statement, so the syntax is 
print(’Hello, World!’)

We will discuss about functions soon. Now it’s time for discuss an example given in “Think Python” implementing the use of print statement and mathematical operations. The example is

If you run a 10 kilometer race in 43 minutes 30 seconds, what is your average time per mile? What is your average speed in miles per hour? (Hint: there are 1.61 kilometers in a mile).

It’s a simple program and logic is also simple. Isn’t it?

Logic is, we have to first find distance he ran in one minute in terms of mile (i.e., miles/minute) and then we have to multiply it with 60 (for converting into miles/hr).

I have done this program in python and its screenshot is given below.


Notice that I have to use a comma after the print'The average speed in miles per hour is' statement; otherwise you will get an error like this.


That’s it! My first program is over. Now I want to study more about variables, expressions and statements and will be back soon.

Thanks

AJAY

Sunday, May 22, 2011

Debugging

Before going to explain debugging, I want to discuss, what a program means. As every computer student knows, a program is a sequence of instructions that specifies how to perform a computation. Computation can be mathematical or symbolic. Symbolic computation means computations like searching and replacing text in a document. Usually a program may have instructions to get input, display input, mathematical operations, conditional operations and iterative operations.

There can be chances to occur errors in the programs. Those errors are called as bugs. Tracking and solving those bugs is called as debugging. Debugging is one of the most intellectually rich, challenging, and interesting parts of programming. There is a classification regarding the type of errors.
  • Syntax errors
  • Runtime errors
  • Semantic errors
Syntax error occurs when the programmer violates the syntax of code. Syntax refers to the structure of a program and the rules about that structure. Python displays a message if a syntax error occurs.

Eg : (1+4) is correct but 1+4) is a syntax error.

Runtime errors cannot be seen until program execution starts. It usually indicates that something exceptional has happened. So it is also called as exceptions.

If a Semantic error occurs, the computer will not generate any error messages.  But the program will do something different than it is supposed to do. This error occurs due to the logical mistake of the programmer.

Debugging is also like an experimental science. Once you have an idea about what is going wrong, you modify your program and try again. If your hypothesis was correct, then you can predict the result of the modification, and you take a step closer to a working program. If your hypothesis was wrong, you have to come up with a new one. Programming is the process of gradually debugging a program until it does what you want.

Thanks

AJAY

Saturday, May 21, 2011

Python - Introduction

I have explained you about the history and deployment of Python in my previous post. Now I just started “Think Python” and here posting introduction to Python.

As other languages like C, C++, Java, Pearl and Visual Basic, Python is also a high level language. High level languages are written using easily understandable formal languages. They are easy to write and read. They can be run on different computers with no modifications, which increase its portability.

There are also low level languages which is the only language that a computer can directly execute. They are also called as machine languages or assembly languages. High level languages need to be converted into low level language before execution. This process time makes a disadvantage for high level language.

Two types of programs are used for this purpose.
  • Interpreter 
  • Compiler

Interpreter process a program a little at a time usually line by line. A compiler reads the program and translates it completely before the program starts running. Once a program is compiled, you can execute it repeatedly without further translation. Here the high level language is called as source code and translated code is called as object code.

Python programs are executed by interpreters. There are two modes to use the interpreter.
  • Interactive mode 
  • Script mode

In interactive mode, we can type Python programs and the interpreter prints the result:

>>> 2 + 4
6

Interactive mode

The >>> symbol is the prompt which is used by the interpreter to indicate that it is ready.
Alternatively, you can store code in a file and use the interpreter to execute the contents of the file, which is called a script. Python scripts have extension ‘.py’.
For testing small pieces of code interactive mode is convenient and for larger lines of codes script method is recommended.
More explanations regarding testing and debugging will be posted soon.
Thanks
AJAY

Thursday, May 19, 2011

Python - The Origin

My instructor Pramode.C.E has sent me the links of a text about the Python programming language – “Think Python by Allen B. Downey”. Before I’m going to think Python, I want to introduce this programming language to you.

Guido van Rossum
The implementation of Python was started in December 1989 by Guido van Rossum. Python 2.0 was released on 16 October 2000, with many major new features including a full garbage collector and support for Unicode. Python 3.0 (also known as Python 3000 or py3k), a major, backwards-incompatible release, was released on 3 December 2008 after a long period of testing. I’m proud to inform you that Python has twice been awarded as TIOBE Programming Language of the Year (2007, 2010), which is given to the language with the greatest growth in popularity over the course of the year (as measured by the TIOBE index).

Python is a multi-paradigm programming language. Rather than forcing programmers to adopt a particular style of programming, it permits several styles: object-oriented programming and structured programming are fully supported, and there are a number of language features which support functional programming and aspect-oriented programming (including by metaprogramming and by magic methods). Many other paradigms are supported using extensions, such as pyDBC and Contracts for Python. Python uses dynamic typing and a combination of reference counting and a cycle-detecting garbage collector for memory management. An important feature of Python is dynamic name resolution (late binding), which binds method and variable names during program execution. Python is often used as a scripting language for web applications, e.g. via mod_wsgi for the Apache web server.

Python Logo
For many operating systems, Python is a standard component. A number of Linux distributions use installers written in Python: Ubuntu uses the Ubiquity installer, while Red Hat Linux and Fedora use the Anaconda installer. YouTube and the original BitTorrent client are the popular users of Python. Large organizations that make use of Python include Google, Yahoo!, CERN, NASA, ILM, and ITA. Most of the Sugar software, which is an open source desktop environment designed with the goal of being used by children for learning as a part of  One Laptop per Child is written in Python.

Users and admirers of Python—most especially those considered knowledgeable or experienced—are often referred to as PythonistsPythonistas, and Pythoneers. I’m going to be a Pythonist by its true sense and you can expect posts regarding Python on the coming days.

Thanks

AJAY

Wednesday, May 18, 2011

Hackers - The real meaning

Who is a hacker? If you think a hacker is someone who is breaking into systems to stole,destroy or modify something, then you must read this post.

Unfortunately, many people have been fooled into using the word 'hacker' instead of crackers. Crackers are the people who breaks into computers with malicious or criminal intentions or recovers encrypted passwords from data that has been stored in or transmitted by a computer system or associated to accounts.

Hackers are entirely opposite to crackers in terms of their intention. A community of enthusiast computer programmers and systems designers, originated in the 1960s around the Massachusetts Institute of Technology's (MIT's) Tech Model Railroad Club (TMRC) and MIT Artificial Intelligence Laboratory. The members of this culture originated the term hacker. This community is notable for launching the free software movement. The World Wide Web and the Internet itself are also hacker artifacts.

The basic difference is: hackers build things, crackers break them. Hackers solve problems and build things, and they believe in freedom and voluntary mutual help. 

Thanks

AJAY

Monday, May 16, 2011

Why Open Source?

You may have questions regarding why I selected open source technologies for my project. As a student and a growing developer I can justify it. We know, all software has source code. Open source software grants every user access to that code. Freedom means choice. Choice means power. That's why I believe open source is inevitable. It returns control to the customer. You can see the code, change it and learn from it. Bugs are found and fixed quickly. When customers are unhappy with one vendor, they can choose another without overhauling their entire infrastructure. No more technology lock-in. No more monopolies. We believe open source simply creates better software. Everyone collaborates, the best technology wins. Not just within one company, but among an Internet-connected, worldwide community.

In the proprietary model, development occurs within one company. Programmers write code, hide it behind binaries, and charge customers to use the software--then charge them more to fix it when it breaks. The problem worsens when you become tied to a company's architecture, protocols, and file formats.

Free and open source software (FOSS) is at the root of the most innovative products, technologies and services of our time. Today’s top entrepreneurs are using FOSS as the building blocks for innovation. Instead of writing an entire solution from scratch, developers can assemble large parts of their solutions from liberally licensed FOSS projects, and focus their creative energies. FOSS also serves as a training ground for new developers. Good developers have always known that the way to improve is by reading well-written programs. Good FOSS projects in dynamic communities provide a wealth of examples for students to read, understand, and work on. There’s the real-world experience of participating in a distributed team.


Open source is not nameless, faceless, and it's not charity. Nor it is solely a community effort. What you see today is a technology revolution driven by market demand.

Thanks

AJAY

Monday, May 9, 2011

Linux - The new era begins

Linux is an operating system based on the free software concept. The thoughts on creation of the Linux was developing since the success of UNIX. The UNIX operating system released in 1970 became influential to other system authors because of its availability and portability. From that time many developers are investing their time on projects regarding the creation of a UNIX-like system. The GNU Project, started in 1983 by Richard Stallman, had the goal of creating a "complete Unix-compatible software system" composed entirely of free software.

Richard Stallman
In 1991, in Helsinki, Linus Torvalds began a project that later became the Linux kernel. Linus Torvalds had wanted to call his invention Freax, a blend of "freak", "free", and "x" (as an allusion to Unix). He wrote the program specifically for the hardware he was using and independent of an operating system because he wanted to use the functions of his new PC with an 80386 processor. Development was done on MINIX using the GNU C compiler, which is still the main choice for compiling Linux today. Ari Lemmke, Torvald's coworker at the University of Helsinki who was one of the volunteer administrators for the FTP server at the time, did not think that "Freax" was a good name. So, he named the project "Linux" on the server without consulting Torvalds. Later, Torvalds consented to "Linux".

Linus Torvalds
Torvalds first published the Linux kernel under its own licence, which had a restriction on commercial activity. In 1992, he suggested releasing the kernel under the GNU General Public License. Linux and GNU developers worked to integrate GNU components with Linux to make a fully-functional and free operating system.

Torvalds announced in 1996 that there would be a mascot for Linux, a penguin. The name Tux was suggested by James Hughes as derivative of Torvalds' UniX. Some Tux graphics are shown below.


The largest part of the work on Linux is performed by the community: the thousands of programmers around the world that use Linux and send their suggested improvements to the maintainers. Various companies have also helped not only with the development of the Kernels, but also with the writing of the body of auxiliary software, which is distributed with Linux.


I have installed Ubuntu 10.04 and I feel good with this open source OS. If you want to download Ubuntu for your system click here.

Thanks

AJAY