Wednesday, December 24, 2014

Conditional statements

We will write some common programs to get you with the conditional statements


num=input()
num = int(num)
if num>3:
    print "number greater than 3"
else:
    print "number less than or equal to 3"

input:
4
Output:
number greater than 3

The above example seems simple but note the alignment after if and else statement that alignment should me maintained.

Now consider next example:

num=input()
num = int(num)
if num>3:
    print "number greater than 3"
    print "the number is: " + num
    if num>7:
        print "number is also greater than 7"
else:
    print "number less than or equal to 3"

input:
4
output:
number greater than 3
the number is: 4

input:
8
output:
number greater than 3
the number is: 8
number is also greater than 7

input:
2
output:
number less than or equal to 3

From the above example its quite clear from how alignment works for if else conditions.

we will see some more examples in next post.

Tuples

TUPLES:

In many ways, tuples are similar to the lists.

Difference between tuples and List:
1. Tuples are immutable while List are mutables.
    Immutable means that we can not modify the tuples like appending an element, inserting an item         an item at a particular position or deleting one or more elements. These are the operations which         can be done on lists.
2. Tuples are heterogeneous data structure while lists are homogeneous sequence.
    It means tuples are sequence of different kind of items while lists are sequence of same kind of           items.

>>>a=1,2,3,4,5,6
>>>a
(1,2,3,4,5,6)
>>>

however a.append(5) or a[3]=5 kind of statements will give you errors because as mentioned tuples are immutable that is they can not be modified.

but the printing of elements of tuples are similar as printing method of lists.

>>>print a[0]
1
>>>print a[:3]
(1,2,3)
>>>print a[::-1]
(6,5,4,3,2,1)


Tuesday, December 23, 2014

Modifying list

Here we will now see how to modify a list:

>>>list=[2,3,4,5,"Hello",97,32,"Hey","World",12]
>>>list
[2,3,4,5,'Hello',97,32,'Hey','World',12]

#here the list is not sorted
>>>print sorted(list)
[2,3,4,5,12,32,97,'Hello','Hey','World']

'''
Here you can observe that the number is first sorted then the strings are sorted
'''
>>>print list
[2,3,4,5,'Hello',97,32,'Hey','World',12]

'''
Now you observe that after printing sorted(list) again printing list is not printing the sorted one because we have not modified the list. We have just printed the list.
To modify list look next step
'''
>>>list=sorted(list)
>>>print list
[2,3,4,5,12,32,97,'Hello','Hey','World']

'''
Suppose we want to modify the list on some particular index
actually its a very easy task'''
>>>list[3]=17
>>>print list
[2,3,4,17,12,32,97,'Hello','Hey','World']
>>>list[-1]=14
>>>print list
[2,3,4,5,12,32,97,'Hello','Hey',14]
''' Now we want to add some elements in the list'''
>>>list.append(37)
>>>print list
[2,3,4,5,12,32,97,'Hello','Hey',14,37]
NOTE: The new element is added at the -1 location.

#list.append(37) is equivalent to list[len(list):]=[37]
#len(list) returns the list size.

>>>list.insert(3,94)
'''it inserts an item in the given location. The first argument decides the location. Second argument is the data item which is to be inserted.'''
>>>list
[2,3,4,94,5,12,32,97,'Hello','Hey',14,37]
>>>del list[2]
#it deletes the item at the give index location
>>>list
[2,3,94,5,12,32,97,'Hello','Hey',14,37]


More on lists

We will extend our study on list:

>>>list= [2,3,4,5,"Hello",6]

>>>print list[-1:-3]
[]
>>>print list[-3:-1]
[5,'Hello']
>>>print list[-4:]
[5,'Hello',6]
>>>print list[1:]
[3,4,5,'Hello',6]
>>>print list[:]
[2,3,4,5,'Hello',6]

">>>list[para1:para2:para3]"
'''
Here you can see that we have three parameters namely para1, para2, para3.
para1-> first parameter from where our operation on list will start.
para2-> second parameter onto which our operation on list will end (ofcourse excluding that index location)
para3->third parameter also known as step parameter which decides how many index location to jump each time during operation
'''
>>>print list[1:4:2]
[3,5]
>>>print list[0:5:3]
[2,5]

# we can also use this feature to reverse the list
>>>print list[::-1]
[6,'Hello',5,4,3,2]






Lists in Python

We will now see lists creation in python

variableName = []
# This defines that the variableName is a list.

variableName = [2,3,4,5,"Hello",7]
#after seeing above example you can be sure that there is no limitation on data type of a list
#same list contain variables as well as a string

