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.

name = input("What is your name? ") print("Hello " + name + "!")

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).

age = input("How old are you? ") print(type(age)) # This shows it's still a string age = int(age) # Convert to integer print(type(age)) # Now it’s a number
print(age) # Print the number

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:

num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) result = num1 + num2 print("The result is:", result)

Or make it neater using an f-string:

print(f"{num1} + {num2} = {result}")

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 4 – Simple Calculator 🧮

Let’s take it a step further by making a mini calculator that can add, subtract, multiply, or divide.

print("Simple Calculator") print("------------------") num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) print("Choose operation: + - * /") op = input("Enter operator: ") if op == "+": print(f"Result: {num1 + num2}") elif op == "-": print(f"Result: {num1 - num2}") elif op == "*": print(f"Result: {num1 * num2}") elif op == "/": print(f"Result: {num1 / num2}") else: print("Invalid operator!")

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 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

Popular posts from this blog

Math Behind Logic Gates

6502 - Part 2 Reset and Clock Circuit

Building a 6502 NOP Test