Archive for the ‘Python’ Category

learn python

Posted: April 7, 2015 in Python

Hello All,

Though PHP is powerful language, python also has its own capabilities.

This blog is dedicated to learn python step by step

After downloading python latest version form https://www.python.org/downloads/

There are multiple python editors available on market.

Eclipse is one of the best editor for python too. We can play by installing the python plugin.

The following code illustrates from variable declaration to loops and lists of python.

The following points would be helpful for the php experts.

1. No $ sign provided for variables in python

2. No start or End tags (<?php and ?>)

3. No braces used for if for loops. Here indentation will take care of that.

4. No switch case in python

Create a test.py file and copy the following code and all examples and you can change the code and re execute it.

As these examples are provided in organized way, your learning time would be saved and you would be familiar with topics quickly.

 

In Eclipse Ctrl +F11 is the shortcut to run the code.

 

#!/usr/bin/python

”’
Created on Mar 31, 2015

@author: chandrasekhar
”’

# print statement

print “Hello World”;

#### authmatic operation ####

## Addition

a =10;
b = 5;

c = a + b;

print (c);

## Multiplication

a =10;
b = 5;

c = a * b;

print (c);

# concatination

a = “string 1”;
b = “string 2″;

c = a+” And “+b;

print (c);

# condition

a = 10;
b = 5;

if a > b:
print (“a bigger”);
else:
print (“b bigger”);

########### Lists ########################

list1 = [1,2,3,4,5]

print (“list values index 1”)
print (list1[1])

list1[4] = 10

print (“new list values index 4″)
print (list1[4])

”’
Range function

”’

print(list(range(1,5))); # output is [1, 2, 3, 4]

########### Loops ##########################

fruits = [“applie”, “banana”, “mango”]
for fruit in fruits:
print (“current fruit “+fruit)

name = “chandra sekhar”;

for letter in name:
print (“current letter in name is “+letter);

########## date functions ##################

import datetime;
today = datetime.date.today();
print (today);

# formatting date

”’

%d is the day number
%m is the month number
%b is the month abbreviation
%y is the year last two digits
%Y is the all year

”’

print (today.strftime(‘We are the %d, %b %Y’));

”’
add 10 days to current date

”’

import datetime

#start_date = “10/04/15”
today = datetime.date.today();
start_date = today.strftime(‘%m/%d/%y’)

date_1 = datetime.datetime.strptime(start_date, “%m/%d/%y”)

end_date = date_1 + datetime.timedelta(days=10)

print (end_date)

 

 

 

 

 

Advertisements