Exercício Python #004 - Dissecando uma Variável
Table of Contents
Introduction
In this tutorial, we will create a Python program that reads user input and displays the type of that input along with various information about it. This exercise is designed for beginners to help you understand Python's data types and how to interact with user input.
Step 1: Set Up Your Python Environment
Before we start coding, ensure you have Python installed on your system. You can download it from the official Python website.
- Open your preferred code editor (e.g., VSCode, PyCharm) or use an online Python compiler.
- Create a new Python file. You can name it something like
input_info.py
.
Step 2: Read User Input
We will use the input()
function to capture user input. This function allows us to prompt the user for data.
- Start by writing the following code:
user_input = input("Digite algo: ")
Step 3: Determine the Type of Input
Next, we will determine and display the type of the input captured.
- Use the
type()
function to find out the data type:input_type = type(user_input) print("O tipo primitivo é:", input_type)
Step 4: Gather Additional Information
Now, we will extract and display more information about the input.
-
Check if the input is numeric:
print("É númerico?", user_input.isnumeric())
-
Check if the input is alphabetic:
print("É alfabético?", user_input.isalpha())
-
Check if the input is alphanumeric:
print("É alfanumérico?", user_input.isalnum())
-
Check if the input contains only spaces:
print("Só tem espaços?", user_input.isspace())
-
Check if the input is in uppercase:
print("Está em maiúsculas?", user_input.isupper())
-
Check if the input is in lowercase:
print("Está em minúsculas?", user_input.islower())
Step 5: Run Your Program
-
Save your Python file.
-
Open your terminal or command prompt.
-
Navigate to the directory where your file is saved.
-
Run the program using:
python input_info.py
-
Follow the prompt and enter any data. The program will display the type and properties of your input.
Conclusion
You have successfully created a Python program that reads user input and provides detailed information about its type and properties. This exercise helps you understand how to work with user input and utilize built-in string methods effectively.
As a next step, consider experimenting with other data types or enhancing the program to handle different scenarios, such as validating user input or accepting multiple entries. Happy coding!