1/5
## Conditionals in Vyper We'll now cover the conditional statement in Vyper. This statement allows us to control the flow of our code based on different conditions. It's similar to the `if else` statement you might be familiar with in other programming languages. Let's create an example of an `if else` conditional statement in Vyper. We'll create an external function, we'll call this function `if_else`, which takes a single input of `uint256`. It will return a `uint256` value as well. ```python @external def if_else(x: uint256) -> uint256: ``` Now, we'll write the `if else` conditional statement. Let's say `if` the input `x` is less than or equal to 10, then we'll return 1. ```python @external def if_else(x: uint256) -> uint256: if x <= 10: return 1 ``` Then we'll add the `else` statement, we'll return 0 for this example. ```python @external def if_else(x: uint256) -> uint256: if x <= 10: return 1 else: return 0 ``` If we wanted another conditional statement in between, we would say `else if`, but in Vyper, we would type `elif`. ```python @external def if_else(x: uint256) -> uint256: if x <= 10: return 1 elif x <= 20: return 2 else: return 0 ``` For this example, we forgot to make this a pure function, so let's do that. ```python @external pure def if_else(x: uint256) -> uint256: if x <= 10: return 1 elif x <= 20: return 2 else: return 0 ``` We'll now compile the contract. Click on the Vyper icon, Compile `ifElse.vy`. We'll now call this function. We'll put 1 for `x` and call the function. We get a 1 back. We'll now put 20 for `x` and call the function. We get a 2 back. Lastly, we'll put 100 for `x` and call the function. We get a 0 back. This has been an introduction to `if else` conditional statements in Vyper.
A practical guide to conditional statements in Vyper - This lesson provides an example of how to use conditional statements (if-elif-else) in Vyper to create a function that takes a uint256 input and returns different values based on the input. The lesson covers how to set up the function, define the conditions, and return the appropriate value.
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