+ 1
Python
How to work with dictionary and modules
3 Respuestas
+ 4
You can learn them in Python courses, such as Python Developer: https://www.sololearn.com/learn/courses/python-developer
Still here's the basics:
Dictionary:
Dictionary is a collection of key-value pairs, each key holding a value. Like here, where name holds "Alan" and age holds 20:
person = {
"name": "Alan",
"age": 20
}
Access values:
print(person) # Outputs the dictionary
print(person["name"]) # Outputs "Alan"
Add, update and remove values:
person["name"] = "John" # Value of 'name' is "John"
person["profession"] = "Programmer" # Added new key 'profession' with value "Programmar"
person.pop["age"] # Removes the key-value pair of "age"
Module:
Modules are files that have variables, classes, functions and more set-up in a way that you can reuse them in your code to make tasks easier and optimized.
Import a module with import:
import math
Using a math function:
print(math.sqrt(25)) #square root of 25
Learn more about these in Python course and other resources 👍
0
Creating a module: Save your functions or variables in a .py file, and you can import them into other Python scripts.
Importing modules: You use the import keyword to access the functionality of the module.
Using functions from the module: You can call the functions or access variables as needed.