Lesson 1 - What Are Variables in Python? Learn Input and Output Basics

Learn the basics of variables in Python with simple examples. Understand how input() and print() work for user interaction. Convert data types for numbers and floats easily. Explore free online tools to run Python code without installation. Perfect for beginners starting their programming journey.

PYTHON

Prof. Leonardo Gomes Guidolin

5/8/20241 min read

What Are Variables in Python?

Variables are memory spaces used to store data. In Python, you don’t need to declare a variable type—Python automatically assigns the type based on the value. This makes Python very beginner-friendly.

Example: Defining and Using Variables in Python

# Variable definitions

name = "Alice"

age = 25

height = 1.65

# Displaying values

print("Name:", name)

print("Age:", age)

print("Height:", height)

Python Input and Output Commands

Input and output functions are essential to interact with users. Python uses input() to get data from the user and print() to show information on the screen.

User Input Example

name = input("Enter your name: ")

age = input("Enter your age: ")

print("Hello,", name, "! You are", age, "years old.")

⚠️ Note: The input() function always returns a string. To use other types like integers or floats, you need to convert the value:

age = int(input("Enter your age: "))

height = float(input("Enter your height: "))

Displaying Output with print()

print("Hello, World!")

print("The sum of 2 + 2 is", 2 + 2)

Best Tools to Test Python Code Online

You can write and run Python code without installing anything! Here are some great online platforms:

  • Replit – Run Python in your browser and share code easily.

  • Google Colab – Ideal for beginners and data science projects.

  • Python Tutor – Visualize Python code execution step-by-step.

  • Jupyter Notebook – Great for interactive learning and scientific projects.

  • OnlineGDB – Online debugger and compiler for testing Python scripts.

These tools are perfect for practicing Python without needing a local setup.

Conclusion

Understanding how to use variables and basic input/output commands is one of the first steps in learning Python programming. With these concepts, you can already build simple, interactive programs. Keep exploring and coding!