Showing posts with label Perl / Python Program. Show all posts
Showing posts with label Perl / Python Program. Show all posts

Importat Python Programs



PYTHON PROGRAMS




1. Write a Hello World Python Program


#!/usr/bin/python


print "Hello World!";




2. String Concatenation in python programming

#String concatenation worda='computer'; wordb='science'; print("worda is ",worda); print("wordb is",wordb); wordc=worda+" " +wordb; print("wordc is",wordc); wordd=worda*3; print("wordd is ",wordd); str = 'HelloWorld!' length=len(str);
print ("str :",str);
print("length:",length);
print ("first character is",str[0]);
print ("print character from 2rd to 6th :", str[2:7]       ); print ("Prints string starting from 3rd character:",str[2:]); print ("Prints string two times",str * 2);
print ("Prints concatenated string :",str + "TEST" );
print(str[-1]); #print last character
print(str[-6]);#print character from last 6th position print(str[:-2]);# Everything except the last two characters




3. Write a python program to perform function in Lists


#Python Lists
#!/usr/bin/python
print("\t \t \t Python Lists");
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']
print("Prints complete list:",list);
print("Prints first element of the list : ",list[0]);



print("Prints elements starting from 2nd to 4th:",list[1:3]); print("Prints elements starting from 3rd element:",list[2:]); print("Prints list two times:",tinylist * 2);
print("Prints concatenated lists: ", list + tinylist );


#modify the 4th elements in the list
print("Before modifying the 4th element in list :",list[4]);
list[4]='efgh';
print("4th element in list :",list[4]);
print(" complete list:",list);


#Appending new elements
list.append('ijkl');
print("After appending list:",list);


#deleting an element in list
del list[2];
print("List :",list);



4. Write a python program to perform functions in tuples


#Python Tuples


print("\t \t \t Python tuples");
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2  ) tinytuple = (123, 'john') print("\ncomplete tuple :",tuple);


print("Prints first element of the tuple:", tuple[0]); print("Prints elements starting from 1nd to 3th:", tuple[1:4]); print("Prints elements starting from 3rd element:", tuple[2:]); print("Prints tuple two times:", tinytuple * 2    );
print ("concatenated tuple:",tuple + tinytuple );




5. Write a python program to perform functions in Dictionary.


#Python Dictionary
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
print ("Python Dictionary:" );
print ("complete dictionary",tinydict );
print ("Key :",tinydict.keys()); # Prints all the keys print ("values:",tinydict.values()); # Prints all the values



6. Write a python Program to select odd number from the lists


#!/usr/bin/python
a=[11,12,13,14,15,16,17,18,19,20,21,31,44,45,10]; print("List is:",a);
n=len(a); print("length:",n); i=0;
print("Odd number"); for i in range(len(a)): if(a[i]%2==1): print(a[i]);


7. Conditional statement in Python


>>> x = int(raw_input("Please enter an integer: "))
>>> if x < 0:
... x = 0
... print 'Negative changed to zero'
... elif x == 0:
... print 'Zero'
... elif x == 1:
... print 'Single'
... else:
... print 'More'


8. For Statement in Python


>>> # Measure some strings:
... a = ['cat', 'window', 'defenestrate']
>>> for x in a:
... print x, len(x)


cat 3
window 6


9. The Range() and Len() in Python


>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in range(len(a)):
... print i, a[i]
...



10. Prime Number using Python.


>>> for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
... print n, 'equals', x, '*', n/x
... break
... else:
... # loop fell through without finding a factor
... print n, 'is a prime number'
..




11. Simple Class Program in python


>>> class Complex:
... def    init   (self, realpart, imagpart):
... self.r = realpart
... self.i = imagpart
...
>>> x = Complex(3.0, -4.5)
>>> x.r, x.i
(3.0, -4.5)




12. Module Program in Python (Fibonacci Series)


A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a
module, the modules name (as a string) is available as the value of the global variable    name   . For instance, use your favorite text editor to create a file called fibo.py in the current directory with the following contents:
Step  1:
Fibo.py
def fib(n):   # write Fibonacci series up to n a, b = 0, 1
while b < n:
print b,
a, b = b, a+b


Step 2:
>>> import fibo


