Author name: Prasanna

Python Programming – Basics of Python

In this Page, We are Providing Python Programming – Basics of Python. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Python Programming – Basics of Python

Token

A token is a string of one or more characters that is significant as a group. Consider an expression:

sum=6+2

The tokens in the above expression are given in table 2-1:

Token

Token type

Sum

Identifier

=

Assignment operator

6

Integer literal

+

Addition operator

2

Integer literal

The process of converting a sequence of characters into a sequence of tokens is called “lexical analysis”. A program or function that performs lexical analysis is called a lexical analyzer, lexer, or tokenizer. A lexer is generally combined with a parser (beyond the scope of this book), which together analyze the syntax of computer language. Python supports the following categories of tokens: NEWLINE, INDENT, DEDENT, identifiers, keywords, literals, operators, and delimiters.

Keywords

The following identifiers (as shown as output in the following code) are used as reserved words (or “keywords”) of the language, and cannot be used as ordinary identifiers.

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

One can also check if an identifier is a keyword or not using its keyword ( ) function.

>>> import keyword
>>> keyword . iskeyword ( ' hi ' )
False
>>> keyword . iskeyword ( ' print ' )
True

Delimiters

The delimiter is a character that separates and organizes items of data. An example of a delimiter is the comma character, which acts as a field delimiter in a sequence of comma-separated values. Table 2-11 provides a list of tokens that serves as delimiters in Python.

Delimiters

()[]@{},:.;=
+=-=*=/=//=%=&=l=∧=>>=<<=**=

The following example shows how the use of delimiters can affect the result.

 

>>> 5+6/2                                         # no delimiter used
8 . 0
>>> (5+6)/2                                      # delimiter used
5 . 5

Following are few points that a Python programmer should be aware of:

  • The period (.) can also occur in floating-point and imaginary literals.
  • The simple and augmented assignment operators, serve lexically as delimiters but also perform operations.
  • ASCII characters “, #, and \ have special meaning as part of other tokens or are otherwise significant to the lexical analyzer.
  • Whitespace is not a token but serves to delimit tokens.

Integer function

The following function operates on integers (plain and long).

int.bit_length ( )
Return the number of bits necessary to represent an integer (plain or long) in binary, excluding the sign and leading zeros.

>>> n=-37
>>> bin(n)       # bin ( ) convert' integer number to a binary string
' -0b100101 ' 
>>> n.bit_length ( )
6 
>>> n=2**31 
>>> n
2147483648L 
>>> bin(n)
'0b10000000000000000000000000000000'
>>> n.bit_length ( )
32

Float functions

Some of the functions for floating-point numbers are discussed below.

float.as_integer_ratio ( )
Return a pair of integers whose ratio is exactly equal to the original float and with a positive denominator.

>>> ( -0 .25 ) . as_integer_ratio ( )
(-1 , 4)

float.is_integer ( )
Return True if the float instance is finite with integral value, otherwise it return False.

>>> (-2 . 0) . is_integer ( )
True
>>> (3 . 2) . is_integer ( )
False

Python Programming – Basics of Python Read More »

Basics of Python – Error

In this Page, We are Providing Basics of Python – Error. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Basics of Python – Error

Error

An error (or software bug) is a fault in a computer program that produces an incorrect or unexpected result or causes it to behave in unintended ways. Most bugs arise from mistakes and errors made by people in either a program’s source code or its design. Usually, errors are classified as: syntax error, run-time error, and logical error.

Syntax error

Syntax error refers to an error in the syntax of tokens and/or sequence of tokens that are intended to be written in a particular programming language. For compiled languages, syntax errors occur strictly at compile-time. A program will not compile until all syntax errors are corrected. For interpreted languages, however, not all syntax errors can be reliably detected until run-time.

>>> prin ' Hi '
SyntaxError: invalid syntax 
>>> print " Hi '
SyntaxError: EOL while scanning string literal

Run-time error

A run-time error is an error that can be detected during the execution of a program. The code appears to be correct (it has no syntax errors), but it will not execute. For example, if a programmer has written a correct code to open a file using the open ( ) function, and if the file is corrupted, the application cannot carry out the execution of the open ( ) function, and it stops running.

Logical error

A logical error (or semantic error) is a bug in a program that causes it to operate incorrectly, but not terminate abnormally. A logical error produces an unintended or undesired output or other behavior, although it may not immediately be recognized. The logic error occurs both in compiled and interpreted languages.

Unlike a program with a syntax error, a program with a logical error is a valid program in the language, though it does not behave as intended. The only clue to the existence of logic errors in the production of wrong solutions. For example, if a program calculates the average of variables a and b, instead of writing the expression c= (a+b) / 2, one can write c=a+b / 2, which is a logical error.

>>> print a+b / 2 
6 . 5
>>> print ( a+b ) / 2
5 . 0

Basics of Python – Error Read More »