WHAT!? You Can Do THIS In Python?!
- Python is a general purpose programming language that is often applied in scripting roles.
- So, Python is programming language as well as scripting language.
- Python is also called as Interpreted language
- Python is an experiment in how much freedom programmers need.
- Too much freedom and nobody can read another's code; too little and expressiveness is endangered.
so right here we went ahead and defined a variable called banana and now let's
go ahead and print it using the asterisk if you go ahead and run this it's
going to print them all as separate elements.
var: str = 'Banana'
print(*var)
and one cool thing you can do with this is add a separator and add some sort
of symbol and it's going to create this funky text.
var: str = 'Banana'
print(*var, sep='.')
now what you witnessed here is an unpacking operator which means if you were to create a list with the unpacking operator and you were to go ahead and print this list you'd get a list back for eachletter in banana.
var: str = 'Banana'
list_: list[str] = [*var]
print(list_)
Also Read - The python Logic
Also Read -
Never do this in Python
this can be quite useful if you have some arguments that you want to pass in
as a tuple into a function
so for example we have a and b here that we
want to pass into this function here and when we actually go ahead and call
this it's going to take these two variables it's going to unpack them and put
them inside A and B and we're going to be able to use that function with these
arguments.
args: tuple = (1, 'a')
def func(a, b):
print(a, b)
func(*args)