|
by via Mobile Web Weekly
"Mr Branding" is a blog based on RSS for everything related to website branding and website design, it collects its posts from many sites in order to facilitate the updating to the latest technology.
To suggest any source, please contact me: Taha.baba@consultant.com
|
This article was peer reviewed by Tim Severin and Adrian Sandu. Thanks to all of SitePoint’s peer reviewers for making SitePoint content the best it can be!
Not since the shuttering of Google Reader has there been quite so many outcries of surprise and annoyance amongst tech fans. Facebook’s announcement that their popular developer service platform, Parse, will shut in a years time caused ripples of panic amongst developers who rely on it. It’s always been a bad idea to be too reliant on a centralized, commercial service as it may not always last for ever. Parse wont be the first or the last to close and it’s a good lesson to us all to be flexible.
But enough discussion, let’s get down to practical steps on what to do with your Parse powered application.
Continue reading %Migrating Your Android or iOS App from Parse%
When we’re looking for a service that protects our personal info and browsing activity, we want to make sure we’re choosing one that comes highly recommended. Case in point, proXPN’s VPN service, rated four stars by PC Mag. Get a lifetime subscription for $39.
This VPN service will give you unlimited traffic bandwidth as it encrypts your browsing, keeping your private information and online activity safe, even on public Wi-Fi. It’ll even automatically shut down sensitive programs if your VPN connection drops. All the meanwhile, it also gives you extra freedom when you’re browsing abroad—connect easily to geo-blocked content like Netflix and YouTube. Use proXPN VPN on up to four devices.
Save 89% on a lifetime subscription to proXPN—get it for $39 at SitePoint Shop.
Continue reading %Save 89% on a Lifetime Subscription to a Top-Rated VPN%
In Python, you may have come across things like file(), print(), open(), range(), etc. Those are called built-in functions. That is, functions already provided by the language itself which you can execute by referencing (calling) to them. But, what is a function anyway? This is what we are going to learn in this tutorial, the Python way!
Functions are composed of a set of instructions combined together to get some result (achieve some task), and are executed by calling them, namely by a function call. Results in Python can either be the output of some computation in the function or None. Those functions can be either built-in functions (mentioned above) or user-defined functions. Functions defined within classes are called methods.
Now that we know what is meant by a function, let's see how we can define functions in Python. In order to do that, we use the def statement, which has the following syntax:
def function_name(parameters):
statement(s)
return expression
The parameters in the function definition are optional, as some functions do not require parameters to be passed at the time of the function call. If more than one parameter is passed, the parameters are separated by commas, and they are bound to the arguments in the function that correspond to the parameters passed. The statements (function body) are executed when the function is called.
The return statement is an optional statement which serves as the exit point of the function where an expression could be returned to the caller, or if no expression is identified, it will be like returning a None value.
Let's go through some examples to grasp the idea of functions more. You will notice that functions save us from repeating ourselves, as they provide a block of reusable code to call whenever we want to perform some regular task we ought to perform.
Say that we would like to display the name of any employee entered in the system. This can look as follows:
employee_name = 'Abder'
def print_name(name):
print name
print_name(employee_name)
As you can see, if we want to call a function, we simply identify the following:
If you type print_name(employee_name) before the function definition, Python will complain as follows:
Traceback (most recent call last):
File "test.py", line 3, in <module>
print_name(employee_name)
NameError: name 'print_name' is not defined
So you should define the function before calling it.
Let's take another example. This time we will use lists. Assume we have the following list:
numbers_list = [1,2,3,4,5]
Let's say now we want to insert new numbers using this function:
numbers_list = [1,2,3,4,5]
def insert_numbers(numbers_list):
numbers_list.insert(5, 8)
numbers_list.insert(6, 13)
print 'List \"inside\" the function is: ', numbers_list
return
insert_numbers(numbers_list)
print 'List \"outside\" the function is: ', numbers_list
Notice that the output of this Python script will be:
List "inside" the function is: [1, 2, 3, 4, 5, 8, 13] List "outside" the function is: [1, 2, 3, 4, 5, 8, 13]
What can we conclude from this? We can conclude that the parameters are passed by reference. That is, the parameters in the called function are the same as the passed arguments (variable/identity) by the caller, as opposed to passed by value where the called function parameters are a copy of the caller passed arguments.
Let's use our knowledge of functions to slightly build a more interesting application. Let's build a simple calculator. This calculator will allow the user to enter two numbers, and perform addition, subtraction, multiplication, and division on the two numbers.
def add(x,y):
return x+y
def subtract(x,y):
return x-y
def multiply(x,y):
return x*y
def divide(x,y):
return x/y
x = 8
y = 4
print '%d + %d = %d' % (x, y, add(x, y))
print '%d - %d = %d' % (x, y, subtract(x, y))
print '%d * %d = %d' % (x, y, multiply(x, y))
print '%d / %d = %d' % (x, y, divide(x, y))
Go ahead, try it out and see what output you get.
Lambda functions are anonymous functions Python creates at runtime using the lambda construct. So far, we have learned that functions are defined using the def statement. Lambda functions are, however, defined in a different manner. Let's take an example to clarify how Lambda functions work.
Say we want to write a function that returns the double value of the passed argument. Using the def statement, we would do the following:
def double(x):
return x*2
x = 10
print double(x)
The Lambda way of writing this function is as follows:
x = 10 double = lambda x: x*2 print double(x)
There are different ways you can use Lambda in. Check this tutorial for more information on Lambda functions.
As we can see, functions are considered an important feature in Python, allowing you to reuse code rather than reinventing the wheel.