Python Essentials 1
loop - else
Python allows the else keyword to be used with the for and while loops too. The else block appears after the body of the loop. The statements in the else block will be executed after all iterations are completed. The program exits the loop only after the else block is executed.
for i in range(1):
print("#")
else:
print("#")
Output:
#
#
Functions
def message(number):
print("Enter a number:", number)
Positional Paramenter Passing
# Example 1
def my_function(a, b, c):
print(a, b, c)
my_function(1, 2, 3)
# Example 2
def introduction(first_name, last_name):
print("Hello, my name is", first_name, last_name)
introduction("Luke", "Skywalker")
introduction("Jesse", "Quick")
introduction("Clark", "Kent")
Keyword Parameter Passing
# Example 1
def introduction(first_name, last_name):
print("Hello, my name is", first_name, last_name)
introduction(first_name = "James", last_name = "Bond")
introduction(last_name = "Skywalker", first_name = "Luke")
Mixing Positional and Keyword Parameter Passing
adding(3, c = 1, b = 2)
The previous is accepted and as long as you put the positional arguments before keyword arguments.
Default Values for Functions Parameters
# Example 1
def introduction(first_name, last_name="Smith"):
print("Hello, my name is", first_name, last_name)
def introduction(first_name="John", last_name="Smith"):
print("Hello, my name is", first_name, last_name)
None
def strange_function(n):
if(n % 2 == 0):
return True
print(strange_function(2))
print(strange_function(1))
The output will be:
True
None