http://www.diveintopython.net/power_of_introspection/and_or.html
'a' or 'b' = 'a'
'a' and 'b' = 'b'
Monday, August 17, 2015
Thursday, August 13, 2015
expand list to function arguments
http://stackoverflow.com/questions/7745952/python-expand-list-to-function-arguments
Note: A list is not expanded as arguments by itself.
def a(b,c):
print(b)
d = [2,3]
a(d) : Will not work because the given list is not expanded.
a(*d) will work: see https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists
Note: A list is not expanded as arguments by itself.
def a(b,c):
print(b)
d = [2,3]
a(d) : Will not work because the given list is not expanded.
a(*d) will work: see https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists
i18n in python
Internationalization is also called "i18n" (I-eighteen-N): There are 18 letters between I and N in "internationalization.
python has a package called i18n package to support internationalization.
https://pypi.python.org/pypi/python-i18n/0.3.0
python has a package called i18n package to support internationalization.
https://pypi.python.org/pypi/python-i18n/0.3.0
git command with [ ]: git -c i18n.logoutputencoding='UTF-8' branch
git -c i18n.logoutputencoding='UTF-8' branch
% in python
http://stackoverflow.com/questions/997797/what-does-s-mean-in-python
It is a string formatting syntax (which it borrows from C).
print("my name is: %s" % 'hello') : is same as
printf("my name is: %s", "hello") : % = , of C
It is a string formatting syntax (which it borrows from C).
print("my name is: %s" % 'hello') : is same as
printf("my name is: %s", "hello") : % = , of C
tuples and list
A tuple is a sequence of immutable Python objects. Tuples are sequences,
just like lists. The differences between tuples and lists are, the
tuples cannot be changed unlike lists and tuples use parentheses,
whereas lists use square brackets.
immutable Python objects: once created their value never changes.
immutable Python objects: once created their value never changes.
python tuples
http://www.tutorialspoint.com/python/python_tuples.htm
To write a tuple containing a single value you have to include a comma, even though there is only one value −
tup1 = (50,);
To write a tuple containing a single value you have to include a comma, even though there is only one value −
tup1 = (50,);
Wednesday, August 12, 2015
What is class
https://www.jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/
Class: Grouping of related data and functions. The function associated with class is referred as method.
#Class and object:
Classes can be thought of as blueprints for creating objects. When I define a Customer class using the
Class: Grouping of related data and functions. The function associated with class is referred as method.
#Class and object:
Classes can be thought of as blueprints for creating objects. When I define a Customer class using the
class
keyword, I haven't actually created a customer.
Instead, what I've created is a sort of instruction manual for constructing "customer"
objects. pass statement in python
http://www.tutorialspoint.com/python/python_pass_statement.htm
The pass statement is a null operation; nothing happens when it executes. The pass is also useful in places where your code will eventually go, but has not been written yet (e.g., in stubs for example):
The pass statement is a null operation; nothing happens when it executes. The pass is also useful in places where your code will eventually go, but has not been written yet (e.g., in stubs for example):
Syntax
pass
Example
#!/usr/bin/python for letter in 'Python': if letter == 'h': pass print 'This is pass block' print 'Current Letter :', letter print "Good bye!"When the above code is executed, it produces following result −
Current Letter : P Current Letter : y Current Letter : t This is pass block Current Letter : h Current Letter : o Current Letter : n Good bye!
class and object in python
https://www.jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/
http://stackoverflow.com/questions/1593441/constructors-in-python
http://stackoverflow.com/questions/3691101/what-is-the-purpose-of-str-and-repr-in-python
http://stackoverflow.com/questions/3694371/how-do-i-initialize-the-base-super-class-in-python
#Super class
A superclass is a class that has been extended by another class. It
allows the extending class to inherit its state and behaviors.
Note:
1. self = this (self is equivalent to this of C#, C++) = representation
of specific instance of the class.
2. self is must for all method definition except for @staticmethod
3. Object: Object is an instance of a class
#define class attribute: self.attribute1 = 0
self.attribute1 = 0
self.attribute2 = "Hello"
Simple Example-1
#---This is the class part---# class Arit: def add(self,a,b): return a+b def subs(self,a,b): return a-b #---This is the part that uses the class---# n1=10 n2=4 operation=Arit() print "The addition is", operation.add(n1,n2) print "The substraction is", operation.subs(n1,n2)
OR
#---This is the class part---#
class Arit():
def add(self,a,b):
return a+b
def subs(self,a,b):
return a-b
#---This is the part that uses the class---#
n1=10
n2=4
operation=Arit()
print "The addition is", operation.add(n1,n2)
print "The substraction is", operation.subs(n1,n2)
Simple Example-2:
class SimpleClass:
def __init__(self):
self.cumul=0
def accumulate(self,amount):
self.cumul = self.cumul + amount
simpleObject = SimpleClass()
print simpleObject.cumul
simpleObject.accumulate(10)
print simpleObject.cumul
#
class MyClass(object):
def __init__(self, x, y, angle):
self.x = x
self.y = y
self.angle = angle
The constructor is always written as a function called __init__()
. It must always take as its first argument a reference to the instance being constructed. This is typically called self
. The rest of the arguments are up to the programmer.The
object
on the first line is the superclass, i.e. this says that MyClass
is a subclass of object
. This is normal for Python class definitions.You access fields (members) of the instance using the
self.
syntax.# prints during initialization
class Foobar():
"""This will create Foobar type object."""
def __init__(self):
print "Foobar object is created."
def __repr__(self):
return "Type what do you want to see here."
a = Foobar()
print a
# Static method:
class Foobar():
"""This will create Foobar type object."""
@staticmethod
def a():
print "Foobar object is created."
Foobar.a()
# Using print in class
class Foobar():
"""This will create Foobar type object."""
def a(self):
print "Foobar object is created."
b = Foobar()
b.a()
#Constructor with argument:
class Num:
def __init__(self,num):
self.n = num
def getn(self):
return self.n
def getone(self):
return 1
myObj = Num(3)
print myObj.getn()
# Attributes (global and instance specific attribute:
class Car(object):
wheels = 4
def __init__(self, make, model):
self.make = make
self.model = model
mustang = Car('Ford', 'Mustang')
print mustang.wheels
# 4
print Car.wheels
# 4
argv in python
argv -- command line arguments; argv[0] is the script pathname if known
http://stackoverflow.com/questions/13263951/what-is-argv-and-what-does-it-do
default value of argument in python when no argument given at all: arg1 = new
def happyBirthday(FirstName="FN", LastName="LN"):
print("Happy Birthday, dear " + FirstName + " " + LastName + ".")
happyBirthday()
Will print: Happy Birthday, dear FN LN.
happyBirthday("FN1"): will print Will print: Happy Birthday, dear FN1 LN.
happyBirthday("FN1", "LN1"): will print Will print: Happy Birthday, dear FN1 LN1.
print("Happy Birthday, dear " + FirstName + " " + LastName + ".")
happyBirthday()
Will print: Happy Birthday, dear FN LN.
happyBirthday("FN1"): will print Will print: Happy Birthday, dear FN1 LN.
happyBirthday("FN1", "LN1"): will print Will print: Happy Birthday, dear FN1 LN1.
Function in python
def happyBirthday(person): print("Happy Birthday, dear " + person + ".")
Note: parenthesis is must to : A. define a function, B. use a function
def a():
print("a")
a()
variable length argument in Python: **kw
http://pythontips.com/2013/08/04/args-and-kwargs-in-python-explained/
http://stackoverflow.com/questions/287085/what-do-args-and-kwargs-mean
Old Python : it is not necessary to write *args or **kwargs. Only the * (aesteric) is necessary. You could have also written *var and **vars. Writing *args and **kwargs is just a convention. So now lets take a look at *args first.
New Python: * and ** are different
Usage of *args
*args and **kwargs are mostly used in function definitions. *args and **kwargs allow you to pass a variable number of arguments to a function. What does variable mean here is that you do not know before hand that how many arguments can be passed to your function by the user so in this case you use these two keywords. *args is used to send a non-keyworded variable length argument list to the function.
http://stackoverflow.com/questions/287085/what-do-args-and-kwargs-mean
Old Python : it is not necessary to write *args or **kwargs. Only the * (aesteric) is necessary. You could have also written *var and **vars. Writing *args and **kwargs is just a convention. So now lets take a look at *args first.
New Python: * and ** are different
Usage of *args
*args and **kwargs are mostly used in function definitions. *args and **kwargs allow you to pass a variable number of arguments to a function. What does variable mean here is that you do not know before hand that how many arguments can be passed to your function by the user so in this case you use these two keywords. *args is used to send a non-keyworded variable length argument list to the function.
if __name__ == "__main__" : Usage
http://stackoverflow.com/questions/419163/what-does-if-name-main-do
__name__ == "__main__" is set when the module runs directly and is set to module name when an imported module is running at the time of call.
__name__ == "__main__" is set when the module runs directly and is set to module name when an imported module is running at the time of call.
Subscribe to:
Posts (Atom)