Introduction
Python is a versatile and easy-to-learn programming language. It’s known for its simple syntax and readability. In this topic, we’ll explore the fundamental components of Python syntax and structure, which will help you get started with writing and understanding Python code.
Objective
The objective of this lesson is to introduce you to the basic structure of a Python program, including how to declare variables, define functions, and understand Python’s indentation rules. By the end, you’ll have the knowledge to write a simple Python program and execute it on your local machine.
Python Code Example
# This is a simple Python program def greet(name): return f"Hello, {name}!" # Main program starts here if __name__ == "__main__": user_name = input("Enter your name: ") print(greet(user_name))
Explanation of the Program Structure
The structure of the program is simple and easy to understand:
- Function Definition: The function
greet(name)
takes a parameter calledname
and returns a greeting message. - Input: The program asks the user for their name with the
input()
function and stores it in the variableuser_name
. - Calling the Function: The program calls the
greet
function, passing theuser_name
as an argument. - Output: The result is printed to the screen using the
print()
function.
How to Run the Program
- Open a text editor and create a new file named
greet.py
. - Copy and paste the provided Python code into the file.
- Save the file.
- Open your command line or terminal, navigate to the folder where the file is saved, and run the program with the following command:
python greet.py
. - Enter your name when prompted, and the program will greet you!