Wednesday, 8 January 2014

Variable names and keywords in python

Programmers generally choose names for their variables that are meaningful—they document what the variable is used for.

Variable names can be arbitrarily long. They can contain both letters and numbers, but they have to begin with a letter. It is legal to use uppercase letters, but it is a good idea to begin variable names with a lowercase letter.

The underscore character (_) can appear in a name. It is often used in names with multiple words, such as my_name or airspeed_of_unladen_swallow.

If you give a variable an illegal name, you get a syntax error:
>>> 76trombones = 'big parade'
SyntaxError: invalid syntax
>>> more@ = 1000000
SyntaxError: invalid syntax
>>> class = 'Advanced Theoretical Zymurgy'
SyntaxError: invalid syntax

76trombones is illegal because it does not begin with a letter. more@ is illegal because it contains an illegal character, @. But what’s wrong with class?

It turns out that class is one of Python’s keywords. The interpreter uses keywords to recognize the structure of the program, and they cannot be used as variable names.


You might want to keep this list handy. If the interpreter complains about one of your variable names and you don’t know why, see if it is on this list

Using Python to Perform Mathematical Computations

Python programs can perform all sorts of mathematical computations and display the result. To display the addition, subtraction, multiplication, and division of two numbers, xand y, use the following code:
print(x + y)
print(x – y)
print(x * y)
print(x / y)

1 # Compute expression
>>> print((150-3.5) + (542-6))
682.5

>>> print((150-3.5) - (542-6))
-389.5

>>> print((150-3.5) * (542-6))
78524.0

>>> print((150-3.5) / (542-6))
0.2733208955223881
>>>



Values and types in python

A value is one of the basic things a program works with, like a letter or a number. The values we have seen so far are 1, 2, and 'Hello, World!'.

These values belong to different types: 2 is an integer, and 'Hello, World!' is a string, so-called because it contains a “string” of letters. You (and the interpreter) can identify strings because they are enclosed in quotation marks.

The print statement also works for integers.
>>> print(4)
4

If you are not sure what type a value has, the interpreter can tell you.
>>> type('hello, world')
<class 'str'>
>>> type (4)
<class 'int'>
>>> type ('17')
<class 'str'>

Not surprisingly, strings belong to the type str and integers belong to the type int. Less obviously, numbers with a decimal point belong to a type called float, because these numbers are represented in a format called floating-point.
>>> type(3.2)
<class 'float'>

What about values like '17' and '3.2'? They look like numbers, but they are in quotation marks
like strings.
>>> type('17')
<class 'str'>
>>> type('3.2')
<class 'str'>
They’re strings.

When you type a large integer, you might be tempted to use commas between groups of three digits, as in 1,000,000. This is not a legal integer in Python, but it is legal:
>>> print (1,000,000)
1 0 0

Well, that’s not what we expected at all! Python interprets 1,000,000 as a comma-separated se-quence of integers, which it prints with spaces between. This is the first example we have seen of a semantic error: the code runs without producing an error message, but it doesn’t do the “right” thing.

The first program with explanation

Traditionally, the first program you write in a new language is called “Hello, World!” because all it does is display the words, “Hello, World!” In Python, it looks like this:

>>>print ("python is fun")
python is fun

This is an example of a print statement, which doesn’t actually print anything on paper. It displays
a value on the screen. In this case, the result is the words

python is fun

The quotation marks in the program mark the beginning and end of the text to be displayed; they don’t appear in the result. Some people judge the quality of a programming language by the simplicity of the “python is fun!” program. By this standard, Python does about as well as possible.

Tuesday, 7 January 2014

Behind the Scenes: How Python Works

Whether you are running Python code as a script or interactively in a shell, the Python interpreter does a great deal of work to carry out the instructions in your program. This work can be broken into a series of steps, as shown in Figure.


1 The interpreter reads a Python expression or statement, also called the source code, and verifies that it is well formed. In this step, the inter-preter behaves like a strict English teacher who rejects any sentence that
does not adhere to the grammar rules, or syntax,of the language. As soon as the interpreter encounters such an error, it halts translation with an error message.

2. If a Python expression is well formed, the interpreter then translates it to an equivalent form in a low-level language called byte code. When the interpreter runs a script, it completely translates it to byte code.

3. This byte code is next sent to another software component, called the Python virtual machine (PVM), where it is executed. If another error occurs during this step, execution also halts with an error message.

Write some code in Python

We are going to write our first program in python, before we write our we have to know some thing, first of all we need a python software or python shell or python IDE. Click Here to download python shell.

Install it like other software .do the following stuff

1.Go to start menu
2.All program
3.clickpython 3.3
4.Then click python Idle.

There write the following Code

>>>3+4
7
>>>3
3
>>>“Python is really cool!”
'Python is really cool!'
>>> name = “Ken Lambert”
>>> name
'Ken Lambert'
>>> “Hi there “ + name
'Hi there Ken Lambert'
>>> print 'Hi there'
Hi there
>>> print “Hi there”, name
Hi there Ken Lambert

We will Discuss all those thing later. To quit the Python shell, you can either select the window’s close box or press the Control+D key combination.

Page(3)PREV 123 NEXT