1/5
We're learning about structs and strings in Vyper. Structs are a way for us to create new custom types. They're like blueprints for variables, grouping together related data under a single name. This is how we might create a `Person` struct: ```vyper struct Person: favorite_number: uint256 name: String[100] ``` Strings are a typing we haven't learned about yet. A string is a fixed-size array of characters. For example, here's how we might create a string variable: ```vyper example_str: String[100] = "Test String" ``` We'll use structs to store data about multiple people in a list, and to accomplish this, we'll use an array of structs. To begin, let's create a variable in our `deploy` function: ```vyper @deploy def __init__(): self.my_favorite_number = 7 self.index = 0 self.my_name = "Patrick!" ``` We need to make our new `Person` type publicly accessible, so we'll add the keyword `public` before our `String` type: ```vyper my_name: public(String[100]) ``` We can now create an array of `Person` structs in our contract: ```vyper list_of_people: public(Person[5]) ``` This is an array of five `Person` structs, which is where we'll store our new `Person` information. Next, we'll create an external function to add a new `Person` to our `list_of_people` array: ```vyper @external def add_person(name: String[100], favorite_number: uint256): # Add favorite number to the numbers list self.list_of_numbers[self.index] = favorite_number # Add the person to the person's list new_person: Person = Person(favorite_number = favorite_number, name = name) self.list_of_people[self.index] = new_person self.index = self.index + 1 ``` This function takes the `name` and `favorite_number` of a new `Person` and stores that data in our `list_of_people` array. We can now compile and deploy this contract.
A comprehensive guide to working with structs and strings in Vyper. The lesson covers creating structs to define custom types, using them to store data like names and favorite numbers, and working with fixed-size strings in your Vyper code.
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