Posts

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...

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

Python - Loops

Image
Decision making is anticipation of conditions occurring while execution of the program and specifying actions taken according to the conditions. Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need to determine which action to take and which statements to execute if outcome is TRUE or FALSE otherwise. Following is the general form of a typical decision making structure found in most of the programming languages − Python programming language assumes any  non-zero  and  non-null  values as TRUE, and if it is either  zero  or  null , then it is assumed as FALSE value. Python programming language provides following types of decision making statements. Click the following links to check their detail.

Python Deciding factor

Image
Decision making is anticipation of conditions occurring while execution of the program and specifying actions taken according to the conditions. Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need to determine which action to take and which statements to execute if outcome is TRUE or FALSE otherwise. Following is the general form of a typical decision making structure found in most of the programming languages − Python programming language assumes any  non-zero  and  non-null  values as TRUE, and if it is either  zero  or  null , then it is assumed as FALSE value.

Python Comparison Operators

These operators compare the values on either sides of them and decide the relation among them. They are also called Relational operators. Assume variable a holds 10 and variable b holds 20, then − [  Show Example  ] Operator Description Example == If the values of two operands are equal, then the condition becomes true. (a == b) is not true. != If values of two operands are not equal, then condition becomes true. (a != b) is true. <> If values of two operands are not equal, then condition becomes true. (a <> b) is true. This is similar to != operator. > If the value of left operand is greater than the value of right operand, then condition becomes true. (a > b) is not true. < If the value of left operand is less than the value of right operand, then condition becomes true. (a < b) is true. >= If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. (a >= b) is not true. <= ...