A summary of python tips
[]
: brackets or square braces. Lists are enclosed in brackets.()
: parentheses. Tuples are enclosed in parentheses.{}
: curly braces. Dictionaries are enclosed in curly braces, and values can be assigned and accessed using square braces, e.g.,dict[2]
ordict["id"]
.**
: exponent − Performs exponential (power) calculation on operators.a**b
means10
to the power20
ifa = 10
andb = 20
.//
: floor division. The division of operands where the result is the quotient in which the digits after the decimal point are removed.is
: Evaluates to true if the variables on either side of the operator point to the same object and false otherwise.x is y
, hereis
results in1
ifid(x)
equalsid(y)
.
For fundamentals, refer to this post.
r
for reading onlyr+
opens for reading and writing (cannot truncate a file)w
for writing onlyw+
for writing and reading (can truncate a file)rb
for reading a binary file. The file pointer is placed at the beginning of the file.rb+
reading or writing a binary filewb+
writing a binary filea+
opens for appendingab+
Opens a file for both appending and reading in binary. The file pointer is at the end of the file if the file exists. The file opens in the append mode.x
open for exclusive creation, failing if the file already exists (Python 3)
[on True] if [expression] else [on False]
a,b = 1,2
min = b if a>b else a
print(c) # 1
In javascript,
const a = 1,
b = 2;
const min = a > b ? b : a;
console.log(min); // 1
A thread is a lightweight process, and multithreading allows us to execute multiple threads at once. As you know, Python is a multithreaded language. It has a multi-threading package.
The GIL (Global Interpreter Lock) ensures that a single thread executes at a time. A thread holds the GIL and does a little work before passing it on to the next thread. This makes for an illusion of parallel execution. But in reality, it is just threads taking turns at the CPU. Of course, all the passing around adds overhead to the execution.
I have used multi-thread in Email sending module in previous project:
from email.mime.image import MIMEImage # Multipurpose Internet Mail Extensions (MIME)
from threading import Thread
from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template
class EmailThread(Thread):
def __init__(self, sender, to, subject='', body='', html_content='', reply_to=None, cc=None):
...
Thread.__init__(self)
def run(self):
_send(self.sender, self.recipient_list, self.subject, self.message, self.html_content, self.reply_to, self.cc)
def _send(sender, to, subject='', body='', html_content='', reply_to=None, cc=None):
if not isinstance(to, (list, tuple)):
to = (to,)
...
message.mixed_subtype = 'related' # This is critical, otherwise images will be displayed as attachments!
...
img.add_header('Content-Disposition', 'inline', filename='filename')
message.attach(img)
Using Gmail smtp server to send transitional mails:
MAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_HOST_USER=your_gmail_account
EMAIL_HOST_PASSWORD=your_gmail_pwd
EMAIL_USE_TLS=True
Do forget enable your at your Gmail account for this feature.
When one class inherits from another, it is said to be the child/derived/sub class inheriting from the parent/base/super class. It inherits/gains all members (attributes and methods).
Inheritance lets us reuse our code, and also makes it easier to create and maintain applications. Python supports the following kinds of inheritance:
- Single Inheritance - A class inherits from a single base class.
- Multiple Inheritance - A class inherits from multiple base classes.
- Multilevel Inheritance - A class inherits from a base class, which, in turn, inherits from another base class.
- Hierarchical Inheritance - Multiple classes inherit from a single base class.
- Hybrid Inheritance- Hybrid inheritance is a combination of two or more types of inheritance.
Python has a private heap space to hold all objects and data structures. Being programmers, we cannot access it; it is the interpreter that manages it. But with the core API, we can access some tools. The Python memory manager controls the allocation.
Additionally, an inbuilt garbage collector recycles all unused memory so it can make it available to the heap space.
The help()
function displays the documentation string and help for its argument. The dir()
function displays all the members of an object(any kind).
The answer here is no. The modules with circular references to other objects, or to objects referenced from global namespaces, aren’t always freed on exiting Python.
Plus, it is impossible to de-allocate portions of memory reserved by the C library.
Dictionary holds key-value pairs, similar to javascript object
. A dictionary is mutable, and we can also use a comprehension to create it.
In cases when we don’t know how many arguments will be passed to a function, like when we want to pass a list or a tuple of values, we use *args
.
def func(*args):
for i in args:
print(i)
func(3,2,1,4,7) # <-- arguments
**kwargs
takes keyword arguments when we don’t know how many there will be.
def func(**kwargs):
for i in kwargs:
print(i,kwargs[i])
func(a=1,b=2,c=7) # <-- keyword arguments
The words args
and kwargs
are convention, and we can use anything in their place.
import os
file = os.path("path/to/the/file")
with open(file,'r') as myFile:
count = 0
for i in myFile:
if i.isupper():
count += 1
print(count)
from random import shuffle
shuffle(myList)
import random
random.seed( 10 )
print "Random number with seed 10 : ", random.random()
# It will generate same random number
random.seed( 10 )
print "Random number with seed 10 : ", random.random()
# It will generate same random number
random.seed( 10 )
print "Random number with seed 10 : ", random.random()
To remove a list element, you can use either the del
statement if you know exactly which element(s) you are deleting or the remove()
method if you do not know.
- List comprehension can be:
nums = [x**2 if x % 2 == 0 else x**3 for x in range(10)]
print(nums)
# [0, 1, 4, 27, 16, 125, 36, 343, 64, 729]
String special operators:
-
+
Concatenation - Adds values on either side of the operator -
*
Repetition - Creates new strings, concatenating multiple copies of the same string -
[]
Slice - Gives the character from the given index -
[ : ]
Range Slice - Gives the characters from the given range -
in
Membership - Returns true if a character exists in the given string -
not in
Membership - Returns true if a character does not exist in the given string -
r/R
Raw String - Suppresses actual meaning of Escape characters. The syntax for raw strings is exactly the same as for normal strings with the exception of the raw string operator, the letter "r," which precedes the quotation marks. The "r" can be lowercase (r) or uppercase (R) and must be placed immediately preceding the first quote mark. -
%
Format - Performs String formatting See at next section
Built-in functions:
-
cmp(list1, list2)
: Compares elements of both lists. -
len(list)
: Gives the total length of thelist
. -
max(list)
: Returns item from thelist
withmax
value. -
min(list)
: Returns item from thelist
withmin
value. -
list(seq)
: Converts atuple
intolist
.
Python list methods:
-
list.append(obj)
: Appends objectobj
tolist
-
list.count(obj)
: Returns count of how many timesobj
occurs inlist
-
list.extend(seq)
: Appends the contents ofseq
tolist
-
list.index(obj)
: Returns the lowest index inlist
thatobj
appears -
list.insert(index, obj)
: Inserts objectobj
intolist
at offset index -
list.pop(obj=list[-1])
: Removes and returns last object orobj
fromlist
-
list.remove(obj)
: Removes objectobj
fromlist
-
list.reverse()
: Reverses objects oflist
in place -
list.sort([func])
: Sorts objects oflist
, usecompare
func if given
-
capitalize(str)
: Capitalizes first letter of string. -
isalnum(str)
: ReturnsTrue
if string has at least 1 character and all characters are alphanumeric and false otherwise. -
isdigit(str)
: Returns true if string contains only digits and false otherwise. -
islower(str)
: Returns true if string has at least 1 cased character and all cased characters are in lowercase and false otherwise. -
isupper()
: Returns true if string has at least one cased character and all cased characters are in uppercase and false otherwise. -
isnumeric()
: Returns true if a unicode string contains only numeric characters and false otherwise. -
isspace()
: Returns true if string contains only whitespace characters and false otherwise. -
istitle()
: Returns true if string is properly "titlecased", e.g. "This Is Titlecased", and false otherwise. -
str.join(seq)
: Merges (concatenates) the string representations of elements in sequenceseq
into a string, with separator string. -
len(str)
: Returns the length of the string. -
str.ljust(width[, fillchar])
: Returns a space-padded string with the original string left-justified to a total of width column. -
str.lower()
orupper()
: Converts all uppercase letters in string to lowercase, or the other way around. -
str.lstrip()
: Removes all leading whitespace in string. -
max(str)
: Returns the max alphabetical character from the stringstr
. -
min(str)
: Returns the min alphabetical character from the stringstr
. -
str.replace(old, new [, max])
: Replaces all occurrences ofold
in string with new or at mostmax
occurrences ifmax
given. -
strip([chars])
: Performs bothlstrip()
andrstrip()
on string. -
str.swapcase()
: Inverts case for all letters in string. -
str.title()
: Returns "titlecased" version of string, that is, all words begin with uppercase and the rest are lowercase.
-
choice(seq)
: Returns a random item from a list, tuple, or string. -
randrange([start,] stop [,step])
: returns a randomly selected element fromrange(start, stop, step)
, e.g.,randrange(0, 101, 2)
returns a random odd integer from 1 to 100 inclusive -
random()
: returns a random floatr
, such that0
is less than or equal tor
andr
is less than1
. -
seed([x])
: Sets the integer starting value used in generating random numbers. Call this function before calling any other random module function. -
sample(range(1000), 8)
: 8 random samples without replacement.
- Dictionary comprehension:
{key: value for (key, value) in iterable}
Built-in Dictionary functions and methods:
-
cmp(dict1, dict2)
: Compares elements of both dict. -
len(dict)
: Gives the total length of the dictionary. This would be equal to the number of items in the dictionary. -
str(dict)
: Produces a printable string representation of a dictionary -
type(variable)
: Returns the type of the passed variable. If passed variable is dictionary, then it would return a dictionary type.
Python includes following dictionary methods:
-
dict.clear()
: Removes all elements of dictionary dict -
dict.copy()
: Returns a shallow copy of dictionary dict -
dict.fromkeys(seq[, value])
: Create a new dictionary with keys fromseq
and values set tovalue
. -
dict.get(key, default=None)
: For key key, returns value or default if key not in dictionary -
dict.has_key(key)
: Returns true if key in dictionary dict, false otherwise -
dict.items()
: Returns a list of dict's (key, value) tuple pairs -
dict.keys()
: Returns list of dictionary dict's keys -
dict.setdefault(key, default=None)
: Similar to get(), but will set dict[key]=default if key is not already in dict -
dict.update(dict2)
: Adds dictionary dict2's key-values pairs to dict -
dict.values()
: Returns list of dictionary dict's values
- To work with a JSON string object, use
json.loads()
,json.dumps()
. Pay attention to thes
before the left parentheses - The JSON decoder and endcoder are illustrated below:
JSON Decoder
JSON | Python |
---|---|
object | dict |
array | list |
string | unicode |
number (int) | int, long |
number (real) | float |
true | True |
false | False |
null | None |
JSON Encoder
Python | JSON |
---|---|
dict | object |
list, tuple | array |
str, unicode | string |
int, long, float | number |
True | true |
False | false |
None | null |
- When load and dump JSON files, use
json.load()
,json.dump()
- If
open('file_path')
raisesIOError: [Error 2] No such file or directory: 'aa.bb'
, usegetcwd()
to double check the current working directory.
- Do not forget constructor
__init__
- The
__str__
is a special built-in method can be overridden. - When calling a method, do not forget
()
at the end.
# base class
class Pet(object):
def __init__(self, name, species):
self.name = name
self.species = species
def getName(self):
return self.name
def getSpecies(self):
return self.species
def __str__(self):
return ("{0} is a {1}".format(self.name, self.species))
polly = Pet("Polly", "Parrot")
print(polly)
pet_name = polly.getName()
print(pet_name)
# sub-class
class Dog(Pet):
def __init__(self, name, chases_cats):
Pet.__init__(self, name, "Dog")
self.chases_cats = chases_cats
def chasesCats(self):
return self.chases_cats
rommy = Dog("Rommy", True)
print(rommy.getSpecies())
print(rommy.chasesCats())
print("Does {0} , the {1}, chase cats? {2}".format(rommy.getName(), rommy.getSpecies(), rommy.chasesCats() ))
Refer to this link for details.