Wednesday 8 January 2014

Operators and operands

Operators are special symbols that represent computations like addition and multiplication. The values the operator is applied to are called operands. The operators +, -, *, / and ** perform addition, subtraction, multiplication, division and exponen-tiation, as in the following examples:

20+32 hour-1 hour*60+minute minute/60 5**2 (5+9)*(15-7)

In some other languages, ˆ is used for exponentiation, but in Python it is a bitwise operator called XOR. I won’t cover bitwise operators in this book, but you can read about them at wiki.python. org/moin/BitwiseOperators. The division operator might not do what you expect:

>>> minute = 59
>>> minute/60
0

The value of minute is 59, and in conventional arithmetic 59 divided by 60 is 0.98333, not 0. The reason for the discrepancy is that Python is performing floor division When both of the operands are integers, the result is also an integer; floor division chops off the fraction part, so in this example it rounds down to zero. If either of the operands is a floating-point number, Python performs floating-point division, and the result is a float:

>>> minute/60.0
0.98333333333333328

Arithmetic Expressions

An arithmetic expressionconsists of operands and operators combined in a man-ner that is already familiar to you from learning algebra. Table  shows several arithmetic operators and gives examples of how you might use them in Python code.


In algebra, you are probably used to indicating multiplication like this: ab. However, in Python, we must indicate multiplication explicitly, using the multi-plication operator (*), like this: a * b. Binary operators are placed between their operands (a * b, for example), whereas unary operators are placed before their
operands (-a, for example).


Program Comments and Docstrings

In Python we double quotation (") or single quotation mark for Docstrings  and pound sign(#) for single line comment.

"""
This is a python comment
This is a python comment
This is a python comment
"""

# Assign a value to radius
#this is also python comment
radius = 20# radius is now 20 radius 20

# Compute area
area = radius * radius * 3.14159

# Display results
print("The area for the circle of radius", radius, "is", area)

String Concatenation in python

You can join two or more strings to form a new string using the concatenation operator +. Here is an example:

>>> "sony" + " biswas" + " nitol"
'sony biswas nitol'

plus (+) sign use for concatenate two or more string

Drawing the Olympic Rings Logo in python

Here we Have just use simple graphical programming for make some fun, There are many ways to write graphics programs in Python. A simple way to start graphics programming is to use Python’s built-in turtlemodule. Later in the book, we will introduce Tkinterfor developing comprehensive graphical user interface applications.

1 import turtle
2
3 turtle.color("blue")
4 turtle.penup()
5 turtle.goto(-110, -25)
6 turtle.pendown()
7 turtle.circle(45)
8
9 turtle.color("black")
10 turtle.penup()
11 turtle.goto(0, -25)
12 turtle.pendown()
13 turtle.circle(45)
14
15 turtle.color("red")
16 turtle.penup()
17 turtle.goto(110, -25)
18 turtle.pendown()
19 turtle.circle(45)
20
21 turtle.color("yellow")
22 turtle.penup()
23 turtle.goto(-55, -75)
24 turtle.pendown()
25 turtle.circle(45)
26
27 turtle.color("green")
28 turtle.penup()
29 turtle.goto(55, -75)
30 turtle.pendown()
31 turtle.circle(45)
32
33 turtle.done()

Programming error in python

There are Three types of Error in Python Programming .Those are :

1) Syntax Errors
2) Runtime Errors
3) Logic Errors


1) Syntax Errors

The most common erroryou will encounter are syntax errors. Like any programming lan-guage, Python has its own syntax, and you need to write code that obeys the syntax rules. If your program violates the rules—for example, if a quotation mark is missing or a word is misspelled—Python will report syntax errors.

Syntax errorsresult from errors in code construction, such as mistyping a statement, incor-rect indentation, omitting some necessary punctuation, or using an opening parenthesis with-out a corresponding closing parenthesis. These errors are usually easy to detect, because Python tells you where they are and what caused them. For example, the following print statement has a syntax error:




2) Runtime Errors

Runtime errorsare errors that cause a program to terminate abnormally. They occur while aprogram is running if the Python interpreter detects an operation that is impossible to carry out. Input mistakes typically cause runtime errors. Aninput error occurs when the user enters a value that the program cannot handle. For instance, if the program expects to read in a number, but instead the user enters a string of text, this causes data-type errors to occur in the program.

Another common source of runtime errors is division by zero. This happens when the divi-sor is zero for integer divisions. For example, the expression 1 / 0in the following statement would cause a runtime error.



3) Logic Errors :

Logic errorsoccur when a program does not perform the way it was intended to. Errors of this kind occur for many different reasons. For example, suppose you wrote the program in Listing 1.4 to convert a temperature (35 degrees) from Fahrenheit to Celsius.


You will get Celsius –12.55degrees, which is wrong. It should be 1.66. To get the correct result, you need to use 5 / 9 * (35 – 32)rather than 5 / 9 * 35 – 32in the expression. That is, you need to add parentheses around (35 – 32)so Python will calculate that expression first before doing the division.