Python first appeared in 1991, and it has become one of the most popular interpreted programming languages. It’s often called as “Scripted languages” as it is used to quickly write small programs or scripts to automate tasks.
Among interpreted languages, for various historical and cultural reasons, Python has developed a large and active scientific computing and data analysis community. In the last 10 years, Python has gone from a bleeding-edge or “at your own risk” scientific computing language to one of the most important languages for data science, machine learning, and general software development in academia and industry.

Basics of Programming

Printing to the screen

The simplest way to produce output is using the print statement where you can pass more expressions separated by commas. This function converts the expressions you pass into a string and writes the result to standard output as follows
E.g.: print “roses are red,”,”sky is blue”
And the result produced on standard screen is as follows

roses are red, sky is blue

Python has two basic modes

  • Interactive
  • Script


  • It is a command line shells which gives immediate feedback, while running previous statements in active memory. i.e., as we add new line into the interpreter the program is evaluated both in part and in whole
  • In interactive mode what we type is immediately run
  • If you ever feel the need to play with new python statements of your own go to interactive statement and try them out


> 5


> print(5*7)


> “hello” * 4


> “hello”.__class__

<type ‘str’>


  • Invoking the interpreter with a script parameter begins execution of the script and continues until the script is finished.
  • When the script is finished, the interpreter is no longer active.
  • Python files have extension .py.


print “Hello, Python!”

Now, try to run this program as follows −

$ python

This produces the following result −

Hello, Python!


  • Based on the data type of a variable we store integers, decimals and characters
  • The equal sign(=) is used to assign values to the variables
  • i.e., The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable.


A = 100 # An integer assignment

distance = 1000.0 # A floating point

name = “John” # A string

print A

print distance

print name

In the above example A, distance and name are the names of the variables and 100, 1000.0, “john” are the integer, float and string values assigned to the variables.

Python allows us to assign single value to multiple names and it also allow us to assign multiple objects to multiple variables simultaneously.


x = y= z = 10

a,b,c = 1,2,”john”

Since the data stored is of many types, Python has various standard data types that are used to define the operations possible on them and the storage method for each of them.

Python has five standard data types

  • Number
  • String
  • List
  • Tuple
  • Dictionary

Number objects are created when we assign a value to them

d = 1

a= 10

Python supports four different numerical types −

  • int (signed integers)
  • long (long integers, they can also be represented in octal and hexadecimal)
  • float (floating point real values)
  • complex (complex numbers)



It’s a contiguous of characters represented in quotation marks. Python allows for either pairs of single or double quotes. Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end.

str = ‘Hello World!’

print str                       # Prints complete string

print str[0]                # Prints first character of the string

print str[2:5]            # Prints characters starting from 3rd to 5th

print str[2:]             # Prints string starting from 3rd character

print str * 2             # Prints string two times

print str + “TEST” # Prints concatenated string



Lists are the most versatile of Python’s compound data types. A list contains items separated by commas and enclosed within square brackets ([]).The values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes starting at 0 in the beginning of the list and working their way to end -1. The plus (+) sign is the list concatenation operator, and the asterisk (*) is the repetition operator.

list = [ ‘abcd’, 786 , 2.23, ‘john’, 70.2 ]

tinylist = [123, ‘john’]

print list                     # Prints complete list

print list[0]               # Prints first element of the list

print list[1:3]           # Prints elements starting from 2nd till 4th

print list[2:]            # Prints elements starting from 3rd element

print tinylist * 2     # Prints list two times

print list + tinylist  # Prints concatenated lists


result −
[‘abcd’, 786, 2.23, ‘john’, 70.200000000000003]


[786, 2.23]

[2.23, ‘john’, 70.200000000000003]

[123, ‘john’, 123, ‘john’]

[‘abcd’, 786, 2.23, ‘john’, 70.200000000000003, 123, ‘john’]



Tuple is sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. tuples are enclosed within parentheses.
The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as readonly lists


