Search This Blog

Monday, October 5, 2015

Python Signals and IPC

What does this script do?

Creates a tcp server which listens on a port. Implements signals to ensure it automatically shutsdown after a pre-configured duration which is given through command line

Python version: 2.6

[root@host-172-17-95-7 module2]# cat signals_exercise.py
import signal, sys
from socket import *
from optparse import OptionParser

HOST = ""
PORT = 37845
ADDR = (HOST,PORT)
BUFSIZE = 4096

# Create a raw socket
serv = socket(AF_INET, SOCK_STREAM)

def option_parser():
 parser = OptionParser()
 parser.add_option('-t','--time',help="Time for server to server",action='store',type=int,\
 dest='time_to_serv')
 (options, args) = parser.parse_args()
 return options,args


def signal_handler(signum,frm):
 print "Got signal: %s" %signum
 print "Shutting down the server"
 serv.shutdown(SHUT_WR)
 print "Closing the server"
 serv.close()
 sys.exit()

def bind_server():
 print "Binding tcpServer"
 serv.bind((ADDR))
 serv.listen(100) # Listen time is greater than time_to_serv

def trigger_alarm(time_to_serv):
 print "Triggering signal"
 signal.signal(signal.SIGALRM,signal_handler)
 signal.alarm(time_to_serv)
 print "Done!"

if __name__ == '__main__':
 options, args = option_parser()
 bind_server()
 trigger_alarm(options.time_to_serv)

 while True:
 pass
[root@host-172-17-95-7 module2]# python signals_exercise.py -t 5
Binding tcpServer
Triggering signal
Done!
Got signal: 14
Shutting down the server
Closing the server
[root@host-172-17-95-7 module2]#
[root@host-172-17-95-7 module2]# netstat -nap | grep 37845
[root@host-172-17-95-7 module2]# netstat -nap | grep python

Monday, September 28, 2015

Threading and Queues

from scapy.all import *
import threading
import Queue
import time

class WorkerThread(threading.Thread):
    def __init__(self,queue):
        threading.Thread.__init__(self)
        self.queue = queue

    def run(self):
        print "In worker thread"
        while True:
            counter = self.queue.get()
            print "Ordered to sleep for %d seconds" %counter
            time.sleep(counter)
            print "Completed the job. Slept for %d seconds" %counter
            self.queue.task_done()

queue = Queue.Queue()

for i in range(10):
    print "creating worker thread %d" %i
    worker = WorkerThread(queue)
    worker.setDaemon(True)
    worker.start()
    print "Worker thread %d created" %i


for j in range(10):
    queue.put(j)

queue.join()

print "All Tasks Over!"


OUTPUT:
======
/usr/bin/python /home/dvaidyula/PycharmProjects/spse/scapy-2.3.1/tcp_connect_scan.py
WARNING: No route found for IPv6 destination :: (no default route?)
creating worker thread 0
In worker thread
Worker thread 0 created
creating worker thread 1
In worker threadWorker thread 1 created

 creating worker thread 2
In worker threadWorker thread 2 created
creating worker thread 3

In worker threadWorker thread 3 created

creating worker thread 4
In worker thread
 Worker thread 4 created
creating worker thread 5
In worker threadWorker thread 5 created
creating worker thread 6
Worker thread 6 created
creating worker thread 7
In worker thread

In worker thread
 Worker thread 7 created
creating worker thread 8
In worker threadWorker thread 8 created
creating worker thread 9

In worker threadWorker thread 9 created

Ordered to sleep for 0 seconds
 Ordered to sleep for 1 seconds
Ordered to sleep for 2 seconds
Completed the job. Slept for 0 seconds
Ordered to sleep for 3 seconds
Ordered to sleep for 4 seconds
Ordered to sleep for 5 seconds
 Ordered to sleep for 7 secondsOrdered to sleep for 6 seconds

Ordered to sleep for 8 seconds
Ordered to sleep for 9 seconds
Completed the job. Slept for 1 seconds
Completed the job. Slept for 2 seconds
Completed the job. Slept for 3 seconds
Completed the job. Slept for 4 seconds
Completed the job. Slept for 5 seconds
Completed the job. Slept for 6 seconds
Completed the job. Slept for 7 seconds
Completed the job. Slept for 8 seconds
Completed the job. Slept for 9 seconds
All Tasks Over!

