1/5
We're going to make a very minimal favorite things list. We want to store favorite numbers and favorite people with their favorite numbers. Here is some basic code to get started: ```python # favorite things list: # favorite numbers # favorite people with their favorite number ``` To store these items, we're going to use mappings. A mapping is like a dictionary or hashmap. We'll use the key to be the person and the value to be their favorite number. ```python # favorite things list: # favorite numbers # favorite people with their favorite number favorite_numbers: dict[uint256, uint256] favorite_people: dict[address, uint256] ``` Mappings have a default value. If we don't assign a value, the default value is 0. ```python # favorite things list: # favorite numbers # favorite people with their favorite number favorite_numbers: dict[uint256, uint256] favorite_people: dict[address, uint256] def set_favorite_number(number: uint256): favorite_numbers[number] = number ``` Now we have a function to set a favorite number. This is a pretty simple and straightforward function. We take a number and set it to the same number, but we could change this later. ```python # favorite things list: # favorite numbers # favorite people with their favorite number favorite_numbers: dict[uint256, uint256] favorite_people: dict[address, uint256] def set_favorite_number(number: uint256): favorite_numbers[number] = number def set_favorite_person(person: address, number: uint256): favorite_people[person] = number ``` We have another function to set the favorite person. It takes a person, their address, and their favorite number. We set it to the dictionary where we store the favorite person. ```python # favorite things list: # favorite numbers # favorite people with their favorite number favorite_numbers: dict[uint256, uint256] favorite_people: dict[address, uint256] def set_favorite_number(number: uint256): favorite_numbers[number] = number def set_favorite_person(person: address, number: uint256): favorite_people[person] = number def get_favorite_number(number: uint256) -> uint256: return favorite_numbers[number] ``` Lastly, we have a function to get the favorite number. It takes a number and then it returns the favorite number. It simply returns the value of the number from the dictionary. ```python # favorite things list: # favorite numbers # favorite people with their favorite number favorite_numbers: dict[uint256, uint256] favorite_people: dict[address, uint256] def set_favorite_number(number: uint256): favorite_numbers[number] = number def set_favorite_person(person: address, number: uint256): favorite_people[person] = number def get_favorite_number(number: uint256) -> uint256: return favorite_numbers[number] def get_favorite_person(person: address) -> uint256: return favorite_people[person] ``` We now have a function to get the favorite person, which takes a person's address and returns their favorite number.
A basic Vyper smart contract to create a favorites list - This lesson shows how to create a simple smart contract that stores favorite numbers and people with their favorite numbers. It includes using mapping and structs in Vyper.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
The basics of blockchain transactions, how to send and receive money on a blockchain network.
How to write Python based smart contracts using Vyper.
How to read and understand Vyper smart contracts.
Vyper data structures, arrays, structs, hash maps.
How to build a smart contract application and deploy on ZKsync with Moccasin.
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Web3 engineer, educator, and Cyfrin co-founder. Patrick's smart contract development and security courses have helped hundreds of thousands of engineers kickstarting their careers into web3.
Last updated on March 21, 2025
Duration: 2h 08min
Duration: 2h 32min
Duration: 24min
Course Overview
About the course
The basics of blockchain transactions, how to send and receive money on a blockchain network.
How to write Python based smart contracts using Vyper.
How to read and understand Vyper smart contracts.
Vyper data structures, arrays, structs, hash maps.
How to build a smart contract application and deploy on ZKsync with Moccasin.
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Web3 engineer, educator, and Cyfrin co-founder. Patrick's smart contract development and security courses have helped hundreds of thousands of engineers kickstarting their careers into web3.
Last updated on March 21, 2025
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar