We may earn a commission if you make a purchase through the links on our website.

Python Cheat Sheet

Python Cheat Sheet

Hitesh J UPDATED: March 24, 2023

Python is an open-source and versatile programming language used by thousands of web developers, data scientists, and software engineers. It is very popular because it has extensive support libraries, user-friendly data structures, and a vibrant support community.

This Python Cheat Sheet can serve as a quick reference for system administrators and network administrators.

We include all essential categories and items including: network programming libraries, keywords, variables and data types, strings, lists, and more.

Basic Python Libraries for Network Admin

Here is the list of basic Python libraries with a brief explanation.

Name Explanation
PIP Package manager for Python.
pypcap Python interface to pcap a packet capture library.
PyPI Repository of open-source Python packages.
Paramiko It is a Python interface around SSH networking concepts.
EDDIE Tool Python agent used for system and network monitoring, security, and performance analysis.
pycos It provides an Asynchronous Socket class to convert Python's blocking socket to a non-blocking socket.
Diesel Framework to write reliable and scalable network applications in Python.
NAPALM A Python.
Celery It is a task queue implementation for Python web applications to asynchronously execute work outside the HTTP request-response cycle.

Python Keywords

Python has a set of keywords (or reserved words) used to define the syntax of the coding. You can not use these words as identifiers, functions, or variable names. Here is a list of such keywords with examples.

Keyword Explanation Example
False, True Boolean value, result of comparison operations False == (1 > 2), True == (1 < 2)
and, or, not Logical operators x, y = True, False
(x or y) == True # True
(x and y) == False # True
(not y) == True # True
break Ends loop prematurely while(True):
print (“Hi How R U?”)
continue Finishes current loop while(True):
print (“200”)


Defines a new class

Defines a new function

class Bear
def __init__(self):
self.content = 1.0
def drink(self):
self.content = 0.0backs = Bear()
if, elif, else Used for a conditional program that starts with if, tries the elif, and finishes with else. x = int(input(“type your value: “))
if x > 4: print(“Big”)
elif x == 4: print(“Medium”)
else: print(Small”)
for, while for a in [0,1,2,3]:
print(a)b = 1
while b < 4:
b = b + 1
in Checks whether element is in sequence. 30 in [10, 30, 40, 70]     # True
is Checks whether both elements point to the same object. a = b = 5
a is b # True
[5] is [5] # False
None Empty value constant def f():
a = 3
f() in None # True
lamda Function with no name (lamda a: a + 4)(4)      # returns 8
return Terminates execution of the function and passes the flow of performance to the caller. def incrementor(a):
return a + 1
incrementor(3)               # return 4

Data Types

In Python programming, everything is an object. Every value in Python is called an “object,” and every object has a specific data type.

There are various data types in Python. Some of the essential types are listed below:

  • int An integer number that represents an object or number such as 3, 4, 5, etc.
  • float Represents ‘floating-point numbers' such as 0.5, 1.25, 2.0, etc.
  • Strings A collection of one or more characters put in a single quotes, double-quotes, or triple quotes.
Data Types
Text Type str
Numeric Types int, float, complex
Sequence Types list, tuple, range
Mapping Type dict
Set Types set, frozenset
Boolean Type bool
Binary Types bytes, bytearray, memoryview

Math Operators

Math operators in Python perform mathematical operations like addition, subtraction, multiplication, and division. Here we list all of the math operators with explanations and examples.

Keyword Explanation Example
** Exponent 3 ** 2 = 9
% Modulus/Remainder 22 % 8 = 6
// Integer division 20 // 8 = 2
/ Division 22 / 8 = 2.75
* Multiplication 2 * 3 = 6
Subtraction 8 – 2 = 6
+ Addition 2 + 8 = 10
== Equal to
!= Not equal to
< Less than
> Greater than
>= Greater than or Equal to
<= Less than or Equal to

Network Services in Python

There are two levels of network services in Python.

  • High-level access This service helps programmers access the application-level network protocols.
  • Low-level access This service allows you to access the socket support for the operating systems by using Python libraries. You can use low-level access to implement connection-less and connection-oriented protocols for performing network programming.

There are several methods available that help network administrators manage the connections. Some of them are listed below.

Method Explanation
listen() Used to establish and start TCP listeners.
bind() Used to bind-address to the socket.
connect() Used to make a connection with the TCP server.
accept() Used to accept TCP client connection, waiting until a connection arrives.
recv() Used to receive TCP messages.
close() Used to close a socket.
Send() Used to send messages.
socket.gethostname() Used to retrieve the hostname.
mysocket.accept() Returns a tuple with the remote address that has connected.
mysocket.bind( address ) Used to attach the specified address to the socket.
mysocket.getpeername() Used to get the remote address where the socket is connected.
mysocket.connect( address ) Used to assign the data sent through the socket to the given remote address.
mysocket.getsockname() Used to retrieve the address of the socket’s local endpoint.
import socket
socket.getservbyname(‘domain name')
Used to obtain port number using the domain name.
import socket
Used to check support for IPV6.
ipaddress.ip_address(‘') Used to assign an IP address.
ipaddress.ip_network(‘') Used to determine the IPv4 or IPv6 version.
Used to obtain the network from an interface.
Used to find the number of IP addresses in the specified network.
Used to find the netmask of the specified IP address.

Python Internet Modules

Here is the list of all Python internet modules.

Protocol Explanation Port Python Module
Gopher Used to transfer documents. 70 Gopherlib, urllib
Telnet Used to open a command line on a remote computer. 23 telnetlib
IMAP4 Used for fetching emails from the mail server. 143 impalib
POP3 Used for fetching emails from the mail server. 110 poplib
SMTP Used for sending emails. 25 smtlib
FTP Used for file transfers. 20 Ftblib, urllib
MNTP Usenet news 119 mntplib
HTTP Used to access web pages over the internet. 80 Httplib, urllib


We've included all useful Python commands, libraries, and code that you can use as a quick reference in the above guide. Feel free to let us know in the comments if you have any questions.

Python Cheat Sheet FAQs

What are some common uses for Python?

Some common uses for Python include web development, scientific computing, data analysis, artificial intelligence, and desktop applications.

How do I get started with Python?

To get started with Python, you will need to install a Python interpreter and a code editor, such as IDLE or PyCharm. Then, you can start writing and executing Python code, such as basic mathematical operations, loops, and functions.

What are some popular libraries and frameworks for Python?

Some popular libraries and frameworks for Python include NumPy, Pandas, Matplotlib, Django, Flask, and TensorFlow.

What are some best practices for writing Python code?

Some best practices for writing Python code include using clear and descriptive variable names, writing documentation, following the PEP 8 style guide, and using version control, such as Git.

What are the differences between Python 2 and Python 3?

Python 2 and Python 3 are different versions of the Python programming language, with Python 3 being the latest and most updated version. Some of the key differences between the two versions include improved support for unicode, changes to the division operator, and the introduction of new features, such as async and await.