Process finished with exit code 0

Python threads

import thread, time

def worker_thread(id):
    print "Thread ID %d is now alive" %id
    count = 1
    while True:
        print "Thread with ID %d has counter value %d" %(id, count)
        time.sleep(2)
        count += 1


for i in range(5):
    thread.start_new_thread(worker_thread, (i,))


print "Main Thread Going For An Infinite Loop"
while True:
    pass
       

OUTPUT in windows:
===================

Main Thread Going For An Infinite LoopThread ID 4 is now aliveThread ID 2 is now aliveThread ID 0 is now aliveThread ID 3 is now aliveThread ID 1 is now alive

Thread with ID 4 has counter value 1Thread with ID 2 has counter value 1Thread with ID 0 has counter value 1Thread with ID 3 has counter value 1Thread with ID 1 has counter value 1

Thread with ID 3 has counter value 2Thread with ID 2 has counter value 2Thread with ID 4 has counter value 2Thread with ID 0 has counter value 2Thread with ID 1 has counter value 2

Thread with ID 3 has counter value 3Thread with ID 0 has counter value 3Thread with ID 2 has counter value 3Thread with ID 4 has counter value 3Thread with ID 1 has counter value 3

Thread with ID 0 has counter value 4Thread with ID 3 has counter value 4Thread with ID 2 has counter value 4Thread with ID 4 has counter value 4Thread with ID 1 has counter value 4

Scapy and Python

http://www.secdev.org/projects/scapy/doc/usage.html

Scapy is a Python interpreter that enables you to create, forge, or decode packets on the network, to capture packets and analyze them, to dissect the packets, etc. It also allows you to inject packets into the network. It supports a wide number of network protocols and it can handle and manipulate wireless communication packets.

Scapy can be used to perform the jobs done by many network tools, such as nmap, hping, arpscan, and tshark (the command line of wireshark).

The concept behind Scapy is that it is cable of sending and receiving packets and it can sniff packets. The packets to be sent can be created easily using the built-in options and the received packets can be dissected. Sniffing of packets helps in understanding what communication is taking place on the network.

Building a packet:

[root@deeptiWorkSpace scapy-2.3.1]# ./run_scapy
INFO: Can't import python gnuplot wrapper . Won't be able to plot.
INFO: Can't import PyX. Won't be able to use psdump() or pdfdump().
WARNING: No route found for IPv6 destination :: (no default route?)
Welcome to Scapy (2.3.1)
>>> a=IP(ttl=10)
>>> a
<IP  ttl=10 |>
>>> a.src
'127.0.0.1'
>>> a.dst="10.10.12.39"
>>> a
<IP  ttl=10 dst=10.10.12.39 |>
>>> a.src
'10.10.16.199'
>>> a.ttl
10
>>> del(a.ttl)
>>> a
<IP  dst=10.10.12.39 |>
>>> a.ttl
64

Stacking layers
The / operator has been used as a composition operator between two layers. When doing so, the lower layer can have one or more of its defaults fields overloaded according to the upper layer. (You still can give the value you want). A string can be used as a raw layer.

>>> IP()
<IP  |>
>>> IP()/TCP()
<IP  frag=0 proto=tcp |<TCP  |>>
>>> Ether()/IP()/TCP()
<Ether  type=IPv4 |<IP  frag=0 proto=tcp |<TCP  |>>>
>>> IP()/TCP()/"GET / HTTP/1.0\r\n\r\n"
<IP  frag=0 proto=tcp |<TCP  |<Raw  load='GET / HTTP/1.0\r\n\r\n' |>>>
>>> Ether()/IP()/IP()/UDP()
<Ether  type=IPv4 |<IP  frag=0 proto=ipencap |<IP  frag=0 proto=udp |<UDP  |>>>>
>>> IP(proto=55)/TCP()
<IP  frag=0 proto=mobile |<TCP  |>>

