Posts

Showing posts from September, 2019

Variable-length arguments in python

You may need to process a function for more arguments than you specified while defining the function. These arguments are called  variable-length  arguments and are not named in the function definition, unlike required and default arguments. Syntax for a function with non-keyword variable arguments is this − def functionname([formal_args,] *var_args_tuple ): "function_docstring" function_suite return [expression] An asterisk (*) is placed before the variable name that holds the values of all nonkeyword variable arguments. This tuple remains empty if no additional arguments are specified during the function call. Following is a simple example − Live Demo #!/usr/bin/python # Function definition is here def printinfo ( arg1 , * vartuple ): "This prints a variable passed arguments" print "Output is: " print arg1 for var in vartuple : print var return ; # Now you can call printinfo function printinfo (

Pass by reference vs value

All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function. For example − Live Demo #!/usr/bin/python # Function definition is here def changeme ( mylist ): "This changes a passed list into this function" mylist . append ([ 1 , 2 , 3 , 4 ]); print "Values inside the function: " , mylist return # Now you can call changeme function mylist = [ 10 , 20 , 30 ]; changeme ( mylist ); print "Values outside the function: " , mylist Here, we are maintaining reference of the passed object and appending values in the same object. So, this would produce the following result − Values inside the function: [10, 20, 30, [1, 2, 3, 4]] Values outside the function: [10, 20, 30, [1, 2, 3, 4]]

Function Arguments in python

Hi there! You can call a function by using the following types of formal arguments − Required arguments Keyword arguments Default arguments Variable-length arguments Required arguments Required arguments are the arguments passed to a function in correct positional order. Here, the number of arguments in the function call should match exactly with the function definition. To call the function  printme() , you definitely need to pass one argument, otherwise it gives a syntax error as follows − Live Demo #!/usr/bin/python # Function definition is here def printme ( str ): "This prints a passed string into this function" print str return ; # Now you can call printme function printme ()

Python - Functions

Hi Readers! A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. As you already know, Python gives you many built-in functions like print(), etc. but you can also create your own functions. These functions are called  user-defined functions. Defining a Function You can define functions to provide the required functionality. Here are simple rules to define a function in Python. Function blocks begin with the keyword  def  followed by the function name and parentheses ( ( ) ). Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses. The first statement of a function can be an optional statement - the documentation string of the function or  docstring . The code block within every function starts with a colon (:) and is indented. The statement return [

Python - Strings

Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes the same as double quotes. Creating strings is as simple as assigning a value to a variable. For example − var1 = 'Hello World!' var2 = "Python Programming" Accessing Values in Strings Python does not support a character type; these are treated as strings of length one, thus also considered a substring. To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring. For example − Live Demo #!/usr/bin/python var1 = 'Hello World!' var2 = "Python Programming" print "var1[0]: " , var1 [ 0 ] print "var2[1:5]: " , var2 [ 1 : 5 ] When the above code is executed, it produces the following result − var1[0]: H var2[1:5]: ytho Updating Strings You can "update" an existing string by (re)assigning a variabl

Python - Numbers

Number data types store numeric values. They are immutable data types, means that changing the value of a number data type results in a newly allocated object. Number objects are created when you assign a value to them. For example − var1 = 1 var2 = 10 You can also delete the reference to a number object by using the  del  statement. The syntax of the del statement is − del var1[,var2[,var3[....,varN]]]] You can delete a single object or multiple objects by using the  del  statement. For example − del var del var_a, var_b