≡ Menu

Python Introduction Tutorial – Variable, String, Function Examples

Python is an object oriented language with can be used in both scripting and non scripting contexts. Python has clean and easy syntax that makes it easy to read. In this article, we will understand python language from scratch through examples. We will often compare Python programming features/syntax with some popular languages like C/C++. Also, we will be using Linux command line for all the 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.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Carlos Nucette April 10, 2013, 7:50 am

    Nice tutorial, but it would be more useful if it were about python 3.

  • Jyoti Ranjan April 10, 2013, 8:47 am

    Thanks Himanshu. Nice starting python info.. Keep it up.

  • Sushanth Somayaji April 10, 2013, 9:11 am

    The above commands are for Python 2.x, there are a few changes in Python 3.x…
    One of those which I know of is that print statement should have the variable or the string inside parenthesis ()
    e.g.
    print (“Hello world”)
    instead of
    print “Hello World”
    as in Pyhton 2.x
    OR
    str = “Hello World”
    pyhton (str)
    instead of
    print str

  • Bob April 10, 2013, 10:18 am

    Great article as usual. Thanks!!!

  • Anthony April 10, 2013, 11:46 am

    Great article, I like it all. Keep them coming.

  • HBU April 11, 2013, 3:05 am

    What is the purpose of defining num=5 if you specify that a string will be printed 5 times in the function definition itself? Or vice versa?

    print “5times ” * 5 #this should be sufficient to print it 5 times, no?

    Thanks and regards.

  • Rajesh April 12, 2013, 3:03 am

    Good one. expecting next lesson.

  • Mahesh April 13, 2013, 8:11 am

    It was a great post.

    I am in C programming, so i am not an experienced python programmer, but I think
    the last line of the function myFunc(param1,param2) should be:-
    print param1 * param2

    rather than:
    print “5times ” * 5

    Please point out if I am wrong.

  • Richa Singh April 23, 2013, 1:16 am

    Thanks lot !
    Awesome precise piece of start-up.
    Where can I access the advance version lesson of the same (dealing with some new and complex aspects of python as you said)?

  • lesniks May 2, 2013, 11:35 pm

    Thanks lot !
    Awesome precise piece of start-up.
    Where can I access the advance version lesson of the same (dealing with some new and complex aspects of python as you said)?

  • Brian Zimmerman May 3, 2013, 12:07 am

    If you put parenthesis around your print() statement, the above examples are good for both the latest Python 2 (optional for 2.7) and Python 3. It is a good start, please continue explaining Python and I will continue to return.

  • Mustapha Oldache June 24, 2013, 10:56 am

    Hi !
    We want mutch like this. It is so cool to learn python !

  • divya joshi April 5, 2014, 12:31 pm

    thx. and regard