Each packet can be build or dissected (note: in Python _ (underscore) is the latest result):
>>> str(IP())
'E\x00\x00\x14\x00\x01\x00\x00@\x00|\xe7\x7f\x00\x00\x01\x7f\x00\x00\x01'
>>> IP(_)
<IP  version=4L ihl=5L tos=0x0 len=20 id=1 flags= frag=0L ttl=64 proto=ip chksum=0x7ce7 src=127.0.0.1 dst=127.0.0.1 |>

>>> a=Ether()/IP(dst="www.slashdot.org")/TCP()/"GET /index.html HTTP/1.0 \n\n"
>>> print a
▒g寫l▒B▒K▒EC@Ґ

▒▒"▒0PP ▒GET /index.html HTTP/1.0

>>> hexdump(a)
0000   D0 67 E5 AF AB 6C 9E 42  EA 4B FC 5F 08 00 45 00   .g...l.B.K._..E.
0010   00 43 00 01 00 00 40 06  D2 90 0A 0A 10 C7 D8 22   .C....@........"
0020   B5 30 00 14 00 50 00 00  00 00 00 00 00 00 50 02   .0...P........P.
0030   20 00 15 8E 00 00 47 45  54 20 2F 69 6E 64 65 78    .....GET /index
0040   2E 68 74 6D 6C 20 48 54  54 50 2F 31 2E 30 20 0A   .html HTTP/1.0 .
0050   0A                                                 .
>>>


>>> Ether(str(a))
<Ether  dst=d0:67:e5:af:ab:6c src=9e:42:ea:4b:fc:5f type=IPv4 |<IP  version=4L ihl=5L tos=0x0 len=67 id=1 flags= frag=0L ttl=64 proto=tcp chksum=0xd290 src=10.10.16.199 dst=216.34.181.48 options=[] |<TCP  sport=ftp_data dport=http seq=0 ack=0 dataofs=5L reserved=0L flags=S window=8192 chksum=0x158e urgptr=0 options=[] |<Raw  load='GET /index.html HTTP/1.0 \n\n' |>>>>
>>> Ether(str(a))
<Ether  dst=d0:67:e5:af:ab:6c src=9e:42:ea:4b:fc:5f type=IPv4 |<IP  version=4L ihl=5L tos=0x0 len=67 id=1 flags= frag=0L ttl=64 proto=tcp chksum=0xd290 src=10.10.16.199 dst=216.34.181.48 options=[] |<TCP  sport=ftp_data dport=http seq=0 ack=0 dataofs=5L reserved=0L flags=S window=8192 chksum=0x158e urgptr=0 options=[] |<Raw  load='GET /index.html HTTP/1.0 \n\n' |>>>>
>>> Ether(str(a)).hide_defaults()  # Hides all the default values
>>>

Reading PCAP files
==================

You can read packets from a pcap file and write them to a pcap file.

>>> a=rdpcap("/spare/captures/isakmp.cap")
>>> a
<isakmp.cap: UDP:721 TCP:0 ICMP:0 Other:0>

Sending packets
Now that we know how to manipulate packets. Let’s see how to send them. The send() function will send packets at layer 3. That is to say it will handle routing and layer 2 for you. The sendp() function will work at layer 2. It’s up to you to choose the right interface and the right link layer protocol.
>>> send(IP(dst="10.10.16.198")/ICMP())
.
Sent 1 packets.
>>> sendp(Ether()/IP(dst="10.10.16.198",ttl=(1,4)), iface="eth1")
....
Sent 4 packets.

***********************
===== TCP REPLAY =====
***********************
>>> sendp(rdpcap("/tmp/pcapfile")) # tcpreplay
...........
Sent 11 packets.


>>> sendp(rdpcap("/home/dvaidyula/udp_du_bb1_1500.pcap"))
.............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Sent 1501 packets.


Process Creation

import os

def child_process () :
    print "I am the child process and my PID is: %d" %os.getpid()
    print "The child is exiting"


def parent_process():
    print "I am the parent process and my PID is: %d" %os.getpid()
    print "Calling child process"
    childpid = os.fork()

    if childpid == 0:
        #We are inside child
        child_process()
    else:
        #We are inside parent process
        print "We are inside parent process"
        print "The child pid i %d" %childpid

    while True:
        pass

    print "I am still in parent and my PID is: %d" %os.getpid()

