Learn Python in 30 Days — Day 3: User Input + Basic Math
Learn Python in 30 Days — Day 3: User Input + Basic Math
Welcome to Day 3 of the Learn Python in 30 Days series!
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
Yesterday, you learned about variables and data types.
Today, you’ll make your program more interactive by asking the user for input and performing some basic math.
By the end of this lesson, you’ll understand:
-
How to use
input()to get information from the user -
How to convert that input into numbers
-
How to do simple arithmetic
-
How to build a basic calculator
Step 1 – Getting Input from the User
The input() function lets you ask a question and store what the user types.
Tip: Whatever the user types is stored as a string (text), even if they type a number.
Try this for yourself and see results similar to that shown below.
You can also download this example from my GitHub here and run it yourself.Step 2 – Type Conversion
If you want to use a number for math, you must convert the input from text to a number using int() (for whole numbers) or float() (for decimals).
Now you can use age in math!
Try this for yourself and see results similar to that shown below.
You can also download this example from my GitHub here and run it yourself.
Step 3 – Doing Math with Input
Let’s ask for two numbers and add them:
Or make it neater using an f-string:
Step 4 – Simple Calculator 🧮
Let’s take it a step further by making a mini calculator that can add, subtract, multiply, or divide.
Step 5 – What You Learned Today
input()lets your programs interact with users.- Always convert text to numbers before doing math.
- Basic arithmetic operators:
+addition-subtraction*multiplication/division. - You built your first interactive calculator!
Next Up: Day 4 – Operators & Expressions
Tomorrow you’ll explore operators and expressions the building blocks of all Python logic.
All example files for this series are available on my GitHub: Learn-Python-in-30-Days




Comments
Post a Comment