Now that you have Python installed, letβs write and run your first Python program! π
1. Writing Your First Python Program
Open any text editor or Python IDE and create a new file named hello.py.
Now, type the following code:
print("Hello, World!")
π‘ Explanation:
print()
is a built-in function that displays output."Hello, World!"
is a string that gets printed on the screen.
2. Running the Python Program
Method 1: Using the Command Line
- Open the terminal or command prompt.
- Navigate to the folder where hello.py is saved.
- Run the program:
python hello.py
Output:
Hello, World!
Method 2: Using Python IDLE
- Open Python IDLE.
- Click File β New File and enter the code.
- Save the file as hello.py.
- Click Run β Run Module (F5).
3. Running Python in Interactive Mode
You can also run Python commands directly in the terminal without saving a file.
python
Then type:
print("Hello, Python!")
Press Enter, and you will see:
Hello, Python!
4. Python Program with Variables
Letβs modify the program to use variables:
name = "Alice" print("Hello,", name)
Output:
Hello, Alice
5. Taking User Input
We can also ask for user input and display a message:
name = input("Enter your name: ") print("Welcome, " + name + "!")
Example Output:
Enter your name: John Welcome, John!