parent_process()
   

fork Demo - Python

[dvaidyula@qa-test temp]$ cat fork_demo.py
import os

def parent_process():
    print "I am the parent process with pid %s" %os.getpid()
    childId = os.fork()
    print "I am in the parent loop still and pid is %s" %os.getpid()
    print "ChildID is %s" %childId

    if childId ==  0 :
        child_process()
    else:
        print "We are inside child process and pid is %s" %childId

def child_process():
    print "I am the child process and pid is %s" %os.getpid()
    print "The child is exiting"

parent_process()
[dvaidyula@qa-test temp]$
[dvaidyula@qa-test temp]$ py fork_demo.py
I am the parent process with pid 5317
I am in the parent loop still and pid is 5318
ChildID is 0
I am the child process and pid is 5318
The child is exiting
I am in the parent loop still and pid is 5317
ChildID is 5318
We are inside child process and pid is 5318

### EXECVP ####
[dvaidyula@qa-test temp]$ python
Python 2.6.6 (r266:84292, Jan  2 2013, 10:42:24)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>> import os
>>> os.execvp("ping",["ping","127.0.0.1"])
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.023 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.007 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.005 ms

--- 127.0.0.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.005/0.011/0.023/0.009 ms

[dvaidyula@qa-test temp]$  # Note that once we kill, it is exiting the python prompt / parent process


Directory Navigation in python - Script

import os,re,shutil,fnmatch

ex_path="D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\Glob"

# Clean up
if not os.path.exists(ex_path):
    os.mkdir(ex_path)
else:
    shutil.rmtree(ex_path)
    os.mkdir(ex_path)

os.chdir(ex_path)

# Pre-Requisites
dir_list=[]

for i in range(0,4):
    dir_name="dir_%d" %i
    os.mkdir(dir_name)
    dir_list.append([dir_name])

for directory in dir_list:
    print directory[0]
    dir_path=ex_path+"\\" + directory[0]
    print dir_path
    os.chdir(dir_path)
    for i in range(0,4):
        fdesc=open("file_%s_%s" %(directory[0],i),"w")
        fdesc.close()
 
os.mkdir("sub_directory")
change_path=ex_path+"\dir_3\sub_directory"
print change_path
os.chdir(change_path)
fdesc=open("subdirectory.txt","w")
fdesc.close()

for dirpath, dirs, files in os.walk(ex_path):
    path = dirpath.split("\\")
    print len(path)
    print '|',(len(path))*'---','[',os.path.basename(dirpath),']'
    for f in files:
        print "|",(len(path))*'---',f

     
OUTPUT: ======= >>> ================================ RESTART ================================ >>> dir_0 D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\Glob\dir_0 dir_1 D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\Glob\dir_1 dir_2 D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\Glob\dir_2 dir_3 D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\Glob\dir_3 D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\Glob\dir_3\sub_directory 6 | ------------------ [ Glob ] 7 | --------------------- [ dir_0 ] | --------------------- file_dir_0_0 | --------------------- file_dir_0_1 | --------------------- file_dir_0_2 | --------------------- file_dir_0_3 7 | --------------------- [ dir_1 ] | --------------------- file_dir_1_0 | --------------------- file_dir_1_1 | --------------------- file_dir_1_2 | --------------------- file_dir_1_3 7 | --------------------- [ dir_2 ] | --------------------- file_dir_2_0 | --------------------- file_dir_2_1 | --------------------- file_dir_2_2 | --------------------- file_dir_2_3 7 | --------------------- [ dir_3 ] | --------------------- file_dir_3_0 | --------------------- file_dir_3_1 | --------------------- file_dir_3_2 | --------------------- file_dir_3_3 8 | ------------------------ [ sub_directory ] | ------------------------ subdirectory.txt >>> >>> os.stat("D:\Deepti\SPSE-Course-DVD\Exercises\sample.xml" ) nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=219334L, st_atime=1441614421L, st_mtime=1441614386L, st_ctime=1441614421L) >>> os.lstat("D:\Deepti\SPSE-Course-DVD\Exercises\sample.xml") nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=219334L, st_atime=1441614421L, st_mtime=1441614386L, st_ctime=1441614421L)


