Python Introduction Tutorial – Variable, String, Function Examples

by Himanshu Arora on April 10, 2013

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
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

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
print "Hello World"
print "This is my first python program"

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

$ python
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

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
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

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
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
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
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
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"


Here is the output :

$ python
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
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
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.

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

{ 13 comments… read them below or add one }

1 Carlos Nucette April 10, 2013 at 7:50 am

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

2 Jyoti Ranjan April 10, 2013 at 8:47 am

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

3 Sushanth Somayaji April 10, 2013 at 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 ()
print (“Hello world”)
instead of
print “Hello World”
as in Pyhton 2.x
str = “Hello World”
pyhton (str)
instead of
print str

4 Bob April 10, 2013 at 10:18 am

Great article as usual. Thanks!!!

5 Anthony April 10, 2013 at 11:46 am

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

6 HBU April 11, 2013 at 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.

7 Rajesh April 12, 2013 at 3:03 am

Good one. expecting next lesson.

8 Mahesh April 13, 2013 at 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.

9 Richa Singh April 23, 2013 at 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)?

10 lesniks May 2, 2013 at 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)?

11 Brian Zimmerman May 3, 2013 at 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.

12 Mustapha Oldache June 24, 2013 at 10:56 am

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

13 divya joshi April 5, 2014 at 12:31 pm

thx. and regard

Leave a Comment

Previous post:

Next post: