Python Introduction Tutorial – Variable, String, Function Examples

This is a new series. We’ll be publishing several articles on Python on an on-going basis.

1. Hello World Python Program

Here is the simple “Hello World” example in python:

$ vi firstPYProgram.py
print "Hello World"

Note that the ‘print’ in python does not require parenthesis.

Now run the above python program on command line in the following way :

$ python firstPYProgram.py

Here is the output :

Hello World

So we see that the single ‘print’ statement executed and the string “Hello World” was displayed in output.

Now lets try adding another ‘print’ statement to the python script example:

$ cat firstPYProgram.py
print "Hello World"
print "This is my first python program"

Now when you run this again, here is the output:

$ python firstPYProgram.py
Hello World
This is my first python program

This output was expected but what I intended to show was that unlike C, or C++ hello world, python adds a newline implicitly after each print statement.

2. Variables and Strings in Python

Python supports integer numbers and floating point numbers. Though it also supports complex numbers but we will not go into much details of that in this basic tutorial.

Here is how an integer can be used :

myIntVar = 5

Here is how a floating point numbers can be used :

myFloatVar = 5.5
myFloatVar = float(8)

Mathematical operations can be applied in the following way :

var_a = 1
var_b = 4
var_c = var_a + var_b

Note that the operator ** is used in python to make ‘power of’ relationship between two numbers.

The mathematical operation (like the one shown above) are pretty much like the way it’s done in C/C++ but playing with strings is a bit different in python.

Here is an example :

str_a = "The"
str_b = "Geek"
str_c = "Stuff"
string = str_a + str_b +str_c
print string

If you run the python script shown above, this output will be produced :

$ python firstPYProgram.py
TheGeekStuff

So we see that ‘+’ operator can be used in python to concatenate strings easily.

Note that strings in python can be declared using both single and double quotes. Using double quotes is advised when the string contains apostrophes.

For example :

$ cat firstPYProgram.py
str_a = 'The'
str_b = "Geek's"
str_c = 'Stuff'
string = str_a + str_b +str_c
print string

The code above produces the following output :

$ python firstPYProgram.py
TheGeek'sStuff

You can also make a string repeat ‘n’ number of times in python by multiplying the string with that number. Here is an example :

$ cat firstPYProgram.py
print "5times " * 5

Observe that the code above multiplies the string “5times” with the number 5. Here is the output when the above print statement is executed :

$ python firstPYProgram.py
5times 5times 5times 5times 5times

So we see that the string was repeated 5 times in the output.

Though python differs significantly from C/C++ in terms of string manipulation, there is an area ‘string formatting’ which is not much different from the way it is done in C/C++.

Here is an example :

$ cat firstPYProgram.py
string = "[5times]"
num = 5
print "Observe that the string %s will be displayed %d times" % (string, num)
print "5times " * 5

Here is the output of the code above :

$ python firstPYProgram.py
Observe that the string [5times] will be displayed 5 times
5times 5times 5times 5times 5times

So you see that same format specifiers like %s, %d etc can be used in python also.

3. How to Declare Functions in Python?

In python, the declaration of functions is very different when compared with the declaration of functions in C/C++.  Here is how a function is declared in python :

 def myFirstFunction(param1, param2):

If you try to understand the above specified declaration, you will find that in python the keyword ‘def’ is used to begin the declaration of a function. This is followed by the function name and comma separated arguments in the parenthesis.

Note that functions in python do not specify return types. Each function returns either something specific or ‘None’ (equivalent to NULL in C/C++). Similarly, the arguments also do not specify the data-types.

Here is an example of how a function is declared and called in a python script:

def myFunc():
        print "function called"

myFunc()

Here is the output :

$ python firstPYProgram.py
function called

4. Summary- A Basic Python Example Program

Here is a program that will summarize all that we learnt about python in this article.

$ vi firstPYProgram.py
def myFunc(param1, param2):
  print "Observe that the string %s will be displayed %d times" % (param1, param2)
  print "5times " * 5

string = "[5times]"
num = 5
myFunc(string, num)

Here is the output :

$ python firstPYProgram.py
Observe that the string [5times] will be displayed 5 times
5times 5times 5times 5times 5times

In the next part of this series we will get into some new and complex aspects of python.

Enjoyed this article? Stay informed by joining our newsletter!

Comments

You must be logged in to post a comment.

About Author

Tech Foorti is the first largest tech company that helps people to earn money by just writing articles.