Directory Navigation in Python - Command line

import os
import glob
import shutil
import fnmatch
import re


ex_path="D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\Glob"
#Cleanup
print "Cleaning up"
if os.path.exists("D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\NewDirectory"):
    os.rmdir("D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\NewDirectory")

if os.path.exists(ex_path):
    # Remove directory and all its contents
    shutil.rmtree(ex_path)

print "Display the current directory path"
print "   %s" %os.getcwd()

print "Create a new directory"
os.mkdir("D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\NewDirectory")

print "List contents of directory"
print os.listdir("D:\Deepti\SPSE-Course-DVD\Exercises\Module 2")

for item in os.listdir("D:\Deepti\SPSE-Course-DVD\Exercises\Module 2"):
    if os.path.isfile(item):
        print "  %s is a file" %item
    elif os.path.isdir(item):
        print "  %s is a directory" %item
    else:
        print "  %s: Unknown filetype" %item
     
print "Remove directory"
os.rmdir("D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\NewDirectory")

print "List contents of directory"
print os.listdir("D:\Deepti\SPSE-Course-DVD\Exercises\Module 2")



# Pre-requisites

os.mkdir("D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\Glob")


file1=open(os.path.join(ex_path,"pythonfile.py"),"w")
file1.close()

file2=open(os.path.join(ex_path,"textfile.txt"),"w")
file2.close()

file3=open(os.path.join(ex_path,"swapfile.swp"),"w")
file3.close()

print "List contents of directory"
print os.listdir(ex_path)


# Exercise on glob
print "---- Print all the python files ----"
print glob.glob(os.path.join(ex_path,"*.py"))

# Exercise on fnmatch
print "---- Match file pattern using fnmatch ----"
print fnmatch.fnmatch(os.path.join(ex_path,"swapfile.swp"),"*.swp")
print fnmatch.fnmatchcase(os.path.join(ex_path,"SWAPFILE.swp"),"*.swp")
print fnmatch.fnmatchcase(os.path.join(ex_path,"waspfile.swp"),"*.swp")

# Prints False
print fnmatch.fnmatchcase(os.path.join(ex_path,"swapfile.SWP"),"*.swp")

fnmatch_pattern=fnmatch.translate("*.swp")
print fnmatch_pattern

reobj=re.compile(fnmatch_pattern)
print reobj

print reobj.match('swap.swp')

# Print set of files from a list of files
Files=['__init__.py', 'fnmatch_filter.py', 'fnmatch_fnmatch.py', 'fnmatch_fnmatchcase.py', 'fnmatch_translate.py', 'index.rst']
print 'Matches:', fnmatch.filter(Files,"*.py")







 

Thursday, March 5, 2015

Data Types: Tuples, Sets, Dictionaries

Tuples:


Tuples are similar to lists but they are immutable. We can convert as list to tuple ans vice versa.

>>> mytuple=tuple(["Harry Potter",25,100,3])
>>> mytuple

('Harry Potter', 25, 100, 3)

Note that tuple has () instead of a list's []

>>> moviename,runningTime,upVotes,downVotes=mytuple
>>> moviename
'Harry Potter'
>>> runningTime
25
>>> upVotes
100
>>> downVotes

3


Sets:

Sets are unordered collection of unique objects.

List to a set: myset = set(moviename)
Set to list: mylist = list(myset)

>>> a=set([1,2,3,3,2])
>>> a

set([1, 2, 3])

Sets have only unique items

>>> b=set([3,4,5])
>>> b
set([3, 4, 5])
>>> a|b

set([1, 2, 3, 4, 5])

Intersection of sets

>>> a&b
set([3])

>>> 


>>> a-b

set([1, 2])
>>> b-a
set([4, 5])

>>> 

Sets are useful in finding different unique values


Dictionaries:


Dictionaries are unordered key-value pairs

a={}
Empty dictionary

