Hello World!
Still in Progress/edition
Contents
Getting Started with Python: Your First Lines of Code
Welcome to your first steps in Python programming! In this entry, we will cover the essentials to get you writing and understanding your first lines of Python code. We will focus on the basic logic of coding, the structure of a Python script, and how to use some simple functions like print()
.
Writing and Running Code
Python code is executed line by line, from top to bottom. This means that the order in which you write your commands (or "statements") matters. Let’s start with the classic example used in almost every programming language: printing "Hello, World!" to the screen. We recommend you to follow along this entry with your Jupyter notebook, that is, copy and paste the code blocks in your own file.
# This is a comment: it explains what the code does but is not executed. print("Hello, World!")
Check the output, did you get something like this?
Hello, World!
The line `print("Hello, World!")` is a command that tells Python to display the text within the parentheses on the screen. Notice the use of `"` around the text. This is called a string, but don’t worry about that yet! You can use either
Understanding Python Syntax
Python’s syntax is designed to be clear and readable. Each line of code usually performs one task. For example:
print("Welcome to Python!") print("Let's learn the basics.")
Welcome to Python! Let's learn the basics.
These two `print()` statements are executed sequentially. The first line runs, printing "Welcome to Python!", and then the second line runs, printing "Let's learn the basics.".
Comments
Comments are lines in your code that are not executed. They are used to explain what the code does or to leave notes for yourself and others. In Python, comments start with a `#` symbol. For example:
# This prints a greeting to the user print("Hello, World!") # This is an inline comment
Comments are ignored when the code is executed, so they don't affect your program’s output.
Basic Function: `print()`
The `print()` function is one of the most basic but essential tools in Python. It is used to output messages to the screen, helping you see the results of your code or debug it. You can use `print()` to display text, numbers, or even the result of computations (more on that later).
# Displaying different messages using print() print("Sustainable fashion is the future!") print("Support eco-friendly brands.")
Sustainable fashion is the future! Support eco-friendly brands.
The function `print()` takes what’s inside the parentheses and shows it on the screen. If you pass multiple items separated by commas, `print()` will display them all, separated by a space:
print("Sustainable", "fashion", "is", "the", "future!")
Sustainable fashion is the future!
Execution Order
Remember that Python reads and executes code line by line. If there is an error in your code, Python will stop running at the point of error and show a message indicating what went wrong. For example:
print("This line works fine.") prin("This line has a typo!") # Typo here, should be 'print' print("This line will not run.")
This line works fine. --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-2-d3c6f57e1f4f> in <module>() 1 print("This line works fine.") ----> 2 prin("This line has a typo!") # Typo here, should be 'print' 3 print("This line will not run.") NameError: name 'prin' is not defined
Python stops execution at the typo `prin` and doesn’t run the next line. This is useful because it helps you identify and fix problems in your code.
A Note on Spacing and Indentation
Python uses indentation (spaces or tabs at the beginning of a line) to define blocks of code, like those found inside loops or functions. Although we haven't covered these structures yet, remember that consistent indentation is crucial. Improper indentation will cause errors.
Summary
You’ve now written your first few lines of Python code, learned about the importance of execution order, and discovered how to use the `print()` function and comments effectively. As you continue, remember to run your code frequently and use comments to clarify your logic.
For more details, you can always refer to the official Python documentation
The author of this entry is Gustavo Rodriguez