Python Data Persistence – Using range

Python Data Persistence – Using range

Python’s built-in range ( ) function returns an immutable sequence of numbers that can be iterated over by for loop. The sequence generated by the range ( ) function depends on three parameters.

The start and step parameters are optional. If it is not used, then the start is always 0 and the step is 1. The range contains numbers between start and stop-1, separated by a step. Consider an example 2.15:

Example

range (10) generates 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9

range ( 1 , 5 ) results in 1 , 2 , 3 , 4

range ( 20 , 30 , 2 ) returns 20 , 22 , 24 , 26 , 28

We can use this range object as iterable as in example 2.16. It displays squares of all odd numbers between 11-20. Remember that the last number in the range is one less than the stop parameter (and step is 1 by default)

Example

#for-3.py
for num in range( 11 , 21 , 2 ):
sqr=num*num
print ( ' sqaure of { } is { } ' . format( num , sqr ) )

Output:

E:\python37>python for-3.py 
square of 11 is 121 
square of 13 is 169 
square of 15 is 225 
square of 17 is 289 
square of 19 is 361

In the previous chapter, you have used len ( ) function that returns a number of items in a sequence object. In the next example, we use len ( ) to construct a range of indices of items in a list. We traverse the list with the help of the index.

Example

#for-4.py
numbers=[ 4 , 7 , 2 , 5 , 8 ]
for indx in range(len(numbers)):
sqr=numbers[indx]*numbers[indx]
print ( ' sqaure of { } is { } ' . format ( numbers [ indx ] , sqr ) )

Output:

E:\python3 7 >python for - 4.py 
sqaure of 4 is 16 
sqaure of 7 is 49 
sqaure of 2 is 4 
sqaure of 5 is 25 
sqaure of 8 is 64 

E:\python37>

Have a look at another example of employing for loop over a range. The following script calculates the factorial value of a number. Note that the factorial of n (mathematical notation is n!) is the cumulative product of all integers between the range of 1 to n.

Example

#factorial.py
n=int ( input ( " enter number . . " ) )
#calculating factorial of n
f = 1
for i in range ( 1 , n+1 ):
f = f * i
print ( ' factorial of { } = { } ' . format ( n , f ) )

Output:

E:\python37>python factorial.py 
enter number..5 
factorial of 5 = 120

Introduction to Python – Python, Pythonic, History, Documentation

In this Page, We are Providing Introduction to Python – Python, Pythonic, History, Documentation. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Introduction to Python – Python, Pythonic, History, Documentation

Python

Python is a high-level general-purpose programming language that is used in a wide variety of application domains. Python has the right combination of performance and features that demystify program writing. Some of the features of Python are listed below:

  • It is simple and easy to learn.
  • Python implementation is under an open-source license that makes it freely usable and distributable, even for commercial use.
  • It works on many platforms such as Windows, Linux, etc.
  • It is an interpreted language.
  • It is an object-oriented language.
  • Embeddable within applications as a scripting interface.
  • Python has a comprehensive set of packages to accomplish various tasks.

Python is an interpreted language, as opposed to a compiled one, though the distinction is blurry because of the presence of the bytecode compiler (beyond the scope of this book). Python source code is compiled into bytecode so that executing the same file is faster the second time (recompilation from source to bytecode can be avoided). Interpreted languages typically have a shorter development/debug cycle than compiled ones, and also their programs generally also run slowly. Please note that Python uses a 7-bit ASCII character set for program text.

The latest stable releases can always be found on Python’s website (http://www.python.org/). There are two recommended production-ready Python versions at this point in time because at the moment there are two branches of stable releases: 2.x and 3.x. Python 3.x may be less useful than 2.x since currently there is more third-party software available for Python 2 than for Python 3. Python 2 code will generally not run unchanged in Python 3. This book focuses on Python version 2.7.6.

Python follows a modular programming approach, which is a software design technique that emphasizes separating the functionality of a program into independent, interchangeable modules, such that each contains everything necessary to execute only one aspect of the desired functionality. Conceptually, modules represent a separation of concerns and improve maintainability by enforcing logical boundaries between components.

More information on the module is provided in chapter 5. Python versions are numbered in the format A.B.C or A.B, where A is the major version number, and it is only incremented for major changes in the language; B is the minor version number, and incremented for relatively lesser changes; C is the micro-level, and it is incremented for bug-fixed release.

Pythonic

“Pythonic” is a bit different idea/approach of the writing program, which is usually not followed in other programming languages. For example, to loop all elements of an iterable using for statement, usually, the following approach is followed:

food= [ 'pizza', 'burger',1 noodles']
for i in range(len(food)):
print(food[i])

A cleaner Pythonic approach is:

food=['pizza','burger','noodles']
for piece in food:
print(piece)

History

Python was created in the early 1990s by Guido van Rossum at Centrum Wiskunde & Informatica (CWI, refer http://www.cwi.nl/) in the Netherlands as a successor of a language called “ABC”. Guido remains Python’s principal author, although it includes many contributions from others. When he began implementing Python, Guido van Rossum was also reading the published scripts from “Monty Python’s Flying Circus”,.a BBC comedy series from the 1970s. Van Rossum thought he needed a name that was short, unique, and slightly mysterious, so he decided to call the language “Python”. In 1995, Guido continued his work on Python at the Corporation for National Research Initiatives (CNRI, visit http://www.cnri.reston.va.us/) in Reston, Virginia, where he released several versions of the software.

In May 2000, Guido and the Python core development team moved to “BeOpen.com” to form the BeOpen PythonLabs team. In October of the same year, the PythonLabs team moved to Digital Creations (now Zope Corporation, visit http://www.zope.com/). In 2001, the Python Software Foundation (PSF, refer http://www.python.org/psf/) was formed, a non-profit organization created specifically to own Python-related intellectual property. Zope Corporation is a sponsoring member of the PSF.

Documentation

Official Python 2.7.6 documentation can be accessed from the website link: http://docs.python.Org/2/. To download an archive containing all the documents for version 2.7.6 of Python in one of the various formats (plain text, PDF, HTML), follow the link: http://docs.python.Org/2/download.html.

Basics of Python – Built-in Types

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

Basics of Python – Built-in Types

Built-in types

This section describes the standard data types that are built into the interpreter. There are various built-in data types, for e.g., numeric, sequence, mapping, etc., but this book will cover few types. Schematic representation of various built-in types is shown in figure 2-1.

Python Handwritten Notes Chapter 2 img 1

Numeric types

There are three distinct numeric types: integer, floating-point number, and complex number.

Integer

Integer can be sub-classified into three types:

Plain integer

Plain integer (or simply ” integer “) represents an integer number in the range -2147483648 through 2147483647. When the result of an operation would fall outside this range, the result is normally returned as a long integer.

>>> a=2147483647 
>>> type (a) 
<type ' int '>
>>> a=a+1
>>> type (a)
<type ' long '>
>>> a=-2147483648 
>>> type (a)
<type ' int '>
>>> a=a-1
>>> type (a)
<type ' long ’>

The built-in function int(x = 0) converts a number or string x to an integer or returns 0 if no arguments are given.

>>> a =' 57 '
>>> type (a) 
<type ' str '>
>>> a = int (a)
>>> a 
57
>>> type (a)
<type ' int '>
>>> a = 5.7
>>> type (a)
<type ' float '> 
>>> a = int (a)
>>> a 
5 
>>> type (a)
<type ' int '>
>>> int( )
0

Long integer

This represents integer numbers in a virtually unlimited range, subject to available memory. The built-in function long (x=0) converts a string or number to a long integer. If the argument is a string, it must contain a possibly signed number. If no argument is given, OL is returned.

>>> a = 5 
>>> type (a) 
<type ' int '> 
>>> a = long (a) 
>>> a 
5L
>>> type (a) 
<type ' long '> 
>>> long ( )
OL
>>> long (5)
5L
>>> long (5.8) 
5L
>>> long(' 5 ') 
5L
>>> long(' -5 ')
-5L

Integer literals with an L or 1 suffix yield long integers (L is preferred because 11 looks too much like eleven).

>>> a=10L 
>>> type (a) 
<type ' long '>
>>> a=101 
>>> type (a)
<type ' long '>

The following expressions are interesting.

>>> import sys 
>>> a=sys.maxint 
>>> a
2147483647
>>> type (a)
<type ' int '>
>>> a=a+1
>>> a
21474836 48L
>>> type (a)
<type ' long ’>

Boolean

This represents the truth values False and True. The boolean type is a sub-type of plain integer, and boolean values behave like the values 0 and 1. The built-in function bool ( ) converts a value to boolean, using the standard truth testing procedure.

>>> bool ( ) 
False 
>>> a=5
>>> bool (a)
True
>>> bool (0)
False
>>> bool( ' hi ' )
True
>>> bool(None)
False
>>> bool(' ')
False
>>> bool(False)
False
>>> bool("False" )
True
>>> bool(5 > 3)
True

Floating point number

This represents a decimal point number. Python supports only double-precision floating-point numbers (occupies 8 bytes of memory) and does not support single-precision floating-point numbers (occupies 4 bytes of memory). The built-in function float  ( ) converts a string or a number to a floating-point number.

>>> a = 57 
>>> type (a)
<type ' int '>
>>> a = float (a)
>>> a
57.0 
>>> type (a)
<type ' float '>
>>> a = ' 65 ' 
>>> type (a)
<type ' str ' > 
>>> a = float (a) 
>>> a
65.0 
>>> type (a)
<type ' float '>
>>> a = 1e308 
>>> a 
1e+30 8 
>>> type (a)
<type ' float '>
>>> a = 1e309 
>>> a
inf 
>>> type (a)
<type ' float '>

Complex number

This represents complex numbers having real and imaginary parts. The built-in function complex () is used to convert numbers or strings to complex numbers.

>>> a = 5.3
>>> a = complex (a)
>>> a 
(5 . 3 + 0 j)
>>> type (a)
<type ' complex '>
>>> a = complex ( )
>>> a
0 j 
>>> type (a)
<type ' complex '>

Appending j or J to numeric literal yields a complex number.

>>> a = 3 . 4 j 
>>> a 
3 . 4 j
>>> type (a)
<type ' complex '>
>>> a = 3 . 5 + 4 . 9 j 
>>> type (a)
<type ' complex '>
>>> a = 3 . 5+4 . 9 J
>>> type (a)
<type ' complex '>

The real and imaginary parts of a complex number z can be retrieved through the attributes z. real and z. imag.

a=3 . 5 + 4 . 9 J
>>> a . real
3 . 5
>>> a . imag 
4 . 9

Sequence Types

These represent finite ordered sets, usually indexed by non-negative numbers. When the length of a sequence is n, the index set contains the numbers 0, 1, . . ., n-1. Item i of sequence a is selected by a [i]. There are seven sequence types: string, Unicode string, list, tuple, bytearray, buffer, and xrange objects.

The sequence can be mutable or immutable. The immutable sequence is a sequence that cannot be changed after it is created. If an immutable sequence object contains references to other objects, these other objects may be mutable and may be changed; however, the collection of objects directly referenced by an immutable object cannot change. The mutable sequence is a sequence that can be changed after it is created. There are two intrinsic mutable sequence types: list and byte array.

Iterable is an object capable of returning its members one at a time. Examples of iterables include all sequence types (such as list, str, and tuple) and some non-sequence types like diet and file, etc. Iterables can be used in a for loop and in many other places where a sequence is needed (zip(), map (), …). When an iterable object is passed as an argument to the built-in function iter (), it returns an iterator for the object. An iterator is an object representing a stream of data; repeated calls to the iterator’s next () method return successive items in the stream. When no more data are available, a Stoplteration exception is raised instead.

Some of the sequence types are discussed below:

String

It is a sequence type such that its value can be characters, symbols, or numbers. Please note that string is immutable.

>>> a=' Python : 2 . 7 '
>>> type (a)
<type ' str ’>
>>> a [2 ] =' S '
Traceback (most recent call last) :
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

The built-in function str (object =’ ‘ ) returns a string containing a nicely printable representation of an object. For strings, this returns the string itself. If no argument is given, an empty string is returned.

>>> a=57.3
>>> type(a) 
<type 'float'>
>>> a=str(a)
>>> a
' 57.3 '
>>> type (a)
<type ' str '>

Tuple

A tuple is a comma-separated sequence of arbitrary Python objects enclosed in parenthesis (round brackets). Please note that the tuple is immutable. A tuple is discussed in detail in chapter 4.

>>> a=(1 , 2 , 3 ,4) 
>>> type (a)
<type ' tuple '>
'a', 'b', 'c')

List

The list is a comma-separated sequence of arbitrary Python objects enclosed in square brackets. Please note that list is mutable. More information on the list is provided in chapter 4.

>>> a=[1, 2 ,3, 4]
>>> type(a) 
<type ' list '>

Set types

These represent an unordered, finite set of unique objects. As such, it cannot be indexed by any subscript, however, they can be iterated over. Common uses of sets are fast membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference. There are two set types:

Set

This represents a mutable set. It is created by the built-in function set (), and can be modified afterward by several methods, such as add () remove (), etc. More information on the set is given in chapter 4.

>>> set1=set ( )                                                    # A new empty set
>>> set1.add (" cat ")                                           # Add a single member
>>> set1.update ([" dog "," mouse "])                 # Add several members
>>> set1.remove ("mouse")                                # Remove member
>>> set1
set([' dog ', ' cat '])
>>> set2=set([" dog "," mouse "])
>>> print set1&set2                                           # Intersection
set ( [' dog ' ] )
>>> print set1 | set2                                           # Union
set([' mouse ', ' dog ', ' cat '])

The set ( [ iterable ] ) return a new set object, optionally with elements taken from iterable.

Frozenset

This represents an immutable set. It is created by a built-in function frozenset ( ). As a frozenset is immutable, it can be used again as an element of another set, or as a dictionary key.

>>> frozenset ( ) 
frozenset ( [ ] )
>>> frozenset (' aeiou ') 
frozenset{ [' a ', ' i ',' e ',' u ',' o '])
>>> frozenset ( [0, 0, 0, 44, 0, 44, 18] ) 
frozenset (10, 18, 44])

The frozenset ( [iterable] ) return return a new frozenset object, optionally with elements taken from iterable.

Mapping Types

This represents a container object that supports arbitrary key lookups. The notation a [k] selects the value indexed by key k from the mapping a; this can be used in expressions and as the target of assignments or del statements. The built-in function len () returns the number of items in a mapping. Currently, there is a single mapping type:

Dictionary

A dictionary is a mutable collection of unordered values accessed by key rather than by index. In the dictionary, arbitrary keys are mapped to values. More information is provided in chapter 4.

>>> dict1={"john":34,"mike":56}
>>> dict1[" michael "] = 42 
>>> dict1
{' mike ' : 56, ' john ' : 34, ' michael ' : 42}
>>> dictl[" mike "]
56

None

This signifies the absence of a value in a situation, e.g., it is returned from a function that does not explicitly return anything. Its truth value is False.

Some other built-in types such as function, method, class, class instance, file, module, etc. are discussed in later chapters.

Basics of Python – Line Structure

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

Basics of Python – Line Structure

Line structure

A Python program is divided into a number of logical lines.

Physical and logical lines

A physical line is a sequence of characters terminated by an end-of-line sequence. The end of a logical line is represented by the token NEWLINE. A logical line is ignored (i.e. no NEWLINE token is generated) that contains only spaces, tabs, or a comment. This is called a “blank line”. The following code

>>> i=5 
>>> print (5)
5

is the same as:

>>> i=5; print (i)
5

A logical line is constructed from one or more physical lines by following the explicit or implicit line joining rules.

Explicit line joining

Two or more physical lines may be joined into logical lines using backslash characters (\), as shown in the following example:

>>> if 1900 < year < 2100 and 1 <= month <= 12 \
. . .   and 1 <= day <= 31 and 0 <= hour < 24 \
. . .   and 0 <= minute < 60 and 0 <= second < 60 :
. . .   print year

A line ending in a backslash cannot carry a comment. Also, backslash does not continue a comment. A backslash does not continue a token except for string literals (i.e., tokens other than string literals cannot be split across physical lines using a backslash). A backslash is illegal elsewhere on a line outside a string literal.

>>> str=' This is a \
. . .    string example ' 
>>> str
' This is a string example'

Implicit line joining

Expressions in parentheses, square brackets, or curly braces can be split over more than one physical line without using backslashes. For example:

>>> month_names=['Januari','Februari','Maart',          # These are the
. . .    'April ', ' Mei ', 'Juni',                                              # Dutch names
. . .    'Juli','Augustus','September',                                 # for the months
. . .   'Oktober','November','December']                        # of the year

Implicitly continued lines can carry comments. The indentation of the continuation lines is not important. Blank continuation lines are allowed. There is no NEWLINE token between implicit continuation lines. Implicitly continued lines can also occur within triple-quoted strings; in that case, they cannot carry comments.

>>> str=" " "This is 
. . . a string 
. . . example" " "
>>> str
' This is \na string \nexample'
>>> str='' 'This is 
. . . a string 
. . . example'' '
>>> str
' This is \na string \nexample'

Comment

A comment starts with a hash character (#) that is not part of a string literal and terminates at the end of the physical line. A comment signifies the end of the logical line unless the implicit line joining rules are invoked. Also, comments are not executed.

Indentation

Whitespace is important in Python. Actually, whitespace at the beginning of the line is important. This is called indentation. Leading whitespace (spaces and tabs) at the beginning of the logical line is used to determine the indentation level of the logical line, which in turn is used to determine the grouping of statements. This means that statements that go together must have the same indentation. Each such set of statements is a block. One thing that should be remembered is that wrong indentation can give rise to error (IndentationError exception).

>>> i=10
>>> print "Value is ",i 
Value is 10 
>>> print "Value is ",i 
File "<stdin>", line 1
print "Value is ",i 
∧
IndentationError: unexpected indent

The indentation levels of consecutive lines are used to generate INDENT and DEDENT tokens. One can observe that inserting whitespace, in the beginning, gave rise to the IndentationError exception.

The following example shows non-uniform indentation is not an error.

var=100 
>>> if var!=100:
. . .  print 'var does not have value 100'
. . .  else:
. . .                 print 'var has value 100'
. . . 
var has value 100

Need for indentation

In the C programming language, there are numerous ways to place the braces for the grouping of statements. If a programmer is habitual of reading and writing code that uses one style, he or she will feel at least slightly uneasy when reading (or being required to write) another style. Many coding styles place begin/end brackets on a line by themselves. This makes programs considerably longer and wastes valuable screen space, making it harder to get a good overview of a program.

Guido van Rossum believes that using indentation for grouping is extremely elegant and contributes a lot to the clarity of the typical Python program. Since there are no begin/end brackets, there cannot be a disagreement between the grouping perceived by the parser and the human reader. Also, Python is much less prone to coding-style conflicts.

Basics of Python – String Operations

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

Basics of Python – String Operations

String operations

Some of the string operations supported by Python are discussed below.

Concatenation

Strings can be concatenated using + operator.

>>> word=' Python '+' Program '
>>> word
' Python Program '

Two string literals next to each other are automatically concatenated; this only works with two literals, not with arbitrary string expressions.

>>> ' Python ' ' Program '
' Python Program ' 
>>> word1=' Python '
>>> word2=' Program '
>>> word1 word2
File "<stdin>", line 1
word1 word2
                   ∧
SyntaxError: invalid syntax

Repetition

Strings can be repeated with the * operator.

>>> word=' Help '
>>> word * 3
' Help Help Help '
>>> 3 * word
' Help Help Help '

Membership operation

As discussed previously, membership operators in and not in are used to test for membership in a sequence.

>>> word=' Python '
>>> 'th' in word
True
>>> ' T ' not in word
True

Slicing operation

The string can be indexed, the first character of a string has sub-script (index) as 0. There is no separate character type; a character is simply a string of size one. A sub-string can be specified with the slice notation.

>>> word=' Python'
>>> word [2]
' t '
>>> word [0:2]
' Py '

String slicing can be in form of steps, the operation s [ i : j : k ] slices the string s from i to j with step k.

>>> word [1 :6 : 2]
' yhn '

Slice indices have useful defaults, an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.

>>> word [ : 2]
' Py '
>>> word [2 : ]
' thon '

As mentioned earlier, the string is immutable, however, creating a new string with the combined content is easy and efficient.

>>> word [0] = ' J '
Traceback (most recent call last) :
File "<stdin>", line 1, in <module>
TypeError : 'str' object does not support item assignment
>>> 'J ' + word[ 1 : ]
' Jython '

If the upper bound is greater than the length of the string, then it is replaced by the string size; an upper bound smaller than the lower bound returns an empty string.

>>> word [ 2 : 50 ]
'thon '
>>> word [ 4 : 1 ]
' '

Indices can be negative numbers, which indicates counting from the right-hand side.

>>> word [ -1 ]
' n '
>>> word [ -2 ]
' o '
>>> word [ -3 : ] 
' hon '
>>> word [ : -3 ]
' Pyt ' 
>>> word[-0]     # -0 is same as 0
' P '

Out-of-range negative slice indices are truncated, but a single element (non-slice) index raises the IndexError exception.

>>> word [ -100 : ]
' Python '
>>> word [ -100 ]
Traceback (most recent call last) :
File "<stdin>", line 1, in <module>
IndexError : string index out of range

String formatting

String objects have an interesting built-in operator called modulo operator (%). This is also known as the “string formatting” or “interpolation operator”. Given format%values (where a format is a string object), the % conversion specifications in format are replaced with zero or more elements of values. The % character marks the start of the conversion specifier.

If the format requires a single argument, values may be a single non-tuple object. Otherwise, values must be a tuple with exactly the number of items specified by the format string, or a single mapping object (a dictionary). If the dictionary is provided, then the mapping key is provided in parenthesis.

>>> ' %s Python is a programming language. ' % ' Python '
' Python Python is a programming language. '
>>> ' %s Python is a programming language. ' % ( ' Python ' )
' Python Python is a programming language.'
>>>
>>> ' %s has %d quote types. ' % ( ' Python ' , 2 )
' Python has 2 quote types. '
>>> ' %s has %03d quote types. ' % ( ' Python ' , 2 )
' Python has 002 quote types. '  
>>> ' %( language )s has % ( number ) 03d quote types.' % \
. . . { " language " : " Python " , " number " : 2 }
' Python has 002 quote types. '

In the above example, 03 consists of two components:

  • 0 is a conversion flag i.e. the conversion will be zero-padded for numeric values.
  • 3 is minimum field width (optional).
Conversion Meaning
D Signed integer decimal.
1 Signed integer decimal.
E Floating-point exponential format (lowercase).
E Floating-point exponential format (uppercase).
F Floating-point decimal format.
F Floating-point decimal format.
g Floating point format. Uses lowercase exponential format if the exponent is less than -4 or not less than precision, decimal format otherwise.
G Floating point format. Uses uppercase exponential format if the exponent is less than -4 or not less than precision, decimal format otherwise.
s String.

Precision (optional) is given as a dot (.) followed by the precision number. While using conversion type f, F, e, or E, the precision determines the number of digits after the decimal point, and it defaults to 6.

>>> " Today's stock price : %f " % 50
"Today's stock price: 50 000000"
>>> "Today's stock price : %F " % 50
"Today's stock price: 50 000000"
>>>
>>> "Today's stock price : %f " % 50.4625
"Today's stock price : 50 462500"
>>> "Today's stock price : %F " % 50.4625
"Today's stock price : 50 462500"
>>>
>>> "Today's stock price : %f " % 50.46251987624312359
"Today's stock price : 50. 462520"
>>> "Today's stock price : %F " % 50.46251987624312359
"Today's stock price : 50. 462520"
>>>
>>> "Today's stock price : %. 2f " % 50 . 4625
"Today's stock price : 50.46"
>>> "Today's stock price : %. 2F " % 50 . 4625
"Today's stock price : 50.46"
>>>
>>> "Today's stock price : %e " % 50 . 46251987624312359
"Today's stock price : 5 . 046252e+01 "
>>> "Today's stock price : %E " % 50 . 46251987624312359
"Today's stock price : 5 . 046252e+01 "
>>> "Today's stock price : % 3e " % 50 . 46251987624312359
"Today's stock price : 5 . 046e + 01 "
>>> "Today's stock price : % 3E " % 50 . 46251987624312359
"Today's stock price : 5 . 046E + 01 "

While using conversion type g or G, the precision determines the number of significant digits before and after the decimal point, and it defaults to 6.

>>> " Today's stock price : %g" % 50.46251987624312359
"Today's stock price: 50.4625"
>>> "Today's stock price: %G" % 50.46251987624312359
"Today's stock price: 50.4625"
>>>
>>> "Today's stock price: %g" % 0.0000005046251987624312359
"Today's stock price: 5.04625e-07"
»> "Today's stock price: %G" % 0.0000005046251987624312359
"Today's stock price: 5.04625E-07"

Python String Programs

Basics of Python – Operators and Operands

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

Basics of Python – Operators and Operands

Operators and operands

An operator is a symbol (such as +, x, etc.) that represents an operation. An operation is an action or •procedure that produces a new value from one or more input values called operands. There are two types of operators: unary and binary. The unary operator operates only on one operand, such as negation. On the other hand, the binary operator operates on two operands, which include addition, subtraction, multiplication, division, exponentiation operators, etc. Consider an expression 3 + 8, here 3 and 8 are called operands, while V is called operator. The operators can also be categorized into:

  • Arithmetic operators.
  • Comparison (or Relational) operators.
  • Assignment operators.
  • Logical operators.
  • Bitwise operators.
  • Membership operators.
  • Identity operators.

Arithematics operators

Table 2-2 enlists the arithmetic operators with a short note on the operators.

Operator

Description

+

Addition operator- Add operands on either side of the operator.

Subtraction operator – Subtract the right-hand operand from the left-hand operand.

*

Multiplication operator – Multiply operands on either side of the operator.

/

Division operator – Divide left-hand operand by right-hand operand.

%

Modulus operator – Divide left-hand operand by right-hand operand and return the remainder.

**

Exponent operator – Perform exponential (power) calculation on operands.

//

Floor Division operator – The division of operands where the result is the quotient in which the digits after the decimal point are removed.

The following example illustrates the use of the above-discussed operators.

>>> a=20 
>>> b=45.0
>>> a+b
65.0
>>> a-b
-25.0
>>> a*b
900.0 
>>> b/a 
2.25 
>>> b%a
5.0
>>> a**b
3.5184372088832e+58 
>>> b//a
2.0

Relational operators

A relational operator is an operator that tests some kind of relation between two operands. Tables 2-3 enlist the relational operators with descriptions.

Operator

Description

==

Check if the values of the two operands are equal.

!=

Check if the values of the two operands are not equal.

<>

Check if the value of two operands is not equal (same as != operator).

>

Check if the value of the left operand is greater than the value of the right operand.

<

Check if the value of the left operand is less than the value of the right operand.

>=

Check if the value of the left operand is greater than or equal to the value of the right operand.

<=

Check if the value of the left operand is less than or equal to the value of the right operand.

The following example illustrates the use of the above-discussed operators.

>>> a,b=20,40
>>> a==b
False 
>>> a!=b 
True
>>> a<>b 
True 
>>> a>b 
False 
>>> a<b 
True
>>> a>=b 
False 
>>> a<=b 
True

Assignment operators

The assignment operator is an operator which is used to bind or rebind names to values. The augmented assignment is the combination, in a single statement, of a binary operation and an assignment statement. An augmented assignment expression like x+=l can be rewritten as x=x+l. Tables 2-4 enlist the assignment operators with descriptions.

Operator

Description

=

Assignment operator- Assigns values from right side operand to left side operand.

+=

Augmented assignment operator- It adds the right-side operand to the left side operand and assigns the result to the left side operand.

-=

Augmented assignment operator- It subtracts the right-side operand from the left side operand and assigns the result to the left side operand.

*=

Augmented assignment operator- It multiplies the right-side operand with the left side operand and assigns the result to the left side operand.

/=

Augmented assignment operator- It divides the left side operand with the right side operand and assigns the result to the left side operand.

%=

Augmented assignment operator- It takes modulus using two operands and assigns the result to left side operand.

* *=

Augmented assignment operator- Performs exponential (power) calculation on operands and assigns value to the left side operand.

//=

Augmented assignment operator- Performs floor division on operators and assigns value to the left side operand.

The following example illustrates the use of the above-discussed operators.

>>> a,b=20,40 
>>> c=a+b 
>>> c 
60
>>> a,b=2.0,4.5 
>>>c=a+b
>>> C
6.5
>>> c+=a 
>>> c
8.5
>>> c-=a 
>>> c
6.5
>>> c*=a 
>>> c
13.0
>>> c/=a 
>>> c 
6.5
>>> c%=a 
>>> c 
0.5
>>> c**=a 
>>> c 
0.25
>>> c//=a 
>>> c 
0.0

Bitwise operators

A bitwise operator operates on one or more bit patterns or binary numerals at the level of their individual bits. Tables 2-5 enlist the bitwise operators with descriptions.

Operator

Description

&

Binary AND operator- Copies corresponding binary 1 to the result, if it exists in both operands.

|

Binary OR operator- Copies corresponding binary 1 to the result, if it exists in either operand.

Binary XOR operator- Copies corresponding binary 1 to the result, if it is set in one operand, but not both.

~

Binary ones complement operator- It is unary and has the effect of flipping bits.

<<

Binary left shift operator- The left side operand bits are moved to the left side by the number on the right-side operand.

>>

Binary right shift operator- The left side operand bits are moved to the right side by the number on the right-side operand.

The following example illustrates the use of the above-discussed operators.

>>> a,b=60,13 
>>> a&b 
12
>>> a | b
61 
>>> a∧b
49 
>>> ~a
-61 
>>> a< < 2
240 
>>> a>>2
15

In the above example, the binary representation of variables a and b are 00111100 and 00001101, respectively. The above binary operations example is tabulated in Tables 2-6.

Bitwise operation

Binary representation Decimal representation

a&b

00001100

12

a | b

00111101

61

ab

00110001

49

~a

11000011 -61
a<<2 11110000

240

a>>2 00001111

15

Logical operators

Logical operators compare boolean expressions and return a boolean result. Tables 2-6 enlist the logical operators with descriptions.

Operator

Description

and

Logical AND operator- If both the operands are true (or non-zero), then the condition becomes true.

or

Logical OR’operator- If any of the two operands is true (or non-zero), then the condition becomes true.

not

Logical NOT operator- The result is reverse of the logical state of its operand. If the operand is true (or non-zero), then the condition becomes false.

The following example illustrates the use of the above-discussed operators.

>>> 5>2 and 4<8 
True
>>> 5>2 or 4>8 
True
>>> not (5>2)
False

Membership operators

A membership operator is an operator which tests for membership in a sequence, such as string, list, tuple, etc. Table 2-7 enlists the membership operators.

Operator

Description

In

Evaluate to true, if it finds a variable in the specified sequence; otherwise false.

not in

Evaluate to true, if it does not find a variable in the specified sequence; otherwise false.
>>> 5 in [0, 5, 10, 15]
True 
>>> 6 in [0, 5, 10, 15]
False
>>> 5 not in [0, 5, 10, 15]
False 
>>> 6 not in [0, 5, 10, 15]
True

Identity operators

Identity operators compare the memory locations of two objects. Table 2-8 provides a list of identity operators including a small explanation.

Operator

Description

is

Evaluates to true, if the operands on either side of the operator point to the same object, and false otherwise.

is not

Evaluates to false, if the operands on either side of the operator point to the same object, and true otherwise.

The following example illustrates the use of the above-discussed operators.

>>> a=b=3.1
>>> a is b 
True 
>>> id (a)
3 0 9 8 4 5 2 8 
>>> id (b)
30984528 
>>> c,d=3.1,3.1 
>>> c is d 
False 
>>> id (c)
35058472 
>>> id (d)
30984592
>>> c is not d
True 
>>> a is not b
False

Operator precedence

Operator precedence determines how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator. In the expression x=7+3*2, x is assigned 13, not 20, because operator * has higher precedence than +, so it first multiplies 3*2 and then adds into 7.

Table 2-10 summarizes the operator’s precedence in Python, from lowest precedence to highest precedence (from top to bottom). Operators in the same box have the same precedence.

Operator
not, or, and
in, not in
is, is not
=, %, =/, =//, -=, +=, *=, **=
<>, ==, !=
<=, <, >, >=
∧, |
&
>>,<<
+, –
*, /, %, //
∼,+,-
**

Basics of Python – Regular Expression Module

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

Basics of Python – Regular Expression Module

Regular expression module

A regular expression (also called RE, or regex, or regex pattern) is a specialized approach in Python, using which programmers can specify rules for the set of possible strings that need to be matched; this set might contain English sentences, e-mail addresses, or anything. REs can also be used to modify a string or to split it apart in various ways.

Meta characters

Most letters and characters will simply match themselves. For example, the regular expression test will match the string test exactly. There are exceptions to this rule; some characters are special “meta characters”, and do not match themselves. Instead, they signal that some out-of-the-ordinary thing should be matched, or they affect other portions of the RE by repeating them or changing their meaning. Some of the meta characters are discussed below:

Meta character

Description

Example

[ ]

Used to match a set of characters.

[time]

The regular expression would match any of the characters t, i, m, or e.

[a-z]

The regular expression would match only lowercase characters.

Used to complement a set of characters. [time]

The regular expression would match any other characters than t, i, m or e.

$

Used to match the end of string only. time$

The regular expression would match time in ontime, but will not match time in timetable.

*

Used to specify that the previous character can be matched zero or more times. tim*e

The regular expression would match strings like timme, tie and so on.

+

Used to specify that the previous character can be matched one or more times. tim+e

The regular expression would match strings like timme, timmme, time and so on.

?

Used to specify that the previous character can be matched either once or zero times. tim ?e

The regular expression would only match strings like time or tie.

{ }

The curly brackets accept two integer values. The first value specifies the minimum number of occurrences and the second value specifies the maximum of occurrences. tim{1,4}e

The regular expression would match only strings time, timme, timmme or timmmme.

Regular expression module functions

Some of the methods of re module as discussed below:

re. compile ( pattern )
The function compile a regular expression pattern into a regular expression object, which can be used for matching using its match ( ) and search ( ) methods, discussed below.

>>> import re
>>> p=re.compile ( ' tim*e ' )

re. match ( pattern, string )
If zero or more characters at the beginning of the string match the regular expression pattern, match-( ) return a corresponding match object instance. The function returns None if the string does not match the pattern.

re. group ( )
The function return the string matched by the RE.

>>> m=re .match ( ' tim*e' , ' timme pass time ' )
>>> m. group ( )
' timme '

The above patch of code can also be written as:

>>> p=re. compile ( ' tim*e ' )
>>> m=p.match ( ' timme pass timme ' )
>>> m.group ( )
'timme'

re. search ( pattern, string )
The function scans through string looking for a location where the regular expression pattern produces a match, and returns a corresponding match object instance. The function returns None if no position in the string matches the pattern.

>>> m=re.search( ' tim*e ' ' no passtimmmeee ' )
>>> m.group ( )
' timmme '

The above patch of code can also be written as:

>>> p=re.compile ( ' tim*e ' )
>>> m=p.search ( ' no passtimmmeee ' )
>>> m.group ( )
' timmme '

re.start ( )
The function returns the starting position of the match.

re.end ( )
The function returns the end position of the match.

re.span ( )
The function returns a tuple containing the ( start, end ) indexes of the match.

>>> m=re.search ( ' tim*eno passtimmmeee ' )
>>> m.start ( )
7
>>> m.end ( )
13
>>> m.span ( )
( 7 , 13 )

The above patch of code can also be written as:

>>> p=re.compile ( ' tim*e ' )
>>> m=p.search ( ' no passtimmmeee ' )
>>> m.start ( )
7 
>>> m.end ( )
13
>>> m.span ( )
( 7 , 13 )

re. findall ( pattern, string )
The function returns all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found.

>>> m=re.findall ( ' tim*e ' , ' timeee break no pass timmmeee ' )
>>> m 
[ ' time ' , ' timmme ' ]

The above patch of code can also be written as:

>>> p=re . compile ( ' tim*e ' )
>>> m=p.findall ( ' timeee break no pass timmmeee ' )
>>> m
[ ' time ', ' timmme ' ]

re. finditer ( pattern, string )
The function returns an iterator yielding MatchObject instances over all non-overlapping matches for the RE pattern in a string. The string is scanned left-to-right, and matches are returned in the order found.

>>> m=re.finditer ( ' tim*e ', ' timeee break no pass timmmeee ' )
>>> for match in m :
. . .           print match.group ( )
. . .           print match.span ( )
time 
( 0 , 4 ) 
timmme 
( 21 , 27 )

The above patch of code can also be written as:

>>> p=re.compile( ' tim*e ' )
>>> m=p.finditer ( ' timeee break no pass timmmeee ' )
>>> for match in m :
. . .         print match.group ( )
. . .         print match.span ( )
time 
( 0 , 4 ) 
timmme
( 21 , 27 )

Basics of Python – String Methods

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

Basics of Python – String Methods

String methods

Below are listed some of the string methods which support both strings and Unicode objects. Alternatively, some of these string operations can also be accomplished using functions of the string module.

str.isalnum( )
Return True, if all characters in the string are alphanumeric, otherwise False is returned.

>>> str=" this2009 ”
>>> str.isalnum ( )
True
>>> str=" this is string example. . . .wow ! ! ! "
>>> str.isalnum( )
False

str. isalpha ( )
Return True, if all characters in the string are alphabetic, otherwise False is returned.

>>> str=" this "
>>> str.isalpha ( )
True
>>> str=" this is string example. . . .wow ! ! ! "
>>> str.isalpha ( )
False

str.isdigit( )
Return True, if all characters in the string are digits, otherwise False is returned.

>>> str="this2009" 
>>> str.isdigit ( )
False
>>> str=" 2009 "
>>> str.isdigit ( )
True

str . isspace ( )
Return True, if there are only whitespace characters in the string, otherwise False is returned.

>>> str= "    "
>>> str.isspace ( )
True
>>> str=" This is string example. . . .wow ! ! ! "
>>> str.isspace ( )
False

str.islower ( )
Return True, if all cased characters in the string are in lowercase and there is at least one cased character, false otherwise.

>>> str=" THIS is string example. . . .wow! ! ! "
>>> str.islower( )
False
>>> str=" this is string example. . . .wow ! ! ! "
>>> str.islower ( )
True
>>> str=" this2009 "
>>> str.islower ( )
True
>>> str=" 2009 "
>>> str.islower ( )
False

str.lower ( )
Return a string with all the cased characters converted to lowercase.

>>> str="THIS IS STRING EXAMPLE. . . .WOW ! ! ! "
>>> str.lower ( )
' this is string example. . . .wow ! ! ! '

The above can be imitated using the string module’s function string. lower (s), where s is a string.

>>> import string 
>>> str="THIS IS STRING EXAMPLE. . . .WOW ! ! ! " 
>>> string:lower(str)
' this is String example. . . .wow ! ! ! '

str.isupper ( )
Return True, if all cased characters in the string are uppercase and there is at least one cased character, otherwise False is returned.

>>> str="THIS IS STRING EXAMPLE. . . .WOW ! ! ! "
>>> str.isupper ( )
True
>>> str=" THIS is string example. . . .wow ! ! ! "
>>> str.isupper ( )
False

str.upper ( )
Return a string with all the cased characters converted to uppercase. Note that str . upper () . isupper () might be False, if string contains uncased characters.

>>> str=" this is string example. . . .wow ! ! ! "
>>> str.upper ( )
' THIS IS STRING EXAMPLE. . . . WOW ! ! ! '
>>> str.upper ( ) .isupper ( )
True
>>> str="@1234@"
>>> str.upper ( )
' @1234@ '
>>> str.upper ( ).isupper ( )
False

The above can be imitated using the string module’s function string. upper (s), where s is a string.

>>> str=" this is string example. . . .wow ! ! ! "
>>> string.upper (str)
' THIS IS STRING EXAMPLE. . . .WOW ! ! ! ’
>>> string.upper (str).isupper ( )
True 
>>> str="@1234@"
>>> string.upper(str)
'@1234@' 
>>> string.upper (str).isupper ( )
False

str.capitalize ( )
Return a string with its first character capitalized and the rest lowercase.

>>> str=" this Is stRing example. . . . wow ! ! ! "
>>> str.capitalize ( )
' This is string example. . . .wow ! ! ! '

The above can be imitated using the string module’s function string, capitalize (word), where the word is a string.

>>> str=" this Is stRing example. . . .wow ! ! ! "
>>> string.capitalize ( str )
' This is string example. . . .wow ! ! ! '

str.istitle ( )
Return True, if the string is title cased, otherwise False is returned.

>>> str=" This Is String Example. . .Wow ! ! ! "
>>> str . istitle ( )
True
>>> str=" This is string example. . . .wow ! ! ! "
>>> str.istitle ( )
False

str.title ( )
Return a title cased version of the string, where words start with an uppercase character and the remaining characters are lowercase. It will return unexpected results in cases where words have apostrophes etc.

>>> str=" this is string example. . . .wow ! ! ! "
>>> str.title ( )
' This Is String Example. . . .wow ! ! ! '
>>> str=" this isn't a float example. . . .wow ! ! ! "
>>> str.title ( )
" This Isn'T A Float Example. . . .Wow ! ! ! "

str.swapcase ( )
Return a copy of the string with reversed character case.

>>> str='' This is string example. . . .WOW ! ! ! "
>>> str . swapcase ( ) 
' tHIS IS STRING EXAMPLE. . . .wow ! ! ! '

The above can be imitated using the string module’s function string. swap case (s), where s is a string.

>>> str=" This is string example. . . .WOW ! ! ! "
>>> string, swapcase (str) 
' tHIS IS STRING EXAMPLE. . . .wow ! ! ! '

str.count(sub[,start[, end]])
Return the number of non-overlapping occurrences of sub-string sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.

>>> str=" this is string example. . . .wow ! ! ! "
>>> sub=" i "
>>> str.count (sub , 4 , 40)
2
>>> sub=" wow "
>>> str.count(sub)
1

The above can be imitated using string module’s function string. count (s, sub [, start [, end] ] ), where s is a string.

>>> str=" this is string example. . . .wow ! ! ! "
>>> sub=" i "
>>> string.count( str , sub , 4 ,40 )
2
>>> sub=" wow "
>>> string.count( str,sub )
1

str.find(sub[,start[,end]])
Return the lowest index in the string where the sub-string sub is found, such that sub is contained in the slice s [ start: end]. Optional arguments start and end are interpreted as in slice notation. Return -1, if the sub is not found.

>>> str1=" this is string example. . . .wow ! ! ! "
>>> str2=" exam "
>>> str1.find ( str2 )
15
>>> str1.find ( str2 , 10 )
15 
>>> str1.find ( str2 , 40 )
-1

The above can be imitated using the string module’s function string. find (s, sub [, start [, end] ]), where s is a string.

>>> str1=" this is string example. . . .wow ! ! ! "
>>> str2=" exam " 
>>> string.find ( str1 , str2 )
15 
>>> string.find(str1 , str2 , 10 )
15
>>> string.find( str1 , str2 , 40)
-1

The find ( ) method should be used only if there is a requirement to know the position of the sub. To check if the sub is a sub-string or not, use the in operator:

>>> ' Py ' in ' Python '
True

str.rfind(sub[,start[, end]] )
Return the highest index in the string where the sub-string sub is found, such that sub is contained within s [start: end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure.

>>> str1=" this is really a string example. . . .wow ! ! ! "
>>> str2=" is "
>>> strl.rfind ( str2 )
5
>>> str1.rfind ( str2 , 0 , 10 )
5
>>> strl.rfind ( str2 , 10 , 20 )
-1

The above can be imitated using the string module’s function string.rfind(s, sub [, start [, end] ] ), where s is a string.

>>> str1=" this is really a string example. . . .wow ! ! ! " 
>>> str2=" is "
>>> string.rfind ( str1 , str2 )
5
>>> string.rfind ( str1 , str2 , 0 , 10 )
5
>>> string.rfind ( str1 , str2 , 10 , 0 )
-1

str.index ( sub [ , start [ , end ] ] )
Like find ( ), but raise ValueError when the sub-string is not found.

>>> str1=" this is string example. . . .wow ! ! ! "
>>> str2=" exam "
>.> str1. index ( str2 ) 
15 
>>> str1.index ( str2 , 10 )
15 
>>> str1.index ( str2 , 40 )
Traceback ( most recent call last ) :
File "<pyshell#38>", line 1, in <module>
str1 . index ( str2 , 40 ) 
ValueError: substring not found

The above can’ be imitated using string module’s function string, index (s, sub [, start [, end] ] ), where s is a string.

>>> str1 = " this is string example. . . .wow ! ! ! " 
>>> str2=" exam "
>>> string.index ( str1 , str2 )
15
>>> string. index ( str1 , str2 ,10 )
15
>>> string, index ( str1 , str2 , 40 )
Traceback ( most recent call last ) :
File " <stdin> ", line 1, in <module>
File " C : \ Python27 \ lib \ string.py ", line 328, in index 
return s . index ( *args )
ValueError: substring not found

str.rindex ( sub [ , start [ , end ] ] )
Like rfind ( ), but raises ValueError when the sub-string sub is not found.

>>> str1="this is string example. . . .wow ! ! ! "
>>> str2=" is "
>>> strl.rindex ( str1 , str2 )
5
>>> str1.rindex ( str2 , 10 , 20 )
Traceback (most recent call last) : 
File " <stdin> ", line 1, in <module>
ValueError: substring not found

The above can be imitated using the string module’s function string. rfind ( s, sub [, start [, end] ] ), where s is a string.

>>> str1= " this is string example. . . .wow ! ! ! "
>>> str2=" is "
>>> string.rindex ( str1 , str2 )
5
>>> string.rindex ( str1 , str2 , 10 , 20)
Traceback (most recent call last) :
File "<stdin>", line 1, in <module>
File." C : \ Python27 \ lib \ string.py" , line 337, in rindex 
return s.rindex ( *args )
ValueError: substring not found

str.startswith (prefix [ , start [ , end ] ] )
Return True, if string starts with the prefix, otherwise return False. The prefix can also be a tuple of prefixes to look for. With optional start, test string beginning at that position. With an optional end, stop comparing string at that position.

>>> str=" this is string example. . . .wow ! ! ! "
>>> str.startswith( ' this ' )
True
>>> str.startswith( ' is ' , 2)
True
>>> str.startswith( ' this ' , 10 , 17 )
False
>>> pfx=( ' the ' , ' where ' , ' thi ' )
>>> str. startswith ( pfx )
True

str.endswith ( suffix [ , start [ , end ] ] )
Return True, if the string ends with the specified suffix, otherwise return False. The suffix can also be a tuple of suffixes to look for. The “est starts from the index mentioned by optional argument start. The comparison is stopped indicated by optional argument end.

>>> str=" this is string example. . . .wow ! ! ! "
>>> suffix=" wow ! ! ! "
>>> str.endswith ( suffix )
True
>>> suffix= ( " tot ! ! ! ", " wow ! ! ! " )
>>> str.endswith ( suffix )
True
>>> str.endswith ( suffix,20 )
True
>>> suffix= " is "
>>> str.endswith( suffix , 2 , 4 )
True
>>> str.endswith( suffix , 2 , 6 )
False
>>> str.endswith ( (' hey ’,' bye ',' w ! ! ! ' ) )
True

str. join ( iterable )
Return a string which is the concatenation of the strings in the iterable. The separator between elements is the string str providing this method.

>>> str="-" 
>>> seq= ( " a " , " b " , " c " )
>>> str. join ( seq )
' a-b-c '
>>> seq=[ " a " , " b " , " c " ]
>>> str.join (seq) 
' a-b-c '

The above can be imitated using the string module’s function string, join (words [, sep] ), where words is a list or tuple of strings, while the default value for sep is a single space character.

>>> str="-"
>>> seq= ( "a", "b", "c")
>>> string. join (seq, str)
'a-b-c'
>>> seq= [ ”a”, "b”, "c" ]
>>> string.join(seq,str) 
' a-b-c ’

str.replace ( old , new [ , count ] )
Return a string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

>>> str=" this is string example. . . . wow ! ! ! this is really string"
>>> str.replace (" is "," was ")
'thwas was string example. . . .wow ! ! ! thwas was really string’
>>> str=" this is string example. . . .wow ! ! ! this is really string "
>>> str.replace (" is "," was ", 3)
'thwas was string example. . . .wow ! ! ! thwas is really string '

The above can be imitated using the string module’s function string. replace (s, old, new [, max replace ] ), where s is a string and may replace is the same as count (discussed above).

>>> str=" this is string example. . . .wow ! ! ! this is really string"
>>> string.replace (str, " is "," was " )
'thwas was string example. . . .wow ! ! ! thwas was really string'
>>> str=" this is string example. . . .wow ! ! ! this is really string"
>>> string.replace (str," is "," was ", 3)
'thwas was string example. . . .wow ! ! ! thwas is really string'

str.center ( width [ , fillchar ] )
Return centered string of length width. Padding is done using optional argument fillchar (default is a space).

>>> str=" this is string example. . . .wow ! ! ! "
>>> str.center ( 40 ,' a ' )
' aaaathis is string example. . . .wow ! ! ! aaaa'
>>> str="this is string example. . . .wow ! ! ! "
>>> str.center (40)
' this is string example. . . .wow ! ! ! '

The above can be imitated using the string module’s function string, center (s, width [, fillchar ] ), where s is a string.

>>> str=" this is string example. . . .wow ! ! ! "
>>> string.center(str, 40 ,’ a ' )
'aaaathis is string example. . . .wow ! ! ! aaaa'
>>> str="this is string example .... wow!!!"
>>> string.center(str,40)
' this is string example. . . .wow ! ! ! '

str.1just ( width [ , fillchar ] )
Return the string left-justified in a string of length width. Padding is done using the optional argument fillchar (default is a space). The original string is returned if the width is less than or equal to len(str).

>>> str="this is string example. . . .wow! ! ! " 
>>> str . 1 just (50, ' 0 ' )
'this is string example. . . .wow ! ! !000000000000000000 '
>>> str=" this is string example. . . .wow ! ! ! "
>>> str.1just (50)
' this is string example. . . .wow ! ! ! '
>>> str="this is string example. . . .wow ! ! ! "
>>> str.1just (10)
' this is string example. . . .wow ! ! ! '

The above can be imitated using string module’s function string. 1 just (s, width [,fill char] ), where s is a string.

>>> str="this is string example. . . .wow ! ! ! "
>>> string.1just ( str , 50, ' 0 ' )
' this is string example. . . .wow ! ! ! 000000000000000000 '
>>> str=" this is string example. . . .wow ! ! ! "
>>> string.1just ( str , 50 )
'this is string example. . . .wow ! ! ! '

str.rjust ( width [ ,fillchar ] )
Return the right-justified string of length width. Padding is done using the optional argument fillchar (default is a space). The original string is returned if the width is less than or equal to len(str).

>>> str="this is string example. . . .wow ! ! ! "
>>> str.rjust ( 50 , ' 0 ' )
'000000000000000000this is string example. . . .wow ! ! ! '
>>> str=" this is string example. . . .wow ! ! ! "
>>> str.rjust ( 10 , ' 0 ' )
' this is string example. . . .wow ! ! ! '

The above can be imitated using the string module’s function string. rjust (s, width [, fillchar] ), where s is a string.

>>> str="this is string example. . . .wow ! ! ! "
>>> string .r just (str, 50, ’ 0 ’ )
' 000000000000000000this is string example. . . .wow ! ! ! '
>>> str="this is string example. . . .wow ! ! ! "
>>> string.rjust (str , 10 , ' 0 ' ) .
' this is string example. . . .wow ! ! ! '

str.zfill ( width )
Return a string of length width, having leading zeros. The original string is returned, if the width is less than or equal to len(str).

>>> str=” this is string example. . . .wow ! ! ! "
>>> str.zfill (40)
'00000000this is string example. . . .wow ! ! ! ' 
>>> str . zf ill ( 45)
' 0000000000000this is string example. . . .wow ! ! ! '

The above can be imitated using the string module’s function string, zfill (s, width), where s is a string.

>>> str=" this is string example. . . .wow ! ! ! "
>>> string . zfill ( str , 40 ) 
'00000000this is string example. . . .wow ! ! ! ’
>>> string . zfill ( str , 45 ) 
' 0000000000000this is string example. . . .wow ! ! ! '

str.strip ( [ chars ] )
Return a string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace.

>>> str=" 0000000this is string example. . . . wow ! ! ! 0000000 "
>>> str.strip ( ' 0 ' )
'this is string example. . . .wow ! ! ! '

The above can be imitated using the string module’s function string. strip (s [, chars ] ), where s is a string.

>>> str=" 0000000this is string example. . . .wow ! ! ! 0000000"
>>> string, strip (str, ’ 0 ’ )
'this is string example. . . .wow ! ! ! '

str.1strip ( [ chars ] )
Return a string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace.

>>> str=" this is string example. . . .wow ! ! ! "
>>> str.1strip ( ) 
'this is string example. . . .wow ! ! ! '
>>> str="88888888this is string example. . . .wow ! ! ! 8888888 "
>>> str.1strip ( ' 8 ' ) 
'this is string example. . . .wow ! ! ! 8888888 '

The above can be imitated using string module’s function string. lstrip (s [, chars] ), where s is a string.

>>> str="     this is string example. . . .wow ! ! !       "
>>> string.1strip (str)
' this is string example. . . .wow ! ! ! '
>>> str=" 888S8888this is string example. . . .wow ! ! ! 8888888 "
>>> string.1strip ( str , ' 8 ' )
' this is string example. . . .wow ! ! ! 8888888 '

str.rstrip ( [ chars ] )
Return a string with trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace.

>>> str=" this is string example. . . .wow ! ! ! "
>>> str.rstrip ( )
' this is string example. . . .wow ! ! ! '
>>> str="88888888this is string example. . . .wow ! ! ! 8888888 "
>>> str.rstrip (' 8 ')
' 88888888this is string example. . . .wow ! ! ! '

The above can be imitated using string module’s function string.rstrip (s [, chars] ), where s is a string.

>>> str="   this is string example. . . .wow ! ! ! "
>>> string.rstrip ( str )
' this is string example. . . .wow ! ! ! '
>>> str=  "88888888this is string example. . . .wow ! ! ! 8888888 "
>>> string.rstrip ( str,' 8 ' )
' 88888888this is string example. . . .wow ! ! ! '

str.partition(sep)
Split the string at the first occurrence of sep, and return a tuple containing the part before the separator, the separator itself, and the part after the separator.

>>> str="       this is string example. . . .wow ! ! !        "
>>> str.partition (' s ')
('        thi ' ,  ' s ' , ' is string example. . . .wow ! ! !       ' )

str.rpartition (sep)
Split the string at the last occurrence of sep, and return a tuple containing the part before the separator, the separator itself, and the part after the separator.

>>> str="         this is string example. . . .wow! ! !       "
>>> str.rpartition (' s ' )
(' this is ', ' s ', ' tring example. . . .wow ! ! ! ' )

str.split ( [ sep [ , maxsplit ] ] )
Return a list of the words from the string using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit + 1 elements). If maxsplit is not specified or -1, then all possible splits are made. If sep is not specified or None, any whitespace string is a separator.

>>> str="Line1-abcdef \nLine2-abc \nLine4-abcd"
>>> str . split ( )
['Line1-abcdef', ' Line2-abc ', ' Line4-abcd ' ]
>>> str.split(' ',1)
[' Line1-abcdef ', '\nLine2-abc \nLine4-abcd ' ]

The above can be imitated using string module’s function string, split (s [, sep [, maxsplit ] ] ), where s is a string.

>>> str="Line1-abcdef \nLine2-abc \nLine4-abcd"
>>> string.split (str)
[ ' Line1-abcdef ', ' Line2-abc ', ' Line4-abcd ' ]
>>> string. split ( str,'   ',1 )
[ ' Line1-abcdef ', ' \nLine2-abc \nLine4-abcd ' ]

str.rsplit ( [ sep [ , maxsplit ] ] )
Return a list of the words from the string using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done, the rightmost ones. If sep is not specified or None, any whitespace string is a separator. Except for splitting from the right, rsplit () behaves like split () which is described in detail below.

>>> str="       this is string example. . . .wow ! ! !       "
>>> str.rsplit ( )
['this', 'is', 'string', 'example. . . .wow ! ! ! ' ]
>>> str.rsplit ( ' s ' )
['       thi ', ' i ', '   ', ' tring example. . . .wow ! ! ! ' ]

The above can be imitated using string module’s function string. rsplit (s [, sep [, maxsplit ] ] ), where s is a string.

>>> str="          this is string example. . . . wow ! ! !        "
>>> string. rsplit ( str )
[ ' this ' ,  ' is ' , ' string ' , ' example. . . .wow ! ! ! ' ]
>>> string. rsplit ( str , ' s ' )
['          thi' ; ' i ', ' ', 'tring example. . . .wow ! ! !                 ' ]

str. splitlines ( [ keepends ] )
Return a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unless keeping is given.

>>> str="1ine1-a b c d e f\n1ine2- a b c\ n\n1ine4- a b c d "
>>> str.splitlines ( )
[ ' Line1-a b c d e f',   ' Line2- a b c ' , '   ' , ' Line4- a b c d ' ]
>>> str. splitlines (0)
[ ' Line1-a b c d e f ', ' Line2- a b c ', '    ', ' Line4- a b c d ' ]
>>> str.splitLines ( Fa1se )
[ ' Line1-a b c d e f ' , ' Line2- a b c ', '    ', ' Line4- a b c d ' ]
>>> str.sp1itLines (1)
[ ' Line1-a b c d e f\n ' , ' Line2- a b c\n', '\n', ' Line4- a b c d ' ]
>>> str.splitLines ( True )
[ ' Line1-a b c d e’ f\n', ' Line2- a b c\n', ' Line4- a b c d ' ]

str.translate ( table [ , deletechars ] )
Return a string having all characters occurring in the optional argument delete chars are removed, and the remaining characters have been mapped through the given translation table, which must be a string of length 256.

The maketrans ( ) function (discussed later in this section) from the string module is used to create a translation table. For string objects, set the table argument to None for translations that only delete characters.

>>> str=' this is string example. . . .wow ! ! ! '
>>> tab=string.maketrans ( ' e ' , ' E ' )
>>> str.translate ( tab )
' this is string ExamplE. . . .wow ! ! ! '
>>> str.translate ( tab , ' ir ' )
' ths s stng ExamplE. . . .wow ! ! ! '
>>> str.translate ( None , ' ir ' )
' ths s stng example. . . .wow ! ! ! '

The above can be imitated using string module’s function string. translate (s, table [, deletechars ] ), where s is a string.

>>> str=’ this is string example. . . .wow ! ! ! '
>>> tab=string.maketrans (' e ',' E ')
>>> string.translate ( str , tab )
' this is string ExamplE. . . .wow ! ! ! '
>>> string.translate ( str , tab ,' ir ' )
' ths s stng ExamplE. . . .wow ! ! ! '
>>> string.translate ( str , None ,' ir ' )
' ths s stng example. . . .wow ! ! ! '

The following functions are there in the string module but are not available as string methods.

string.capwords(s[,sep])
Split the argument s into words using str. split (), then capitalize each word using str. capitalize ( ), and join the capitalized words using str. join ( ). If the optional second argument sep is absent or None, runs of whitespace characters are replaced by a single space, and leading and trailing whitespace is removed, otherwise, sep is used to split and join the words.

>>> str=" this is string example. . . .wow ! ! ! ''
>>> string.capwords (str)
' This Is String Example. . . .Wow ! ! ! '
>>> string.capwords (str, '    ' )
' This Is String Example. . . .Wow ! ! !    '
>>> string, capwords ( str,' s ' )
' this is sTring example. . . . wow ! ! ! ' 

string.maketrans ( from , to )
Return a translation table suitable for passing to translate (), that will map each character in from into the character at the same position in to, from and to must have the same length.

>>> str=' this is string example. . . .wow ! ! ! '
>>> tab=string.maketrans ( ' t! ' , ' T. ' )
>>> string.translate ( str , tab )
'This is sTring example. . . .wow. . . '
>>> string.translate ( str , tab ,' ir ' )
' Ths s sTng example. . . .wow. . . '
>>> string.translate ( str , None , ' ir ' )
' ths s stng example. . . .wow ! ! ! '

Python String Programs

Basics of Python – String

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

Basics of Python – String

String

Python can manipulate string, which can be expressed in several ways. String literals can be enclosed in matching single quotes (‘) or double quotes (“); e.g. ‘hello’, “hello” etc. They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple- quoted strings), e.g. ‘ hello ‘, “hello”. A string is enclosed in double-quotes if the string contains a single quote (and no double quotes), else it is enclosed in single quotes.

>>> "doesnt"
'doesnt'
>>> "doesn't"
"doesn't"
>>> '"Yes," he said.'
' "Yes., " he said. '

The above statements can also be written in some other interesting way using escape sequences.

>>> "doesn\' t" ;
"doesn ' t”
‘>>> '\"Yes,\" he said.' 
' "Yes," he said.'

Escape sequences are character combinations that comprise a backslash (\) followed by some character, that has special meaning, such as newline, backslash itself, or the quote character. They are called escape sequences because the backslash causes an escape from the normal way characters are interpreted by the compiler/interpreter. The print statement produces a more readable output for such input strings. Table 2-12 mentions some of the escape sequences.

Escape sequence

Meaning

\n

Newline.
\t

Horizontal tab.

\v

Vertical tab.

 

Escape sequence

Meaning
W

A backslash (\).

Y

Single quote (‘).
\”

Double quote (“).

String literals may optionally be prefixed with a letter r or R, such string is called raw string, and there is no escaping of character by a backslash.

>>> str=' This is \n a string '
>>> print str 
This is 
a string
>>> str=r' This is \n a string '
>>> print str 
This is \n a string

Specifically, a raw string cannot end in a single backslash.

>>> str=r ' \ '
File "<stdin>", line 1 
str=r ' \ '
            ∧
SyntaxError: EOL while scanning string literal

Triple quotes are used to specify multi-line strings. One can use single quotes and double quotes freely within the triple quotes.

>>> line=" " "This is
. . . a triple
. . . quotes example" " "
>>> line
' This, is\na triple\nquotes example'
>>> print line
This is
a triple 
quotes example

Unicode strings are not discussed in this book, but just for the information that a prefix of ‘ u ‘ or ‘ U’ makes the string a Unicode string.

The string module contains a number of useful constants and functions for string-based operations. Also, for string functions based on regular expressions, refer to re module. Both string and re modules are discussed later in this chapter.

Python String Programs

Python Programming – Dictionary Methods

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

Python Programming – Dictionary Methods

Dictionary methods

The following are some dictionary methods supported by Python.

diet . clear ( )
Removes all items from the dictionary.

>>> a= { ' Name ' : ' Zara ' , ' Age ' : 7 }
>>> len ( a )
2 
>>> a . clear ( )
>>> len ( a )
0

diet. copy ( )
Returns a copy of the dictionary.

>>> dict1 = { ' Name ' : ' Zara ' , ' Age ' : 7 ]
>>> dict2=dict1 . copy ( )
>>> dict2
{ ' Age ' : 7 , ' Name ' : ' Zara ' }

diet. fromkeys ( seq [ , value ] )
Create a new dictionary with keys from seq and values set to value (default as None).

>>> seq= ( ' name ' , ' age ' , ' gender ' )
>>> a=dict . fromkeys ( seq )
>>> a 
{ ' gender ' : None , ' age ' : None , ' name ' : None }
>>> a=a.fromkeys ( seq , 10 )
>>> a
{ ' gender ' : 10 , ' age ' : 10 , ' name ' : 10 }

diet . get ( key [ , default ] )
Return the value for the key, if the key is in the dictionary, else default. If the default is not given, the default is None.

>>> a= { ' Name ' : ' Zara ' , ' Age ' : 7 }
>>> a.get ( ' Age ' )
7
>>> a. get ( ' Gender ' , ' Never ' )
' Never '
>>> print a.get ( ' Gender ' )
None

diet.has_key ( key )
Test for the presence of a key in the dictionary. Returns True, if the key is present in the dictionary, otherwise False.

>>> a= { ' Name ' : ' Zara ' , ' Age ' : 7 }
>>> a.has_key ( ' Age ' )
True
>>> a.has_key ( ' Gender ' )
False

diet.items ( )
Returns a list of dictionary’s key-value tuple pairs.

>>> a= { ' Name ' : ' Zara ' , ' Age ' : 7 } 
>>> a . items ( )
[ ( ' age ' , 7 ) , ( ' Name ' , ' Zara ' ) ]

diet.iteritems ( )
Returns an iterator over the dictionary’s key-value pairs.

>>> a= { ' Gender ' : ' Female ' , ' Age 1 : 7 , ' Hair color ' : None , ' Name ' : ' Zara ' } 
>>> for b in a . iteritems ( ) :
. . . print ' { 0 } - - - -{ 1 } ' . format ( b [ 0 ] , b [ 1 ] )
. . .
Gender - - - -Female 
Age - - - - 7 
Hair color- - - - None1
Name- - - - Zara

diet.iterkeys ( )
Returns an iterator over the dictionary’s keys.

>>> a= { 1 Gender 1 : ’ Female ' , ' Age ' : 7 , ' Hair color ' : None , ' Name ' : ' Zara ' }
>>> for b in a.iterkeys ( ) :
. . . print b
. . . 
Gender
Age 
Hair color 
Name

diet.itervalues ( )
Returns an iterator over the dictionary’s values.

>>> a= { 1 Gender ' : ' Female ' , ' Age ' : 7 , ' Hair color ' : None , ' Name ' : ' Zara ' }
>>> for b in a.itervalues ( ) :
. . . print b
. . .
Female
7
None
Zara

diet.keys ( )
Returns list of dictionary keys.

>>> a= { ' Name ' : ' Zara ' , ' Age ' : 7 }
>>> a . keys ( )
[ ' Age ' , ' Name ' ]

diet.pop ( key [ , default ] )
If key is.in the dictionary, remove it and return its value, else return default. If default is not given and key is not in the dictionary, a KeyError is raised.

>>> a= { ' Gender ' : ' Female ' , ' Age ' : 7 , ' Hair color ' : None , ' Name ' : ' Zara ' }
>>> a . pop ( ' Age ' , 15 )
7
>>> a . pop ( ' Age ' , 15 )
15 
>>> a
{ ' Gender ' : ' Female ' , ' Hair color ' : None , ' Name ' : ' Zara ' }

diet. popitem ( )
Remove and return an arbitrary key-value pair from the dictionary. If the dictionary is empty, calling popitem () raises an KeyError exception.

>>> a= { ' Gender ' : ' Female ' , ' Age ' : 7 , ' Hair color ' : None , ' Name ' : ' Zara ' } 
>>> a . popitem ( )
( ' Gender ' , ' Female ' )
>>> a . popitem ( )
( ' Age ' , 7 )
>>> a
{ ' Hair color ' : None , ' Name ' : ' Zara ' }

diet . setdefault ( key [ , default ] )
If key is in the dictionary, return its value. If not, insert key with a value of default and return default. The default defaults to None.

>>> a= { ' Name ' : ' Zara ' , ' Age ' : 7 }
>>> a . setdefault ( ' Age ' , 15 )
7
>>> a . setdefault ( ' Gender ' , ' Female ' )
' Female '
>>> a . setdefault ( ' Hair color ' )
>>> a
{ ' Gender ' : ' Female ' , ' Age ' : 7 , ' Hair color ' : None , ' Name ' : ' Zara ' }

diet.update ( [ other ] )
Update the dictionary with the key-value pairs from other, overwriting existing keys, and returns None. The method accepts either another dictionary object or an iterable of key-value pairs (as tuples or other iterables of length two). If keyword arguments are specified, the dictionary is then updated with those key-value pairs.

>>> dict1= { ' Name ' : ' Zara ' , ' Age ' : 7 }
>>> dict2= { ' Gender ' : ' female ' }
>>> dict1 . update ( dict2 )
>>> dict1 . update ( hair_color= ' black ' , eye_color= ' blue ' )
>>> dict1
{ ' eye_color ' : ' blue ' , ' Gender ' : ' female ' , ' Age ' : 7 , ' Name ' : ' Zara ' , ' hair_color ' : ' black ' }

diet . values ( )
Return list of dictionary values.

>>> a= { ' Name ' : ' Zara ' , ' Age ' : 7 }
>>> a . values ( )
[ 7, ' Zara ' ]