Python Cheatsheet Ebook
Python Cheatsheet Ebook
cheatsheet
Beginners
Indian servers WWW.INDIANSERVERS.COM
Main datatypes
Integers (int) - an integer number to represent an object
such as “3”.
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Main datatypes
List - Python Lists are similar to arrays in C. However, the
list can contain data of different types ex ["apple",
"banana", "cherry"]
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Output:- IndianServers
String Replication
'Indian Servers ' * 5 'Indian Servers Indian Servers
Example :- Indian Servers Indian Servers Indian Servers
Output:-
Indian Servers Indian Servers Indian Servers Indian
Servers Indian Servers
As the name implies, this command lets you repeat the same
string several times. This is done using * operator.
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Math operators
Operators Operation Example
** Exponent 2 ** 3 = 8
% Modulus 21 % 2 = 1
// Integer Division 22 / 8 = 2
/ Division 21 / 3 = 7
* Multiplication 3*3=9
- Subtraction 2-1=1
+ Addition 1+1=2
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Built-in Functions
Input() function :- input() function is a simple way to prompt
the user for some input (e.g. provide their name). All user input is
stored as a string
Define a Function
Apart from using in-built functions, Python also allows you to
define your own functions for your program.
Syntax:-
def function_name(aregument1, argument2,....):
statement 1
statement 2
.......
Example:-
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Indian Servers
@indianserverltd Indian Servers 2021
Indian servers WWW.INDIANSERVERS.COM
Indian Servers
@indianserverltd Indian Servers 2021
Indian servers WWW.INDIANSERVERS.COM
Indian Servers
@indianserverltd Indian Servers 2021
Indian servers WWW.INDIANSERVERS.COM
Define a Function
True, False - True and False are truth values in Python.
Indian Servers
@indianserverltd Indian Servers 2021
Indian servers WWW.INDIANSERVERS.COM
Indian Servers
@indianserverltd Indian Servers 2021
Indian servers WWW.INDIANSERVERS.COM
Indian Servers
@indianserverltd Indian Servers 2021
Indian servers WWW.INDIANSERVERS.COM
Indian Servers
@indianserverltd Indian Servers 2021
Indian servers WWW.INDIANSERVERS.COM
Indian Servers
@indianserverltd Indian Servers 2021
Indian servers WWW.INDIANSERVERS.COM
Lists
Tuples are similar to lists — they allow you to display an
ordered sequence of elements. However, they are
immutable and you can’t change the values stored in a
tuple. The advantage of using tuples over lists is that the
former are slightly faster. So it’s a nice way to optimize your
code.
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
List Operations
Combine two lists - To mash up two lists use the + operator.
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
tuples
Tuples are similar to lists — they allow you to display an
ordered sequence of elements. However, they are
immutable and you can’t change the values stored in a
tuple. The advantage of using tuples over lists is that the
former are slightly faster. So it’s a nice way to optimize your
code.
How to Create a Tuple
Note: Once you create a tuple, you can’t add new items
to it or change it in any other way
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Output - (1, 3, 5, 7, 9)
Tuple to a List - Since Tuples are immutable, you can’t
change them. What you can do though is convert a tuple
into a list, make an edit and then convert it back to a
tuple.
Dictionaries
A dictionary holds indexes with keys that are mapped to
certain values. These key-value pairs offer a great way of
organizing and storing data in Python. They are mutable,
meaning you can change the stored information
customer1= {
'username': 'sai-satish',
'online': 'false',
'friends':100
}
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Option 1: new_dict = {}
Option 2: other_dict= dict()
And you can use the same two approaches to add values to
your dictionary:
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
x = new_dict["brand"]
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
If Statements in Python
Just like other programming languages, Python
supports the basic logical conditions from math:
• Equals: a == b
• Not Equals: a != b
• Less than: a < b
• Less than or equal to a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Python Loops
Python has two simple loop commands that are good
to know:
• for loops
• while loops
for loop - for loop is a handy way for iterating over a sequence
such as a list, tuple, dictionary, string, etc.
Nested Loop - You can also stop the loop from running even if the
condition is met. For that, use the break statement both in while
and for loops:
Output -
2 is prime 3 is prime 5 is prime 7
is prime 11 is prime 13 is prime 17
is prime 19 is prime 23 is prime 29
is prime 31 is prime 37 is prime 41
is prime 43 is prime 47 is prime 53
is prime 59 is prime 61 is prime 67
is prime 71 is prime 73 is prime 79
is prime 83 is prime 89 is prime 97
Indian Servers is prime
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Break a loop - You can also stop the loop from running
even if the condition is met. For that, use the break
statement both in while and for loops:
1
Output - 2
3
4
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Sockets
Sockets are interior endpoints built for sending and
receiving data. A single network will have two sockets, one
for each communicating device or program. These sockets
are a combination of an IP address and a Port. A single
device can have ‘n’ number of sockets based on the port
number that is being used. Different ports are available for
different types of protocols.
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Socket Types
SOCK_STREAM - For TCP protocols, Reliable transmission, Packet
sequence, Connection-oriented, Bi-directional
Socket Methods :
Client socket method - connect()
Server socket methods - bind(), listen(backlog), accept()
TCP socket methods -
s.recv() #receive TCP packets
s.send() #send TCP packets
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Server
A server is either a program, a computer, or a device that is devoted to
managing network resources. Servers can either be on the same
device or computer or locally connected to other devices and
computers or even remote. There are various types of servers such as
database servers, network servers, print servers, etc.
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Client
A client is either a computer or software that receives information
or services from the server. In a client-server module, clients
requests for services from servers. The best example is a web
browser such as Google Chrome, Firefox, etc. These web browsers
request web servers for the required web pages and services as
directed by the user. Other examples include online games, online
chats, etc.
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Socket Errors/Exceptions
exception socket.error - A deprecated alias of OSError,
raised when a system function returns a system-related
error.
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Diesel - A clean API for writing network clients and servers. TCP and
UDP supported. Bundles client for HTTP, DNS, Redis, Riak, and MongoDB
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Python Class
Since Python is an object-oriented programming language
almost every element of it is an object — with its methods and
properties.
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Indian Servers
@indianserverltd Indian Servers
Indian servers WWW.INDIANSERVERS.COM
Adding an else clause will help you confirm that no errors were
found:
Indian Servers
@indianserverltd Indian Servers