Wednesday, March 14, 2012

More Stuff on Methods

Hello Everyone, in this post I will discuss the use of some more stuff about methods.

Lets open up the IDLE.

We will create a string and assign it to a variable, like,

>>> python="Hey Visitor, how is your girlfriend?"
>>> print python
Hey Visitor, how is your girlfriend?

Now, what if we want the Python to use the Visitors name. Python should say Hey Thomas, how is your girlfriend?

If you have read the earlier posts, by now you should say, hey its possible in this way.

>>> name=raw_input("Enter Your name here: ")
Enter Your name here: Subir
>>> print "Hey "+name+" How is Your Girlfriend?"
Hey Subir How is Your Girlfriend?

Now lets see the other way.

Suppose we want the Python to display something like "Hey <name>, your age is <age>"
name and age is what the user enters prior to display.

>>> python = "Hey %s, your age is %d"     [%s declares that a string type data will go here, and %d declares that an integer type will go here]
>>> var = ('Subir', '24')  ['%s will be replaced with Subir, and %d will be replaced with 24']
>>> print python % var  [Indicates the Python that print the line python, and in the line wherever you see %, replace it with the elements of var]

Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    print python % var
TypeError: %d format: a number is required, not str
>>> var = ('Subir',24)
>>> print python % var
Hey Subir, your age is 24

Note: %s is used for string type data, and %d is for integer type data, and we know that strings  are written inside quotes, and integers are not inside quotes.
This is the reason why i got an error, when i declared 24 inside quotes.

Lets make a small program, that would ask the user his name, age and country, and display the same.

Lets open up the Python text editor, and type

name = raw_input("Enter Your Name Here: ")
age = input("Enter Your Age Here: ")
country = raw_input("Enter your Country Name Here: ")
python = "Hello %s, If I understand you correctly, Your age is %d, and You are from %s"
varb = (name, age, country)
raw_input("Press Enter, to see if Python understood you correctly")
print python % varb
raw_input("Press Enter To Exit")

Now Save the file as, and execute the program by pressing f5, and This is what i get as my display.

Enter Your Name Here: Subir
Enter Your Age Here: 24
Enter your Country Name Here: India
Press Enter, to see if Python understood you correctly
Hello Subir, If I understand you correctly, Your age is 24, and You are from India
Press Enter To Exit

Now lets jump to another function known as "find", this helps to find the starting index number of a given string.


>>> python = "I knew Python was a Snake, but now i know Python is a Programming Language"
>>> python.find('Python')
>>> python.find('snake')
>>> python.find('whiskey')
>>> python.find('Snake')

If we count the characters(including spaces), starting from 0, as we know everything in Python, like most other programming language starts at index 0, we will find that the word Python starts at index 7.

And whenever Python cannot find the given word, it returns a negative 1, means I don't know what you are searching.

And notice that its case sensitive, finding snake is not equal to finding Snake.

Okay so I hope This was informative for you, practice this, and don't forget to check on my next post.

Thank You.


Post a Comment