Archive

Posts Tagged ‘High Level Programming Language’

Python: Bult-in Functions for Numeric types

April 24, 2009 2 comments

Python has some built-in functions for working with numeric types. Some of them are described below: ———–

Function Name

Descriptions

Code Example

abs

The abs(x) function takes the absolute value of any integer, long integer or floating point number.

When applied to a complex number, the function returns the magnitude of the number, which is distance from that point to the origin in the complex plane.

>>> abs(-5.0)

5.0

>>> abs(-2)

2

>>> abs(5+2j)

5.3851648071345037

coerce

The coerce(x,y) function applies numeric conversion rules(described below the table) to two numbers and return them as tuple.

>>coerce(5,2.5)

(5.0,2.5)

>>coerce(5.5,5+2j)

(5.5+0j,5+2j)

divmod

The functions divmod(a,b) performs long division on two numbers and returns the quotient and remainder.

>>> divmod(5,2)

(2, 1)

>>> divmod(5.5,2.5)

(2.0, 0.5)

pow

The functions pow(x,y[,z]) performs power operation.

As usual, Python coerces the two numbers to a common type if needed. If the resulting type can’t express the correct result then python will show an error message.

An optional third argument to pow specifies the modulo operation to perform on the result.

>>> pow(2,3)

8

>>> pow(2,-1)

0.5

>>> pow(2,5,10)

2

For all the built-in functions and there definitions, please visit this link:
http://docs.python.org/library/functions.html

Numeric Conversion Rule:

  1. If one of the numbers is a complex number then convert the other two a complex number too.
  2. If one of the numbers is floating point number then convert the other to floating point.
  3. If one of the numbers is a long integer then convert the other to a long integer. (Not applicable for Python 3.1)
  4. No previous rules applied, so both are integers, and Python leaves them unchanged.

Python: Variables, Numeric Types and Operators

April 8, 2009 Leave a comment

Python offers four different kinds of numbers with which you can work:integers, long numbers (or longs), floating-point numbers (or floats), and imaginary/complex numbers.

We are familiar with Integer, Long or Floating point Numbers. The new type is Imaginary/Complex number. The imaginary number behaves very much like a float, except that it cannot be mixed with a float. When you see an imaginary number, it will have the letter j trailing it.

Note: Python 2 had separate int and long types for non-floating-point numbers. An int could not be any larger than sys.maxint, which varied by platform. Longs were defined by appending an L to the end of the number, and they could be, well, longer than ints. In Python 3, there is only one integer type, called int, which mostly behaves like the long type in Python 2. Since there are no longer two types, there is no need for special syntax to distinguish them.

To determine the type of a number, you can use a special function that is built into Python, called type. When you use type, Python will tell you what kind of data you’re looking at. Let’s try this with a few examples.

code5In the above examples, we use type() function to determine the type of 2 different numbers: integer, float and complex.

Format Specifiers:

Int: %d

Float: %f

Exponential (For large numbers): %E

Manipulating the Numeric Datatypes:

We can use most of the Python’s operators when working with Numeric data types. the following table lists the operators and how they behavewith numeric types:

code6

Next Post: Useful functions in Python

Code practice – String usage in .NET

April 6, 2009 3 comments

String in .Net, is an Immutable object (Object cannot be modifed). Hence memory allocated for a string remains even if you alter/modify the string through code, forcing the CLR to create new space for the modified string.

//Code Illustration in C#.Net

string str = ""; //memory created for str and value "" is stored
str = "String is Immutable" //new memory space is allocated for str
                            //and value is stored
str += str  + " in .NET" //Again, new memory allocation

This means more string operation you do with, more memory is occupied, though its with the same string variable. Better approach would be to use StringBuilder to do all appends and get the final output as StringBuilder class provides an efficient way to append. And its mutable.

Also string str = “” and string str = String.Empty is not the same in terms of memory allocation. the first creats an object in the memory and the latter does not. Hence prefer the latter

Avoid ToLower() and ToUpper() in code (Remember strings are immutable)

–Writing code is easy, writing good code is not.  Learn, Implement, Share–

Python: Strings

April 6, 2009 Leave a comment

Hello everyone. I am back with Python again. Let’s try to learn it from the scratch. After reading this post, you will be understood about Strings in Python.

Strings are the basic unit of text in Python. Unlike some other programming languages, a single letter is represented as a one-letter string. Let’s try to understand it using some example code:code1

