Skip to main content

First program in Python!!!

Here you go!!..

Learning new programming languages is quite equal to learning new regional language. I have head started with python programming language which is one of the oldest programming languages . It is has OOPS concept so that if we are familiar with OOPS concept then this programming language should be quite easy for us to learn. Only syntax varies.

To start with i have tried to write a smallest program in python language.The problem statement is very simple

Write a program to find 2 consecutive vowels in a string  .Consider those two consecutive vowels as one character and return the number of characters in that string.

I have used an online editor http://www.tutorialspoint.com/execute_python_online.php to learn coding practically.Now let me show you step by step to execute this small program

  1. When you open the  Online EDITOR linkby default you can see root folder and under that root folder there is file named main.py 











2.Now right click on Root folder and select Create new file. This is new class file in which we are going to write functionality. Infact you can write functionality withn the main.py file also. PY is the extension of python class file.















3.Rename newly created file with appropriate file name.

4.Inside the new class file write the functionality














In the above mentioned code


  • removeVowels is the class name .If there is a statement of line which is based on one of teh condition or which belongs to some code block then the parent statement should be end COLON(:). Since all of the functionality will be inside the class name , so we add colon at the end of the class.Here class is a keyword to represent its immediate next word is class name
                             class removeVowels:
  • getLength is the method name. You can see that at the end of the method there is a COLON. it means inside the method some lines of code are there which are strictly inside that method code block. the keyword def represents the word which is present immediately next to the def is METHOD/function.
                           def getLength(randomVariable,word):

     NOTE: in python INDENTATION plays biggest role. Huh!!!.. indentation means TAB. If a second line of the code is depending on the first line then after writing first line of the code and adding COLON at the end of that line you need to press ENTER key. Now you need to press TAB key. in the above two statements under bullets point THE CLASS AND METHOD are inter linked. Hence after writing a line of words for class and adding colon at the end, now press enter key and press TAB key. Write statement for METHOD getLength. You can clearly spot this in the 4th image 

  •  Now you can see another two lines of statement in which there is no colon and no TAB (indentation)














        vowel='aioue'
        for i in range(len(word)-1):
 for loop is not having a tab after the statement vowel='aioue'. Because these two lines of the code are independent .Now whatever we are going to write inside for loop should be indented.Means TAB


  • For loop ends with colon .If condition is inside the for loop and again if condition has some statement. So ideally if condition should have colon at the end and its statement should have one tab space after the if condition
5.Now double click on main.py file so that it gets opened . From this file we need to create instance of that class in the newly created class file.














In the above statements

  • So the functionality is in some other class and some other file.Not in main.py . So logically we need to import that class and its class file to our main.py from which we call the functionality inside that class.Inorder to do so, we need import the class and class file to calling class(main.py). The syntax is 
         from RemoveVowel import removeVowels

        In the above line of code from is a keyword . RemoveVowel word which is present immediately after this from keyword is CLASSFILE name without .py extension. in our case the new file name in which our class and method are present is RemoveVowel.py . So use only the file name here. Now import is also key which and the word which is present immediately after the import keyword is the class name. In our case the class name is removeVowels. You can see the code

  • Create object of the class as per below. In order to access any method,properties,variables inside any CLASS from another class , we need to create the object of that class. The statement is as mentioned below. 
         cls is object of the class removeVowels.open and closed brackets represents you care invoking constructors of the class. This is how we invoke class in any object oriented programming languages 
         cls=removeVowels();

  • Now you have got object of the class. By using the object of the class you class the method as follow
       cls.getLength("wuut")


6. Now execute the code by clicking on execute button in the editor













you can see the output in the output window which is present at the bottom of the editor. You can use the code here CODE


Happy coding!!!.. I will share some more tips and tricks from other programming languages such as server side, client side and database

Comments

Popular posts from this blog

error occurred in deployment step 'recycle iis application pool' :object reference not set to an instance of an object

While deploying using Visual studio, we may get an error "error occurred in deployment step 'recycle iis application pool' :object reference not set to an instance of an object" Solution:  Don't get  panic ..Simply restart the visual studio with the solution which you wanted to deploy on to the site This may help some one. -cheers pradeepa achar

The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults

Hi Guys,   I came across this issue  today. I was totally blank. Later i started observing the issue and realised that, this issue is caused just because of  mismatch of assembly version or any attribute  in Security token service's web.config file (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\WebServices\SecurityToken). Actual issue was i upgraded the assembly version and didn't do changes in web.config file of security token service This blog helped me to fix my issue http://underthehood.ironworks.com/2011/05/sharepoint-2010-an-exception-occurred-when-trying-to-issue-security-token-the-server-was-unable-to-p-1.html thanks pradeepa achar

[Solution] :How to not serialize the __type property on JSON object

While working on ajax- webservice , most of the time we prefer return value from web service method should be in JSON format . If we expect a web service method to return the value as JSON object then it will display few information about the source code through the __type property. Also this property is additional load for response. We ideally need to avoid this property Have a look at the below image in which it is returning JSON object which has my custom properties along with __type property. Here i have used burp suite penetration testing tool to test the loophole in our software. Through fiddler i have got the information about request /response  and wanted to hide error message which gets generated from exception. Usually stack trace may allow hackers to  retrieve error message which  contains code related information by using which hacker can easily find the loophole to hack the function.So when  exception arises hacker should not be...