Posts

Showing posts with the label Python Theory

Scope Variables and Return Statement in Python | Python Theory

Scope of Variables All variables in a program may not be accessible at all locations in that program. This depends on where you have declared a variable. The scope of a variable determines the portion of the program where you can access a particular identifier. There are two basic scopes of variables in Python − Global variables Local variables Global vs. Local variables Variables that are defined inside a function body has a local scope, and those defined outside have a global scope. This means that local variables can be accessed only inside the function in which they are declared, whereas global variables can be accessed throughout the program body by all functions. When you call a function, the variables declared inside it are brought into scope. Following is a simple example − total = 0; # This is global variable. # Function definition is here      def sum( arg1, arg2 ): # Add both the parameters and return them."      total = arg1 + arg2; # Here total

Function In Python Programming | Python Programming

What is a function in Python? In Python, a function is a group of related statements that perform a specific task. Functions help break our program into smaller and modular chunks. As our program grows larger and larger, functions make it more organized and manageable. Furthermore, it avoids repetition and makes code reusable. 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 [expression] exits a function, optionally passing back an expression to the ca

Python Programming Looping Techniques, For Loop in Python Programming and While Loop in Python Programming | Python Programming

Image
Python Programming Looping Techniques, For Loop in Python Programming and While Loop in Python Programming For loop in Python Programming What is for loop in Python Programming? The for loop in Python Programming is used to iterate over a sequence (list, tuple, string) or other iterable objects. Iterating over a sequence is called traversal. Syntax of for Loop for val in sequence:     Body of for Here, val is the variable that takes the value of the item inside the sequence on each iteration. Loop continues until we reach the last item in the sequence. The body of for loop is separated from the rest of the code using indentation. Flowchart of for Loop Example: Python for Loop # Program to find the sum of all numbers stored in a list # List of numbers numbers = [ 6 , 5 , 3 , 8 , 4 , 2 , 5 , 4 , 11 ] # variable to store the sum sum = 0 # iterate over the list for val in numbers:     sum = sum +val # Output: The sum is 48 print ( "The sum is" , sum ) when you run th

Python Programming Namespace | Python Programming

Image
What is Name in Python Programming? A namespace is a system to have a unique name for each and every object in Python. An object might be a variable or a method. Python itself maintains a namespace in the form of a Python dictionary.  Namespaces are one honking great idea -- let's do more of those!  

Python Programming Type Casting/Conversion, Frozen Set | Python Programming

Python Programming Type Casting/Conversion: The process of converting the value of one data type such as integer, string, float, etc. to another data type is called type conversion/ typecasting. Python has two types of type conversion/typecasting. Implicit Type Conversion Explicit Type Casting