Step 3:
>>> fibo.fib(1000)


simple python program | python help | execute python program from windows cmd | any python program | self terminate a python program | python builder | checking python version | python program download | python programming | python program example | python tutorial | python script | Python Programming for Beginners | Linux

create the table in perl that will be automatically updated in database using mysql


https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAX_WcaIL6mcjney_b1CXP70kPbtwDPYglJIAUK0dGJJcF5NImX-yQei9ex8nxt1Qqi0u5vjO6JNQmW6a5qYX1qReYs0CWv3XTrnLhhdyPIQlx5A2tVwJJl_PWWjz7tfCO0o1fm8yzqPe3/s320/perl_logo.jpg

1.create the table in perl that will be automatically updated in database using mysql



1.1 First create the database as follows in mysql



[linuxpert@localhost ~]$ mysql -u root -p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g. Your MySQL connection id is 2
Server version: 5.1.45 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases;

+--------------------+

| Database         |

+--------------------+

| information_schema |

| mysql             |

| test              |

+--------------------+

3 rows in set (0.25 sec)






mysql> create database student; Query OK, 1 row affected (0.02 sec)


mysql> show databases;

+--------------------+

| Database         |

+--------------------+

| information_schema |

| mysql             |

| student          |

| test              |

+--------------------+

4 rows in set (0.00 sec)



mysql> use student; Database changed mysql> connect; Connection id:      3
Current database: student



mysql> show tables; Empty set (0.00 sec)


now the table is empty . To create the table we use the following procedure













1.2 The following packages should be used to connect PERL with Mysql

(use new terminal)



[linuxpert@localhost ~]$ rpm -q perl-DBI

perl-DBI-1.609-4.fc13.i686



[linuxpert@localhost ~]$ rpm -q perl-DBD-MySQL

perl-DBD-MySQL-4.013-3.fc13.i686



1.3 Write the PERL script to connect with mysql as follows



#!usr/bin/perl

use DBI; #to use the build in package we use "Use", DBI is the build in package in perl
my $dbh=DBI->connect("dbi:mysql:student","root",""); #connect to database
if(!$dbh)

{

die("error:$!");

}

$sth=$dbh->prepare("create table students(rollno int,sname varchar(50))");

# create the table

$sth->execute();

$dbh->disconnect;







1.4 Run the Perl script [linuxpert@localhost ~]$ perl connect.pl now see the tables in database (“student”)
mysql> show tables;

+-------------------+

| Tables_in_student |

+-------------------+

| students        |

+-------------------+

1 row in set (0.00 sec)



2. insert the values in perl that will be automatically updated in database using mysql as follows


#!usr/bin/perl

use DBI; #to use the build in package we use "Use", DBI is the build in package in perl
my $dbh=DBI->connect("dbi:mysql:student","root",""); #connect to database
if(!$dbh)

{

die("error:$!");

}




$sth=$dbh->prepare("insert into students values(100,'thamarai')"); # create the table
$sth->execute();

$dbh->disconnect;



2.1 compile the perl [linuxpert@localhost ~]$ perl dbinsert.pl now the output is
mysql> select * from students;

+--------+----------+

| rollno | sname   |

+--------+----------+

|   100 | thamarai |

+--------+----------+

1 row in set (0.00 sec)



2.2 insert the values in perl using execute statement



#!usr/bin/perl

use DBI; #to use the build in package we use "Use", DBI is the build in package in perl
$rollno=200;

$sname="selvi";







my $dbh=DBI->connect("dbi:mysql:student","root",""); #connect to database
if(!$dbh)

{

die("error:$!");

}

$sth=$dbh->prepare("insert into students values(?,?)"); # create the table

$sth->execute($rollno,$sname);

$dbh->disconnect; compile the program as [linuxpert@localhost ~]$ perl dbinsert1.pl now the output is
mysql> select * from students;

+--------+----------+

| rollno | sname   |

+--------+----------+

|   100 | thamarai |

|   200 | selvi   |

+--------+----------+

2 rows in set (0.08 sec)




simple perl program | sample perl program | perl program download | example perl program |execute perl program | perl script | perl builder | perl beginners | perl software | perl program download | perl script | perl program windows | perl tutorial