tuple = ( ‘abcd’, 786 , 2.23, ‘john’, 70.2 )

tinytuple = (123, ‘john’)

print tuple                                # Prints complete list

print tuple[0]                          # Prints first element of the list

print tuple[1:3]                      # Prints elements starting from 2nd till 3rd

print tuple[2:]                       # Prints elements starting from 3rd element

print tinytuple * 2                # Prints list two times

print tuple + tinytuple       # Prints concatenated lists

result −

(‘abcd’, 786, 2.23, ‘john’, 70.200000000000003)


(786, 2.23)

(2.23, ‘john’, 70.200000000000003)

(123, ‘john’, 123, ‘john’)

(‘abcd’, 786, 2.23, ‘john’, 70.200000000000003, 123, ‘john’)



Python’s dictionaries work like associative arrays and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.

Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]).


dict = {}

dict[‘one’] = “This is one”

dict[2] = “This is two”

tinydict = {‘name’: ‘john’,’code’:6734, ‘dept’: ‘sales’}

print dict[‘one’] # Prints value for ‘one’ key

print dict[2] # Prints value for 2 key

print tinydict # Prints complete dictionary

print tinydict.keys() # Prints all the keys

print tinydict.values() # Prints all the values

result −

This is one

This is two

{‘dept’: ‘sales’, ‘code’: 6734, ‘name’: ‘john’}

[‘dept’, ‘code’, ‘name’]

[‘sales’, 6734, ‘john’]


Operators are constructs which can manipulate the values of operands.


here 10 and 5 are operands , – is an operator

In python we have the following operators

  • Arithmetic Operators
  • Comparison (Relational) Operators
  • Assignment Operators
  • Logical Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators

Arithmetic Operators

Arithmetic operators perform arithmetic calculations like addition, subtraction, multiplication, division, modulus etc,

x= 4

y= 5

print(x + y)

print(y – x )

print(x * y)

print(y % x)

print(x ** y)

Comparison (Relational) Operators

These operators compare values and determine the relation between two operand , hence its referred as relational operators.
<,>,<=,>=,<>,!=,== are the comparison operators

x = 4

y = 5

print((‘x > y is’,x>y))

Assignment Operators

These are used for assigning the values from right operand to the left operand

num1 = 4

num2 = 5

print((“Line 1 – Value of num1 : “, num1))

print((“Line 2 – Value of num2 : “, num2))

res = num1 + num2 res += num1

print((“Line 1 – Result of + is “, res))

Logical Operators

These are used for conditional statements like true or false. Logical operators of python are AND,OR and NOT.

a = True

b = False

print((‘a and b is’,a and b))

print((‘a or b is’,a or b))

print((‘not a is’,not a))

Bitwise Operators

Bitwise operator works on bits and performs bit by bit operation.

Assume if a = 60; and b = 13;

Now in binary format they will be as follows −

a = 0011 1100

b = 0000 1101

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a = 1100 0011

Membership Operators

These operators test for membership in a sequence such as list,tuple etc..

In , not in- gives result based on variables present or not.

x = 4

y = 8

list = [1, 2, 3, 4, 5 ];

if ( x in list ):

print(“Line 1 – x is available in the given list”)


print(“Line 1 – x is not available in the given list”)

if ( y not in list ):

print(“Line 2 – y is not available in the given list”)


print(“Line 2 – y is available in the given list”)

Identity Operators

It is used to compare the memory locations of two objects.

Is and isnot are the two identity operators

Operator is: It returns true if two variables point the same object and false otherwise

Operator is not: It returns false if two variables point the same object and true otherwise.


About Shwetha S:

Shwetha.S is M.Sc(Mathematics). She is working as Analyst Intern with Nikhil Guru Consulting Analytics Service LLP (Nikhil Analytics), Bangalore.

Be the first to comment on "BASICS OF PYTHON"

Leave a comment

Your email address will not be published.



Subscribe for Data Analytics Edge Newsletter & Share..:-)

error: Content is protected !!