In Part 1 for Python3, Basics for beginners blog, we read about three python3 usages, viz. Print, Variables and Defining Functions with Parameters. Next we will be learning about, Scope of Variables, Input and Return functions.
1. Scope of Variables
In layman terms, it simply determining boundary for variables, something like namespaces,so we can call variable value on Global level i.e available for whole code and Local level i.e available for a specific function body.
Code
good_coffee="black"
bad_coffee="latte"
how_many_times=2*3+10
def what_you_like(more_times):
print(f"I like {good_coffee} coffee {how_many_times} times better than {bad_coffee} or may be {more_times} times,\nCrazy Right?")
what_you_like(100)
def anything_else():
normal_coffee="Americano"
print(f"{normal_coffee}?, well its better than {bad_coffee} but {good_coffee} is the best Sir")
anything_else()
In above example, variable on very top are Global level while one defined in function body (normal_coffee=”Americano”) and function parameter (more_times) are two local variables for Function 1 what_you_like and Function 2 anything_else respectively.
Output
I like black coffee 16 times better than latte or may be 100 times,
Crazy Right?
Americano?, well its better than latte but black is the best Sir
2. Input Function
Input function is important concept in python programming as it help end user of code to provide dynamic input data to be used as variable elsewhere in whole code.
Code
good_coffee=input("Which coffee do you like?\n")
bad_coffee=input("Which coffee you don't like?\n")
how_many_times=2*3+10
def what_you_like(more_times):
print(f"Because {good_coffee} coffee is {how_many_times} times better than {bad_coffee} may be {more_times},\nCrazy Right?")
what_you_like(100)
In above code, Instead of giving hard coded global variable value, I allowed end user to input values and later my function body use that dynamic input value to display result of code.
Output
Which coffee do you like?
Black
Which coffee you don't like?
Latte
Because Black coffee is 16 times better than Latte may be 100,
Crazy Right?
We can use this input values in local variables as well like instead of hard coding, more_times(100), we can call more_times(user_input_value).
3. Casting, Return and Advanced Input function
Now by default, Input function consider variable provided as text or string. That’s why previous code worked since we gave only Black and Latte as user input when prompted. In order to make our code understand any other data type we use casting or simply put, converting input text to respective type. Let’s append our code with this function.
Code
good_coffee=input("Which coffee do you like?\n")
bad_coffee=input("Which coffee you don't like?\n")
how_many_times=2*3+10
more_times=input("How much more times?\n")
how_much_more_times=int(more_times)
def what_you_like(how_much_more_times):
print(f"Because {good_coffee} coffee {how_many_times} times better than {bad_coffee} may be {how_much_more_times},\nCrazy Right?")
what_you_like(how_much_more_times)
Output
Which coffee do you like?
Black
Which coffee you don't like?
Latte
How much more times?
1000
Because Black coffee 16 times better than Latte may be 1000,
Crazy Right?
Instead of calling print in function body itself, we may return its value for processing later. One key point of return function is it stop python for further execution of any code in respective function. If include return in above example than my code will look like below,
Code
good_coffee=input("Which coffee do you like?\n")
bad_coffee=input("Which coffee you don't like?\n")
how_many_times=2*3+10
more_times=input("How much more times?\n")
how_much_more_times=int(more_times)
def what_you_like(how_much_more_times):
return f"Because {good_coffee} coffee {how_many_times} times better than {bad_coffee} may be {how_much_more_times},\nCrazy Right?"
final_statement=what_you_like(how_much_more_times)
print(final_statement)
Output
Which coffee do you like?
Black
Which coffee you don't like?
Latte
How much more times?
1000
Because Black coffee 16 times better than Latte may be 1000,
Crazy Right?
In upcoming blog, we will read about using Conditions like if/else and loops.