Curso Python #04 - Primeiros comandos em Python3
Table of Contents
Introduction
In this tutorial, we will explore the basic commands in Python 3, focusing on user interaction through the print()
and input()
functions. We will also introduce the concept of variables and how they function within a Python program. By the end of this tutorial, you will have a foundational understanding of Python, enabling you to create simple programs and solve programming challenges.
Step 1: Using the print() Function
The print()
function is essential for displaying output in Python. Here’s how to use it:
-
Basic Syntax:
- To display text, use the following syntax:
print("Hello, World!")
- This will output:
Hello, World!
- To display text, use the following syntax:
-
Printing Variables:
- You can also print the values stored in variables:
message = "Welcome to Python!" print(message)
- You can also print the values stored in variables:
-
Tips for Using print():
- You can print multiple items by separating them with commas:
name = "Alice" age = 30 print(name, age)
- Format output using f-strings for better readability:
print(f"{name} is {age} years old.")
- You can print multiple items by separating them with commas:
Step 2: Using the input() Function
The input()
function allows you to take user input. Here’s how to implement it:
-
Basic Syntax:
- Use
input()
to prompt the user for input:user_input = input("Please enter your name: ") print(f"Hello, {user_input}!")
- Use
-
Data Type Considerations:
- The value returned by
input()
is always a string. Convert it to another type if necessary:age = int(input("Please enter your age: "))
- The value returned by
-
Practical Tips:
- Always validate user input to prevent errors:
while True: try: age = int(input("Please enter your age: ")) break except ValueError: print("That's not a valid age. Please try again.")
- Always validate user input to prevent errors:
Step 3: Understanding Variables
Variables are key to storing data in a program. Here are the basics:
-
Creating Variables:
- Declare a variable and assign a value:
score = 100
- Declare a variable and assign a value:
-
Variable Naming Rules:
- Start with a letter or underscore (no numbers).
- Use descriptive names.
- Avoid spaces and special characters.
-
Using Variables in Programs:
- Variables can be used in calculations:
x = 5 y = 10 total = x + y print(total)
- Variables can be used in calculations:
Step 4: Practicing with Exercises
To reinforce your learning, try these programming challenges:
- Exercise 1: Create a program that asks for a user’s favorite color and prints a message using that color.
- Exercise 2: Write a program that prompts the user for two numbers and outputs their sum.
- Exercise 3: Develop a program that takes a user’s name and age, then displays a message stating how many years until they turn 100.
Conclusion
In this tutorial, we covered the fundamental commands in Python, including how to use print()
and input()
, and the concept of variables. By practicing the exercises provided, you can solidify your understanding of these concepts. As you become more comfortable with Python, continue exploring more complex topics and challenges to enhance your programming skills. Happy coding!