Secure coding

Python Language Basics: Variables, Lists, Loops, Functions and Conditionals

Nitesh Malviya
December 15, 2020 by
Nitesh Malviya


Python is a general purpose high-level, object-oriented, interpreted programming language. Python was created by Guido van Rossum around 1990 and today it is one of the widely used programming languages in day to day life. 

Python being an interpreted language does not require compilation like other languages like Java, C++ etc. Python code can directly be run during run-time. Also, Python supports Object Oriented Programming and its code is very easy to write and developers do not have to spend much time on Syntax, these features making python as one of the best programming language.

Now we have a small idea of what python is and 2 why it should be adopted and used, let's learn the basics of Python Language.

Note - All the codes shown below have been run on Python 2.7 and reader is assumed to already have python setup and running on their system.

Learn Python for Pentesting

Learn Python for Pentesting

Build your Python pentesting skills with four hands-on courses courses covering Python basics, exploiting vulnerabilities, and performing network and web app penetration tests.

Hello World in Python

The first step any programmer takes is to learn how to write hello world and it can be done simple in python by using following command -  

print “Hello World”

As seen it is very easy to program in python as compared to other programming language where one has to type too much and focus on syntax for printing a mere “Hello World”

Now we know how to print Hello World, let’s deep dive into python basics and get started. 


Variables are basically reserved memory locations for storing values. Unlike other languages, one does not have to specify the type of variable to be declared. The interpreter automatically identifies the type of variable and allocates memory accordingly.

Let’s see how variables can be declared - 

As seen above, we have not defined the variable type but when checked, the interpreter shows their type and allocates memory accordingly.

Multiple assignments are also possible. Example 

a=b=c=1 will declare a, b and c having value 1. 


Lists are used for storing multiple items in a single variable. Items in the list are separated by command and enclosed within square brackets. Items in the lists can be of different data types and are similar to arrays in Java language. The first item has an index of 0 and not 1.

Below screenshot shows list declaration and how various operations on lists are performed. These operations are self explanatory - 

Loops in Python

We all know about loops in programming language. Let’s understand how various loops can be defined and used in Python. 

Note - Below screenshots are obtained by running code in an online interpreter. I am using for running python code. 

  • While -  While loop can be declared and used as shown below - 

  • For - For loop can be used as follows - 

  • Break - Break can be used as follows - 

  • Continue - Continue can be used as follows - 

  • Pass - Pass can be used as follows - 


Functions are a block of code which is used to perform the same operation again and again. Functions help us to reuse the same code and provide better modularity.

Syntax -

def function_name(parameters):

   code to run in the function


Example -

def print_string(str):

   print str


The above function has name print_string and takes a parameter str which is printed as defined on this line - “print str”

Calling a Function

The above function can be called as shown below - 


Tuple is a collection of objects. Tuples are like lists but the main difference is tuples are immutable and cannot be modified or altered. Thus, one cannot update a tuple and if there is a need to update the tuple then the user has to define a new one.

Below screenshot shows how tuple can be defined and how various operation can be done over tuples - 

Learn Secure Coding

Learn Secure Coding

Build your secure coding skills in C/C++, iOS, Java, .NET, Node.js, PHP and other languages.


Dictionary is a key-value pair in which each key is separated from its value by colon(:). Multiple key value pairs are separated by , and the whole dictionary is enclosed in {}. Keys need to be unique while values may not be. 

Various operations can be performed on dictionary as follows - 



Nitesh Malviya
Nitesh Malviya

Nitesh Malviya is a Security Consultant. He has prior experience in Web Appsec, Mobile Appsec and VAPT. At present he works on IoT, Radio and Cloud Security and open to explore various domains of CyberSecurity. He can be reached on his personal blog - and Linkedin -