Python

Python Data Persistence – Deleting Rows

Python Data Persistence – Deleting Rows

Similarly, we can programmatically delete a certain record from a table. This operation is also more often than not conditional. Hence, the ‘WHERE’ clause appears in the parameterized DELETE query. Following script (deleteqry.py) deletes row belonging to the user-specified product.

Example

import sqlite3
conn=sqlite3.connect('mydb.sqlite')
nm=input('enter product to delete:1)
qry='delete from Products where name=?'
cur=conn.cursor( )
try:
cur.execute(qry, (nm,))
print ('record deleted')
conn.commit ( )
except:
print ('error in delete operation')
conn.rollback( )
conn.close( )

To delete the user input product, run the above script from the command prompt.

E:\python37>python deleteqry.py 
enter product to delete: Printer 
record deleted

Execute select query in SQLite console to verify that deleted product doesn’t appear in the list.

sqlite> select * from products;
ProductID      Name         Price
---------        ---------      --------
    1                Laptop        25000
    2                   TV           32000
    3              Router           2000
   4               Scanner        5000
   6               Mobile          15000

The next section explains how to programmatically retrieve records from a table.

Python Data Persistence – Deleting Rows Read More »

Python Data Persistence – ResultSet Object

Python Data Persistence – ResultSet Object

We need to call execute ( ) method on cursor object with a SELECT query string as its argument and a query result set is built which is similar to an iterator of rows returned in response to the query. The module provides the following methods to traverse the result set:

fenchone (): Next row in the query result set is retrieved and returned in the form of a tuple. The method returns None if there are no more rows to be fetched,

fetchall (): This method returns a list of all available rows in the result set. It contains a sequence corresponding to each row. You can employ a regular for loop to traverse the rows, as follows:

Example

import sqlite3
conn=sqlite3.connect('mydb.sqlite')
cur=conn.cursor()
qry="select * from Products;"
cur.execute(qry)
rows=cur.fetchall()
for row in rows:
        print (row) 
conn.close()

Run the above code (‘ selectqry. py’) from the command prompt.

E:\python37>python selectqry.py
(1,  'Laptop', 25000)
(2, ’TV', 40000)
(3, 'Router', 2000)
(4, 'Scanner' , 5000)
(5, 'Printer' , 9000)
(6, 'Mobile', 15000)

The fact that execute () method runs a parameterized query can be used to good effect to search for a certain condition in the table. The following code snippet accepts the product’s name as input and displays its price.

Example

import sqlite3
conn=sqlite3.connect('mydb.sqlite') nm=input ('Enter name of product:') cur=conn.cursor( )
qry="select * from Products where name=?";
cur.execute(qry, (nm,))
row=cur.fetchone( )
print (row)
conn.close( )

When the above script is run from the command prompt, then it above script shows the following output:

E:\python37>python selecttqry.py 
Enter name of product:TV 
(2, 'TV', 40000)

Individual items in the ‘row’ tuple can be accessed by index. The row can also be unpacked in separate variables as under:

Example

row=cur fetchone( ) 
print ( ' ID: ', row [ 0] , 'Name: 1, row[1], 'price:', row [2 ] ) 
id, nm, p=row 
print ( ID:', id, 'Name:', nm, 1 price: ' , p)

Python Data Persistence – ResultSet Object Read More »

Python Data Persistence – User Defined Functions

Python Data Persistence – User Defined Functions

The SQLite database engine by itself is equipped with several built-in functions for finding string length, changing case to upper/lower case, rounding a number, etc. However, it doesn’t have the provision to define a new function with customized functionality. The SQLite module, however, has the provision to do so’ with the help of the create_function () method available to the connection object.

In the following example, we try to represent the price of the product rounded to thousands and attach a ‘k‘ alphabet to it. In other words, 40000 is represented by 40k. First, we define a regular Python function (myfunction) that accepts a number, divides it by 1000, and appends ’k’ to its string conversion. The create_f unction () method has the following prototype:

Example

create_function(SQLFunction, parameters,PythonFunction)

In other words, it assigns a name to the Python function(a function in our case) that can be used as a function in the SQL query.

Example

import sqlite3
conn=sqlite3.connect('mydb.sqlite') def myfunction(num):
return str(round(num/1000))+"k" conn.create_function('priceinK' , 1,myfunction) cur=conn.cursor()
qry="select name, priceinK(price) from products;"
cur.execute(qry)
rows=cur.fetchall( )
print (rows)
conn.close ( )

Output of above code snippet is:

Example

[('Laptop', '25k'), ('TV', '40k'), ('Router', '2k'),
('Scanner', '5k'), ('Printer', '9k'), ('Mobile',
'15k') ]

SQLite also has several built-in aggregate functions such as SUM, AVG, COUNT, etc. to be applied to one or more columns in a table. For example, the query selects SUM (price) from Products’returns sum of values in the price column of all rows. Using the create_aggregate() method defined to be used with the cursor object, it is possible to define a customized aggregate function.

In the following script, a regular Python class named my class s is defined and it contains a step( ) method which is mandatory for the user-defined aggregate function. The step() method increments the count for each product name ending with ‘r\ The create_aggregate () method attaches a name that can be used in the SQL query. When this aggregate function is called, the value returned by finalize () method of the class is in fact the result of the SELECT statement.

Example

import sqlite3
conn=sqlite3.connect('mydb.sqlite')
class myclass:
def__init__(self):
self.count=0
def step(self, string):
if string.endswith('r'):
self.count=self.count+1
def finalize (self) :
return self.count
conn.create_aggregate(1MyF',1,myclass)
cur=conn.cursor()
qry="select MyF(name) from products;"
cur.execute(qry)
row=cur.fetchone()
print ('number of products with name ending with ' r1 : ' , (row) [0] )
conn.close()

The output of the above script is:

Example

number of products with the name ending with : 3

 

Python Data Persistence – User Defined Functions Read More »

Python Data Persistence – Backup and Restore Database

Python Data Persistence – Backup and Restore Database

It is extremely important to secure an organization’s data with periodic backup so that the same can be used to fall back in case of any damage. The sqlite3 module provides iterdump ( ) function that returns an iterator of the entire data of a database in the form of SQL statements. This includes CREATE TABLE statements corresponding to each table in the database and INSERT statements corresponding to rows in each table.

Let us demonstrate the effect of iterdump ( ) with the following example. First, we create a database with one table and insert a record in it. After that, we create a dump of the database. Run the following script and open the resultant backup.sql file with an editor.

Example

import sqlite3
conn=sqlite3.connect('sample.db')
qry='create table names (name text (20), address
text(20));'
conn.execute(qry)
qry="insert into names values('Anurag', 'Mumbai');"
cur=conn.cursor()
try:
cur.execute(qry) print ('record added') conn.commit()
except:
print ('error in insert operation')
conn.rollback()
conn.close()
#creating dump
conn=sqlite3.connect('sample.db')
f=open('dump.sql','w')
for line in conn.iterdump():
f.write('{}\n'.format(line))
f.close()
conn.close ()

The dump file, created, will look like the following:

Example

BEGIN TRANSACTION; 
CREATE TABLE names (name text (20), address 
text(20)); 
INSERT INTO "names" VALUES('Anurag' ,'Mumbai'); 
COMMIT;

To restore the database from the dumped version in ‘newsample.db’, we have to read its contents and execute SQL statements in it with the help of executescript ( ) method of the cursor object.

>>> conn=sqlite3.connect('newsample.db')
>>> f=open('dump.sql1,1r')
>>> qry=f.read( )
>>> f.close ( )
>>> cur=conn.cursor ( )
>>> cur.executescript(qry)
>>> conn, close ( )

The new database gets constructed from the backup. To verify, run a select query on its names table and display the result.

>>> conn=sqlite3.connect('newsample.db')
>>> cur=conn.cursor()
>>> cur.execute('select * from names;')
>>> row=cur.fetchone( )
> > > row
('Anurag', 'Mumbai')

As you can see the result is the same data inserted in the original database. As mentioned earlier, SQLite recognizes NULL, INTEGER, REAL, TEXT, BLOB as native data types. They are mapped to respective Python data types as per the following table:(table 8.1)

Python TypeSQLite type
NoneNULL
IntINTEGER
FloatREAL
StrTEXT
BytesBLOB

The type system of the sqliteT module can be extended to store additional Python types in the SQLite database via object adaptation. You can let the sqlite3 module convert SQLite types to different Python types via converters. Discussion on adapters and converters is kept outside the scope of this book.

Before we discuss other DB-API compatible modules, one more thing is worth mentioning here. We have used The execute () method – and its other variants execute any( ) and execute scripts) – as defined in the cursor class of the sqlite3 module. These methods are also available for use with the connection object. However, as mentioned in the official documentation of the sqlite3 module, they are non-standard methods. It simply means that DB API recommends these methods be defined in cursor class and the connection object as defined in other modules (pymysql or pyodbc module for example) may not be able to call these execute() methods.

Python Data Persistence – Backup and Restore Database Read More »

Python Data Persistence – Using pymysql Module

Python Data Persistence – Using pymysql Module

To make a Python program interact with a MySQL database, we need to install a DB-API compliant module. As mentioned earlier in this chapter, there are many alternatives available for this purpose. In this section, we shall discuss the use of pymysql module. In any case, the functionality of any DB-API compatible module is more or less similar, with a few differences.

The pymysql module is not a part of Python’s standard library. Hence, we have to install it using pip utility.

E:\python37>pip3 install pymysql

As per the DB-API standards, the first step is to establish a connection with the database to be used. Usage of connecting () function in pymysql module is a little different. Remember that MySQL databases are hosted on a server. Hence, the server’s URL and login credentials (user ID and password) must be passed to connect () function. Additionally, if you are trying to connect to an existing database, its name should also be provided. If you are going to create a new database (or use an existing database later), you needn’t provide its name in the connect ( ) function’s parameter list and just connect to the server.

Example

>>> import pymysql
>>> con=pymysql . connect ( ' localhost ' , 'root' , '***' )

MySQL provides the ‘CREATE DATABASE’ statement to start a new database. Execute this statement through the cursor object obtained from the connection object.

Example

>>> cur=con.cursor ( )
>>> cur.execute('create database mynewdb')

You can now start using this (or any other existing database) either by select_db () method or executing the ‘USE DATABASE’ statement.

Example

>>> con.select_db('mynewdb')
>>> #or
>>> cur.execute('use mynewdb')

Now that the new database has been created and is in use, you are now in a position to create a table and perform insert, update, delete and select operations on it exactly as we did on an SQLite database. The only thing you need to take into account is MySQL data types which are different from SQLite data types. (Table 8,2)

Integer typesTINYINT, SMALLINT, MEDIUMINT, INTEGER, BIGINT
Float typesFLOAT, DOUBLE , DECIMAL, NUMERIC
String typesVARCHAR, TEXT, BLOB, CHAR, NCHAR
Date/time typesDATE , TIME, DATETIME
Binary typesBLOB, LONGBLOB

Example

>>> qry=' ' '
CREATE TABLE Products (
ProductID INTEGER PRIMARY KEY AUTO_INCREMENT,
Name VARCHAR (20),
Price INTEGER
)
' ' '
>>> cur.execute(qry)

You can follow the process, as detailed in previous sections of this chapter, for insert, delete, and select operations.

Python Data Persistence – Using pymysql Module Read More »

Python Data Presistence – Nested Loops

Python Data Presistence – Nested Loops

Nesting is a very popular term in programming parlance. It indicates the existence of a certain entity inside another that is of the same type. If you have a situation where an if statement appears inside another if statement, it is termed as nested if. Similarly, a loop within another loop constitutes nested loops. It is also possible to have nested functions, classes, and so on. We are going to discuss nested loops here.
As mentioned above, a nested loop means the existence of a loop within a loop. The following diagram illustrates the situation (figure 2.19):

Python Data Presistence - Nested Loops chapter 2 img 1

What happens when such nesting of the loop is done? Each repetition of the outer loop encounters the inner loop which has to complete its own repetitions before the next round of the outer loop starts. As a result, if the outer loop is designed to perform m iterations and the inner loop is designed to perform n iterations, the innermost statement will be executed men times.

There are a number of instances around us where looping activities are performed in a nested manner. The clock for example has three loops. Outer loop counting hours has a nested loop for minutes, which in turn has a seconds loop.

Any type of loop (while or for) can appear in any other type. However, in practice, we find nesting for loops. The following example displays all prime numbers between 1 and 100. Outer loop iterates over a range object. Inner loop checks whether each number in the outer range is prime or not.

Example

#nestedfor.py
for num in range ( 1 , 101):
for x in range(2,num):
if num%x==0:
break
x=x+1
else:
print (num,sep=’ ‘, end = ‘ ‘ )

Output:

E:\python37>python nestedfor.py
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 93 59 61 67 71 73 79 83 89 97
E:\python37>

 

 

Python Data Presistence – Nested Loops Read More »

Python Data Presistence – Variables

Python Data Presistence – Variables

When you use an object of any of the above types – of any type for that matter – (as a matter of fact everything in Python is an object!) it is stored in the computer’s memory. Any random location is allotted to it. Its location can be obtained by the built-in id ( ) function.

Example

>>> id(10)
1812229424
>>> id(‘Hello’)
2097577807520
>>> id([10,20,30])
2097577803464

However, order to refer to the same object repetitively with its id() is difficult. If a suitable name (by following rules of forming identifier) is given to an object, it becomes convenient while referring to it as and when needed. To bind the object with a name, the ‘=’ symbol is used. It is called the assignment operator.

Here, an int object 5 is assigned a name ‘radius’. The id( ) of both is same.

Example

>>> id(5)
1812229264
>>> radius=5
>>> id(radius)
1812229264

The name ‘radius’ can now be used in different expressions instead of its id ( ) value.

Example

>>> diameter=radius*2
>>> diameter
10
>>> area=3. 142*radius*radius
>>> area
78.55
>>> circumference=2*3.142*radius
>>> circumference
31.419999999999998

Dynamic Typing

Python is a dynamically typed language. This feature distinguishes it from C Family languages like C, C++, Java, and so on. These languages are statically typed. What is the difference?

The difference is the manner in which a variable behaves. In statically typed languages, the variable is in fact a named location in the memory. Moreover, it is configured to store data of a certain type before assigning it any value. Data of any other type is not acceptable to the respective language compiler. Type of Variable is announced first, and data of only that type is acceptable. This makes these languages statically typed.

Look at the following statements in a Java program. A string variable is declared and assigned a string value. However, if we try storing the value of any other type then the Java compiler reports an error.

Example

String somevar;
somevar=”some string value”;
somevar=999;

Java Compiler error

Error: incompatible types: int cannot be converted to java. lang.String

On the other hand, a variable in Python is not bound permanently to a specific data type. In fact, it is only a label to an object in memory. Hence, Java-like prior declaration of variable’s data type is not possible, nor is it required. In Python, the data assigned to a variable decides its data type and not the other way round.

Let us define a variable and check its id ( ) as well as type ( ).

Example

>>> somevar=’some string value’
>>> id(somevar)
2166930029568
> > > type(somevar)
<class ‘str’>

‘somevar’ is a string variable here. But it’s just a label. So you can put the same label on some other object.

Let us assign an integer to Somevar’ and check id () as well as type () again.

Example

>>> somevar=999
>>> id(somevar)
2166929456976
>>> type(somevar)
<class 1int’>

Two things to note here:

  1. You didn’t need a prior declaration of variable and its type.
  2. Variable’s type changed according to data assigned to it. That’s why Python is called a dynamically typed language.

Sequence Operators

As described earlier, string, list, and tuple objects are sequence types. Obviously, arithmetic operators won’t work with them. However, the symbols ‘+’ and can. In this context, they are defined as concatenation and repetition operators respectively.
The concatenation operator (‘+’) appends the contents of the second operand to the first. Of course, both operands must be of the same type.

Example

>>> #concatenation of strings
>>> str1=’Hello World.’
>>> str2=’Hello Python.’
> > > str1+str2
‘Hello World.Hello Python.’
> > > #concatenation of lists
. . .
>>> list1= [1,2,3,4]
> > > list2= [‘one’, ‘two’, ‘three’ ,’four’ ]
> > > listl+list2
[1, 2 , 3 , 4 , ‘ one ‘ , ” two ‘ , ‘ three ‘ , ‘ four’]
>>> ttconcatenation of tuples
. . .
>>> tup1=(1,2,3,4)
>>> tup2=(‘one’,’two’,’three’, ‘four’)
>>> tupl+tup2
(1, 2 , 3, 4, ‘one’, ‘two’, ‘three’, ‘ four’)

Repetition operator(‘*’) concatenates multiple copies of a sequence. The sequence to be replicated is the first operand, the second operand is an integer that specifies the number of copies.

Example

>>> #repetition operator with string
. . .
>>> strl=’Hello.’
>>> str1*3
‘Hello.Hello.Hello.’
>>> #repetition operator with list
. . .
>>> list1= [1,2,3,4]
>>> list1*3
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
>>> #repetition operator with tuple
tup1=(1,2,3,4)
>>> tup1*3
(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)

Index operator (‘[]!) extracts an item at a given position in a sequence. As you know, the sequence is an ordered collection of items and each item has a positional index starting from 0. The expression seq[i] fetches ill item from a given sequence.

Example

>>> #indexing of string
. . .
>>> str1=’Monty Python and the Holy Grail ‘
>>> str1 [21]
‘ H ‘
>>> #indexing of list
. . .
>>> list1=[l,2,3,4,5,6,7,8,9,10]
>>> list1 [6]
7
>>> list1= [‘Python ‘ , ‘‘Java’, ‘C++’, ‘Ruby’, ‘Kotlin’]
>>> .list1 [3]
‘Ruby’
>>> #indexing of tuple
. . .
>>> tup1=(-50, 3.142, 2+3j, True, 50
>>> tup1[2]
(2+3j) )

Slice operator (‘[:]’) fetches a part of the sequence object. The expression has two integers on either side of the symbol inside square brackets. The first integer is an index of the first item in the sequence and the second integer is an index of the next item up to which slice is desired. For example seqflj] returns items from i* position to (j-l)the position. The first integer is 0 by default. Second integer defaults to the last index of the sequence. Remember index starts from 0.

Example

>>> #slicing of string
… strl=’Monty Python and the Holy Grail’
>>> strl [13:16]
‘and’
>>> listl=[‘Python’, ‘Java’, ‘C++’, ‘Ruby’, ‘Kotlin’]
>>> listl [1:3]
[‘Java’, ‘C++’]
>>> tupl=(-50, 3.142, 2 + 3j, True, 50)
>>> tupl[2:4]
( (2 + 3j), True)

There are two ‘Membership’ operators in Python. The in operator checks if an operand exists as one of the items in a given sequence and returns True if so, otherwise, it returns False. The not-in operator does the opposite. It returns False if the operand doesn’t belong to a given sequence, otherwise it returns True.

Example

>>> #membership operator with string
. . .
>>> str1=’Monty Python and the Holy Grail’
>>> ‘Holy’ in str1
True
>>> ‘Grill’ not in str1
True
>>> #membership operator with list
. . .
>>> list1=[‘Python’, ‘Java’, ‘C++’, ‘Ruby’, ‘Kotlin’]
>>> ‘C#’ in list1
False
>>> ‘Ruby’ not in list1
False
>>> #membership operator with tuple
. . .
>>> tup1=(-50, 3.142, 2+3j, True, 50)
>>> 3.142 in tup1
True
>>> 3.142 not in tup1
False

Mutability

As already mentioned, everything in Python is an object. A Python object is either mutable or immutable. What is the difference?
To put it simply, the object whose contents can be changed in place is mutable. As a result, changes are not possible in the contents of immutable objects. Of the built-in objects numbers, string, and tuple objects are immutable. On the other hand, list and dictionary objects are mutable.

Let us try to understand the concept of mutability with the help of the id() function that we have earlier used. First, define a variable and assign an integer to it.

Example

>>> x=100
>>> id(x)
1780447344

Remember that a variable is just a label of the object in memory. The object is stored in a location whose id() is given as shown in example 1.31 above. (The location is allotted randomly. So, when you try it on your machine, it may be different.) To understand what goes on inside the memory, have a look at the diagram (figure 1.4) below. Figure 1.4 (a) shows a label assigned to an integer object 100.
Next, assign x to another variable y.

Python Data Presistence - Variables chapter 1 img 1

Example

>>> y=x
>>> id(y)
1780447344
>>> y
100

Figure 1.4 (b) shows that id() of y is the same as id ( ) of x. It means both x and y are labels of the same object in memory, in this case, the number 100.
Now, increment x by 1 (x=x+l). A new integer object 101 is located at a different location and it is bound to name x. Now x label is detached from 100 but y still remains on 100. (refer figure 1.4 (c))

Example

>>> x=x+1
>>> X
101
>>> id(x)
1780447376
>>> id(y)
1780447344

id (x) has changed but id (y) remains as earlier.

It is clear that the location containing object 100 doesn’t get replaced by 101, instead, it is stored in a new location. Hence, we say that a Number object is immutable. String and tuple objects are also immutable. If we try to change the sequence of characters in a string or any of the items in a tuple, the TypeError message appears effectively meaning that items in a string/tuple sequence can’t be altered because they are immutable.

Example

>>> str1=’Hello World’
>>> str1[6]
‘ W’
>>> str1 [6] = ‘ z ‘
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: ‘str’ object does not support item
assignment
>>> tup1=(-50, 3.142, 2+3j, True, 50)
>>> tup1[1]
3.142
>>> tup1[1]=2.303
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: ‘tuple’ object does not support item assignment

This restriction doesn’t apply to list or dictionary objects. You can add, remove, or modify a list or dictionary. Hence, they are mutable.
Following code demonstrates how an item in list/dictionary is modified.

Example

>>> list1=[‘python’, ‘java’,’C++’ , ‘Ruby’, ‘kotlin’]
>>> list[2] = ‘c#’
>>> list1
[‘Python’ , ‘Java’ , ‘C#’ , ‘Ruby’ , ‘ Kotlin’ ]
>>> dict1= {“Mumbai’ , : ‘Maharastra’ , ‘Hyderebad’ : ‘Telangana’ , ‘patna’ , : ‘Bihar’}
>>>dict1 [‘Hyderabad’ ] = ‘Andhra pradesh’
>>>dict1
{‘Mumbai’ : ‘ Maharastra’ , ‘Hyderabad’ : ‘Andhra pradesh’ ‘patna’ : ‘Bihar’ }

Addition/removal of items in list and dictionary is being explained later in this chapter. That brings us to one of the questions left unanswered earlier. What is the difference between list and tuple? Now the answer is clear. Tuple is immutable. List is mutable.

Python Data Presistence – Variables Read More »

Python Data Presistence – Methods of Built-in Data Type Classes

Python Data Persistence – Methods of Built-in Data Type Classes

Python built-in object of a certain type is characterized by attributes and methods, and as defined in the built-in class of the corresponding name. Methods defined in s tr class for example are available for use to each string object. So is the case of list, tuple, and dictionary objects.

In this section, some commonly used methods of built-in type classes are described. (Two new terms pop up here – class and methods. You’ll come to know about them in the chapter on Object-oriented programming. For the time being treat a method as a function only.)

String Methods

Various methods in str class fall into the following categories:

Related to Case of Alphabets

1. capitalize ( ): Method changes the first letter of the given string to uppercase and returns another string object.

Example

>>> str1=1 python string’
>>> str2=str1.capitalize( )
>>> str2
‘Python string’

2. lower ( ): Method returns a string object by replacing all alphabets in given string with respective lowercase equivalents.

Example

>>> str1=’Python String’
>>> str2=strl.lower( )
>>> str2
‘python string’

3. upper ( ): Method ensures that the resulting string consists of all uppercase alphabets.

Example

>>> str1=’Python String’
>>> str2=str1.upper()
>>> str2
‘PYTHON STRING’

4. title ( ): Method titlecases the string having first alphabet of each word in uppercase.

Example

>>> str1=’python string’
>>> str2=strl.title()
>>> str2
‘Python String’

5. swapcase ( ): Method replaces uppercase alphabets by lowercase and vice versa.

Example

>>> str1=’Simple is Better than Complex.’
>>> str2=str1.swapcase()
>>> str2
‘SIMPLE IS bETTER THAN COMPLEX.’
>>> str1=str2.swapcase()
>>> str1
‘Simple is Better than Complex.’

Find/Replace Methods

1. find( ): Method returns index of first occurrence of a substring in given string. If not found, the method returns -1

Example

>>> str1=’Simple is Better than Complex.’
>>> str1 .find (‘p1’ )
3
>>> strl .find (‘bet’ )
-1

2. index( ): Method is similar to find() except this one raises ValueError if the substring is not found.

Example

>>> str1=’Simple is Better than Complex.’
>>> str1. index (‘p1’ )
3
>>> strl.index(‘bet’)
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
ValueError: substring not found

3. replace ( ): Method requires two string parameters. All occurrences of first parameter get replaced by second parameter.

Example

>>> str1=’all animals are equal. Some are more equal
>>> str1.replace(‘are’, ‘were’)
‘all animals were equal. Some were more equal’

4. count( ): Method returns an integer corresponding to the number of times a substring occurs in a given string.

Example

>>> str1=’Simple is Better than Complex.’
>>> str1.count(‘pi1)
2

Methods Returning Boolean Result

1. isalpha ( ): Method returns True if all characters in given string are alphabetic i.e a-z or A-Z.

Example 

>>> str1=’BpbOnline’
>>> str1.isalpha()
True
>>> str2=’BPB Publications’
>>> str2.isalpha()
False

2. isdigit ( ): Method returns True if the string is made of all digits. If not, it returns False.

Example

>>> str1= ‘8860322236’
>>> str1.isdigit ( )
True
>>> str1= ‘ (+91) 8860322236’
>>> str1 . isdigit ( )
False

3. islower( ) : If all alphabets in given string are in lowercase,this method returns True otherwise returns False.

Example

>>> str1=’pythonrocks’
>>> str1.is lower( )
True

4. isupper ( ): Method returns True if all alphabets in given string are in uppercase not considering other characters.

Example

>>> str1=’IIT JEE’
>>> str1.isupper( )
True

5. startswith( ): method returns True if the given string has substring parameter or any of string items is in a tuple parameter. If it doesn’t, then False is returned.

Example

>>> name=1 Mr. John’
>>> name.startswith(‘Mr’)
True
>>> name=’Dr. Watson’
>>> name.startswith((‘Mr’ , True ‘Dr’))
True

6. endswith ( ): Method checks whether the substring parameter is at the end of a given string and returns True if so, otherwise returns False. The substring to be checked can also be in the tuple parameter.

Example

>>> name=’Mr. John’
>>> name.endswith(‘on’)
False
>>> name=’Dr. Watson’
>>> name.endswith((‘on’, True 1 ‘hn’))
True

Join/split

1. join( ): Method concatenates all string objects in a list or tuple. Items in list/tuple are separated by given string object.

>>> list1= [‘Python’, 1 C++’, ‘Ruby’, 1Kotlin’]
>>> sep=’ and ‘
>>> sep.join(list1)
‘Python and C++ and Ruby and Kotlin’
>>> listl= [‘192’, ‘168’, ‘0’ , ‘1’ ]
>>> sep=’.’
>>> sep.join(listl)
‘192.168.0.1’

2. split ( ): Method separates splits given string into parts wherever the substring parameter is found. The parts are returned in the form of a list object.

Example

>>> str1=’all animals are equal. Some are more equal’
>>> strl.split(‘ ‘)
[‘all’, ‘animals’, ‘are’, ‘equal.’, ‘Some’, ‘are’, ‘more’, ‘equal’]
>>> strl=’192.168.0.1′
>>> strl.split(‘.’)
[ ‘ 192’, ‘ 168’, ‘O’, ‘1’ ]

3. strip ( ): If no parameter is present in the parentheses of this method, then leading and trailing spaces are removed. The parameter can be a string of one or more characters. It acts as a set of characters to be removed.

Example

>>> str1=’ Hello Python ‘
>>> strl.strip( )
‘Hello Python’
>>> str1=’all animals ‘are equal’
>>> str1.strip(‘alu’)
‘ animals are eq’

4. format( ): Method returns a formatted string by interpolating placeholders in the given string by values of objects. Values of parameters in the method’s parentheses fillup the place holders marked by {}.

Example

>>> name=’Virat Kohli’
>>> runs=10385
>>> print (‘{ } is Captain of India. He has scored {} runs in ODI.’.format(name,runs))
Virat Kohli is Captain of India. He has scored 10385 runs in ODI.

The curly bracket placeholders in the string are filled in the same order in which parameters appear in parentheses. You can also refer to the parameter by name and use them in any order.

Example

>>> name=’Virat Kohli’
>>> runs=10385
>>> print (‘{nm} is Captain of India. He has scored {r} runs in ODI.1.format(r=runs,nm=name))
Virat Kohli is Captain of India. He has scored 10385 runs in ODI.

Python also supports C-style string formatting using the ‘ %’ sign as a substitution character. The format specification symbols (like %d, %f, %c, and so on; famously used in print( ) statement of C program) are available for use in Python too.

Example

>>> name=1Virat Kohli’
>>> runs=10385
>>> print (“%s is Captain of India. He has scored %d runs in ODI.” % (name,runs))
Virat Kohli is Captain of India. He has scored 10385 runs in ODI.

List Methods

As described earlier, the list is a mutable data type. It means, it is possible to change the contents of a list object after it has been defined. In this section, you will learn to handle methods in list class that can add/modify/rein o ve items in a list object.

1. append ( ): Method adds a new item at the end of the given list.

Example

>>> list1=[‘Python’,’C++’ ’Ruby’, Kotlin’]
>>> list1.append(‘JS’)
>>> list1
[‘Python’, ‘C++’, ‘Ruby’, ‘Kotlin , ‘JS’]
>>>

2. insert ( ): Method needs two parameters. the Second parameter is the new object to be added. The first parameter is the index at which it will be inserted. Again, the sequence index starts from 0.

Example

>>> list1=[‘Python’,’C++’ ‘Ruby’ ‘Kotlin’]
>>> list1.insert(2,’Java’)
>>> list1
[‘Python’, ‘C++’, ‘Java’, ‘Ruby’ ‘Kotlin’]
>>>

3 . count ( ): Method returns an integer corresponding to number of times a certain item appears in given list.

Example

>>> list1= [3,5,9,3,6]
>>> list1.count(3)
2
>>> str1=’all animals are equal. Some are more equal’
>>> list1=strl.split(‘ ‘)
>>> list1
[‘all’, ‘animals’, ‘are’, ‘equal.’, ‘Some’, ‘are’, ‘more’, ‘equal’]
>>> list1.count(‘are’)
2
> > >

4. index ( ): method returns the index of the first occurrence of a certain value if found in a given list. If it is not found, then the method raises ValueError.

Example

>>> list1= [‘ all’ , ‘animals’, ‘are’, ‘equal.’, ‘Some’, ‘are’, ‘more’, ‘equal’]
>>> list1.index(‘are’)
2
>>> listl.index(‘were’)
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
ValueError: ‘were’ is not in list
>>>

5 . reverse ( ): Order of items in given list is reversed by using this

>>> list1=[‘Python’, ‘C++ , ‘Ruby’, ‘ Kotlin’, 1 JS’ ]
> > > listl.reverse( )
> > > list1
[ ‘ JS ‘, ‘Kotlin’, ‘Ruby’, C++’, ‘Python’]
> > >

6 . sort ( ): Items in the given list object are rearranged in ascending order unless the method’s ‘reverse’ parameter set to True.

Example

>>> list1= [‘Python’, ‘C++’, ‘Ruby’, ‘Kotlin’, ‘JS’]
>>> list1.sort()
>>> list1
[‘C++’, ‘JS’, ‘Kotlin’, ‘Python’, ‘Ruby’]
>>> list2= [‘all’, ‘animals’, ‘are’, ‘equal.’,
‘Some’, ‘are’, ‘more’, ‘equal’]
>>> list2.sort(reverse=True)
>>> list2
[‘more’, ‘equal.’, ‘equal’, ‘are’, ‘are’, ‘animals’, ‘all’, ‘Some’]
> > >

7. remove ( ): Method causes removal of an item’s first occurrence from the given list. If the item to be removed is not found in the list, then ValueError is raised.

Example

8 . pop ( ): Method is similar to remove() method. However, this method returns the removed item. The pop( ) method by default removes the last item in the list. If it contains an index parameter, the item at the specified index is removed and returned.

Example

>>> list1= [‘Python’, ‘C++’, ‘Ruby’, ‘Kotlin’, ‘JS’]
>>>lang=list1.pop( )
>>>>>> list1.sort( )
>>> list1
[‘C++’, ‘JS’, ‘Kotlin’, ‘Python’, ‘Ruby’]
>>> list2= [‘all’, ‘animals’, ‘are’, ‘equal.’,
‘Some’, ‘are’, ‘more’, ‘equal’]
>>> list2.sort(reverse=True)
>>> list2
[‘more’, ‘equal.’, ‘equal’, ‘are’, ‘are’, ‘animals’,
‘all’, ‘Some’] ‘
> > >

If you remember, a tuple is an immutable object. As a result, the above methods performing addition, removal, sorting, and so on; can’t be used with tuple. Only count ( ) and index ( ) methods are available for a tuple object. Their behavior is similar to list methods of the same name.

Dictionary Methods

Like List, a dictionary object is also mutable. However, the dictionary is not a sequence. Its items do not have an index. So, index-based insertion or removal is not supported. Following methods are defined in the built-in dictionary class:

1. get ( ): Method retrieves value component corresponding to key parameter.

Example

>>> dict1 = {‘Mumbai 1 :’Maharashtra’, ‘Hyderabad’:’Telangana’, ‘Patna1:’Bihar’}
>>> dict1.get(‘Patna’)
‘Bihar’
>>>

2. pop ( ): Method removes k-v pair item from given dictionary object corresponding to key parameter, an returns its value component.

Example

>>> dict1 = {1 Mumbai’: Maharashtra’ , ‘Hyderabad’:’Telangana’, ‘Patna’:’Bihar’}
>>> state=dictl.pop( ‘Mumbai’)
>>> state
‘Maharashtra’
>>>

3 . popitem ( ): Method returns a k-v pair in the form of a tuple.

Example

>>> dictl={‘Mumbai’:’Maharashtra’,
‘Hyderabad’:1Telangana’, ‘Patna’:’Bihar’}
>>> t=dictl.popitem()
>>> t
(‘Patna’, ‘Bihar’)
>>>

4. update ( ): Method is used to add a new k-v pair item as well as modify the value of the existing key. The update() method takes another • dictionary object as a parameter. There may be one or more items in it. If its key is not used in a given dictionary, a new k-v pair is added. If the key is already present, its value is replaced by the new value. This process takes place for all items in the diet parameter.
In following code snippet, dictl is updated by adding a new state- capital pair.

Example

>>> dict1={‘MaharashtraBombay’Andhra Pradesh1:1 Hyderabad’, ‘UP’ :’Lucknow’}
>>> dict1.update({‘MP’ :’Bhopal1})
>>> dict1
{‘Maharashtra’: ‘Bombay’, ‘Andhra Pradesh’: ‘Hyderabad’, ‘UP’: ‘Lucknow’, ‘MP’: ‘Bhopal’}
>>>

The initial dictionary object gets updated by another diet parameter. Capitals of two states are modified and one pair is added.

Example

>>> dict1={‘Maharashtra’: ‘Bombay’, ‘Andhra Pradesh’: ‘Hyderabad’, ‘UP’: ‘Lucknow’, ‘MP’: ‘Bhopal’}
>>> dict2={‘AndhraPradesh’:’Amaravati’,’Telangana’:’Hyderabad’, ‘Maharashtra’:’Mumbai’}
>>> dictl.update(dict2)
>>> dict1
{‘Maharashtra’: ‘Mumbai’, ‘Andhra Pradesh’: ‘Amaravati’, ‘UP’: ‘Lucknow’, ‘MP’: ‘Bhopal’, ‘Telangana’: ‘Hyderabad’}
>>>

You can also add a new k-v pair simply by assigning value to the unused key by the following syntax:

dict1[newkey]=value

Here is an example:

Example

>>> dict1 ={‘Maharashtra’: ‘Bombay’ , ‘Andhra Pradesh ‘ : ‘ Hyderabad ‘ , ‘ UP’ : ‘Lucknow’ , ‘MP’ , ‘Bhopal’}
>>> dict1 [‘Maharastra’] = ‘Mumbai’
>>> dict1
{‘Maharashtra’: ‘Bombay’ , ‘Andhra Pradesh ‘ : ‘ Hyderabad ‘ , ‘ UP’ : ‘Lucknow’ , ‘MP’ , ‘Bhopal’}
>>>

Dictionary View Methods

1. items ( ): Method returns a view object consisting of two-item tuples, one for each k-v pair. This view object can be converted to a list of k-v tuples.

Example

>>> dict1 = {‘Maharastra’: ‘Mumbai’ , “Andhra pradesh’ :’Amaravati’ , ‘UP’: ‘Lucknow’ , ‘MP’: ‘Bhopal’, ‘Telangana’ : ‘Hyderabad’}

>>> items=dict1.items( )

>>> items

dict_items([{‘Maharastra’ , ‘Mumbai’), (‘Andhra pradesh’ , ‘Amaravati’), (‘UP’, ‘Lucknow’), (‘MP’, ‘Bhopal’), (‘Telangana’ , ‘Hyderabad’}])

>>> list(items)

[(‘Maharastra’ , ‘Mumbai’), (‘Andhra pradesh’, ‘Amaravati’), (‘UP’, ‘Lucknow’), (‘MP’, ‘Bhopal’), (‘Telangana’ , ‘Hyderabad’}])

>>>

2.keys ( ):

The method returns a view object consisting of all keys in a given dictionary. This view object can be converted to a list of keys.

Example

>>> dict1 = {‘Maharastra’: ‘Mumbai’ , ‘Andhra pradesh’ :’Amaravati’ , ‘UP’: ‘Lucknow’ , ‘MP’: ‘Bhopal’, ‘Telangana’ : ‘Hyderabad’}

>>> keys=dict1.keys( )

>>> keys

dict_keys([‘Maharastra’ , ‘Andhra pradesh’ , ‘UP’ , ‘MP’ , ‘Telangana’])

>>> list (keys)

[‘Maharastra’ , ‘Andhra pradesh’ , ‘UP’ , ‘MP’ , ‘Telangana’]

>>>

3 . values ( ): Method returns a view object consisting of all values in a given dictionary. This view object can be converted to a list of values.

Example 

>>> dictl={1 Maharashtra’: ‘Mumbai’, ‘Andhra Pradesh’: ‘Amaravati’, ‘UP’: ‘Lucknow’, ‘MP’:
‘Bhopal’, ‘Telangana’: ‘Hyderabad’}
>>> values=dictl.values()
>>> values
dict_values([‘Mumbai’, ‘Amaravati’, ‘Lucknow’,
‘Bhopal’, ‘Hyderabad’])
>>> list(values)
[‘Mumbai’, ‘Amaravati’, ‘Lucknow’, ‘Bhopal’,
‘Hyderabad’3
>>>

All view objects returned by items(), keys(), and values() methods get automatically refreshed whenever the underlying dictionary object gets updated.

Example

>>> dictl={‘Maharashtra’:’Bombay’,’Andhra Pradesh’:
‘Hyderabad’, ‘UP’:’Lucknow’}
>>> items=dictl.items()
>>> keys=dictl.keys >>> values=dictl.values >>> items
dict_items([(‘Maharashtra’ , ‘Bombay’), (‘Andhra Pradesh’, ‘Hyderabad’), (‘UP’, ‘Lucknow’)])
>>> dict2={‘Andhra
Pradesh’:’Amaravati’,’Telangana’:’Hyderabad’,
‘Maharashtra’:’Mumbai’}
>>> dictl.update(dict2)
>>> items
dict_items([(‘Maharashtra’, ‘Mumbai’), (‘Andhra
Pradesh’, ‘Amaravati’), (‘UP’, ‘Lucknow’),
(1Telangana’, ‘Hyderabad’)])
>>>

We have thus reached the end of a fairly lengthy introductory chapter of this book. As mentioned in the preface, this (and next) chapter is intended to be a quick-start tutorial for Python. So try and explore Python as much as you can with Python’s interactive shell. The next chapter explains how Python’s scripting mode works. Python’s conditional and looping techniques are also discussed there.

Python Data Presistence – Methods of Built-in Data Type Classes Read More »

Python Data Presistence – Data Types

Python Data Presistence – Data Types

Data and information, these two words are so common nowadays – they are on the lips of everybody around us. But many seem to be confused about the exact meaning of these words. So much so, people use them almost as if they are synonymous. But they are not.
The computer is a data processing device. Hence, data is a raw and factual representation of objects, which when processed by the computer program, generates meaningful information.

Various pieces of data items are classified into data types. Python’s data model recognizes the following data types (figure 1.3):

Python Data Presistence - Data Types chapter 1 img 1

Number Types

Any data object having a numerical value (as in mathematical context) is a Number. Python identifies integer, real, complex, and Boolean number types by the built-in type names int, float, complex, and bool respectively. Any number (positive or negative) without a fractional component is an integer, and the fractional component is afloat. The boolean object represents truth values True and False, corresponding to 1 and 0 respectively.

A number object is created with a literal representation using digit characters. Python has a built-in type ( ) function to identify the type of any object.

Example

>>> #this is an integer
. . . 100
100
>>> type ( 100 )
<class ‘ int ‘ >
>>> #this is a float
. . . 5.65
5.65
>>> type ( 5 . 65 )
<class ‘float’>
>>> #this is bool object
. . . True
True
>>> type ( True )
<class ‘ bool ‘ >

Any number with a fractional component (sometimes called the mantissa) is identified as a floating object. The fractional component is the digits after the decimal point symbol. To shorten the representation of a float literal with more digits after the decimal point, symbols ‘e’ or ‘E’ are used.

Example

>>> #this is float with scientific notation
. . . 1 . 5e – 3
0 . 0015
>>> type ( 1 . 5e – 3 )
<class ‘ float ‘ >

A complex number consists of two parts – real and imaginary – separated by ‘+’ or sign. The imaginary part is suffixed by ‘j’ which is defined as an imaginary number which is the square root of \(\sqrt{-1}\) (• A complex number is represented as x+yj.

Example

>>> 2 + 3j
( 2+3j )
>>> type ( 2+3j )
<class ‘ complex ‘ >

Arithmetic Operators

All number types can undergo arithmetic operations. Addition (‘+’), subtraction multiplication (‘*’) and division (7’) operators work as per their traditional meaning. In addition, few more arithmetic operators are defined in Python, which are:

  • Modulus or remainder operator (‘%’), returns the remainder of the division of the first operand by the second. For example, 10%3 returns 1.
  • Exponent operator (‘**’), computes the first operand raised to second. For example, 10**2 returns 100.
  • The floor division operator (7/’) returns an integer not greater than the division of the first operand by the second. For example, 9//2 returns 4.

Example

>>> #addition operator
. . . 10+3
13
>>> #subtraction operator
. . . 10-3
7
>>> #multiplication operator
. . . 10*3
30
>>> #division operator
. . . 10/3
3.3333333333333335
>>> #modulus operator
. . . 10%3
1
>>> #exponent operator
. . . 10**3
1000
>>> #floor division operator
. . . 10//3
3

Sequence Types

An ordered collection of items is called a sequence. Items in the sequence have a positional index starting with 0. There are three sequence types defined in Python.
1. String: Ordered sequence of any characters enclosed in single, double, or triple quotation marks forms a string object. Each character in the string object is accessible by index.

Example

>>> #string using single quotes
. . . ‘Hello. How are you?’
‘Hello. How are you?’
>>> #string using double quotes
. . .  “Hello. How are you?”
‘Hello. How are you?’
>>> #string using triple quotes
. . . ”’Hello. How are you?”’
‘Hello. How are you?’

2. List: An ordered collection of data items, not necessarily of the same type, separated by a comma and enclosed in square brackets [ ] constitutes a List object. The list is a sequence type because its items have a positional index starting from 0.

3. Tuple: A tuple is also an ordered collection of items, which may be of dissimilar types, each separated by a comma and enclosed in parentheses ( ). Again each item in tuple has a unique index.

Example

>>> [ ‘ pen ‘ , 15 , 25 . 50 , True ]
[ ‘pen ‘ , 15 , 25 . 5 , True ]
>>> type ( [ ‘ pen ‘ , 15 , 25 . 50 , True ] )
<class ‘ list’ >
>>> ( ‘ Python ‘ , 3 . 72 , ‘ Windows ‘ , 10 , 2 . 5E04 )
( ‘ Python ‘ ,  3 . 72 ,  ‘ Windows ‘ , 10 , 25000 . 0 )
>>> type ( ( ‘ Python ‘ ,  3 . 72 , ‘ Windows ‘ , 10 , 2 . 5E04 ) )
<class ‘ tuple ‘ >

Apart from the type of brackets – [ ] or ( ) – List and Tuple appear similar. However, there is a crucial difference between them – that of mutability. This will come up for explanation just a few topics afterward.

Mappings Type

A mapping object ‘maps’ the value of one object with that of other. Python’s dictionary object is an example of mapping. A language dictionary is a collection of pairs of words and corresponding meanings. Two parts of the pair are key (word) and value (meaning). Similarly, a Python dictionary is also a collection of key-value pairs, separated by a comma and is put inside curly brackets {}. The Association of the key with its value is represented by putting V between the two.

Each key in a dictionary object must be unique. The key should be a number, string, or tuple. (All are immutable objects). Any type of object can be used as the value in the pair. The same object can appear as the value of multiple keys.

Example

>>> {1:’one’, 2:’two’, 3 :’three’}
{1: ‘one’ , 2: ‘two’ , 3 : ‘three’}
>>> type({1:’one’, 2:’two’, 3:’three’})
<class ‘diet’>
>>> {‘Mumbai’:’Maharashtra’ ,
‘Hyderabad’:’Telangana’, ‘Patna’: Bihar ‘}
{‘Mumbai’: ‘Maharashtra’ , ‘Hyderabad’: ‘Telangana’,
‘Patna’: ‘Bihar’}
>>> type({‘Mumbai’:’Maharashtra’ ,
‘Hyderabad’:’Telangana’ , eclass 1 diet’> ‘Patna’: Bihar ‘})
>>> {‘Windows’: t’Windows XP’, ‘Windows 10 ‘ ] ,
‘Languages’: [‘Python’, ‘ Java’] }
{‘Windows’: [‘Windows XP ‘Windows 10’ 1 ,
‘Languages’: [‘Python’, ‘Java’]}
>>> type({‘Windows’:[‘Windows XP’ ‘Windows 10’],
‘Languages’ : [‘Python’, ‘Java’]})
<class ‘diet’>

 

Python Data Presistence – Data Types Read More »

Python Data Presistence – Indents

Python Data Presistence – Indents

The use of indents is one of the most unique features of Python syntax. As mentioned above, each statement starts at the first character position of the next available line on the online shell. In the case of the script, blank lines are ignored. In many situations, statements need to be grouped together to form a block of certain significance. Such circumstances are the definitions of function or class, a repetitive block of statements in the loop, and so on. Languages such as C/C++ or Java put series of statements in a pair of opening and closing curly brackets. Python uses the indentation technique to mark the blocks. This makes the code visually cleaner than clumsy curly brackets.

Whenever you need to start a block, use: symbol as the last character in the current line, after that press Enter, and then press Tab key once to leave fixed whitespace before writing the first statement in the new block. Subsequent statements in the block should follow the same indent space. If there is a block within the block you may need to press the Tab key for each level of block. Look at the following examples:

Indented Block in Function

Example

>>> def calculate_tax (sal) :
. . .            tax=sal*10/100
. . .           if tax>5000:
. . .                     tax=5000
. . .          netsal=sal-tax
. . .          return netsal
. . .
>>>

Indents in Class

Example 

>>> class Example:
. . .              def init_(self, x) :
. . .              self.x=x
. . .             if x>100:
. . .                      self.x=100
. . .
> > >

Indents in Loop

Example

>>> for i in range (4) :
. . .         for j in range(4) :
. . .                       print ( i , j )
. . .

 

Python Data Presistence – Indents Read More »