Passion of IT

Scripting languages: Python

There are a lot of scripting languages, the following are the most important:

  • Javascript: it allows to perform the interaction with user of html page, JQuery is an evolution of javascript and is made of javascript
  • SQL: script to interact with DB: DDL (data definition language) to modify the structure of the table, DML (data manipulation language) to inquiry it
  • BASH: linux script to perform any operation with linux API
  • Groovy: to execute java code on runtime
  • Python: scripting language that allow the following operations: to have an access to operative system API, to execute SQL query, to make Rest web services, to build web application

Python is very easy to use because in linux is already installed the compiler, it is enough to write a text file with py extension (such as example.py)and digit


python example.py

Python doesn’t have a type of variables like PHP.

In Python the block of instructions (that is for example content in a cycle) is defined by the indentation of the code: there are not any parents like php or java

there  are a lot of python instructions, this is a small list


print "2 + 2 is", 2+2
s = 'hello world i am Michele'
print len(s) #24
print s[-1] #last letter e
print s[1]#second letter e
print s[1:10] #ello worl
print u'hello everybody !'
print str(u"abc")
a = ['spam', 'eggs', 100, 1234]
print a[0] #spam
a[0:1] = [0, 3]
print a; #[0, 3, 'eggs', 100, 1234]
a[0:2] = []
print a; #['eggs', 100, 1234]
b=[1,a,2]
print b #[1, ['eggs', 100, 1234], 2]
del b[2:3]
print b
a = ['cat', 'window', 'the cat is on the window'] #[1, ['eggs', 100, 1234]]
for x in a:
........................print x, len(x)
c=0
while c<10:
.....try:
..........ca = raw_input('Choose a number: ')
..........c = int(ca)
..........break
....except ValueError:
.........print "Error"
def fib2(n=100): # fibonacci series
..........result = []
..........a, b = 0, 1
..........while b < n:
...................result.append(b)
...................a, b = b, a+b
...........return result
print fib2(n=100) #[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
print fib2(100) #[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

p=lambda a, b: a+b
print p(10,20)      #30
a = [66.6, 333, 333, 1, 1234.5]
print a.count(333), a.count(66.6), a.count('x') #2 1 0
a.insert(2, -1)
a.append(333)
print a #[66.6, 333, -1, 333, 1, 1234.5, 333]
print a.index(333) #1
a.remove(333)
print a #[66.6, -1, 333, 1, 1234.5, 333]
a.reverse()
print a #[333, 1234.5, 1, 333, -1, 66.6]

a.sort()
print a.pop() #list like a stack
print a.pop(0)

t = 12345, 54321, 'hello!'

u = t, (1, 2, 3, 4, 5)

basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
fruits = set(basket) #no duplications
print fruits
'orange' in fruits
a = set('abracadabra')
b = set('alacazam')
tel = {'jack': 4098, 'sape': 4139}

tel['guido'] = 4127
tel['jack']

f=open('/home/michele/Desktop/tmp/workfile1', 'w') #files

f.write('String')
f.close()
f1=open('/home/michele/Desktop/tmp/workfile1', 'r')
f1.seek(5)
f1.read()
f1.close()

Class ComplexNumber:

..........def __init__(self, real, image):
..........self.r = real
..........self.i = image
x= ComplexNumber(3.0, -4.5)
x.r

import os

#operative system API such as know the time and explore three structure of file system

os.getcwd()
os.chdir('/')
#!/usr/bin/python
import datetime
print datetime.datetime.now()

for dirname, dirnames, filenames in os.walk('/home/michele/Desktop/1'):
........# print path to all subdirectories first.
.......for subdirname in dirnames:
.......# print path to all subdirectories first.
...................print os.path.join(dirname, subdirname)
..........for filename in filenames:
..................# print path to all filenames.
.................print os.path.join(dirname, filename)

 

 

Also in python is possible execute query script with sql library:

import MySQLdb

# Open database connection
db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )

# prepare a cursor object using <i>cursor()</i> method
cursor = db.cursor()

# execute SQL query using <i>execute()</i> method.
cursor.execute("SELECT VERSION()")

# Fetch a single row using <i>fetchone()</i> method.
data = cursor.fetchone()

print "Database version : %s " % data

# disconnect from server
db.close()

and with this framework tested by me is possible to make html application and rest services

 

This is a small my study about python.

It is easy to use, versatile and complete.

It is the best scripting language that I know

No Comments Yet

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite="
"> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Recent Comments

Michele Rizzithe website doesn't exist. Ara you a robot? In any case it's difficult that an automatic system writes a comment here since there are two captchas...
Hello there! This is kind of off topic but I need some guidance from an established blog. Is it very hard to set up your own blog? I'm not very t...
We are a group of volunteers and opening a new scheme in our community. Your web site offered us with valuable information to work on. You've done a...
November 2024
M T W T F S S
« Dec    
 123
45678910
11121314151617
18192021222324
252627282930  

Login