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
A token is a string of one or more characters that is significant as a group. Consider an expression:
The tokens in the above expression are given in table 2-1:
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.
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
. . .
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
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.
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.
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
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