>>>print variableName
[2,3,4,5,'Hello',7]

>>>

>>> print variableName[]
#this will give you an error
>>>print variableName[0]
2
>>>print variableName[2]
4
>>>print variableName[-1]
7
>>>print variableName[5]
7
>>>print variableName[-2]
'Hello'
>>>print variableName[4]
'Hello'
>>>print variableName[-6]
2
>>>print variableName[6]
#this will give you error as we have no data items at 7th location in the list.
>>>print variableName[3:5]
[5,'Hello']
#':' refers to 'to'. 3:5 refers from index location 3 to index location 5. last index location is excluded from the list.

''' Thus you can infer that in python we can access the list from both sides.
From front side the index starts from 0 then 1,2,3 and so on.
From rear side the index starts from -1 then -2,-3,-4 and so on
'''

A Demo SIMPLE Program

EXAMPLE-1

print "Hello World!"

# save file helloWorld.py
#press F5 to run

Hello World!
>>>

EXAMPLE-2

firstName = raw_input("Enter your First name: ")
lastName = raw_input("Enter your Last name: ")
age = input("Enter your age: ")
print "Hello! Mr. " + firstName + " " + lastName
print "You are " + age + " " +"years old"

# save file basicInfo.py
#press F5 to run

Enter your First name: SACHIN
Enter your Last name: LARA
Enter your age: 43
Hello! Mr. SACHIN LARA
You are 43 years old


#NOTE: variable names in python are case sensitive
'''
FirstName, FIRSTNAME, firstName
all are different variables
so one should develop the habit of writing a variable of one kind only.
'''

Real Programs

To make real program:

Open IDLE(Python GUI)

Select File->New Window (ctrl + N)

A window will appear.
Type your program
save your file with ".py" extension.
In New Window (in which you have written your program) Goto RUN->RUN MODULE(F5)


If you want to open your existing file:

Select File->Open->Browse to the location where you have saved your python file-> select your file-> click on open

COMMENTING IN YOUR REAL PROGRAM

Commenting in your program is really a good habit.
because you may forget why a particular part of the code was written.
It also helps other programmer to easily understand and edit in program.

# THIS IS A COMMENT LINE
#THIS IS A SINGLE LINE COMMENT
'#' skips all the words written in that particular line.

''' THIS IS
A MULTILINE
COMMENT '''

INPUT and RAW INPUT

We will learn here about giving inputs:

syntax:

variableName = input()

// This will assign the input value to the variableName

variableName = input("Enter a number: ")

E.g.:
>>>a=input()
10                                               //entering input
>>>print a
10                                               //output value of a



//Output will be of the form - Enter a number:
//Entering a number will assign the value to the variableName

*/ However entering a string to this will give an error

for this we have another command know as raw input*/

variableName = raw_input()

// this will assign the input as string to the variable name

variableName=raw_input("Hello: ")

E.g.:
>>>FirtstName = raw_input("Hello! ")
Hello! ABC
>>>FirstName
ABC


Monday, May 12, 2014

Print and basic mathematical operations

Printing and basic mathematical operation:

Command print is use to print the result.

The first two commands are general print instructions for printing number. Print 54 has just printed only number 54.

Next three commands are used for printing the the resultant numbers after mathematical numbers.

"**" this command is used for power operation. 2**5 = 2^5 = 32
5**2 = 5^2 = 25

the next command is Division command here the resultant is always the greatest integer value [35/5] = 7
[32/6]=5
this is because the both operands are integers thus it will return an integer value.

last command (modular operator) is used here is remainder operator displaying the remainder of two operand.

clearing command line


After you have installed the Interpreter now lets start with basic mathematical operations.

Introduction:  After opening the python command line, you will see the command prompt.
If you are using windows, type the following command to clear the screen

import os
os.system("cls")

if you are using linux or mac then the following may work

import os
os.system("clear")

the above code will clear your screen and there will be only a command prompt.
however clearing the window is just not mandatory. It depends on user wether he wants to clear the screen or not.





Download and Install

DAY 1:

Python is an easy learning programming language. It has high level data structures which is quite efficient and widely used. It lets you work quickly and

This tutorial is for those who want to learn python fast and in efficient way.

Pre-requisite: If you are familiar with other programming language then its going to be quite easy. If not, no worry you can still learn it.

Download Python 2.7.3.
A I have been using this version of python thus it would be easy for me to make you learn.

Install the Python interpreter on your system.Interpreter reads the python programs and carries out their instruction.

Be ready to learn.