Photo by Bonneval Sebastien on Unsplash
Welcome back to the series of 'Important concepts for Python Developer interview'. This is part two of python basics which we need to cover for Python Developer Interview. If you have not read Basics part1 then please go through it. The complete series is available at following link
Following topics we are going to cover in this article.
Loops and Control statements
Functions
Let's start our topics.
Loops and control statements in Python:
While loop:
Syntax:
while test_expression:
Body of while
For loop:
Syntax:
for item in sequence:
Body of for
If we want to iterate in 'for' loop for specific number of times then range() comes handy.
Important point to remember in case of while and for loop is it can have else statement. So when does this 'else' is executed? In case of for loop, the else part is executed if the items in the sequence used in loop exhausts. And in case of while the else part is executed if the condition in the loop evaluates to 'False'. The break keyword can be used to stop a for/while loop. In such cases, the else part is ignored.Below are the examples in case of for and while loop:
#for loop
fooditems = [ 'cake', 'bread', 'biscuits' ]
for food in fooditems:
print("ate "+food)
else:
print("No food items left.")
Result:
ate cake
ate bread
ate biscuits
No food items left.
#while loop
i = 0
while i < 3:
print("I am in While loop")
i+=1
else:
print("ohh! this is how else is executed")
Result:
I am in While loop
I am in While loop
I am in While loop
ohh! this is how else is executed
Now we will take look into control statements present in Python.
break : Breaks out of the innermost enclosing for or while loop
continue : Continues with the next iteration of the loop
pass : Does nothing. It can be used when a statement is required syntactically but the program requires no action.
If you want more information on control statements then refer link.
Functions in Python:
Syntax:
def function_name(parameters):
"""docstring"""
function_body
def : introduces a function definition
docstring : optional string literal used as function’s documentation string
parameters: These are the values which we might need to pass while calling the function, not every function needs to pass the parameters
Functions can have return statement, if it does not have return statement then it ideally returns "None"
The variables and parameters in the function have local scope that means, those cannot be accessed/modified from outside the function.
If you want to modify the outside variable inside the function then it must have global keyword.
We can pass the arguments to a function in three types:
Positional Arguments
Keyword Arguments
Default argument values
- Positional Arguments:
This is the most common way of passing the arguments to functions, here arguments are passed to function as parameters based on position:
def say(message, friend):
print("Hello you have message, "+ message+ ", from "+friend)
say("pick my call", "Andy")
Here all the parameters are required, if you miss to pass the parameter you will get an error "TypeError: say() missing 1 required positional argument: 'friend' "
Note: Here passing the arguments in correct order is very necessary as changing the position will alter the response of the function.
- Keyword Arguments:
By using Keyword Arguments, we need not have to specify the arguments in sequential manner but have to specify each argument as 'key=value' and all keys should match function definition parameters.
def say(message, friend):
print("Hello you have message, "+ message+ ", from "+friend)
say(friend = "Andy", message= "pick my call")
In above example, you can see that the position of arguments is altered but that does not affect the functionality. You can try running above program to check output.
But if you miss spell the 'message' as 'msg' then you will get error "TypeError: say() got an unexpected keyword argument 'msg' "
- Default argument values:
We can define default values of some of the parameters, this way we can optionally provide their values while calling the actual function. Let's say we want a default message to be passed in say() function is "call me when you get free" when there is no value for 'message' parameter
def say(friend, message="call me when you get free"):
print("Hello you have message, "+ message+ ", from "+friend)
say("Andy")
Note: Only those parameters which are at the end of the parameter list can have default argument values i.e. you cannot have a parameter with a default argument value preceding a parameter without a default argument value in the function’s parameter list.
For example: def say(message="call me when you get free", friend): will throw error "SyntaxError: non-default argument follows default argument "
That's all for today's article. Let's meet next week with new article in this series.