How It Works:

If you use different quotes, they may look different to you; to the Python interpreter; however all of them can be used in the same situations and are very similar.

Three different types of quotes are used in Python. First, there are the single and double quotes.

Python has one more special way of constructing strings, one that will almost always avoid the entire issue of requiring an escape character and will let you put in new lines as well: the triple quote. If you ever use a string enclosed in three quotes in a row—either single or double quotes, but all three have to be the same kind—then you do not have to worry about escaping any single instance of a single or double quote. Until Python sees three of the same quotes in a row, it won’t consider the string ended, and it can save you the need to use escape characters in some situations. See the 3rd line of code in the above figure.

Putting More than One Strings together:

To put more than one string together you can use ‘+’ operator. Suppose you want to put the two strings “Hello” and “Everyone” together. So use the following code:–

code21

Another way to specify strings is to use a format specifier. It works by putting in a special sequence of characters that Python will interpret as a placeholder for a value that will be provided by you.

code3That %s is the format specifier for a string. Several other specifiers are there. Each specifier acts as a placeholder for that type in the string; and after the string, the % sign outside of the string indicates that after it, all of the values to be inserted into the format specifier will be presented there to be used in the string.

Displaying Strings with Print:

For displaying text, a special feature is built into useful languages, one that helps the programmer display information to users. The basic way to do this in Python is by using the print function:

code4print is a function—a special name that you can put in your programs that will perform one or more tasks behind the scenes. Normally, you don’t have to worry about how it happens. In this case, the print function is an example of a built-in function, which is a function included as a part of Python, as opposed to a function that you or another programmer has written. The print function performs output—that is, it presents something to the user using a mechanism that they can see, such as a terminal, a window, a printer, or perhaps another device (such as a scrolling LED display).

In the next post I will discuss about the Numbers and Operators in Python.

Python: Experimenting with Variables and Expressions

March 30, 2009 Leave a comment

Python understand the standard logical operators including + , – , * , /. Take the following example:


>>> 8/2
4
>>> 5+4*6
29

Python uses operator precedence rules to decide what operation to do first. For this reason the 2nd example results 29 rather than 54. We can use parenthesis to control the order of operation:—

>>> (5+4)*6
54

Variables:

Variables can be used to hold values over time. The following code computes the total number of student in a school taking values from Morning Section and Day section:–

>>> Tot_morn=550
>>> Tot_day=800
>>> Tot_student=Tot_morn + Tot_day
>>> Tot_student
1350

A variable is always a reference to a value. Variables do not have types but objects do.

[Note: Python is a loosely typed language. The same variable name can be used to store integer at one time and string at another time. ]

Python doesn’t require variable declaration. But we can’t access a variable until we assigned a value to it. If we try to access a undefined variable, the following exception will come up:–
>>> print(A)
Traceback (most recent call last):
File “<pyshell#0>”, line 1, in <module>
Print(A)
Nameerror: name ‘A’ is not defined

In the above example we have tried to print the contents of the undefined variable A.
In Python, errors are represented by exception object that surrounding the code can handle.

Python is case sensitive. The two variables a1 and A1 are different.

Python: Starting the Python Interpreter

March 27, 2009 Leave a comment

After installing the Python Software, you can start the Python interpreter from the command line. Change to the directory where the interpreter lives, or add the directory to your path. Then type:

python

Once you start the interpreter, Python displays something like this:–

Python 3.1a1 (r31a1:70244, Mar 8 2009, 18:15:03) [MSC v.1500 32 bit (Intel)] on win32
Type “copyright”, “credits” or “license()” for more information.
>>>

>>> prompt implies that interpreter is ready for you to type in some Python.

Start with the HELLO WORLD example:–

>>>print(”Hello World!”)

Hello World!

To exit from the interpreter use exit()

>>>exit()

Python: Introduction

March 27, 2009 Leave a comment

Python is a general-purpose high-level programming language.

Python supports multiple programming paradigms (primarily object oriented, imperative, and functional) and features a fully dynamic type system and automatic memory management, similar to Perl, Ruby, Scheme, and Tcl. Like other dynamic languages, Python is often used as a scripting language.

The language has an open, community-based development model managed by the non-profit Python Software Foundation.

To learn Python programming language, one must have the python software which can be obtained from the following link:—

http://www.python.org/download/releases/

Please download the necessary releases.The latest is Pyhton 3.0.1

[Note: We are discussing about the python for windows only]