>>> a={"Movie":"Harry Potter", "sitCom":"Big bang theory"}
>>> print a
{'Movie': 'Harry Potter', 'sitCom': 'Big bang theory'}










Variables and Data Types


             Reference
Name ----------------> Deepti
Variable                     Object


  • "Name", which is a variable acts as a reference to "Deepti" which is an object
  • It is the object that has the data type associated with it,


>>> id(name)
42209536
>>> hex(id(name))
'0x2841100'
>>> name.__repr__
<method-wrapper '__repr__' of str object at 0x02841100>
>>> 


>>> name = "Deepti"
>>> name = 'deepti'
>>> name = "Deepti\nvaidyula"
>>> name
'Deepti\nvaidyula'



  • # The 'r' here tells that it is a raw string and don't interpret the \n

>>> name = r"Deepti\nvaidyula" 
>>> name
'Deepti\\nvaidyula'

>>> print name
Deepti\nvaidyula
>>> name = "Deepti\nvaidyula"
>>> print name
Deepti
vaidyula


  • # Python also supports unicode

>>> name = u"Deepti"
>>> name

u'Deepti'

# Convert unicode to String - use str
>>> str(name)
'Deepti'
>>> name
u'Deepti'


  • Use 'unicode' to convert to unicode




  • We cannot directly change string objects in memory as they are immutable

>>> name = "Daniel"
>>> name[0]
'D'
>>> name[0] = 'a'

Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    name[0] = 'a'

TypeError: 'str' object does not support item assignment
>>> name = "Radcliffe"
>>> name

'Radcliffe'


>>> a="Daniel"
>>> name = a
>>> name
'Daniel'
>>> a

'Daniel'



>>> a="Radcliffe"
>>> a
'Radcliffe'
>>> name
'Daniel'

String Concatenation

fn = "deepti"
ln = "Vaidyula"

full_name = fn + ln
print full_name
full_name = fn + ' ' + ln
print full_name

>>> 
deeptiVaidyula
deepti Vaidyula

  • Python like most high languages has garbage collection. When any object is no longer referenced by any variable, it is automatically cleaned up.
  • Repeated sequence in string
>>> rhyme = "twinkle"
>>> print rhyme*2 + " little star"
twinkletwinkle little star

String Slicing


  • string[start:end:steps]


>>> num="123456789"
>>> print num[5]
6
>>> print num[5:8]
678
>>> print num[5:8:2]
68
>>> print num[5:8:2]
KeyboardInterrupt
>>> print num[5:8:1]
678
>>> print num[5:9:1]
6789
>>> print num[5:9:2]
68
>>>


  • num.find returns the index

>>> num.find("5")
4

>>> num.find("abc")
-1
>>>
"abc" is not present in num

>>> name = "Daniel Radcliffe"
>>> name.split()
['Daniel', 'Radcliffe']
>>> 

>>> name = "Daniel:Radcliffe"
>>> name.split(":")
['Daniel', 'Radcliffe']

>>> name.replace("Daniel", "Ginny")

'Ginny:Radcliffe'

String Formatting

>>> ip="192.168.12.38"
>>> print "Hack this ip : %s" %ip
Hack this ip : 192.168.12.38

Operations and Lists



>>> 2 + 2
4
>>> list=[1,2,3,4]
>>> list[0]
1
>>> list[-1]
4
>>> len(list)

4


  • Lists are heterogenous. So, we can mix numbers and alphabet


>>> mylist=[1,2,3,4,["a","b","c"]]
>>> mylist
[1, 2, 3, 4, ['a', 'b', 'c']]
>>> len(mylist)

5
>>> mylist.append(5)
>>> mylist
[1, 2, 3, 4, ['a', 'b', 'c'], 5]
>>> mylist.reverse()
>>> print mylist.reverse()
None
>>> mylist.pop()
5
>>> mylist
[1, 2, 3, 4, ['a', 'b', 'c']]
>>> mylist.insert(4,"inserted")
>>> mylist

[1, 2, 3, 4, 'inserted', ['a', 'b', 'c']]
>>> mylist

[1, 2, 4, 'inserted', ['a', 'b', 'c']]