-
1. Introduction to Python
-
2. Python Basics
-
3. Working with Data Structures
-
4. Functions and Modules
-
5. Object-Oriented Programming (OOP)
-
6. File Handling
-
7. Error and Exception Handling
-
8. Python for Data Analysis
-
9. Advanced Topics in Python
-
10. Working with APIs
-
11. Python for Automation
-
12. Capstone Projects
- 13. Final Assessment and Quizzes
1.2.2 Writing your first Python script
Writing your first Python script is an exciting step in your journey as a programmer. A Python script is a file containing Python code that can be executed by the Python interpreter. In this section, we'll guide you through creating and running your first Python script.
1. Creating a Python Script
-
Open Your IDE or Text Editor:
- If you're using PyCharm or VSCode, open the IDE.
- For Jupyter Notebooks, you can create a new notebook.
- If you are using a simple text editor, create a new file and save it with a .py extension (e.g., hello.py).
-
Writing Code:
In the file, type the following Python code:
print("Hello, World!")
This is a very basic Python script. The print() function is used to display text or data on the screen. In this case, it will output "Hello, World!" when you run the script.
2. Running the Python Script
After writing your script, you need to execute it to see the output.
2.1 Running Python in an IDE
-
PyCharm:
- Click on the Run button (usually a green triangle) in the top right corner of PyCharm, or press Shift + F10.
- PyCharm will execute the script, and you should see the output Hello, World! in the console at the bottom of the window.
-
VSCode:
- Open the script file in VSCode.
- You can run the Python script by opening the terminal (Ctrl + ~) and typing:
python hello.py
- You will see Hello, World! printed in the terminal.
2.2 Running Python in the Command Line or Terminal
If you're not using an IDE, you can still run the Python script from the command line or terminal.
-
Open the terminal:
- On Windows, open Command Prompt or PowerShell.
- On macOS or Linux, open the Terminal.
-
Navigate to the folder where the script is located:
Use the cd command to change directories (folder):
cd path/to/your/script
-
Run the script:
Type the following command:
python hello.py
This will run the script, and you should see Hello, World! printed in the terminal.
3. Understanding the Code
Let's break down the code:
print("Hello, World!")
- print(): This is a built-in Python function used to output text or data to the screen.
- "Hello, World!": This is a string of text enclosed in quotation marks. In Python, strings are sequences of characters, and they can be enclosed in either single or double quotation marks ('Hello' or "Hello").
When you run this script, Python will execute the print() function, and the text inside the parentheses will be displayed on the screen.
4. Modifying Your First Script
Once you’ve successfully run your first script, you can start modifying it to see how different changes affect the output. For example, try changing the text inside the print statement:
print("Welcome to Python programming!")
Then run the script again, and you'll see the new output.
5. Next Steps
After writing and running your first Python script, you can move on to more complex concepts. You can:
- Experiment with different Python functions.
- Learn about variables and data types.
- Explore basic operations like arithmetic, conditional statements, and loops.
This foundational script helps you get comfortable with writing Python code, and the skills you develop will be essential for more advanced Python programming.
Commenting is not enabled on this course.