As an Amazon Associate I earn from qualifying purchases.
Python: Passing *args and **kwargs into a function
# *args is the usual notation for optional, non-keyword arguments
# **kwargs is the usual notation for optional, keyword arguments
def test_var_args(regular_argument, *args, **kwargs):
print ("- regular argument:", regular_argument)
for arg in args:
print ("- unnamed arguments arg:", arg)
for key in kwargs:
print ("- named arguments: %s: %s" % (key, kwargs[key]))
test_var_args(1, "two", 3, four=4, five="five")
- regular argument: 1 - unnamed arguments arg: two - unnamed arguments arg: 3 - named arguments: four: 4 - named arguments: five: five
find similar posts:
Python
0
comments
Python: Passing *args and **kwargs into a function
# *args is the usual notation for optional, non-keyword arguments
# **kwargs is the usual notation for optional, keyword arguments
def test_var_args(regular_argument, *args, **kwargs):
print ("- regular argument:", regular_argument)
for arg in args:
print ("- unnamed arguments arg:", arg)
for key in kwargs:
print ("- named arguments: %s: %s" % (key, kwargs[key]))
test_var_args(1, "two", 3, four=4, five="five")
- regular argument: 1
- unnamed arguments arg: two
- unnamed arguments arg: 3
- named arguments: four: 4
- named arguments: five: five
find similar posts:
Python
0
comments
Python bubble sort
Since I had to learn python for my machine learning and self-driving classes and I had to review the basics. Not to waste my efforts, I am creating a new guide, you can see more in:
https://github.com/UkiDLucas/python_zen_interviews
https://github.com/UkiDLucas/python_zen_interviews
def bubble_sort(the_list: list, verbose: int=0):
"""
author: @UkiDLcuas
This function changes the provided list.
The list can contain integers, decimal numbers, strings, etc.
The list is sorted in ascending order (first to last).
The function does not return anything.
"""
if verbose > 0:
iteration = 0
# count remining bubbles ( aka step backwards)
start = len(the_list)-1 # end, zero-based list
stop = 0 # beginning
step = -1 # backwards
for remaining_bubbles in range(start, stop, step):
for i in range(remaining_bubbles):
if verbose > 0:
iteration = iteration + 1
print("iteration", iteration, "remaining_bubbles", remaining_bubbles, "index", i)
print(" ", the_list)
print(" comparing if is", the_list[i], "bigger than", the_list[i+1])
if the_list[i] > the_list[i+1]:
# swap
temp = the_list[i+1] # temp placehoder for the value to be moved
the_list[i+1] = the_list[i] # bubble up
the_list[i] = temp # bubble down
if verbose > 0:
print("*** finished", len(the_list), "element list in ", iteration, "iterations")
find similar posts:
GitHub,
Python,
tech
0
comments
Python bubble sort
Since I had to learn python for my machine learning and self-driving classes and I had to review the basics. Not to waste my efforts, I am creating a new guide, you can see more in:
https://github.com/UkiDLucas/python_zen_interviews
https://github.com/UkiDLucas/python_zen_interviews
def bubble_sort(the_list: list, verbose: int=0):
"""
author: @UkiDLcuas
This function changes the provided list.
The list can contain integers, decimal numbers, strings, etc.
The list is sorted in ascending order (first to last).
The function does not return anything.
"""
if verbose > 0:
iteration = 0
# count remining bubbles ( aka step backwards)
start = len(the_list)-1 # end, zero-based list
stop = 0 # beginning
step = -1 # backwards
for remaining_bubbles in range(start, stop, step):
for i in range(remaining_bubbles):
if verbose > 0:
iteration = iteration + 1
print("iteration", iteration, "remaining_bubbles", remaining_bubbles, "index", i)
print(" ", the_list)
print(" comparing if is", the_list[i], "bigger than", the_list[i+1])
if the_list[i] > the_list[i+1]:
# swap
temp = the_list[i+1] # temp placehoder for the value to be moved
the_list[i+1] = the_list[i] # bubble up
the_list[i] = temp # bubble down
if verbose > 0:
print("*** finished", len(the_list), "element list in ", iteration, "iterations")
find similar posts:
GitHub,
Python
0
comments
Subscribe to:
Posts (Atom)
apt quotation..
“A man should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects.” by Robert A. Heinlein (author, aeronautical engineer, and naval officer)