Hi Guys,
Join our telegram channel and group :)
Telegram Group: https://t.me/randomskoolQnATelegram Channel: https://t.me/randomskool
In this tutorial, we will learn what are the looping concepts in python, and how we can reduce the work in python using functions and will go through different ways of restricting variables accessibility using scope such as a local and global scope.
Why do we need a loop?
To minimize unnecessary effort. Suppose we need to multiply two different sets of numbers in continuously. In a traditional way we will do multiplication one by one but using loop concept all we need to write 2 or 3 lines of code multiplication done in minutes or second.
Traditional Way:
2x2 = 4
2x3 = 6
2x5 = 10
2x6 = 12
2x7 = 14
2x8 = 16
2x9 = 18
2x10 =20
Coders Way using loop concept:
Explanation:
In the above example all we are defining the range of numbers and put a for a loop.
That's it everything below the for loop will execute once the range end value is reached.
There are two types of looping statements:
1. For loop: When the range is known.
2. While loop: When the condition satisfied.
Let's see for and while loop with example and explanation.
For Loop Explanation :
When the starting and ending numbers are known, then we can use for loop with the help of range function.
Note: We can use for loop with any iterable object such as list, tuple, dictionaries, and many more.
String iteration is also possible with for loop.
Example :
For loop with range
For loop with a list
For loop with a String
While loop Explanation:
While loop is used when we are dealing with conditions if the condition is met or satisfied loop will continuously be running otherwise stop in between.
Moreover, in the below example we are using while loop, in order to use while loop effectively first initialized the variable after the condition and increment the variable that we have initialized in the very first time otherwise while the loop will run an infinite number of times.
Function
Below are the key features of functions in python programming:-
1. Functions make code reusable
2. Functions work on the phenomena of modular programming.
3. Functions can limit the accessibility of different other functions or variables using a scope.
Syntax of function:
1. Declaration Part: Functions declared with the help of def keyword.
def addition( number_1, number_2):
statments
.
.
.
addition(number_1, number_2)
2. Passing Parameters: Parameter passing is important because if we declare the function but we have not passed the value for which purpose function is written then it is of no use.
The parameter passed in between brackets () such as (number_1, number_2).
Two types of parameter are present:
a) Formal parameter: When we pass the actual value to some other identifier or variable at the time of function call.
b) Actual parameter: When we pass the actual value at the time of function call.
3. Function Calling: When we call a function to perform a designated task by passing actual or formal parameters.
How Functions are reusable?
In the above example, we are passing a and b explicitly. But we can change the values dynamically by using the input function and taking input from the user.
And, if there is a requirement that comes up between that someone needs to perform subtraction then all we simply need to create a new function of subtraction. That's it.
Let's discuss the Local and Global Scope with the help of interesting and mind-boggling examples.
The program consists of 12 lines out of a few lines that are blank and can be ignored. I will try to explain each and every line briefly.
Let's start.
1. Using # for comment.
2. Declaring the global variable named as outside and pass the value as 8.
3. Print the global variable value before change it is showing as 8 in the console.
4. Declaring the function named as fun( ).
5. In function fun( ) we are declaring the same variable outside with different value ( 9 ) but this value is defined and accessible only within the scope of this function fun( ) this type of variable that is accessible within the scope of a function or locally is called as a local variable.
6. In function fun( ) we are using global keyword because we would like to override the existing global variable whose value is 8 and want further our new local variable whose value is 9 considered as global variable further in the program.
7. fun( ) is calling the function
8. At last, we are printing the value of the outside variable and get to know that it is now overridden by a local variable which we already convert using the global keyword.
In the next tutorial, we will learn more interesting python concepts such as a lambda, map, filter, reduce, and *args & **kwargs.
Click on Next Tutorial (Python Programming 101 - Lambda | Map | Filter | Reduce | *args vs **kwargs)
For Complete Python Programming 101 tutorial series ( Click here ).
For Queries and Contact there are multiple ways:
1. From telegram search @randomskoolQnA and @randomskool
2. From Contact Us Page
3. From Comment
For Daily Updates Please subscribe to the feed.
For Job-related Updates Please fill the subscriber feedback form.
Join our telegram channel and group :)
Telegram Group: https://t.me/randomskoolQnATelegram Channel: https://t.me/randomskool
Hello, I have browsed most of your posts. This post is probably where I got the most useful information for my research. Thanks for posting, maybe we can see more on this. Are you aware of any other websites on this subject. scope management
ReplyDelete