The dict () function in Python
In Python, the dict () function creates a dictionary type value. Quantrimang will learn more about the syntax, parameters and usage of the dict () function through this article. Invites you to read the track.
The syntax of the dict () function in Python
The dict () function has several different forms as follows:
class dict(**kwarg)
class dict(mapping, **kwarg)
class dict(iterable, **kwarg)
Note: ** kwarg allows you to pass as many parameters as you want without knowing the number in advance.
The parameter using the keyword argument is a pre-defined parameter. So the keyword argument is in the form kwarg = the value will be passed to the dict () function to create the dictionary.
Dict () does not return any value.
Example 1: Create Dictionary using the keyword argument.
numbers = dict(x=5, y=0)
print('numbers = ',numbers)
print(type(numbers))
empty = dict()
print('empty = ',empty)
print(type(empty))
Run the program, the result is:
empty = dict()
print('empty = ',empty)
print(type(empty))
Example 2: Create Dictionary uses Iterable
# keyword argument không được truyền vào
numbers1 = dict([('x', 5), ('y', -5)])
print('numbers1 =',numbers1)
# keyword argument được truyền vào
numbers2 = dict([('x', 5), ('y', -5)], z=8)
print('numbers2 =',numbers2)
# zip() tạo iterable trong Python 3
numbers3 = dict(dict(zip(['x', 'y', 'z'], [1, 2, 3])))
print('numbers3 =',numbers3)
Run the program, the result is:
numbers1 = {'y': -5, 'x': 5}
numbers2 = {'z': 8, 'y': -5, 'x': 5}
numbers3 = {'z': 3, 'y': 2, 'x': 1}
Example 3: Create Dictionary uses mapping
numbers1 = dict({'x': 4, 'y': 5})
print('numbers1 =',numbers1)
# Không nhất thiết phải dùng dict()
numbers2 = {'x': 4, 'y': 5}
print('numbers2 =',numbers2)
# keyword argument được truyền vào
numbers3 = dict({'x': 4, 'y': 5}, z=8)
print('numbers3 =',numbers3)
The result is:
numbers1 = {'x': 4, 'y': 5}
numbers2 = {'x': 4, 'y': 5}
numbers3 = {'x': 4, 'z': 8, 'y': 5}
Previous article: Function delattr () in Python
Next article: Function in dir () Python
You should read it
May be interested
- The function set () in Pythonin this article, tipsmake.com will learn with you about set (), syntax, parameters and specific examples. invites you to read the track.
- Help () function in Pythonthe built-in help () function in python is used to display documents and invoke the help system of modules, functions, classes, keywords ...
- Sum () function in Pythonthe built-in function sum () in python returns the sum of all numbers in iterable.
- The slice () function in Pythonthe slice () function in python returns a slice object that helps you determine how to cut an existing string.
- The chr () function in Pythonin python, the chr () function returns a character (a string) from an integer that represents the unicode of the returned character.
- The iter () function in Pythoncontinuing with the topic of python's built-in functions, the article will introduce you to the iter () function with the syntax, usage as well as specific examples. invites you to read the track.
- The pow () function in Pythonthe pow () function built into python returns the value of x with the power of y (xy). if there is a third parameter, the function returns x to the power y, the module z.
- The reversed () function in Pythonthe reversed () function is one of the built-in functions in python, used to reverse the original string returning the iterator.
- Exec () function in Pythonthe exec () function used to execute python programs dynamically can be string or object code. how does exec () function syntax, what parameters do it have, and how is it used? invites you to read the track.
- Built-in Python functionsin the previous article, you learned that python has a basic function type, one is a built-in function, two are user-defined functions. today, we will learn about a list of python's built-in functions.