Curso Python #04 - Primeiros comandos em Python3

3 min read 1 year ago
Published on Aug 03, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

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:

  1. Basic Syntax:

    • To display text, use the following syntax:
      print("Hello, World!")
      
    • This will output: Hello, World!
  2. Printing Variables:

    • You can also print the values stored in variables:
      message = "Welcome to Python!"
      print(message)
      
  3. 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.")
      

Step 2: Using the input() Function

The input() function allows you to take user input. Here’s how to implement it:

  1. Basic Syntax:

    • Use input() to prompt the user for input:
      user_input = input("Please enter your name: ")
      print(f"Hello, {user_input}!")
      
  2. 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: "))
      
  3. 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.")
      

Step 3: Understanding Variables

Variables are key to storing data in a program. Here are the basics:

  1. Creating Variables:

    • Declare a variable and assign a value:
      score = 100
      
  2. Variable Naming Rules:

    • Start with a letter or underscore (no numbers).
    • Use descriptive names.
    • Avoid spaces and special characters.
  3. Using Variables in Programs:

    • Variables can be used in calculations:
      x = 5
      y = 10
      total = x + y
      print(total)
      

Step 4: Practicing with Exercises

To reinforce your learning, try these programming challenges:

  1. Exercise 1: Create a program that asks for a user’s favorite color and prints a message using that color.
  2. Exercise 2: Write a program that prompts the user for two numbers and outputs their sum.
  3. 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!